Question for Rock Paper Scissors game program
My teacher wants the user to enter in their choices with characters instead of numbers.
R - Rock
P - Paper
S - Scissors.
After the user enters R for their choice, what would I use to convert the character to a number (int)? Can i use a switch?
Was trying to use if statements like:
Code:
if (guess == R)
{
userGuess = 1;
}
if (guess == P)
{
userGuess = 2;
}
if (guess == S)
{
userGuess = 3;
}
Re: Question for Rock Paper Scissors game program
Quote:
My teacher wants the user to enter in their choices with characters instead of numbers.
Once entered as a char, just use it as a char. For example, something like the following
Code:
char choice;
cout << "Enter choice (R, P, S): ";
cin >> choice;
switch (choice) {
case 'R':
case 'r':
//code for Rock
break;
case 'P':
case 'p':
//code for Paper
break;
case 'S':
case 's':
//code for Scissors
break;
default:
//error handling for incorrect choice
}
Re: Question for Rock Paper Scissors game program
well the computer generating it's choices as 1, 2 or 3. So how do i compare
Re: Question for Rock Paper Scissors game program
ok, i think i can compare. so ill work on that. ty
Re: Question for Rock Paper Scissors game program
try this
Code:
enum SELECTION {
ROCK = 1,
SCISSORS = 2,
PAPER = 3,
LIZARD = 4,
SPOCK = 5
};
Re: Question for Rock Paper Scissors game program
Love Big Bang Theory! :p
But, teacher wants user to imput letters. Im not doing very well trying to compare an int to a char.
Any ideas? Ive tried some things that compile, but they dont run.
EDIT. Let me also explain that the computer i do have it randomly selecting 1,2 or 3 for rock, paper, scissors. But the user input has to be R, P or S for rock paper scissors.
Re: Question for Rock Paper Scissors game program
Quote:
Originally Posted by
psfign
Love Big Bang Theory! :p
But, teacher wants user to imput letters. Im not doing very well trying to compare an int to a char.
Any ideas? Ive tried some things that compile, but they dont run.
EDIT. Let me also explain that the computer i do have it randomly selecting 1,2 or 3 for rock, paper, scissors. But the user input has to be R, P or S for rock paper scissors.
Don't compare a number to a letter. Convert a letter to a number and compare two numbers, or you could follow the example that 2kaud gave using the switch statement.
Re: Question for Rock Paper Scissors game program
I gave you an example in post #2 how to handle user input of R, P or S. Use a similar method for the 1, 2, or 3 for the computer go. If you post your code so far as we'll be able to give better guidance.
Re: Question for Rock Paper Scissors game program
yea, i havent looked at doing that switch 2k gave yet. But a couple follow up questions. Am i correct in the thought that i could do something like:
Code:
num = Computer's selection
if (num == 1)
return 'R';
or on the switch like 2k gave...can i do for the user input:
Code:
switch (choice) {
case 'R':
case 'r':
return 1;
break;
either one of those would be ok?
Re: Question for Rock Paper Scissors game program
Those could work if you use them properly.
Re: Question for Rock Paper Scissors game program
Yea I know 2k, but i was trying other things that i thought would work....that obviously didnt work.
and looking at your example, im assuming i can use return '1' as code for Rock
Re: Question for Rock Paper Scissors game program
Quote:
Originally Posted by
GCDEF
Those could work if you use them properly.
Thanks!
Re: Question for Rock Paper Scissors game program
Why don't you convert 1 or 2 or 3 for the computers guess to the char 'R', or 'P' or 'S' - then use the char everywhere?
Code:
const char conv[] = "RPSB";
....
char guess = (num >= 1 && num <= 3) ? conv[num - 1] : conv[3];
Re: Question for Rock Paper Scissors game program
We havent learned how to do the conv like that, so i think I'd have to do it as
Code:
num = Computer's selection
if (num == 1)
return 'R';
Thanks for your help again guys! I didnt post my code yet bc i dont want you guys thinking im trying to get you to do it for me.
I'm wondering if I should throw in the Lizard & Spock options. Not sure how the prof would react! :p
Re: Question for Rock Paper Scissors game program
Quote:
i'm wondering if i should throw in the lizard & spock options. Not sure how the prof would react!
a++:thumb: