Question

Write C++ program, program should include at least the following functions:  getComputerGuess(): this function should...

Write C++ program,

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 enter a choice using a

menu. It then returns the user’s selected choice. Here is the menu:

“1) Rock 2)Paper 3) Scissors 4) Quit”

You should allow the game to keep playing until the user decides to quit,

that’s when the user chooses 4 from the menu.

findTheWinner(int, int): this function has two parameters (one is the

user’s choice and the other one is the computer’s randomly generated

choice). Based on the passed arguments the function should display the

user’s choice, the computer’s choice and the winner.

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

#include <cstdlib>

#include <iostream>

using namespace std;

int getComputerGuess(){

return (rand()%3);

}

void findTheWinner(int userIp, int compIp){

//Displaying the results.

cout<<"\nUSER'S CHOICE : " ;

switch(userIp){

case 1 : cout<<"Rock.\t"; break;

case 2 : cout<<"Paper.\t"; break;

case 3 : cout<<"Scissors.\t"; break;

}

cout<<"COMPUTER'S CHOICE : ";

switch(compIp){

case 1 : cout<<"Rock.\n"; break;

case 2 : cout<<"Paper.\n"; break;

case 3 : cout<<"Scissors.\n"; break;

}

  

if(!(userIp >= 1 && userIp <= 4)){

cout<<"Invalid Input";

  

}

else if(userIp == 1 && compIp == 1){

cout<<"WINNER : NO WINNER(Tie).\n\n";

}

else if(userIp == 2 && compIp == 2){//1 : User Wins & 2 : Computer Wins & 0 : Tie

cout<<"WINNER : NO WINNER(Tie).\n\n";

}

else if(userIp == 3 && compIp == 3){

cout<<"WINNER : NO WINNER(Tie).\n\n";

}

else if(userIp == 1 && compIp == 2){// Rock wins between Rock and Paper.

// Rock wins between Rock and Scissors.

// Scissors win between Paper and Scissors.

cout<<"WINNER : User.\n\n";

}

else if(userIp == 2 && compIp == 1){

cout<<"WINNER : Computer.\n\n";

}

else if(userIp == 1 && compIp == 3){

cout<<"WINNER : User";

}

else if(userIp == 3 && compIp == 1){

cout<<"WINNER : Computer.\n\n";

}

else if(userIp == 3 && compIp == 2){

cout<<"WINNER : User.\n\n";

}

else if(userIp == 2 && compIp == 3){

cout<<"WINNER : Computer.\n\n";

}

  

}

int main()

{

int userInput, computerInput;

//Displaying the Menu.

cout<<" 1) Rock.\n 2) Paper.\n 3) Scissors.\n 4) Quit.";

//Taking User Input.

cout<<"\nPlease enter your choice : ";

cin>>userInput;

//Getting Computer's Choice using rand() function.

computerInput = getComputerGuess();  

  

//Calling function to display Winner.

findTheWinner(userInput,computerInput);

while(1){

  

//Displaying the Menu.

cout<<" 1) Rock.\n 2) Paper.\n 3) Scissors.\n 4) Quit.";

//Taking User Input.

cout<<"Please enter your choice : ";

cin>>userInput;

  

//Exit Game if user enters 4.

if(userInput == 4){

exit(0);

}

  

//Getting Computer's Choice using rand() function.

computerInput = getComputerGuess();  

  

//Calling function to display Winner.

findTheWinner(userInput,computerInput);

  

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write C++ program, program should include at least the following functions:  getComputerGuess(): this function should...
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 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...

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

    (c++ only)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, the user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard using a menu in a function, userChoice, that returns a character. Next, there should be a function, computerChoice, that generates the computer’s play. A random number in the range of 1 through 3 is generated. If the...

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

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

  • (C++) Hey guys we just went over loops and it got me confused, it has me...

    (C++) Hey guys we just went over loops and it got me confused, it has me scrathcing my head for the past two days. I would appreciate any help you guys have to offer. Few places I have a hard time is get input and determine winner(cummulative part). Write a program that lets the user play the Rock, Paper, Scissors game against the computer. The computer first chooses randomly between rock, paper and scissors, but does not display its choice....

  • 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 program: Your task is to write a program which asks the user for a move...

    C program: Your task is to write a program which asks the user for a move (Rock, Paperor Scissors) and then, based on the user’s move, chooses a response (Rock, Paper or Scissors). This process continues until the user decides to exit the game. If the program “knew” the rules of the game, is would be trivial to make the program choose Paper when the user plays Rock, and Scissors when the user plays Paper, etc. Instead, your program should...

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

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

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