Question

C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...

C++

You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid entry and waits for that to happen.

Computer randomly chooses among scissors, rock or paper at each hand.

Scissors beats paper. Rock defeats scissors and paper whips rock.

It is highly recommended to have a modular implementation. Write a function which accepts two arguments one for computer’s choice and the other for the user’s pick and returns whether computer is winner of the hand or the user. You may also write two functions where one maps scissors, rock or paper to 3 positive integers with respect to an order and the other maps the other way around.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
using namespace std;

void winner(string userChoice, string computerChoice, int& computerScore, int& userScore) {
   if (userChoice == computerChoice) {
       return;
   }
   if (userChoice == "scissors") {
       if (computerChoice == "rock") {
           computerScore++;
       }
       else {
           userScore++;
       }
       return;
   }
   else if (userChoice == "rock") {
       if (computerChoice == "scissors") {
           userScore++;
       }
       else {
           computerScore++;
       }
       return;
   }
   else {
       if (computerChoice == "scissors") {
           computerScore++;
       }
       else {
           userScore++;
       }
       return;
   }
}

int main()
{
   vector < string > names(4);
   names[1] = "scissors";
   names[2] = "rock";
   names[3] = "paper";
  
   int computerScore = 0;
   int userScore = 0;
  
   srand(time(NULL));
  
   while(true) {
       cout << "Enter users's choice : ";
       string userChoice;
       cin >> userChoice;
       if (userChoice != names[1] && userChoice != names[2] && userChoice != names[3]) {
           cout << "User's entry is not valid!" << endl;
           continue;
       }
       string computerChoice = names[rand() % 3 + 1];
       cout << "Computer's choice is : " << computerChoice << endl;
       winner(userChoice, computerChoice, computerScore, userScore);
       if (computerScore == 3) {
           cout << "The winner is computer!";
           break;
       }
       else if (userScore == 3) {
           cout << "The winner is user!";
           break;
       }
   }
      
   return 0;
}

Enter userss choice Computers choice is : scissors Enter userss choice rock scissors Computers choice is Enter userss ch

comment down for any queries

please give a thumbs up

Add a comment
Know the answer?
Add Answer to:
C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are asked to implement scissors-rock-paper game where the players are a computer and a user....

    You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user's entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid entry...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • Write a MARIE program to implement one round of rock paper scissors game. Your program should...

    Write a MARIE program to implement one round of rock paper scissors game. Your program should represent the three moves ‘rock’, ‘paper’ and ‘scissors’ with numbers 1, 2 and 3 respectively. When the program is run, there should be two input prompts, one after the other, to ask for player 1’s and player 2’s moves (a number 1, 2 or 3). Then the program would compare both numbers and apply the following rules to work out the winner. Paper beats...

  • Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock p...

    Help needed in Perl language program! Note : Perl Language Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock paper scissors) is an extremely popular hand game most often played by children. Often, it is used as a method of selection similar to flipping a coin or throwing dice to randomly select a person for some purpose. Of course, this game is not truly random since a skilled player can often recognize and exploit the non-random behavior of...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT