-
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:
-
Re: Question for Rock Paper Scissors game program
I'm with 2kaud. Throw it in. It adds complexity (though not much, really). I'd just be sure to output and comment the code with the rules. Not everyone may know about that variation of the game.
-
Re: Question for Rock Paper Scissors game program
Update:
I'm not any closer than I was yesterday!
No...Im not posting my code. lol
I'm embarrassed this stuff isnt sticking
-
Re: Question for Rock Paper Scissors game program
You should post what you have, explain where you are stuck or what is confusing. That way we can help you.
-
Re: Question for Rock Paper Scissors game program
Quote:
Originally Posted by
psfign
Update:
I'm not any closer than I was yesterday!
No...Im not posting my code. lol
I'm embarrassed this stuff isnt sticking
There are several structural approaches, one of which is something like
Code:
if(UserInput == 'R')
{
if(ComputerGuess == 1)
...
else
if(ComputerGuess == 2)
...
}
else
if(UserInput == 'S')
{
...
}
etc.
You may want to use switch statements instead of if. Either will work.
More than that and you'll need to post your code.
-
Re: Question for Rock Paper Scissors game program
well this is what i have so far. It wont cout the answer. Im stuck.. :(
Code:
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h.>
using namespace std;
char userChoice(void);
void winOrLose(int,int);
char getAns(void);
int play(void);
int main()
{
char ans;
char choice;
int num;
int uChoice;
srand(time(NULL));
cout << "ROCK PAPER SCISSORS.\n\n\n"
<< "Play against your computer.\n\n"
<< " Make your selection and\n"
<< " the computer will randomly choose as well,\n"
<< " and then the game will be scored.'\n\n"
<< "Rules:\n\n"
<< " PAPER covers ROCK\n"
<< " ROCK smashes SCISSORS\n"
<< " SCISSORS cut PAPER\n\n";
system ("pause");
do
{
system ("cls");
num = 1 + rand( ) % 3;
uChoice = userChoice();
winOrLose(choice,num);
ans = getAns();
}
while(ans=='Y');
return 0;
}
char userChoice(void)
{
char choice;
int uChoice;
do
{
cout << "Enter r, p, or s" ;
cin >> choice; cin.clear(); cin.ignore(10,'\n');
cout<<endl<<endl;
}
while (choice != 'N');
{
if(choice == 'r')
{
uChoice = 1;
}
if(choice == 'p')
{
uChoice = 2;
}
if(choice == 's')
{
uChoice = 3;
}
}
return uChoice;
}
char getAns(void)
{
char ans;
do
{
ans='N';
cout<<"Do you want to play again? ";
cin>>ans;
ans=toupper(ans);
}
while(ans!='Y'&& ans!='N');
return ans;
}
void winOrLose(int uChoice, int num)
{
if (uChoice == num)
{
cout << "Computer wins - PAPER covers ROCK " << endl;
}
else
{
cout << setw(35) << " " << "DRAW" << endl;
}
cout<<endl<<endl;
}
-
Re: Question for Rock Paper Scissors game program
OK. Look at this
Code:
do
{
cout << "Enter r, p, or s" ;
cin >> choice; cin.clear(); cin.ignore(10,'\n');
cout<<endl<<endl;
}
while (choice != 'N');
{
The while belongs to the do loop. So for the code in red the do loop is repeated until the user enters an 'N' - which I don't think is what you mean.
The green { is probably left over from a while loop but now just serves as the start of a compound statement - and can removed togther with its partner.
-
Re: Question for Rock Paper Scissors game program
Thanks! I think i have an issue with my winOrLose function too though.
Code:
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h.>
using namespace std;
char userChoice(void);
char getChoice(void);
void winOrLose(int,int);
char getAns(void);
int play(void);
int main()
{
char ans;
char choice;
int num;
int uChoice;
srand(time(NULL));
cout << "ROCK PAPER SCISSORS.\n\n\n"
<< "Play against your computer.\n\n"
<< " Make your selection and\n"
<< " the computer will randomly choose as well,\n"
<< " and then the game will be scored.'\n\n"
<< "Rules:\n\n"
<< " PAPER covers ROCK\n"
<< " ROCK smashes SCISSORS\n"
<< " SCISSORS cut PAPER\n\n";
system ("pause");
do
{
system ("cls");
num = 1 + rand( ) % 3;
uChoice = userChoice();
winOrLose(uChoice,num);
ans = getAns();
}
while(ans=='Y');
return 0;
}
char userChoice(void)
{
char choice;
int uChoice;
do
{
cout << "Enter r, p, or s" ;
cin >> choice; cin.clear(); cin.ignore(10,'\n');
cout<<endl<<endl;
}
while (choice != 'r'&& choice != 'p'&& choice != 's');
return choice;
if(choice == 'r')
{
uChoice = 1;
}
if(choice == 'p')
{
uChoice = 2;
}
if(choice == 's')
{
uChoice = 3;
}
return uChoice;
}
char getAns(void)
{
char ans;
do
{
ans='N';
cout<<"Do you want to play again? ";
cin>>ans;
ans=toupper(ans);
}
while(ans!='Y'&& ans!='N');
return ans;
}
void winOrLose(int uChoice, int num)
{
if (uChoice == 1 && num == 2)
{
cout << "Computer wins - PAPER covers ROCK " << endl;
}
else if (uChoice == 2 && num == 1)
{
cout << "User wins - PAPER covers ROCK" << endl;
}
else if (uChoice == 3 && num == 1)
{
cout << "Computer wins - ROCK smashes SCISSOR" << endl;
}
else if (uChoice == 3 && num == 2)
{
cout << "User wins - SCISSORS cut PAPER" << endl;
}
else if (uChoice == 1 && num == 3)
{
cout << "User wins - ROCK smashes SCISSORS" << endl;
}
else if (uChoice == 2 && num == 3)
{
cout << "Computer wins - SCISSORS cut PAPER" << endl;
}
else
{
cout << "Draw";
}
cout<<endl<<endl;
}
-
Re: Question for Rock Paper Scissors game program
Code:
while (choice != 'r'&& choice != 'p'&& choice != 's');
return choice;
Why the return here? You are now not converting choice to uchoice so this statement is not needed.
userChoice should also return an int and not a char.
-
Re: Question for Rock Paper Scissors game program
That got it! Thanks! Now, all i have to do is tally up wins/losses
-
Re: Question for Rock Paper Scissors game program
Quote:
Originally Posted by
psfign
That got it! Thanks! Now, all i have to do is tally up wins/losses
That's easy! But you should really learn how to debug your programs using the debugger once they compile but don't work as expected.
-
Re: Question for Rock Paper Scissors game program
I always have a problem with keeping score total. How do I return the wins/losses/draws to keep score each time. So am I correct that I cant return the score in a void funtion like this? Do i need to do a int function?
Code:
void winOrLose(int uChoice, int num)
{
int wins = 0;
int losses = 0;
int draws = 0;
if (uChoice == 1 && num == 2)
{
system ("cls");
losses++;
cout << "Player: ROCK\n\n"
<< "Computer: PAPER\n\n"
<< "Computer wins - PAPER covers ROCK \n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 2 && num == 1)
{
system ("cls");
wins++;
cout << "Player: PAPER\n\n"
<< "Computer: ROCK\n\n"
<< "User wins - PAPER covers ROCK\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 3 && num == 1)
{
system ("cls");
losses++;
cout << "Player: SCISSORS\n\n"
<< "Computer: ROCK\n\n"
<< "Computer wins - ROCK smashes SCISSOR\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 3 && num == 2)
{
system ("cls");
wins++;
cout << "Player: SCISSORS\n\n"
<< "Computer: PAPER\n\n"
<< "User wins - SCISSORS cut PAPER\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 1 && num == 3)
{
system ("cls");
wins++;
cout << "Player: ROCK\n\n"
<< "Computer: SCISSORS\n\n"
<< "User wins - ROCK smashes SCISSORS\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 2 && num == 3)
{
system ("cls");
losses++;
cout << "Player: ROCK\n\n"
<< "Computer: SCISSORS\n\n"
<< "Computer wins - SCISSORS cut PAPER\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 1 && num == 1)
{
system ("cls");
draws++;
cout << "Player: ROCK\n\n"
<< "Computer: ROCK\n\n"
<< "Tie\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 2 && num == 2)
{
system ("cls");
draws++;
cout << "Player: PAPER\n\n"
<< "Computer: PAPER\n\n"
<< "Tie\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 3 && num == 3)
{
system ("cls");
draws++;
cout << "Player: SCISSORS\n\n"
<< "Computer: SCISSORS\n\n"
<< "Tie\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
cout<<endl<<endl;
}
-
Re: Question for Rock Paper Scissors game program
You do it something like this
Code:
int cwin = 0,
closs = 0,
uwin = 0,
uloss = 0;
....
if (uChoice == 1 && num == 2) {
cout << "Computer wins - PAPER covers ROCK " << endl;
cwin++;
uloss++;
}.....
-
Re: Question for Rock Paper Scissors game program
i have that but it resets after every turn
-
Re: Question for Rock Paper Scissors game program
Where are you defining the variables? They should be global.
-
Re: Question for Rock Paper Scissors game program
Code:
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h.>
using namespace std;
int userChoice(void);
char getChoice(void);
void winOrLose(int,int);
char getAns(void);
int play(void);
int main()
{
char ans;
char choice;
int num;
int uChoice;
int wins = 0;
int losses = 0;
int draws = 0;
srand(time(NULL));
cout << "ROCK PAPER SCISSORS.\n\n\n"
<< "Play against your computer.\n\n"
<< " Make your selection and\n"
<< " the computer will randomly choose as well,\n"
<< " and then the game will be scored.'\n\n"
<< "Rules:\n\n"
<< " PAPER covers ROCK\n"
<< " ROCK smashes SCISSORS\n"
<< " SCISSORS cut PAPER\n\n";
system ("pause");
do
{
system ("cls");
num = 1 + rand( ) % 3;
uChoice = userChoice();
winOrLose(uChoice,num);
ans = getAns();
}
while(ans=='Y');
return 0;
}
int userChoice(void)
{
char choice;
int uChoice;
do
{
cout << "Enter your choice\n"
<< "R -Rock\n"
<< "P - Paper\n"
<< "S - Scissors\n";
cin >> choice; cin.clear(); cin.ignore(10,'\n');
cout<<endl<<endl;
}
while (choice != 'r'&& choice != 'p'&& choice != 's');
if(choice == 'r')
{
uChoice = 1;
}
if(choice == 'p')
{
uChoice = 2;
}
if(choice == 's')
{
uChoice = 3;
}
return uChoice;
}
char getAns(void)
{
char ans;
do
{
ans='N';
cout<<"Do you want to play again? ";
cin>>ans;
ans=toupper(ans);
}
while(ans!='Y'&& ans!='N');
return ans;
}
void winOrLose(int uChoice, int num)
{
int wins = 0;
int losses = 0;
int draws = 0;
if (uChoice == 1 && num == 2)
{
system ("cls");
losses++;
cout << "Player: ROCK\n\n"
<< "Computer: PAPER\n\n"
<< "Computer wins - PAPER covers ROCK \n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 2 && num == 1)
{
system ("cls");
wins++;
cout << "Player: PAPER\n\n"
<< "Computer: ROCK\n\n"
<< "User wins - PAPER covers ROCK\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 3 && num == 1)
{
system ("cls");
losses++;
cout << "Player: SCISSORS\n\n"
<< "Computer: ROCK\n\n"
<< "Computer wins - ROCK smashes SCISSOR\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 3 && num == 2)
{
system ("cls");
wins++;
cout << "Player: SCISSORS\n\n"
<< "Computer: PAPER\n\n"
<< "User wins - SCISSORS cut PAPER\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 1 && num == 3)
{
system ("cls");
wins++;
cout << "Player: ROCK\n\n"
<< "Computer: SCISSORS\n\n"
<< "User wins - ROCK smashes SCISSORS\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 2 && num == 3)
{
system ("cls");
losses++;
cout << "Player: ROCK\n\n"
<< "Computer: SCISSORS\n\n"
<< "Computer wins - SCISSORS cut PAPER\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 1 && num == 1)
{
system ("cls");
draws++;
cout << "Player: ROCK\n\n"
<< "Computer: ROCK\n\n"
<< "Tie\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 2 && num == 2)
{
system ("cls");
draws++;
cout << "Player: PAPER\n\n"
<< "Computer: PAPER\n\n"
<< "Tie\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
else if (uChoice == 3 && num == 3)
{
system ("cls");
draws++;
cout << "Player: SCISSORS\n\n"
<< "Computer: SCISSORS\n\n"
<< "Tie\n\n"
<< " Wins: "<< wins << " Losses: " << losses << " Draws: " << draws << endl;
}
cout<<endl<<endl;
}
-
Re: Question for Rock Paper Scissors game program
Code:
void winOrLose(int uChoice, int num)
{
int wins = 0;
int losses = 0;
int draws = 0;
int main()
{
char ans;
char choice;
int num;
int uChoice;
int wins = 0;
int losses = 0;
int draws = 0;
These need to be defined just once at the top of the program as global. The ones defined in main are not changed by winOrLose and the ones that are changed in winOrLose are reset every time!
-
Re: Question for Rock Paper Scissors game program
****! I shoulda saw the double declaration there. My bad! Thanks, that cleared it up!
-
Re: Question for Rock Paper Scissors game program
If you decide to throw in the lizard and spock options, your if tests in winorLose are going to get awkward. Have a look at this where there are no if tests at all for winOrLose as the result is based upon lookup table which will be easier to extend.
Code:
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cassert>
using namespace std;
#define NOITEMS 3
//The items that can be used
enum items {rock = 0, paper, scissors};
//Function prototypes
items userChoice(void);
void winOrLose(items, items);
char getAns(void);
int play(void);
char getChar(void);
//Info about the results
struct Results {
char *text; //Text to display
int wtot; //Index into totals for a win/draw
};
//Elements of totals
#define TDRAW 0
#define TCWIN 1
#define TUWIN 2
//Holds game totals
int totals[3] = {0};
//Results
Results res[] = { {"Draw", TDRAW}, //0
{"User wins - PAPER covers ROCK", TUWIN}, //1
{"User wins - SCISSORS cut PAPER", TUWIN}, //2
{"User wins - ROCK smashes SCISSORS", TUWIN}, //3
{"Computer wins - PAPER covers ROCK", TCWIN}, //4
{"Computer wins - SCISSORS cut PAPER", TCWIN}, //5
{"Computer wins - ROCK smashes SCISSOR", TCWIN}}; //6
//Outcome of play - [computer][user]
int rps[NOITEMS][NOITEMS] = {{0, 1, 6}, {4, 0, 2}, {3, 5, 0}}; //Index into res array
int main()
{
//Seed random number generator
srand((unsigned int)time(NULL));
//Output intro
cout << "ROCK PAPER SCISSORS.\n\n\n"
<< "Play against your computer.\n\n"
<< " Make your selection and\n"
<< " the computer will randomly choose as well,\n"
<< " and then the game will be scored.'\n\n"
<< "Rules:\n\n"
<< " PAPER covers ROCK\n"
<< " ROCK smashes SCISSORS\n"
<< " SCISSORS cut PAPER\n\n";
//system ("pause");
//Play the games while user still wants to
do {
//system ("cls");
winOrLose(userChoice(), (items)(rand() % 3));
} while (getAns() == 'Y');
//Output statistics of the game
int games = totals[TDRAW] + totals[TCWIN] + totals[TUWIN];
cout << endl << games << " games played" << endl;
cout << "Computer - Won: " << totals[TCWIN] << " Lost: " << games - totals[TDRAW] - totals[TCWIN] << endl;
cout << "User - Won: " << totals[TUWIN] << " Lost: " << games - totals[TDRAW] - totals[TUWIN] << endl;
cout << "Draws: " << totals[TDRAW] << endl;
return 0;
}
//Get a character from the keyboard
char getChar(void)
{
char ch;
cin >> ch;
cin.clear();
cin.ignore(10,'\n');
return (ch);
}
//Get user play choice
items userChoice(void)
{
char choice;
do {
cout << "Enter r, p, or s: " ;
choice = tolower(getChar());
cout << endl;
} while (choice != 'r' && choice != 'p' && choice != 's');
if (choice == 'r')
return rock;
if (choice == 'p')
return paper;
return scissors;
}
//Ask user to play again
char getAns(void)
{
char ans;
do {
cout << "Do you want to play again? ";
ans = toupper(getChar());
} while (ans != 'Y' && ans != 'N');
return ans;
}
//Show result and update totals
void winOrLose(items uChoice, items num)
{
assert (rps[num][uChoice] >= 0 && rps[num][uChoice] <= 6);
assert (res[rps[num][uChoice]].wtot >= 0 && res[rps[num][uChoice]].wtot <= 2);
cout << res[rps[num][uChoice]].text << endl;
totals[res[rps[num][uChoice]].wtot]++;
}
See if you can understand it.:thumb:
-
Re: Question for Rock Paper Scissors game program
Judas Priest! No, i dont understand that lol!
But, I just looked at my grade for the paper rock scissors. Got my first 100 without having to go back and make corrections! And that was the semester project. So that was the last school work, so anything from here going forward is just going to be me learning on my own. I plan to go back and try to fully understand the things that still confuse me before i go further.