-
April 22nd, 2013, 04:07 PM
#16
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.
-
April 23rd, 2013, 04:40 PM
#17
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
-
April 23rd, 2013, 05:33 PM
#18
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.
-
April 23rd, 2013, 05:51 PM
#19
Re: Question for Rock Paper Scissors game program
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.
-
April 28th, 2013, 12:08 AM
#20
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;
}
-
April 28th, 2013, 06:06 AM
#21
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2013, 11:40 AM
#22
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;
}
Last edited by psfign; April 28th, 2013 at 11:42 AM.
-
April 28th, 2013, 01:28 PM
#23
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2013, 02:19 PM
#24
Re: Question for Rock Paper Scissors game program
That got it! Thanks! Now, all i have to do is tally up wins/losses
-
April 28th, 2013, 03:16 PM
#25
Re: Question for Rock Paper Scissors game program
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2013, 03:39 PM
#26
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;
}
-
April 28th, 2013, 04:15 PM
#27
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++;
}.....
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2013, 04:26 PM
#28
Re: Question for Rock Paper Scissors game program
i have that but it resets after every turn
-
April 28th, 2013, 04:36 PM
#29
Re: Question for Rock Paper Scissors game program
Where are you defining the variables? They should be global.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2013, 04:39 PM
#30
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|