/*
Program designed to play rock, paper, scissors with a user.
*/
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
cout << "Rock, Paper, Scissors GO!" << endl; // Little introduction here
// Input section
char choice;
int seed;
cout << "To play, please enter R, for rock, P, for paper, or S, for scissors: " << endl; // Defines the choices for the user.
cin >> choice; // Keeps the choices for the player.
cout << "Please enter the seed for the random number for the computer: " << endl; // Need the seed for the computer.
cin >> seed; // Keeps the seed for the computer.
cout << "You have entered: " << seed << endl; //Shows the user their seed choice
// Process Section
char computerCh; //computerCh is the choice of the computer
int R, S, P; //User input
srand(seed);
computerCh = rand() % 3 + 1;
//Computer's choices and outcomes
if (computerCh <= 0)
{
cout << "Computer chose Rock." << endl;
}
elseif (computerCh <= 1)
{
cout << "Computer chose Paper." << endl;
}
elseif (computerCh <= 2)
{
cout << "Computer chose Scissors." << endl;
}
// User's choices
// This section is for rock.
if (choice == R && computerCh <= 0)
{
cout << "You tied with the computer!" << endl;
}
elseif (choice == P && computerCh <= 1)
cout << "Paper beats rock! You lost." << endl;
elseif (choice == S && computerCh <= 2)
{
cout << "Rock beats scissors! You won!" << endl;
}
// This section is for paper.
if (choice == P && computerCh <= 1)
{
cout << "You tied with the computer!" << endl;
}
elseif (choice == R && computerCh <= 0)
{
cout << "Paper beats rock! You won!" << endl;
}
elseif (choice == S && computerCh <= 2 )
{
cout << "Scissors beat papers. You lost." << endl;
}
// This section is for scissors.
if (choice == S && computerCh <= 2)
{
cout << "You tied with the computer!" << endl;
}
elseif (choice == R && computerCh <= 0)
{
cout << "Rock beats scissors. You lost." << endl;
}
elseif (choice == P && computerCh <= 1)
{
cout << "Scissors beats paper! You won!" << endl;
}
// This is for switching the cases.
switch (computerCh)
{
case 0:
computerCh = R;
case 1:
computerCh = P;
case 2:
computerCh = S;
}
return EXIT_SUCCESS;
}
Line 29. The correct syntax is computerCh = rand() % 3;, because you want to generate a value between 0 and 2.
Line 20. Remember to restrict the choices of the user. If I enter letter F, the program won't work. Try doing an if statement to restrict it.
Line 102 to 110 and 32 to 48 can be merged in one switch() statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
switch(computerCh) // Remember that in each case you have to put a break!!!
{
case 0:
computerCh = 'R';
cout << "Computer chose Rock." << endl;
break;
case 1:
computerCh = 'P';
cout << "Computer chose Paper." << endl;
break;
case 2:
computerCh = 'S';
cout << "Computer chose Scissors." << endl;
break;
}
Then, in the sections you can nest if statements like this: