C++ 1)Copy the code, make sure you correct any errors, add in comments but not s
ID: 3832644 • Letter: C
Question
C++
1)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
this code is from pictures
#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.
PROGRAMMING EXAMPLE: The Game of Rock, Paper, and Scissors Children often play the game of rock, paper, and scissors. This game has two players each of whom chooses one of the three objects: rock, paper, or scissors. If player 1 chooses rock and player 2 chooses paper, player 2 wins the game because paper Watch covers the rock. The game is played according to the following rules the Video If both players choose the same object, this play is a tie If one player chooses rock and the other chooses scissors, the player choosing the rock wins this play because the rock breaks the scissors. If one player chooses rock and the other chooses paper, the player choosing the paper wins this play because the paper covers the rock. If one player chooses scissors and the other chooses paper, the player choosing the scissors wins this play because the scissors cut the paper. Write an interactive program that allows two people to play this game Input This program has two types of input: users' responses when asked to play the game. The The players' choices Output should be output as well. Two players play this game. Players enter their choices via the keyboard. Each PROBLEM ANALYSIS player enters R or r for Rock, P or p for Paper, or s or s for Scissors. While the first player enters a choice, the second player looks elsewhere. Once both entries AND ALGORITHM are in, if the entries are valid, the program outputs the players' choices and declares the winner of the play. The game continues until one of the players decides to quit DESIGN the game. After the game ends, the program outputs the total number of plays and the number of times that each player won. This discussion translates into the following algorithm: Provide a brief explanation of the game and how it is played. 2. Ask the users if they want to play the game 3 Geunavs both playersExplanation / Answer
#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
using namespace std;
int main(){
char ch;
int win = 0;
int tie = 0;
int lose = 0;
do{
int choice;
cout << "-- Lets play Rock, Paper, Scissors! --" << endl;
cout << "Press 1 for Rock, 2 for Paper, 3 for Scissors:" << endl;
cin >> choice;
int ai = rand() % 3 + 1;
cout << "The computer chose: " << ai << endl;
if(choice == 1 && ai == 1){
cout << "Rock meets Rock its a tie!" << endl;
tie++;
}
else if(choice ==1 && ai== 2){
cout << "Rock is covered by Paper the computer wins!." << endl;
lose++;
}
else if(choice == 1 && ai == 3){
cout << "Rock crushes Scissors you win!" << endl;
win++;
}
else if(choice == 2 && ai == 1){
cout << "Paper covers Rock you win!" << endl;
win++;
}
else if(choice == 2 && ai == 2){
cout << "Paper meets Paper its a tie!" << endl;
tie++;
}
else if(choice == 2 && ai == 3){
cout << "Paper is cut by Scissors the computer wins!" << endl;
lose++;
}
else if( choice == 3 && ai == 1){
cout << "Scissors are crushed by Rock computer wins!" << endl;
lose++;
}
else if( choice == 3 && ai == 2){
cout << "Scissors cuts Paper you win!" << endl;
win++;
}
else if(choice == 3 && ai == 3){
cout << "Scissors meet Scissors its a tie!" << endl;
tie++;
}
else{
cout << "You didn't select 1, 2, or 3" << endl;
}
cout << "Wins: " << win << endl;
cout << "Ties:" << tie << endl;
cout << "Losses:" << lose << endl;
cout << "Would you like to play again? Y/N" << endl;
cin >> ch;
system("CLS");
}while(ch == 'Y' || ch == 'y');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.