Complete and modify the code ( see code below ) or game \"Rock Paper Scissors\"
ID: 3541941 • Letter: C
Question
Complete and modify the code (see code below) or game "Rock Paper Scissors" so that the player plays against the computer (use a random genreator to generate the computer's play) and there is a record that is kept on file for the game. A record should be created if the player is playing the first time. If the player has already played before, his/her name record should be loaded into the computer and the competiton continued from there. Remember to document the program explaining what the code does and how it has been modified.
#include <iostream>
using namespace std;
enum objectType {ROCK, PAPER, SCISSORS};
//Function prototypes
void displayRules();
objectType retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
void gameResult(objectType play1, objectType play2, int& winner);
void displayResults(int gCount, int wCount1, int wCount2);
int main()
{
//Step 1
int gameCount; //variable to store the number of
//games played
int winCount1; //variable to store the number of games
//won by player 1
int winCount2; //variable to store the number of games
//won by player 2
int gamewinner;
char response; //variable to get the user's response to
//play the game
char selection1;
char selection2;
objectType play1; //player1's selection
objectType play2; //player2's selection
//Initialize variables; Step 2
gameCount = 0;
winCount1 = 0;
winCount2 = 0;
displayRules(); //Step 3
cout << "Enter Y/y to play the game: "; //Step 4
cin >> response; //Step 5
cout << endl;
while (response == 'Y' || response == 'y') //Step 6
{
cout << "Player 1 enter your choice: "; //Step 6a
cin >> selection1; //Step 6b
cout << endl;
cout << "Player 2 enter your choice: "; //Step 6c
cin >> selection2; //Step 6d
cout << endl;
//Step 6e
if (validSelection(selection1)
&& validSelection(selection2))
{
play1 = retrievePlay(selection1);
play2 = retrievePlay(selection2);
gameCount++; //Step 6e.i
gameResult(play1, play2, gamewinner); //Step 6e.ii
if (gamewinner == 1) //Step 6e.iii
winCount1++;
else if (gamewinner == 2)
winCount2++;
}//end if
cout << "Enter Y/y to play the game: "; //Step 6f
cin >> response; //Step 6g
cout << endl;
}//end while
displayResults(gameCount, winCount1,
winCount2); //Step 7
return 0;
}//end main
Explanation / Answer
#include <iostream>
#include <time>
enum Choice { Rock, Paper, Scissors };
enum Result { Draw, Win, Lose };
char Menu();
void CheckWinner(int PlayerChoice);
int main()
{
// initialize the computer's random number generator
srand(time(0));
// declare variables
char choice;
do
{
choice = Menu();
switch (choice)
{
case 'R': // fall through
case 'r': cout << " Player: Rock "; CheckWinner(Rock); break;
case 'P': // fall through
case 'p': cout << " Player: Paper "; CheckWinner(Paper); break;
case 'S': // fall through
case 's': cout << " Player: Scissors "; CheckWinner(Scissors); break;
default : cout << " ";
}
} while ((choice != 'Q') && (choice != 'q'));
// end program
return 0;
}
char Menu()
{
char MenuChoice;
cout << "Choose your weapon: ";
cout << "(R)ock ";
cout << "(P)aper ";
cout << "(S)cissors ";
cout << "(Q)uit ";
cout << "---------- ";
cout << "Choice: ";
cin >> MenuChoice;
return MenuChoice;
}
void CheckWinner(int PlayerChoice)
{
int random = rand()%3;
switch (random)
{
case Rock: cout << "Computer: Rock "; break;
case Paper: cout << "Computer: Paper "; break;
case Scissors: cout << "Computer: Scissors "; break;
}
Outcome;
if (PlayerChoice == Rock)
{
switch (random)
{
case Rock: Result = Draw; break;
case Paper: Result = Lose; break;
case Scissors: Result = Win; break;
}
}
if (PlayerChoice == Paper)
{
switch (random)
{
case Rock: Result = Win; break;
case Paper: Result = Draw; break;
case Scissors: Result = Lose; break;
}
}
if (PlayerChoice == Scissors)
{
switch (random)
{
case Rock: Result = Lose; break;
case Paper: Result = Win; break;
case Scissors: Result = Draw; break;
}
}
switch (Result)
{
case Win: cout << "Player Wins! "; break;
case Lose: cout << "Computer Wins! "; break;
case Draw: cout << "Draw "; break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.