Question

(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. It displays a menu of choices: rock, paper, scissors, or quit, and prompts the user to select a choice. If the user’s input does not match a choice on the menu, the program displays the menu again and prompts the user to reenter (You can assume the user always enters an integer). It then determines who is the winner, then displays the user’s and computer’s choice and a message to indicate the winner (user or computer or tie). The rules to determine the winner are: rock beats scissors, scissors beats paper and paper beats rock. The program loops to allow the user to play repeatedly multiple games, and at each iteration of the loop, the cumulative scores are displayed (i.e. how many times the user won and how many times the computer won). The user has the option to quit at the end of a game.

Additional requirements –Make sure you meet all the requirements to avoid losing points

Follow the requirements in the “Homework Notes Function Headers”.

You are required to implement your program with at least 3 functions that are called in the main() function. You are allowed to implement additional functions to make your program more modular.

getUserChoice: displays the menu, prompts the user to enter the choice, performs input validation, displays the menu again if the input is invalid. Returns the user’s choice (rock, paper, scissors or quit) if it is valid.

getComputerChoice: uses the random number generator to randomly select a choice and returns the choice.

determineWinner: takes as arguments the user’s choice and the computer’s choice and determines the winner. Prints the user’s and computer’s choices and who is the winner (computer or user or tie). This function also prints the user’s and computer’s cumulative score (i.e. how many times the user won and how many times the computer won).

Your program must generate a different set of random values each time it is executed.

Your main() function will have two parts: (1) a driver to demonstrate your determineWinner function is correct, and (2) the game itself

// Driver part

Loop over the possible computer choices

Loop over the possible user choices // Nested loop

Call determineWinner

End loop over the user choices

End loop over the computer choices

// Note: The cumulative scores should not be reinitialized to zero when the driver part is completed

// Game part

call getComputerChoice

call getUserChoice

loop as long as user’s choice is not Quit

call determineWinner

call getComputerChoice

call getUserChoice

end loop

Driver Implementation You should represent the choices (rock, paper, scissors) as integers. For example 0, 1 and 2 represent rock, paper and scissors respectively. For the computer choice, a random number in the range 0 to 2 can be generated through the formula on slide 69, chapter 3. For consistency across the submissions to make the grading task more efficient, copy and paste this code for the driver part:

for (int computerChoice = 0; computerChoice < 3 ; computerChoice++)

for (int userChoice = 0; userChoice < 3; userChoice++)

{

      determineWinner(userChoice, computerChoice);

}

Driver part Computer choice is rock user choice is rock It is a tie User score E 0, computer score E Computer choice is rock, user choice is paper You win! User score 1, computer score 0 Computer choice is rock, user choice is scissors Computer wins User score 1, computer score 1 Computer choice is paper user choice is rock Computer wins User score 1, computer score 2 Computer choice is paper, user choice is paper It is a tie User score 1, computer score 22 Computer choice is paper, user choice is scissors You win! User score F 2, computer score 2 Computer choice is scissors user choice is rock You win! User score 3, computer score 2 Computer choice is scissors, user choice is paper Computer wins User score E 3, computer score F Computer choice is scissors user choice is scissors It is a tie User score E 3, computer score 3 Game part Menu of choices 0 Rock 1 Paper Scissors Quit

My program

#include
#include
#include

using namespace std;

int getUserChoice();
int getComputerChoice();
int determineWinner(int, int);

int main()
{
// Driver part
cout <<"Driver part\n";
cout << "=========\n";

for (int computerChoice = 0; computerChoice < 3 ; computerChoice++)
for (int userChoice = 0; userChoice < 3; userChoice++)
{
determineWinner(userChoice, computerChoice);
}
// Game part
cout <<"\nGame part\n";
cout << "=========\n";
getComputerChoice();
getUserChoice();
return 0;
}

int getUserChoice()
{
int choice;
while (true)
{
cout << "Menu of choices\n";
cout << "----------------\n";
cout << "0 - Rock\n";
cout << "1 - Paper\n";
cout << "2 - Scissors\n";
cout << "3 - Quit\n";
cin >> choice;

switch(choice)
{
case 0:
return 0;
break;
case 1:
return 1;
break;
case 2:
return 2;
break;
case 3:
exit(0);
break;
case 4:
if (choice != 0 || choice != 1|| choice != 2)
return true;

}
}
return choice;
}

int getComputerChoice()
{
unsigned int seed = time(0);
srand(seed);
//Generate number between 0 and 2
int randomNum = rand() % 3;
return randomNum;
}

int determineWinner(int userChoice, int computerChoice)

{

if (userChoice == 0 && computerChoice == 0)
{
cout << "Computer choice is rock, user choice is rock\n";
cout << "It is a tie!\n";
}

else if (userChoice == 1 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is paper\n";
cout << "It is a tie!\n";
}

else if (userChoice == 2 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is scissors\n";
cout << "It is a tie!\n";
}

else if (userChoice == 0 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is rock\n";
cout <<"You win!\n";
}

else if (userChoice == 1 && computerChoice == 0)
{
cout << "Computer choice is rock, user choice is paper\n";
cout <<"You win!\n";
}

else if (userChoice == 2 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is scissors\n";
cout <<"You win!\n";
}

else if (userChoice == 0 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is rock\n";
cout <<"Computer wins!\n";
}

else if (userChoice == 2 && computerChoice == 0)
{
cout << "Computer choice is rock, user choice is scissors\n";
cout <<"Computer wins!\n";
}

else if (userChoice == 1 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is paper\n";
cout <<"Computer wins!\n";
}
}

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

#include<iostream>
#include<string>
//#include<>
using namespace std;
int getUserChoice();
int getComputerChoice();
int determineWinner(int, int);
int main()
{
   // Driver part
   cout <<"Driver part\n";
   cout << "=========\n";
   for (int computerChoice = 0; computerChoice < 3 ; computerChoice++)
       for (int userChoice = 0; userChoice < 3; userChoice++)
       {
           determineWinner(userChoice, computerChoice);
       }
   // Game part
   cout <<"\nGame part\n";
   cout << "=========\n";
   int youWin =0;
   int compWin = 0;
   int tie =0;
   while(true){
       int computerChoice = getComputerChoice();
       int userChoice = getUserChoice();
       if(userChoice==3)
           break;
       int result = determineWinner(userChoice, computerChoice);
       if(result==0){
           tie++;
       }
       if(result==1){
           youWin++;
       }
       if(result==-1){
           compWin++;
       }
       cout<<"Your Score = "<<youWin<<"\tComputer Score = "<<compWin<<endl;
   }
   return 0;
}
int getUserChoice()
{
   int choice;
   while (true)
   {
       cout << "Menu of choices\n";
       cout << "----------------\n";
       cout << "0 - Rock\n";
       cout << "1 - Paper\n";
       cout << "2 - Scissors\n";
       cout << "3 - Quit\n";
       cin >> choice;
       switch(choice)
       {
           case 0:
               return 0;
               break;
           case 1:
               return 1;
               break;
           case 2:
               return 2;
               break;
           case 3:
               exit(0);
               break;
           default:
               cout << "\ninvalid Input! \n ";
       }
   }
   return choice;
}
int getComputerChoice()
{
   unsigned int seed = time(0);
   srand(seed);
   //Generate number between 0 and 2
   int randomNum = rand() % 3;
   return randomNum;
}
int determineWinner(int userChoice, int computerChoice)
{
   if (userChoice == 0 && computerChoice == 0)
   {
       cout << "Computer choice is rock, user choice is rock\n";
       cout << "It is a tie!\n";
       return 0;
   }
   else if (userChoice == 1 && computerChoice == 1)
   {
       cout << "Computer choice is paper, user choice is paper\n";
       cout << "It is a tie!\n";
       return 0;
   }
   else if (userChoice == 2 && computerChoice == 2)
   {
       cout << "Computer choice is scissors, user choice is scissors\n";
       cout << "It is a tie!\n";
       return 0;
   }
   else if (userChoice == 0 && computerChoice == 2)
   {
       cout << "Computer choice is scissors, user choice is rock\n";
       cout <<"You win!\n";
       return 1;
   }
   else if (userChoice == 1 && computerChoice == 0)
   {
       cout << "Computer choice is rock, user choice is paper\n";
       cout <<"You win!\n";
       return 1;
   }
   else if (userChoice == 2 && computerChoice == 1)
   {
       cout << "Computer choice is paper, user choice is scissors\n";
       cout <<"You win!\n";
       return 1;
   }
   else if (userChoice == 0 && computerChoice == 1)
   {
       cout << "Computer choice is paper, user choice is rock\n";
       cout <<"Computer wins!\n";
       return -1;
   }
   else if (userChoice == 2 && computerChoice == 0)
   {
       cout << "Computer choice is rock, user choice is scissors\n";
       cout <<"Computer wins!\n";
       return -1;
   }
   else if (userChoice == 1 && computerChoice == 2)
   {
       cout << "Computer choice is scissors, user choice is paper\n";
       cout <<"Computer wins!\n";
       return -1;
   }
}

Add a comment
Know the answer?
Add Answer to:
(C++) Hey guys we just went over loops and it got me confused, it has me...
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
  • package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame {...

    package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame { private String userChoice, computerChoice;    public RPSGame() { userChoice = "rock"; computerChoice = "rock"; }    public String getUserChoice() { return userChoice; }    public String getComputerChoice() { return computerChoice; }    public void setUserChoice(String aUserChoice) { userChoice = aUserChoice; }    public void setComputerChoice(String aComputerChoice) { computerChoice = aComputerChoice; }    public String toString() { return "User Choice: " + userChoice + "...

  • i have created a program for a game of rock, paper, scissors but i must make...

    i have created a program for a game of rock, paper, scissors but i must make it run more than once in some kind of loop. i want to add a function named runGame that will control the flow of a single game. The main function will be used to determine which game mode to initiate or exit program in Player vs. Computer, the user will be asked to enter their name and specify the number of rounds for this...

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

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

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

  • please help me fix this. I'm getting it all mixed up in user defined functions. here's...

    please help me fix this. I'm getting it all mixed up in user defined functions. here's the question. This week we'll write a C program that lets the user play the game of Rock, Paper, Scissors against the computer. Your program will call a user-defined function named display_rules that displays the rules of the game. It then proceeds to play the game. To determine the winner, your program will call another user- defined function called determine_winner that takes two integer...

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