Question

In: Computer Science

CSIS 113A C++ Programming Level1 Rock, Paper, Scissors Introduction In this assignment you will create a...

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;
}

Solutions

Expert Solution

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.


Related Solutions

Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming...
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming 1) Develop a function that prompts the user to enter their choice (1=Rock 2=Paper 3=Scissors) Return either a 1, 2, or 3 depending on the value the user has entered Do not continue the program until the user has entered a valid choice of 1, 2, 3 2) Develop a function that generates the computer player's choice Return either a 1, 2, or 3...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create the code so that when the program is run the user will see in the console the following: We are going to play rock paper scissors. Please choose 1 for Rock 2 for Paper or 3 for scissors. The program will have your choice which is the integer-valued typed in by the user and compChoice which will be the randomly generated value of either...
Rock-Paper-Scissors Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The...
Rock-Paper-Scissors Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The user inputs the number of rounds which should be positive and odd. This is a free form assignment but structure your code similar to the test cases provided. USING MATLAB ONLY!!! Program Inputs • How many rounds would you like to play (odd rounds only)?: XXX – XXX should be positive and odd. Restart the game if the input is not positive or odd....
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an int field called strength and a char field called type. You may make them either private or protected. The Tool class should also contain the function void setStrength(int), which sets the strength for the Tool. Create 3 more classes called Rock, Paper, and Scissors, which inherit from Tool. Each of these classes will need a default constructor that sets the strength to 1 and...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11 in Chapter 6 asked you to design a program that plays the Rock, Paper, Scissors game. In the program, the user enters one of the three strings—"rock", "paper", or "scissors"—at the keyboard. Add input validation (with a case-insensitive comparison) to make sure the user enters one of those strings only. Modifications: Allow the user to input "r", "p", "s" or the full strings "Rock",...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will enter an integer to determine whether they use rock, paper or scissors. Player 2 will also enter an integer to determine whether they use rock, paper or scissors. Use named constants for rock, paper and scissors and set their values as shown below. Use if-else statements to determine the results of the game. Use the named constants to compare with the player 1 and...
Create a rock paper scissors game against the computer using bunch of methods. -No two dimensional...
Create a rock paper scissors game against the computer using bunch of methods. -No two dimensional arrays -No java.util.random -No ragged arrays -Methods only, and one dimensional arrays
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT