Hi all. I know that others have posted about Rock Paper Scissors, but none of their posts are exactly what I am needing.
My issue is that for my program, we are required to use at least 1 switch and 1 if statement, as well as keep track of the wins and display this along with the number of games played to the user before ending the game.
Here is what I have so far:
/*
* rockPaperScissors
* a game of Rock Paper Scissors
*
* Zack Conway
* Created: 9/7/11
* Last Modified: 9/10/11
*
* References:
*
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
string playerName;
char replay;
int cpuPick, playerPick, gamesPlayed, win, loss;
gamesPlayed = 0;
win = 0;
loss = 0;
cout << "Hello player, please enter your name" << endl;
cin >> playerName;
cout << endl << "Welcome " << playerName << ", are you feeling lucky?" << endl;
cout << "So we are going to play a game of Rock, Paper, Scissors." << endl;
cout << "Are you ready to test your luck?" << endl << endl;
restart:
srand( time(0) );
cpuPick = rand() % 3 + 1;
cout << "Please select from the following:" << endl;
cout << "1 - Rock" << endl;
cout << "2 - Paper" << endl;
cout << "3 - Scissors" << endl << endl;
cout << "Please make your selection." << endl;
cin >> playerPick;
cout << endl <<"Lets see how you do." << endl;
switch (cpuPick)
{
case 1:
if ( playerPick == 1 )
cout << "Rock and rock, nobody wins." << endl;
cout << "Try again:" << endl;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
if ( playerPick == 2 )
cout << "Paper covers rock." << endl;
cout << "Great job." << endl;
win++;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
if ( playerPick == 3 )
cout << "Scissors are crushed by rock." << endl;
cout << "Maybe next time." << endl;
loss++;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
case 2:
if ( playerPick == 1 )
cout << "Paper covers rock." << endl;
cout << "Maybe next time." << endl;
loss++;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
if ( playerPick == 2 )
cout << "Paper and paper do nothing." << endl;
cout << "Try again." << endl;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
if ( playerPick == 3 )
cout << "Scissors cut paper." << endl;
cout << "Great job." << endl;
win++;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
case 3:
if ( playerPick == 1 )
cout << "Rock breaks scissors." << endl;
cout << "Great job." << endl;
win++;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
if ( playerPick == 2 )
cout << "Scissors cut paper." << endl;
cout << "Maybe next time." << endl;
loss++;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
if ( playerPick == 3 )
cout << "Scissors and scissors do nothing." << endl;
cout << "Try again." << endl;
gamesPlayed++;
cout << "Want to play again? Press 'Y' or 'y' if you do." << endl << endl;
cin >> replay;
if (replay == 'Y' || replay == 'y')
{
system("CLS");
goto restart;
}
else
{
system("CLS");
cout << "You won " << win << " out of " << gamesPlayed << ". Thats great." << endl;
cout << "And you only lost " << loss << " times." << endl;
return(0);
}
break;
}
}
It seems like everything works fine when you select 1 for rock, but does not work if you select 2 for paper or 3 for scissors.
I know where must be a logical error somewhere, but am having a hard time finding it. If you have any suggestions or might know where I should begin, that would be very appreciated.
Thanks,
SquigsSquigley