In: Computer Science
CSIS 113A C++ Programming Level1
Rock, Paper, Scissors
Introduction
In this assignment you will create a program that will simulate the popular children’s game “Rock, Paper, Scissors”; using only Boolean logic and practice using the for-loop and logical operators as well as continue getting used to data of various types.
Skills: string input/output, data types, logical operators, and control structures
Rock-Paper-Scissors
Rock-Paper-Scissors is a game where two players, on the count of 3, show the rock/paper/scissors symbol with their hand. Rock smashes scissors, scissors cuts paper, and paper covers rock:
Once both players have played valid gestures, there are two ways to determine the winner: one using string comparison and Boolean logic and the other using modular arithmetic; you’ll write a solution using both approaches. For this assignment, only “rock”, “paper”, and “scissors” are considered valid gestures.
Definitions:
1. Valid game: a game in which both players select a valid gesture
2. Invalid game: a game in which a player selects a gesture that is not “rock”, “paper”, or “scissors”. (HINT: DeMorgan’s law will help negation of valid game)
main.cpp
Your program should use a for-loop to play 7 consecutive games (must be a variable) of Rock-PaperScissors determining the winner using only the four decision statements. For each game, your program should:
1) Prompt Player 1 for their selection. The inputs for each game are given below. a. If Player 1 enters an invalid selection you should update the count of invalid games and then begin a new game.
2) Prompt Player 2 for their selection. The inputs for each game are given below.
a) If Player 2 enters an invalid selection you should update the count of invalid games and then begin a new game.
3) If both players have selected a valid gesture determine the winner using string comparison and if/else logic.
4) For each valid game completed, update the count of the number of wins by Player 1, the number of wins by Player 2, or the number of ties as needed.
Test input:
• Game 1: Player one plays “scissors” and Player 2 plays “paper”
• Game 2: Player 1 plays “paper” and Player 2 plays “papers”
• Game 3: Player 1 plays “rock” and Player 2 plays “rock”
• Game 4: Player 1 plays “rocks”
• Game 5: Player 1 play “paper” and Player 2 plays “scissors”
• Game 6: Player 1 plays” scissors” and Player 2 plays “rock”
• Game 7: “Player 1 plays “paper” and Player 2 plays “rock”
Test Run: Your code should look exactly like the one below
Player 1 Enter Selection: scissors
Player 2 Enter Selection: paper
Player 1 Enter Selection: paper
Player 2 Enter Selection: papers
Player 1 Enter Selection: rock
Player 2 Enter Selection: rock
Player 1 Enter Selection: rocks
Player 1 Enter Selection: paper
Player 2 Enter Selection: scissors
Player 1 Enter Selection: scissors
Player 2 Enter Selection: rock
Player 1 Enter Selection: paper
Player 2Enter Selection: rock
=============================
Games Won
=============================
Player 1: 2
Player 2: 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tie Games : 1
Invalid Games : 2
=============================
struct Player{ String gesture; int wins{}; }; int main() { // TODO: (1) declare constant for the number of games to play // TOOO: (2) declare and initailize your other counters here (number of ties, invalid games) Player player_1; Player player_2; // TODO: (12) wrap in for-loop once working for 1 game. // TODO: (3) get user input for player 1, commented out correct code for now, simulating with rock first player_1.gesture = "rock"; // cout << "Player 1 enter selection: "; // getlline(cin, player_1.gesture); // TODO: (5) write a boolean proposition here, finish what it should be, notice how the name reads bool gesture_is_rock; bool gesture_is_paper; bool gesture_is_scissors; bool is_valid_gesture; // TDOO: (6) determine if player 1 entered invalid move if (!is_valid_gesture) { // TODO: (7) what should be done if invalid? How do you continue the loop } // TODO: (8) Do the exact same for Player 2 reusing the boolean variables // TODO: (9) Determine if there was a tie, otherwise, there was a win bool is_tied_game; if (is_tied_game) { // TODO: (10) what should be done if tie? } else { // TODO: (11) consider how player one can win: bool player_1_wins; if (player_1_wins) { } else { } } // TODO: (4) Setup the way the output should be displayed based on the document return 0; }
The C++ code for the above problem is:
#include <iostream>
using namespace std;
struct Player {
string gesture;
int wins{};
};
int main() {
const int no_of_games = 7;// constant for the number of games to play
int no_of_ties = 0, invalid_games = 0;// number of ties and invalid games are declared and initailized
Player player_1;
Player player_2;
int i;
for(i = 0; i < 7; i++) {// wrapping in for-loop
cout << "Player 1 enter selection: ";
getline(cin, player_1.gesture); // get user input for player 1
// boolean proposition
bool gesture_is_rock = player_1.gesture == "rock";
bool gesture_is_paper = player_1.gesture == "paper";
bool gesture_is_scissors = player_1.gesture == "scissors";
bool is_valid_gesture = gesture_is_rock + gesture_is_paper + gesture_is_scissors;
// validate if player 1 entered invalid move
if (!is_valid_gesture) {
invalid_games++;
continue;
}
cout << "Player 2 enter selection: ";
getline(cin, player_2.gesture); // get user input for player 2
// doing the exact same for Player 2 reusing the boolean variables
gesture_is_rock = player_2.gesture == "rock";
gesture_is_paper = player_2.gesture == "paper";
gesture_is_scissors = player_2.gesture == "scissors";
is_valid_gesture = gesture_is_rock + gesture_is_paper + gesture_is_scissors;
// validate if player 2 entered invalid move
if (!is_valid_gesture) {
invalid_games++;
continue;
}
// determine if there was a tie, otherwise, there was a win
bool is_tied_game = player_1.gesture == player_2.gesture;
if (is_tied_game)
no_of_ties++;
else {
// ways player one can win
bool player_1_wins = (player_1.gesture == "rock" && player_2.gesture == "scissors") ||
(player_1.gesture == "scissors" && player_2.gesture == "paper") ||
(player_1.gesture == "paper" && player_2.gesture == "rock");
if (player_1_wins)
player_1.wins++;
else
player_2.wins++;
}
}
// output displayed
cout << "=============================\n";
cout << "Games Won\n";
cout << "=============================\n";
cout << "Player 1: "<< player_1.wins << endl;
cout << "Player 2: "<< player_2.wins << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "Tie Games: "<< no_of_ties << endl;
cout << "Invalid Games: "<< invalid_games << endl;
cout << "=============================\n";
return 0;
}
The sample of the code is:
The output sample of the code is:
Comment down if you have any queries regarding the code.