Question

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 do so the results are usually very satisfying with no room for fighting or error.

Although the game has a lot of complexity to it, the rules to play it are pretty simple.
The game is played where players deliver hand signals that will represent the elements of the game; rock, paper and scissors. The outcome of the game is determined by 3 simple rules:

  • Rock wins against scissors.
  • Scissors win against paper.
  • Paper wins against rock.

1. Write rps1-algorithm.txt to play rock, paper, scissors game. For reference see: www.worldrps.com
The 2-player game will be interactive: human vs. computer!
The game will replay until the human player decides to quit. Human input will be through the
keyboard, entering R or r for rock, P or p for paper, S or s for scissors, or Q or q to quit. Computer
input will be randomly generated (0-rock, 1-paper, 2-scissors).

Add score keeping to the game to track the numbers of computer wins and human wins -- do not
count ties. Output the numbers of computer wins and human wins after all play ends.
play game:
-->>generate a random number between 0 to 2 for computer's choice of rock, paper, or scissors
prompt for userPlay
input userPlay
randomly generate computerPlay
determine winner
output computerPlay and userPlay and who won
if user won, add 1 to userScore, else if computer won, add 1 to computerScore
output computerScore and userScore with appropriate labels
prompt for userPlay
if human wants to quit, then END
-->>repeat

Make sure you have ALL the possible test cases!

2. Write rps1.cpp from rps1-algorithm.txt
In the coding for this game you will use code blocks, loops, and if and/or switch structures. Organizing
code into "code blocks" will prepare you for moving code blocks into functions in future studies.
Functions for:
--introduction (void function)
--computerPlay (value returning function)
--userPlay (value returning function)
--determine winner and keep score (value returning function)

Develop the program by translating the algorithm code block by code block, function by function in the order needed. Develop functions as a test program.

For example:
Welcome to the game of Rock, Paper, Scissors. You will be asked to choose either R=rock or P=paper or S=scissors, and the computer will do the same. The winner is determined by Rock wins against Scissors, or Scissors wins against Paper, or Paper wins against Rock. The game winner is the player with the most scores. The best player is the player with the highest overall score.


Choose: [R=rock, P=paper, S=scissors, or Q=quit]: p
Computer: S, Human: P, Computer wins

Choose: [Rock,Paper,Scissors,Quit]: p
Computer:P, Human:P, tie

Choose: [R=rock, P=paper, S=scissors, or Q=quit]: r
Computer: S, Human: R, you win!
Computer wins: 1 Human wins: 1

Choose: [R=rock, P=paper, S=scissors, or Q=quit]: p
Computer: R, Human: P, you win!
Computer wins: 1 Human wins: 2

Choose: [R=rock, P=paper, S=scissors, or Q=quit]: s
Computer: P, Human: S, you win!
Computer wins: 1 Human wins: 3

Choose: [Rock,Paper,Scissors,Quit]: q

Thanks for playing!

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

Here is the answer ,

1. algorithm

Step 1 ; declare the functions , for intorductio, userplay , computerplay and determine winner

step 2 : declare the variables for storing user input and computer random number as user_option as charactor and computer_option as integer

step 3 : initailize computer score and user score to zero

step 4 : call introduction function

step 5 : call userPlay() function and assign the retrun value to user_option

step 6 : call computerPlay() function and assign the retrun value to computer_option

step 7 : call the determineWinner function with argurments as computer_option and user_option and assign the resutl to a variable called result

step 8 : if the resutl is 1 add one to the the user score or if it is 0 then add one to computer score , if it is 2 then it is a tie no score added

step 9 : call userPlay() function again and stroe it in user_option

step 10 : if user_option not equals to q or Q then go to step 6

step 11 : stop

functions

userPlay()

setp 1 : start

step 2 : prompt for user input and validate the input ,

step 3 : return user input

function computerPlay

step 1: start

step 2 : use rand() funciton to get a random number and take the remainder by dividingn the random number by 3

sterp 3 : return the remainder

step 4 : stop

function determineWinner(computer-option,user-option)

step 1 : start

step 2 : if user and computer make the same option return 2 and print tie

step 3 : check if the computer option is 0 and user input is s ,then Rock wins against scissors return 0

step 4 : check if the computer option is 2 and user input is p ,then Scissors win against paper return 0

step 5 : check if the computer option is 1 and user input is r ,then Paper wins against rock return 0

step 6 : else return 1 , user wins

step 7 stop

cpp code , i have commented every line , if you have any doubts please ask in the comment

#include <iostream>

using namespace std;

// delcare all functions
void introduction();
int computerPlay();
char userPlay();
int determineWinner(int com_o, char user_o);

int main() {

// delclare all variables
int com_option;
char hu_option;
// make user score and computer score as 0
int user_score=0;
int com_score = 0;
int result;

// call the introduction function
introduction();
// userPlay() function returns the user option and store it in variable hu_option
hu_option = userPlay();
// while loop is to check if the user decide to quit , ie user press q or Q
while((hu_option !='Q' && hu_option !='q')){  
// calls the computerPlay() function that returns a random number from 0 - 2
com_option = computerPlay();
// determineWinner() function returns 0 if computer wins and 1 if user wins or return 2 if it a tie
result = determineWinner(com_option,hu_option);
// increament user score if human wins
if(result == 1){
     user_score +=1;
     cout<<"\n Computer wins: " << com_score <<" Human wins: "<<user_score;
}else if(result == 0){
     // increament computer score by 1 if computer wins
     com_score +=1;
}
// repeat the game
hu_option = userPlay();

}
// if user exit from the game show
cout<<"\n Thanks for playing!\n";

}
// introduction function just prints welcome msg
void introduction (){
    cout<<"Rock Paper Scissors \n";
}
// function computer play returns random number from 0-2
// function rand() retruns a random number , if we take mode (%) 3 of any number then it returs the remainder
// ie if number is 8 ,then 8 % 3 = is 2 , ie 8/3 = 2 and 2 is the remainder, 9 % 3 = 0; and 1 % 3 = 1;
int computerPlay (){
    return rand() % 3;
}
// function userPlay() returns a valid user option
char userPlay (){
    char hu_option;
    cout<<"\n Choose: [R=rock, P=paper, S=scissors, or Q=quit]:";
    cin>>hu_option;
    // this while ensures that user enters a valid option ,ie any other charactor entered then promt angain for the charactor
    while(!(hu_option =='R'||hu_option =='r'||hu_option =='P'||hu_option =='p'||hu_option =='S'||hu_option =='s'||hu_option =='Q'||hu_option =='q')){
        cout<<"\n Wrong option please choose again \n";
        cout<<"\n Choose: [R=rock, P=paper, S=scissors, or Q=quit]:";
        cin>>hu_option;
    };
    return hu_option;
}
// function that determines the winner
int determineWinner(int com_o, char user_o){
  
    char com_option;
    switch(com_o){
        case 0 :
         com_option = 'R';break;
        case 1 :
         com_option = 'P';break;
        case 2 :
         com_option = 'S';break;
    }
    // check if the computer and user selects the same optin then print tie
    if(com_o ==0 && (user_o == 'r' || user_o == 'R')){
        cout<<"\n Computer: "<<com_option<<", Human: " << user_o<<"tie\n";
        return 2;
    }
    else if(com_o ==1 && (user_o =='p' || user_o == 'P')){
        cout<<"\n Computer: "<<com_option<<", Human: " << user_o<<", tie\n";
        return 2;
    }
    else if(com_o ==2 && (user_o == 's' || user_o == 'S')){
        cout<<"\n Computer: "<<com_option<<", Human: " << user_o<<", tie\n";
        return 2;
    }
    //Rock wins against scissors
    else if(com_o == 0 && (user_o == 's'||user_o == 'S')){
       cout<<"\n Computer: "<<com_option<<", Human: " << user_o<<", Computer wins";
        return 0;
    }
    //Scissors win against paper.
    else if(com_o == 2 && (user_o == 'p'||user_o == 'P')){
        cout<<"\n Computer: "<<com_option<<", Human: " << user_o<<", Computer wins";
        return 0;
    }
    //Paper wins against rock.
    else if(com_o == 1 && (user_o == 'r'||user_o == 'R')){
        cout<<"\n Computer: "<<com_option<<", Human: " << user_o<<", Computer wins";
        return 0;
    }
  
        cout<<"\n Computer: "<<com_option<<", Human: " << user_o<<", User wins";
        return 1;
  
}

Out put

Rock Paper Scissors Choose: [R=rock, P=paper, S=scissors, or Q=quit]:p Computer: P, Human: P, tie Choose: [R=rock, P=paper, S=scissors, or Q=quit]:r Computer: P, Human: r, Computer wins Choose: (R=rock, P=paper, S=scissors, or Q=quit]:s Computer: R, Human: s, Computer wins Choose: (R=rock, P=paper, S=scissors, or Q=quit]:s Computer: P, Human: s, User wins Computer wins: 2 Human wins: 1 Choose: (R=rock, P=paper, S=scissors, or Q=quit]:9 Thanks for playing!

Add a comment
Know the answer?
Add Answer to:
This program should be in c++. Rock Paper Scissors: This game is played by children and...
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 computer game based on rock-paper- scissors in C++. The 2-player game will be interactive:...

    Write a computer game based on rock-paper- scissors in C++. The 2-player game will be interactive: human vs. computer! Making the game interactive presents a new issue for us -- how do we get the computer&#39;s choice? To solve this, we will use a &quot;random number generator&quot;, which is the basis for all computer gaming. This enables the properly programmed computer to act like it is actually &quot;thinking&quot; and making its own decisions. This is the largest, most involved program...

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

  • Rock, Paper, Scissors - Write a MATLAB script file to play Rock, Paper, Scissors with the...

    Rock, Paper, Scissors - Write a MATLAB script file to play Rock, Paper, Scissors with the computer. x=rand; if x< 1/3 then it is Rock, if l/3<= x < 2/3 it is Paper else it is Scissors end if This is how the game should look like on the screen: Enter "r" for Rock, "p" for Paper, or "s" for Scissors", or enter "q" to Quit the game r leftarrow say, this is what you entered Computer choice: Scissors RESULT:...

  • Create a python program to play rock paper scissors using a while loop and if statements....

    Create a python program to play rock paper scissors using a while loop and if statements. I have started but have gotten stuck in a continuous loop. Please look over my code and help me find where I went wrong. Here it is: import random #Choice weapons=['Rock' ,'Paper' ,'Scissors'] print('Rock, Paper, Scissors!') print('Rock, Paper, Scissors!') print('Shoot!') human=input('Choose Rock, Paper, Scissors, or Quit! ') print('')#Blank Line while human != 'Quit': human_choice=human computer=random.choice(weapons) print(computer) if human==computer: print("It's a tie!") elif human=='Rock': if...

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

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

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

  • Objective: Write a program that simulates a game of rock, paper, scissors between a human and...

    Objective: Write a program that simulates a game of rock, paper, scissors between a human and the computer in best 2 out of 3 rounds. Requirements: . The player can enter either "rock", "paper", or "scissors'". o If the player enters anything other than that the computer automatically gets a point . The computer randomly selects one of the gestures o Use the Random type to make this easier o Also make sure you import java.util.Random o You can use...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates 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