Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING THIS CODE Create the program Rock, Paper, Scissor Copy the code, make sure

ID: 3831502 • Letter: U

Question

USING THIS CODE

Create the program Rock, Paper, Scissor

Copy the code, make sure you correct any errors, add in comments but not steps however, explain what happens during the steps in your comments

Look at the description of what each function does and include in your comments

*********************

We are now ready to write the algorithm for the function main.

1. Declare the variables.

2. Initialize the variables.

3. Display the rules.

4. Prompt the users to play the game.

5. Get the users’ responses to play the game.

6. while (response is yes) {

a. Prompt player 1 to make a selection.

b. Get the play for player 1.

c. Prompt player 2 to make a selection.

d. Get the play for player 2.

e. If both plays are legal: { i. Increment the total game count. ii. Declare the winner of the game. iii. Increment the winner’s game win count by 1. }

f. Prompt the users to determine whether they want to play again.

g. Get the players’ responses. }

7. Output the game results.

********************************************************************************************************************************************************************************************

#include <iostream>
using namespace std;

enum objectType { ROCK,
PAPER,
SCISSORS };
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
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
//..................Function displayRules......................
void displayRules()
{
cout << " Welcome to the game of Rock, Paper, "
<< "and Scissors." << endl;
cout << " This is a game for two players. For each "
<< "game, each" << endl;
cout << " player selects one of the objects Rock, "
<< "Paper, or Scissors." << endl;
cout << " The rules for winning the game are: " << endl;
cout << "1. If both players select the same object, it "
<< "is a tie." << endl;
cout << "2. Rock breaks Scissors: So player who selects "
<< "Rock wins." << endl;
cout << "3. Paper covers Rock: So player who selects "
<< "Paper wins." << endl;
cout << "4. Scissors cuts Paper: So player who selects "
<< "Scissors wins." << endl
<< endl;
cout << "Enter R or r to select Rock, P or p to select "
<< "Paper, and S or s to select Scissors." << endl;
}
//........................................................
//................Function validSelection.................
bool validSelection(char selection)
{
switch (selection) {
case 'R':
case 'r':
case 'P':
case 'p':
case 'S':
case 's':
return true;
default:
return false;
}
}
///........................................................
//..........Function retrievePlay..........................
objectType retrievePlay(char selection)
{
objectType object;
switch (selection) {
case 'R':
case 'r':
object = ROCK;
break;
case 'P':
case 'p':
object = PAPER;
break;
case 'S':
case 's':
object = SCISSORS;
}
return object;
}
//....................................................................
//.....................Function gameResult...........................
void gameResult(objectType play1, objectType play2,
int& winner)
{
objectType winnerObject;
if (play1 == play2) {
winner = 0;
cout << "Both players selected ";
convertEnum(play1);
cout << ". This game is a tie." << endl;
}
else {
winnerObject = winningObject(play1, play2);
//Output each player's choice
cout << "Player 1 selected ";
convertEnum(play1);
cout << " and player 2 selected ";
convertEnum(play2);
cout << ". ";
//Decide the winner
if (play1 == winnerObject)
winner = 1;
else if (play2 == winnerObject)
winner = 2;
//Output the winner
cout << "Player " << winner << " wins this game."
<< endl;
}
}
//............................................................
//.......................Function convertEnum.................
void convertEnum(objectType object)
{
switch (object) {
case ROCK:
cout << "Rock";
break;
case PAPER:
cout << "Paper";
break;
case SCISSORS:
cout << "Scissors";
}
}
//..............................................................
//....................Function winningObject....................
objectType winningObject(objectType play1, objectType play2)
{
if ((play1 == ROCK && play2 == SCISSORS)
|| (play2 == ROCK && play1 == SCISSORS))
return ROCK;
else if ((play1 == ROCK && play2 == PAPER)
|| (play2 == ROCK && play1 == PAPER))
return PAPER;
else
return SCISSORS;
}
//...............................................................
//...................Function displayResults.....................
void displayResults(int gCount, int wCount1, int wCount2)
{
cout << " The total number of plays: " << gCount
<< endl;
cout << "The number of plays won by player 1: "
<< wCount1 << endl;
cout << "The number of plays won by player 2: "
<< wCount2 << endl << endl;
}
//................................................................
int main()
{
int count1 = 0, count2 = 0, win, count = 0;
char choice,c1,c2;
displayRules();
do {
cout << "First Player input: ";
// read user 1 input
scanf(" %c", &c1);
objectType obj1 = retrievePlay(c1);
cout << "Second Player input: ";
// read user 2 input
scanf(" %c", &c2);
objectType obj2 = retrievePlay(c2);
// finding result
gameResult(obj1, obj2, win);
// win is 1 user 1 win else user2 wins
if (win == 1)
count1++;
else
count2++;
count++; // increment the game count
// display the result
displayResults(count, count1, count2);
cout << "press 'y' to continue or any other key to exit : ";
scanf(" %c", &choice);
} while (choice == 'Y' || choice == 'y');
return 0;
}

*****************************************************************************************************************************************************************************

#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 //Place the definitions of the functions displayRules, //validSelection, retrievePlay, convertEnum, winningObject, //gameResult, and displayResults as described previously here.

Explanation / Answer

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <limits>

class Player
{
public:

Player(std::string const& name) : name(name), hands_won(0) {}

virtual Player() {}

virtual void play() = 0;

virtual void printWinningMessage() = 0;

std::string hand;
std::string name;
int hands_won;
};

class HumanPlayer : public Player
{
public:

HumanPlayer(std::string const& name) : Player(name) {}

virtual void play()
{
std::cout << " What will you chose? rock, paper, or scissors? ";
std::cin >> hand;//player makes choice
}

virtual void printWinningMessage()
{
std::cout << "You win!" << std::endl;
}
};

class ComputerPlayer : public Player
{
public:

ComputerPlayer(std::string const& name) : Player(name) {}

void play()
{
std::string choices[3]{"rock", "paper", "scissors"};
hand = choices[std::rand()%3]; //select rock, paper, or scissors at random
}

virtual void printWinningMessage()
{
std::cout << name << " wins!" << std::endl;
}
};

enum TopLevelChoice { PLAY_GAME, EXIT_GAME };
enum PlayMode { HUMAN_VS_COMPUTER, COMPUTER_VS_COMPUTER };

TopLevelChoice get_toplevel_choice();
PlayMode get_play_mode();
void play_game();
void play_game(Player& player1, Player& player2);
void play_human_vs_computer();
void play_computer_vs_computer();
void pick_round_winner(Player& player1, Player& player2);
void print_winner(Player& player1, Player& player2);
bool is_game_over(Player& player1, Player& player2);

int main()
{
std::srand(time(NULL));
while ( get_toplevel_choice() != EXIT_GAME )
{
play_game();
}
return 0;
}

TopLevelChoice get_toplevel_choice()
{
std::cout << "--------------------------------------------------- ";
std::cout << "Welcome to Rock, Paper, Scissors. To play, type '1'. To exit, type '2'. ";
std::cout << "--------------------------------------------------- ";
int choice;
std::cin >> choice;
if ( std::cin ) // Reading was successful.
{
switch (choice)
{
case 1:
    return PLAY_GAME;
case 2:
    return EXIT_GAME;
default:
    std::cout << "You made an invalid choice. Try again... ";
}
return get_toplevel_choice();
}
else
{
std::cout << "You made an invalid choice. Try again... ";


std::cin.clear();

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');


return get_toplevel_choice();
}
}

PlayMode get_play_mode()
{
std::cout << "------------------------------------------- ";
std::cout << "Select a mode: ";
std::cout << "1. human vs computer 2. computer vs computer ";
std::cout << "------------------------------------------- ";
int mode;
std::cin >> mode;
if ( std::cin )
{
switch (mode)
{
case 1:
    return HUMAN_VS_COMPUTER;
case 2:
    return COMPUTER_VS_COMPUTER;
default:
    std::cout << "You made an invalid choice. Try again... ";
}
return get_play_mode();
}
else
{
std::cout << "You made an invalid choice. Try again... ";


std::cin.clear();

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

return get_play_mode();
}
}

void play_game()
{
PlayMode choice = get_play_mode();
if ( choice == HUMAN_VS_COMPUTER )
{
play_human_vs_computer();
}
else
{
play_computer_vs_computer();
}
}

void play_game(Player& player1, Player& player2)
{
do
{
player1.play();
player2.play();


std::cout << std::endl;
std::cout << player1.name << " chose : " << player1.hand << std::endl;
std::cout << player2.name << " chose : " << player2.hand << std::endl;

if (player1.hand == player2.hand)
{

std::cout << "        It's a draw!";
continue;
}


pick_round_winner(player1, player2);

} while ( !is_game_over(player1, player2));

print_winner(player1, player2);
}


void play_human_vs_computer()
{
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;

HumanPlayer humanPlayer(name);
ComputerPlayer computerPlayer("Computer");
play_game(humanPlayer, computerPlayer);
}

void play_computer_vs_computer()
{
ComputerPlayer computerPlayer1("Computer 1");
ComputerPlayer computerPlayer2("Computer 2");
play_game(computerPlayer1, computerPlayer2);
}

void pick_round_winner(Player& player1, Player& player2)
{

if ( (player1.hand == "rock" && player2.hand == "scissors") ||
(player1.hand == "scissors" && player2.hand == "paper") ||
(player1.hand == "paper" && player2.hand == "rock") )
{
player1.printWinningMessage();
player1.hands_won += 1;
}
else
{
player2.printWinningMessage();
player2.hands_won += 1;
}
}

void print_winner(Player& player1, Player& player2)
{
Player& winner = (player1.hands_won == 2) ? player1 : player2;
std::cout << "Congratulations, " << winner.name << ". You win the game! ";
}

bool is_game_over(Player& player1, Player& player2)
{
return (player1.hands_won == 2 || player2.hands_won == 2);
}