I am new and only know the basics of C++. Any help will be appreciated. I want to know how to make my code less lines, more efficient, proper practice and play better.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstdio>
using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::getchar;
short CPUplay(int num) {
srand(time(nullptr) + num);
short CPUchoice = (rand() % 3) + 1;
return CPUchoice;
}
int playerPlay() {
string playerPlay;
int playValue = 1;
while (1 == 1) {
cout << "rock, paper or scissors: ";
cin >> playerPlay;
if (playerPlay == "rock") {
playValue = 1;
break;
}
else if (playerPlay == "paper") {
playValue = 2;
break;
}
else if (playerPlay == "scissors") {
playValue = 3;
break;
}
else {
cout << "Not a valid input." << endl;
}
}
return playValue;
}
int main()
{
int numRounds;
int CPUscore = 0;
int playerScore = 0;
cout << "Welcome to Rock, Paper, Scissors!" << endl;
cout << "How many games would you like to play? ";
cin >> numRounds;
while (!cin) {
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid input.";
cout << "How many rounds would you like to play? ";
cin >> numRounds;
}
for (int i = 0; i < numRounds; i++) {
cout << "\033[2J\033[1;1H";
int playerTurn = playerPlay();
int CPUturn = CPUplay(i);
switch (playerTurn) {
case 1: // When player chose rock
if (CPUturn == 1) { //When CPU chose rock
cout << "The computer chose rock. Draw." << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
else if (CPUturn == 2) { //When CPU chose paper
CPUscore++;
cout << "The computer chose paper. You lose this round :(." << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
else if (CPUturn == 3) { // When CPU chose scissors
playerScore++;
cout << "The computer chose scissors. You win this round!!" << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
break;
case 2: // When player chose paper
if (CPUturn == 1) { //When CPU chose rock
playerScore++;
cout << "The computer chose rock. You win this round!!." << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
else if (CPUturn == 2) { //When CPU chose paper
cout << "The computer chose paper. Draw." << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
else if (CPUturn == 3) { // When CPU chose scissors
CPUscore++;
cout << "The computer chose scissors. You lose this round!!" << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
break;
case 3: // When player chose scissors
if (CPUturn == 1) { //When CPU chose rock
CPUscore++;
cout << "The computer chose rock. You lose this round :(" << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
else if (CPUturn == 2) { //When CPU chose paper
playerScore++;
cout << "The computer chose paper. You win this round!" << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
else if (CPUturn == 3) { // When CPU chose scissors
cout << "The computer chose scissors. You draw." << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
break;
}
cout << "Press enter to continue... ";
cin.ignore();
cin.get();
}
cout << "\033[2J\033[1;1H";
if (CPUscore > playerScore) {
cout << "You lose the match :(" << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
} else if (playerScore > CPUscore) {
cout << "You win the match!!" << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
} else if (CPUscore == playerScore) {
cout << "Match drawn. " << endl;
cout << "Score: " << endl;
cout << "You: " << playerScore << endl;
cout << "Computer: " << CPUscore << endl;
}
return 0;
}
while(1==1)is just a complicated way to writewhile(true), so the latter is preferred ;-). \$\endgroup\${and}to make it much more readable. \$\endgroup\${ playValue = 1; break; }on a single line. \$\endgroup\$