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.
Printable View
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.
Update:
I'm not any closer than I was yesterday!
No...Im not posting my code. lol
I'm embarrassed this stuff isnt sticking
You should post what you have, explain where you are stuck or what is confusing. That way we can help you.
There are several structural approaches, one of which is something like
etc.Code:if(UserInput == 'R')
{
if(ComputerGuess == 1)
...
else
if(ComputerGuess == 2)
...
}
else
if(UserInput == 'S')
{
...
}
You may want to use switch statements instead of if. Either will work.
More than that and you'll need to post your code.
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;
}
OK. Look at this
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.Code:do
{
cout << "Enter r, p, or s" ;
cin >> choice; cin.clear(); cin.ignore(10,'\n');
cout<<endl<<endl;
}
while (choice != 'N');
{
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.
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;
}
Why the return here? You are now not converting choice to uchoice so this statement is not needed.Code:while (choice != 'r'&& choice != 'p'&& choice != 's');
return choice;
userChoice should also return an int and not a char.
That got it! Thanks! Now, all i have to do is tally up wins/losses
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;
}
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++;
}.....
i have that but it resets after every turn
Where are you defining the variables? They should be global.
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;
}