(C++) Hey guys we just went over loops and it got me confused, it has me scrathc
ID: 3812111 • Letter: #
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);
}
My program
#include
#include
#include
using namespace std;
int getUserChoice();
int getComputerChoice();
int determineWinner(int, int);
int main()
{
// Driver part
cout <<"Driver part ";
cout << "========= ";
for (int computerChoice = 0; computerChoice < 3 ; computerChoice++)
for (int userChoice = 0; userChoice < 3; userChoice++)
{
determineWinner(userChoice, computerChoice);
}
// Game part
cout <<" Game part ";
cout << "========= ";
getComputerChoice();
getUserChoice();
return 0;
}
int getUserChoice()
{
int choice;
while (true)
{
cout << "Menu of choices ";
cout << "---------------- ";
cout << "0 - Rock ";
cout << "1 - Paper ";
cout << "2 - Scissors ";
cout << "3 - Quit ";
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 ";
cout << "It is a tie! ";
}
else if (userChoice == 1 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is paper ";
cout << "It is a tie! ";
}
else if (userChoice == 2 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is scissors ";
cout << "It is a tie! ";
}
else if (userChoice == 0 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is rock ";
cout <<"You win! ";
}
else if (userChoice == 1 && computerChoice == 0)
{
cout << "Computer choice is rock, user choice is paper ";
cout <<"You win! ";
}
else if (userChoice == 2 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is scissors ";
cout <<"You win! ";
}
else if (userChoice == 0 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is rock ";
cout <<"Computer wins! ";
}
else if (userChoice == 2 && computerChoice == 0)
{
cout << "Computer choice is rock, user choice is scissors ";
cout <<"Computer wins! ";
}
else if (userChoice == 1 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is paper ";
cout <<"Computer wins! ";
}
}
Explanation / Answer
#include<iostream>
#include<string>
//#include<>
using namespace std;
int getUserChoice();
int getComputerChoice();
int determineWinner(int, int);
int main()
{
// Driver part
cout <<"Driver part ";
cout << "========= ";
for (int computerChoice = 0; computerChoice < 3 ; computerChoice++)
for (int userChoice = 0; userChoice < 3; userChoice++)
{
determineWinner(userChoice, computerChoice);
}
// Game part
cout <<" Game part ";
cout << "========= ";
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<<" Computer Score = "<<compWin<<endl;
}
return 0;
}
int getUserChoice()
{
int choice;
while (true)
{
cout << "Menu of choices ";
cout << "---------------- ";
cout << "0 - Rock ";
cout << "1 - Paper ";
cout << "2 - Scissors ";
cout << "3 - Quit ";
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 << " invalid Input! ";
}
}
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 ";
cout << "It is a tie! ";
return 0;
}
else if (userChoice == 1 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is paper ";
cout << "It is a tie! ";
return 0;
}
else if (userChoice == 2 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is scissors ";
cout << "It is a tie! ";
return 0;
}
else if (userChoice == 0 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is rock ";
cout <<"You win! ";
return 1;
}
else if (userChoice == 1 && computerChoice == 0)
{
cout << "Computer choice is rock, user choice is paper ";
cout <<"You win! ";
return 1;
}
else if (userChoice == 2 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is scissors ";
cout <<"You win! ";
return 1;
}
else if (userChoice == 0 && computerChoice == 1)
{
cout << "Computer choice is paper, user choice is rock ";
cout <<"Computer wins! ";
return -1;
}
else if (userChoice == 2 && computerChoice == 0)
{
cout << "Computer choice is rock, user choice is scissors ";
cout <<"Computer wins! ";
return -1;
}
else if (userChoice == 1 && computerChoice == 2)
{
cout << "Computer choice is scissors, user choice is paper ";
cout <<"Computer wins! ";
return -1;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.