This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I'm supposed to write a rock paper scissors game using do while loops. I'm almost done but I can't figure out two things:
1) I can't get the program to announce lost games (it announces lost rounds but not lost games). A game is the first to two wins.
2) I am supposed to use a while loop to prevent ties. I thought something like putting "while (playerChoice != cpuChoice)" under the cpuChoice declaration and then putting the rest of the gameplay loop in curly brackets would work but it created an infinite loop.
Here's my code:
/*
Rock, Paper, Scissors
*/
#include <iostream> //for cout and cin
#include <ctime> //seeds random
using namespace std;
int main()
{
cout << "\\n Rock, Paper, Scissors";
cout << "\\n\\nIn this game the player chooses rock(1), paper(2) or scissors(3) and so does the computer."
<< "\\nScissors beats paper, rock beats scissors, paper beats rock."
<<"\\nFirst to 2 wins takes the round and there are no ties";
srand(time(NULL));
int playerChoice;
int win = 0;
int lose = 0;
int goAgain;
int gameWin = 0;
int gameLoss = 0;
do
{
cout << "\\n\\nLet's play Rock, Paper, Scissors!" << endl;
do
{
cout << "\\nPress 1 for rock, 2 for paper, 3 for scissors:" << endl;
cin >> playerChoice;
cin.ignore();
int cpuChoice = rand() % 3 1;
cout << "\\nThe computer chose: " << cpuChoice << endl;
if (playerChoice == 1 && cpuChoice == 3)
{
cout << "You chose rock, the computer chose scissors. You win!" << endl;
win ;
}
else if (playerChoice == 2 && cpuChoice == 1)
{
cout << "You chose paper and the computer chose rock. You Win!" << endl;
win ;
}
else if (playerChoice == 3 && cpuChoice == 2)
{
cout << "You chose scissors and the computer chose paper. You win!" << endl;
win ;
}
else if (playerChoice == 1 && cpuChoice == 2)
{
cout << "You chose rock and the computer chose paper. You lost :(" << endl;
lose ;
}
else if (playerChoice == 2 && cpuChoice == 3)
{
cout << "You chose paper and the computer chose scissors. You lost :(" << endl;
lose ;
}
else if (playerChoice == 3 && cpuChoice == 1)
{
cout << "you chose scissors and the computer chose rock. You lost :(" << endl;
lose ;
}
} while (win < 2 && lose < 2);
if (win = 2)
{
cout << "Congratulations, you won the game!" << endl;
gameWin ;
}
else if (lose = 2)
{
cout << "Looks like you lost the game :(" << endl;
gameLoss ;
}
cout << "\\nWant to play again? Y/N";
cin >> goAgain;
cin.ignore();
} while (goAgain == 'Y');
return 0;
}
Subreddit
Post Details
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/programming...