-
April 28th, 2013, 05:09 PM
#31
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!
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, 05:14 PM
#32
Re: Question for Rock Paper Scissors game program
****! I shoulda saw the double declaration there. My bad! Thanks, that cleared it up!
-
April 28th, 2013, 05:31 PM
#33
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.
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 29th, 2013, 09:09 AM
#34
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.
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
|