Question

You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

code:

#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;
int computerWin=0,userWin=0;

void playRockPaperScissor(char userChoice, char computerChoice){
   //check here for the computer wins of user win score update when tie there is no score of both
   if((computerChoice == 'r' || computerChoice == 'R') && (userChoice == 'r' || userChoice == 'R'))
   {
       cout << "It was a tie!" << endl;
   }
   else if ((computerChoice == 'p' || computerChoice == 'P') && (userChoice == 'p' || userChoice == 'P'))
   {
        cout << "It was a tie!" << endl;
   }
   else if ((computerChoice == 's' || computerChoice == 'S') && (userChoice == 's' || userChoice == 'S'))
   {
        cout << "It was a tie!" << endl;
   }
   else if ((computerChoice == 'r' || computerChoice == 'R') && (userChoice == 's' || userChoice == 'S'))
   {
        cout << "Computer won!" << endl;
        //userWin--;
        computerWin++;
   }
   else if ((computerChoice == 'p' || computerChoice == 'P') && (userChoice == 'r' || userChoice == 'R'))
   {
       //userWin--;
        computerWin++;
        cout << "Computer won!" << endl;
   }
   else if ((computerChoice == 's' || computerChoice == 'S') && (userChoice == 'p' || userChoice == 'P'))
   {
        cout << "Computer won!" << endl;
        //userWin--;
        computerWin++;
   } else {
        cout << "You won!" << endl;
        userWin++;
        //computerWin--;
   }
}

int main()
{
    char userChoice;
   char computerChoice[]="rps"; //computer choice char array
  
    int r;
    unsigned seed;
  
    do{
          //The choices
        cout << "\n===================\n";
        cout << "R. Rock\n";
        cout << "P. Paper\n";
        cout << "S. Scissors\n";
        cout << "X. Quit the game.\n";
        cout << "===================\n";
        cout << "Please enter your choice::: ";
        cin >> userChoice;
      
         //display the user choice.
        if (userChoice == 'R' || userChoice == 'r') //Rock
        {
            cout << "You picked Rock.\n";
        }
      
        else if (userChoice == 'P' || userChoice == 'p') //Paper
        {
            cout << "You picked Paper.\n";
        }
      
        else if (userChoice == 'S' || userChoice == 's') //Scissors
        {
            cout << "You picked Scissors.\n";
        }
        else if (userChoice == 'x' || userChoice == 'X') //Scissors
        {
            break;
        }else{
           cout<<"Wrong choice!";
           exit(0);
       }
      
      
        //generate random number 1 to 3
        seed = time(0);
        srand(seed); //For the random generator.
          
       r = rand() % 3 + 1; //Computers pick between 1 to 3 for r p s or R P S.
      
      
       //assign the random number to the computer choice char array to check for the r,p,s
       if (computerChoice[r] == 'r') //Rock
        {
            cout<< "Computer picked Rock.\n";
        }
      
        else if (computerChoice[r] == 'p') //Paper
        {
            cout << "Computer picked Paper.\n";
        }
      
        else if (computerChoice[r] == 's') //Scissors
        {
            cout << "Computer picked Scissors.\n";
        }
      
       playRockPaperScissor(userChoice, computerChoice[r]); //method to play Rock Paper Scissor

       //check for the computer win 3 times
       if(computerWin==3){  
           cout << "\n==================================\n";
           cout<<"*\tCongrats! Computer wins.\t*";
           cout << "\n==================================\n";
           break;
       }
       //check for the user win 3 times
       if(userWin==3){
           cout << "\n=================================\n";
           cout<<"*\tCongrats! User wins.\t*";
           cout << "\n=================================\n";
           break;
       }          
   }while(userChoice != 'x' || userChoice != 'X');
   system("pause");
   return 0;
}

sample output:


===================
R. Rock
P. Paper
S. Scissors
X. Quit the game.
===================
Please enter your choice::: s
You picked Scissors.
Computer picked Scissors.
It was a tie!

===================
R. Rock
P. Paper
S. Scissors
X. Quit the game.
===================
Please enter your choice::: s
You picked Scissors.
Computer picked Scissors.
It was a tie!

===================
R. Rock
P. Paper
S. Scissors
X. Quit the game.
===================
Please enter your choice::: s
You picked Scissors.
Computer picked Scissors.
It was a tie!

===================
R. Rock
P. Paper
S. Scissors
X. Quit the game.
===================
Please enter your choice::: s
You picked Scissors.
You won!

===================
R. Rock
P. Paper
S. Scissors
X. Quit the game.
===================
Please enter your choice::: p
You picked Paper.
Computer picked Paper.
It was a tie!

===================
R. Rock
P. Paper
S. Scissors
X. Quit the game.
===================
Please enter your choice::: r
You picked Rock.
Computer picked Paper.
Computer won!

===================
R. Rock
P. Paper
S. Scissors
X. Quit the game.
===================
Please enter your choice::: s
You picked Scissors.
Computer picked Paper.
You won!

===================
R. Rock
P. Paper
S. Scissors
X. Quit the game.
===================
Please enter your choice::: r
You picked Rock.
Computer picked Scissors.
You won!

=================================
*       Congrats! User wins.    *
=================================

Add a comment
Know the answer?
Add Answer to:
You are asked to implement scissors-rock-paper game where the players are a computer and a user....
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
  • 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...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • 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...

  • 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 python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • 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...

  • In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

    In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....

  • 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...

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