Rock, Paper, Scissors

Hello :)

I'm trying to write a code for Rock Paper Scissors using a random generator.

However I need to have the choices for the user be the letters p, r, or s and they need to be able to be entered as capital (P) or lowercase (p) and mean the same thing (paper). I don't know how to assign a general name (in this case pChoice) to the options given to the user. Is that even possible?

Here is my code:

1 #include <iostream>
2 #include <cstdlib> // for the random number generation
3 #include <ctime> // for the time function
4 using namespace std;
5
6 int main()
7 {
8 int cChoice;
9 int pChoice (P = 1, R = 2, S = 3); //error not declared
10
11 cout << "Please enter your move: 'P' for Paper, 'R' for Rock, 'S' for 12 Scissors";
13 cin >> pChoice ;
14
15 srand(time(0));
16
17 cChoice = rand() % 3 + 1;
18 cout << "Computer's move is" << cChoice;
19
20 if (cChoice == 1 && pChoice == 1)
21 cout << "Tie!!";
22
23 else if (cChoice == 2 && pChoice == 2)
24 cout << "Tie!!";
25
26 else if (cChoice == 3 && pChoice == 3)
27 cout << "Tie!!";
28
29 else if (cChoice == 1 && pChoice == 3)
30 cout << "You Win! Scissors Cut Paper!!";
31
32 else if (cChoice == 2 && pChoice == 1)
33 cout << "You Win! Paper Covers Rock!!";
34
35 else if (cChoice == 3 && pChoice == 2)
36 cout << "You Win! Rock Breaks Scissors!!";
37
38 else if (cChoice == 1 && pChoice == 2)
39 cout << "Computer Wins! Paper Covers Rock!!";
40
41 else if (cChoice == 2 && pChoice == 3)
42 cout << "Computer Wins! Rock Breaks Scissors!!";
43
44 else if (cChoice == 3 && pChoice == 1)
45 cout << "Computer Wins! Scissors Cut Paper!!";
46
47 else (pChoice > 3)
48 cout << "Invalid move!!"; // 47:2: error: expected ';' before 'cout'
49
50
51 return 0;
51 }
sure it is :)
char lut[256] = {0};
lut['s'] = 3;
lut['S'] = 3;
lut['r'] = 2;
lut['R'] = 2;
lut['p'] = 1;
lut['P'] = 1;

pcnum = lut[pchoice]; //pchoice is a character, or needs to be treated as one. Let the user input RPS as a single character.

if pcnum == cchoice tie etc.

if you want to validate input, anything from lut[pchoice] == 0 is bad.
Last edited on
You should make char variable to accept P, R or S and check with if-else or switch statements, then make integer variable which will be 1,2 or 3 depending on P, R and S, so it should be something like this:
1
2
3
4
5
6
7
8
9
10
char char_pChoice;
int pChoice;
std::cout<<"Please enter your move: 'P' for Paper, 'R' for Rock, 'S' for Scissors"<<std::endl;
std::cin>>char_pChoice;
if(char_pChoice=='P')
pChoice=1;
else if(char_pChoice=='R')
pChoice=2;
else if(char_pChoice=='S')
pChoice=3;

and then proceed with your code :)
Thank you for your help! Your examples really helped me solve that issue I was stuck on :)

How could I make it so the program ends after an invalid answer is inputed. For example:

program: "Please enter your move: 'P' for Paper, 'R' for Rock, 'S' for 12 Scissors"
user: L
program: "Invalid move!!"

*Program ends*

Right now I am getting the following:

program: "Please enter your move: 'P' for Paper, 'R' for Rock, 'S' for 12 Scissors"
user: L
program: "Invalid move!!"
program: "Computer's move is ___"

In my given example make else statement which will set pChoice to, for example 4, and it should be fine :)
Topic archived. No new replies allowed.