Question

Write an algorithm C++ Rock, Paper, Scissors Updated Take the solution to Lab 3 and make...

Write an algorithm

C++

Rock, Paper, Scissors Updated

Take the solution to Lab 3 and make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch. Make sure the user knows why he/she has won or lost – print a message like “Paper covers Rock” or “Scissors cuts Paper”.

Ask the user how many times he/she wants to play and incorporate that into a for loop or allow the user to keep playing until he/she says to stop using a do-while loop.

Add the code to count how many wins, losses and ties that the user has and print the results at the end of the program before stopping the program.

Allow the user to exit a game, not the program, if he wants to – use a code of ‘X’. Also keep a count of exits from a game. Make sure your run has at least one exit.

Also have the program determine if a user did not type in R, P, S or X and print “Bad Code” and allow the user to continue the game. Run at least once with a “Bad Code”.

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

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std;
// Function Declaration
int computerPicks();
char getUserChoice(int &exits);
char displayComputerChoice(int comp);
void playGame(char userchoice, char computerchoice,int &wins,int &losses,int &ties);
int main()
{
// Declarig the variables
char userchoice, computerchoice, ch;
string winner;
int wins=0,losses=0,ties=0,exits=0;
srand ((unsigned)time(0));
while (true)
{
userchoice = getUserChoice(exits);
if(userchoice!='X')
       {
   // Calling the function
int comp = computerPicks();
computerchoice = displayComputerChoice(comp);
playGame(userchoice, computerchoice,wins,losses,ties);      
       }
  
    cout << "\nDo you Want to play again(Y/N) ?";
cin >> ch;

if (ch == 'y' || ch == 'Y')
{

continue;
}
else
{
   cout<<"No of Wins :"<<wins<<endl;
   cout<<"No of Losses :"<<losses<<endl;
   cout<<"No of Ties :"<<ties<<endl;
   cout<<"No of Exits :"<<exits<<endl;
break;
}
     
  
}

return 0;
}
/* Function implementation which
* randomly picks the computer's choice
*/
int computerPicks()
{
int computerchoice;

// Computer randomly picking number(either 0 or 1 or 2)
computerchoice = (rand() % (3));

return computerchoice;
}
// This function will get the computer choice
char displayComputerChoice(int comp)
{
char computerchoice;
// Getting the computer choice
if (comp == 0)
computerchoice = 'R';
else if (comp == 1)
computerchoice = 'P';
else if (comp = 2)
computerchoice = 'S';

/* based on the computer choice
* the corresponding case will get executed
*/
switch (computerchoice)
{
case 'R':
cout << "Computer Picks Rock" << endl;
break;
case 'P':
cout << "Computer Picks Paper" << endl;
break;
case 'S':
cout << "Computer Picks Scissors" << endl;
break;
}
return computerchoice;
}

// This function will get the valid user choice entered by the user
char getUserChoice(int &exits)
{
char userchoice;
/* This loop continues to execute until user
* enter valid number(either 'R' or 'P' or 'S' or 'Q')
*/
while (true)
{
// Getting the users choice
cout << "Enter 'R'= Rock , 'P'=Paper , 'S'=Scissors , 'X'= Exit :";
cin >> userchoice;

// Based on the users choice the corresponding case will get executed
switch (userchoice)
{
case 'R':
case 'r':
{
cout << "\nPlayer Picks Rock" << endl;
break;
}

case 'P':
case 'p':
{
cout << "\nPlayer Picks Paper" << endl;
break;
}
case 'S':
case 's':
{
cout << "\nPlayer Picks Scissors" << endl;
break;
}
case 'x':
case 'X':
{
   cout<<"** Game Exit **"<<endl;
exits++;
break;
}

default:
{
cout << "\nBad-Code." << endl;
continue;
}
}
break;
}
  
if(islower(userchoice))
userchoice=userchoice-32;

return userchoice;
}

// This function will start the game between player and computer
void playGame(char userchoice, char computerchoice,int &wins,int &losses,int &ties)
{
// Declaring variables
char rock = 'R', paper = 'P', scissors = 'S';
string winner;
/* Based on user and computer selected numbers
* the corresponding block will get executed
*/
if (userchoice == rock && computerchoice == scissors)
{
   cout<<"Rock smashes Scissors"<<endl;
winner = ":: Player Wins ::";
wins++;
}
else if (userchoice == scissors && computerchoice == rock)
{
   cout<<"Rock smashes Scissors"<<endl;
winner = ":: Computer Wins ::";
losses++;
}
else if (userchoice == rock && computerchoice == paper)
{
   cout<<"Paper covers Rock"<<endl;
winner = ":: Computer Wins ::";
losses++;
}
else if (userchoice == paper && computerchoice == rock)
{
   cout<<"Paper covers Rock"<<endl;
winner = ":: Player Wins ::";
wins++;
}
else if (userchoice == paper && computerchoice == scissors)
{
   cout<<"Scissors cuts Paper"<<endl;
winner = ":: Computer Wins ::";
losses++;
}
else if (userchoice == scissors && computerchoice == paper)
{
   cout<<"Scissors cuts Paper"<<endl;
winner = ":: Player Wins ::";
wins++;
}
else
{
winner = ":: Draw ::";
ties++;
}

// Displaying the Result.
cout << winner << endl;
}

___________________________

Output:

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Write an algorithm C++ Rock, Paper, Scissors Updated Take the solution to Lab 3 and make...
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
  • Use Dev C++ for program and include all of the following. Im lost. Make sure the...

    Use Dev C++ for program and include all of the following. Im lost. Make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch. Make sure the user knows why he/she has won or lost – print a message like “Paper covers Rock” or “Scissors cuts Paper”. Ask the user how many times he/she wants to...

  • This is a C++ question need to complete project 2. attached the completed simple version of...

    This is a C++ question need to complete project 2. attached the completed simple version of the program below but need the updated version which is listed in the project 2 outline Project 2 Rock, Paper, Scissors Updated Take the solution to Lab 3 and make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch....

  • JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There...

    JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There are two players. Each player secretly chooses either, paper, rock or scissors. At the count of three, each player reveals what they have chosen. The winner of the game is determined this way: paper beats rock (because paper covers rock) rock beats scissors (because rock crushes scissors) scissors beat paper (because scissors cut paper) If the two players choose the same item, it is...

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

  • C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

    C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats paper; paper beats rock. You must use these specific functions in your program. These are the prototypes: char generateP2toss(); int checkThrow(char, char); void printStatistics(int, int, int, int); void finalStatistics(int, int, int, int); void welcome(); Notes on these functions: generateP2toss() will generate the computer's toss (the computer is player2). It returns either an "r", "p", or "s/" checkThrow(char char) will check the throw by passing...

  • Write a program to score the paper-rock-scissor game. Each of two users types in either P,R...

    Write a program to score the paper-rock-scissor game. Each of two users types in either P,R or S. The program call function Find winner to announces the winner. For determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or it is a tie. Be sure to allow the users to use lower case as well as uppercase letters. Your program should include a loop that lets the user play again until the user says she or he...

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

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

  • Write this program using C++ , (Rock, Paper, Scissors Game – Page 373) This programming assignment...

    Write this program using C++ , (Rock, Paper, Scissors Game – Page 373) This programming assignment is from the textbook with some slight modifications and clarifications. Here is what I am going to except from you. Your program should include at least the following functions: getComputerGuess(): this function should return a random value generated by the computer using rand function (read more about random numbers in Chapter 3 pages 126 – 128). getUsersChoice(): this function will ask the user to...

  • Write a program in python that lets the user play the game Rock, Paper, Scissors against...

    Write a program in python that lets the user play the game 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. 2 corresponds to paper, and 3 corresponds to scissors. To set up the random number library, write the following at the top of your code: import random random.seed(300) Use...

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