Question

Write a program in the Codio programming environment that allows you to play the game of...

Write a program in the Codio programming environment that allows you to play the game of Rock / Paper / Scissors against the computer. Within the Codio starting project you will find starter code as well as tests to run at each stage. There are three stages to the program, as illustrated below. You must pass the tests at each stage before continuing in to the next stage.  We may rerun all tests within Codio before grading your program. Please see the executable version of the program within the Codio project for the definitive version of what it should look like, as sometimes conversion to html to display on this page can affect how the output lines up.

The first stage sets the computer guess to always be 'R' for Rock each move. Running the program with the first stage should look like the following:

CS 141 Program #1: Rock/Paper/Scissors

Welcome to the game of Rock/Paper/Scissors where you play against

the computer. On each move the computer will choose R, P, or S, then

the user will be prompted for their choice, and then the score will

be updated. P beats R, R beats S, and S beats P. The score starts

at 0. Add one if the person wins, subtract one if the computer wins.

The game ends if the score reaches -5 or + 5.

User input of 'x' or 'X' causes the game to exit.

Here we go!

Select how the computer chooses its move:

1. Always choose Rock

2. Random guess

3. Random guess with graphical score

Enter your choice: 1

1. Your move: r

   Computer chose R

   Tie. Score: 0

2. Your move: R

   Computer chose R

   Tie. Score: 0

3. Your move: p

   Computer chose R

   User's point. Score: 1

4. Your move: S

   Computer chose R

   Computer's point. Score: 0

5. Your move: x

Exiting program...

Tie game!

Ending program...     

The second stage uses a random number generator to set the computer's guess to be 'R' for Rock, 'P' for Paper, or 'S' for Scissors.  In order for your output to match what is expected, you must do the following:
1. Get a random number using rand() % 3 to give you a result of  0, 1, or 2

2. Use the result of the previous step to decide which letter to assign to the computer's guess. If it is 0, assign 'R'. If it is 1, assign 'P'. If it is 2, assign 'S'.
Running the second stage should look like the following:

CS 141 Program #1: Rock/Paper/Scissors

Welcome to the game of Rock/Paper/Scissors where you play against

the computer. On each move the computer will choose R, P, or S, then

the user will be prompted for their choice, and then the score will

be updated. P beats R, R beats S, and S beats P. The score starts

at 0. Add one if the person wins, subtract one if the computer wins.

The game ends if the score reaches -5 or + 5.

User input of 'x' or 'X' causes the game to exit.

Here we go!

Select how the computer chooses its move:

1. Always choose Rock

2. Random guess

3. Random guess with graphical score

Enter your choice: 2

1. Your move: r

   Computer chose P

   Computer's point. Score: -1

2. Your move: P

   Computer chose P

   Tie. Score: -1

3. Your move: r

   Computer chose R

   Tie. Score: -1

4. Your move: p

   Computer chose P

   Tie. Score: -1

5. Your move: x

Exiting program...

Computer wins!

Ending program...

The third stage is the same as the second except it displays the score graphically rather than as a number. Running the third stage should look like the following:

CS 141 Program #1: Rock/Paper/Scissors

Welcome to the game of Rock/Paper/Scissors where you play against

the computer. On each move the computer will choose R, P, or S, then

the user will be prompted for their choice, and then the score will

be updated. P beats R, R beats S, and S beats P. The score starts

at 0. Add one if the person wins, subtract one if the computer wins.

The game ends if the score reaches -5 or + 5.

User input of 'x' or 'X' causes the game to exit.

Here we go!

Select how the computer chooses its move:

1. Always choose Rock

2. Random guess

3. Random guess with graphical score

Enter your choice: 3

1. Your move: r

   Computer chose P

   Computer's point.

    -5 -4 -3 -2 -1 0 1 2 3 4 5

   ----------------------------------

   ..............^

2. Your move: s

   Computer chose P

   User's point.

    -5 -4 -3 -2 -1 0 1 2 3 4 5

   ----------------------------------

   .................^

3. Your move: p

   Computer chose R

   User's point.

    -5 -4 -3 -2 -1 0 1 2 3 4 5

   ----------------------------------

   ....................^

4. Your move: s

   Computer chose P

   User's point.

    -5 -4 -3 -2 -1 0 1 2 3 4 5

   ----------------------------------

   .......................^

5. Your move: x

Exiting program...

User wins!

Ending program...

What You Need to Know

  • Environment:  How to use the codio.com environment to edit, compile and run a program.  
    On the codio.com starting page select the "Join Class" option at the upper-right. You will be prompted to enter a class token, which is: visa-compact Alternatively connect to the Codio course using this link
  • Once you are in Codio, you should use the project called: Prog1: Rock-Paper-Scissors
    Within Codio project described above you can run the solution by doing the following:
    1. Select the Tools / Terminal menu option
    2. Within the terminal window, type in:  ./solution
  • C++ Language concepts needed: simple input ( cin) and output (cout);  Using char and int variables; Incrementing a variable using ++; The assignment statement (=); How to compare variable values; Repeating a group of statements using a while loop; Choosing between alternatives using an if statement, including compound conditions; Using #include <cstdlib> so that the random number generator rand() works; Using the mod (%) operator; Breaking out of a loop using break;

Grading Rubric

The grading rubric is included in your starter code within your Codio project, and is included here as well.

55 Execution points

Option 1: (Computer always chooses R) and:

   2 User input of X immediately exits program

                3 User input of x (lower-case) immediately exits program

10 Program handles both lower and upper case user input (e.g. 'r' and 'R'); Move number increments each move

15 Score updates correctly for all user inputs: r, p, s

Option 2: Computer's move is random and:

   7 Predetermined input sequence correctly leads to a win for computer, with assessment results hidden

   8 Predetermined input sequence correctly leads to a win for computer, with assessment results hidden

      Option 3:

                  2        Correctly displays and updates the graphical score

   8       Correctly displays and updates the graphical score, with assessment results hidden

45 Programming Style (Given only if program runs substantially correctly)

10 Meaningful identifier names

10 Comments: Has header. Comments on each block of code.

15 Appropriate data and control structures. Code duplication is avoided.

10 Code Layout: Appropriate indentation and blank lines.

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

#include<iostream>
#include <string>   
#include <stdlib.h>
#include <time.h>
int getComputerChoice();
int getPlayerChoice();
bool isTie(int playerChoice,int computerChoice);
bool isPlayerWinner(int playerChoice,int computerChoice);
int main()
{

   char option;
   int computerChoice;
   int playerChoice;
   //srand funtion outside the loop
   //if we put inside the for loop same random number is generated
   //Srand will seed the random number generator to prevent random numbers from being the same every time
   srand(time(NULL));

   string computerSelection;
   string playerSelection;
  
   do
   {

       cout<<"ROCK PAPER SCISSORS MENU\n";
       cout<<"-------------------------\n";
       cout<<"p)Play Game\n";
       cout<<"q)Quit\n";
       cout<<"Enter your option\n";
       cin>>option;
       switch(option)
       {
           case 'p':

                   playerChoice =    getPlayerChoice();  
                   if(playerChoice==1)
                       cout<<"You chose: Rock\n";
                   else if(playerChoice==2)
                       cout<<"You chose: Paper\n";
                   else if(playerChoice==3)  
                       cout<<"You chose: Scissors\n";
                  
                   computerChoice = getComputerChoice();
                   if(computerChoice==1)
                       cout<<"The computer chose: Rock\n";
                   else if(computerChoice==2)
                       cout<<"The computer chose: Paper\n";
                   else if(computerChoice==3)  
                       cout<<"The computer chose: Scissors\n";
                      
                   if(isTie(playerChoice,computerChoice))  
                   {
                       cout<<"It's a TIE!\n";
                   }
                   else if(isPlayerWinner(playerChoice,computerChoice))
                   {
                       cout<<"You WIN!\n";
                   }
                   else
                   {
                       cout<<"Sorry you LOSE.\n";
                   }
                   break;
           case 'q':
                   break;
           default:

                   cout<<"Invalid Choice\n";      
       }
   }while(option!='q');

  

  
   return 0;
}

int getComputerChoice()
{

   int randnum;  
   //Generate number between 1 and 3
   randnum = 1 + (rand() % 3);
   return randnum;
}

int getPlayerChoice()
{

   int playerChoice;
   while(true)
   {

       cout<<"Rock, Paper or Scissors?\n";
       cout<<"1) Rock\n";
       cout<<"2) Paper\n";
       cout<<"3) Scissors\n";
       cout<<"Please enter your choice:\n";
       cin>>playerChoice;
       switch(playerChoice)
       {

           case 1:
               return 1;
               break;
           case 2:
               return 2;
               break;
           case 3:
               return 3;
               break;
           default:
               cout<<"Please enter 1 or 2 or 3 only\n";
       }
   }

   return playerChoice;
}

bool isTie(int playerChoice,int computerChoice)
{

   if(playerChoice==computerChoice)
       return true;
   else
       return false;
}

bool isPlayerWinner(int playerChoice,int computerChoice)
{  

   if(playerChoice==1 && computerChoice==3)
       return true;
   else if(playerChoice==3 && computerChoice==2)
       return true;
   else if(playerChoice==2 && computerChoice==1)
       return true;
   else  
       return false;
}

Add a comment
Know the answer?
Add Answer to:
Write a program in the Codio programming environment that allows you to play the game of...
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
  • 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...

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

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

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

  • Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember...

    Write a Python program (rock_paper_scissors.py) that allows two players play the game rock paper scissors. Remember the rules: 1. Rock beats scissors 2. Scissors beats paper 3. Paper beats rock The program should ask the users for their names, then ask them for their picks (rock, paper or scissors). After that, the program should print the winner's name. Note, the players may pick the same thing, in this case the program should say it's tie. when the user input an...

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

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

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • Please help I am struggling with this and 3*Math.random())+1; and how to use it within the...

    Please help I am struggling with this and 3*Math.random())+1; and how to use it within the program For this project you will write a Java program that will play a simple game of Rock, Paper, Scissors.  If you have never played this game before the rules are simple - each player chooses one of Rock, Paper or Scissors and they reveal their choices simultaneously. • The choice of Rock beats the choice of Scissors ("Rock smashes Scissors") • The choice of...

  • Write a Python program (using python 3.7.2) that lets the user play the game of Rock,...

    Write a Python program (using python 3.7.2) 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 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...

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