C++ assignment please answer in c++!! I need help with this rock paper scissors
ID: 3927533 • Letter: C
Question
C++ assignment please answer in c++!! I need help with this rock paper scissors assignment I have tried to do it and have my source code and just lost my mind and i need some help. I need a,b,c,d as my function and have an output like first picture in terminal. I will post my source code in the comment. Thanks for your help in advance. I need help with this rock paper scissors assignment I have tried to do it and have my source code and just lost my mind and i need some help. I need a,b,c,d as my function and have an output like first picture in terminal. I will post my source code in the comment. Thanks for your help in advance.My source code #include <iostream> #include <iomanip> #include <string> using namespace std; const int ROCK = 1; const int PAPER = 2; const int SCISSORS = 3;
char userInput(); string generateRand(); string parsePlayer(char temp); int playerWon(int player, int computer); bool playAgain();
int main(){ char player = userInput(); string parsedPlayer = parsePlayer(player); string computer = generateRand(); if(playerWon(player, generateRand()) == 3) cout << "its a TIE "; else if(playerWon(player, generateRand()) == 2) cout << "You lost "; else cout << "You won "; bool done = playAgain(); while(done){ player = userInput(); if(playerWon(player, generateRand()) == 3) cout << "its a TIE "; else if(playerWon(player, generateRand()) == 2) cout << "You lost "; else cout << "You won "; cin.get(); done = playAgain(); } cout << "Thank you "; return 0; }
char userInput(){ cout << "Welcome to the Rock/Paper/Scissors game! " << "Here are your choices: "; cout << setw(13) << right << "R/r:" << " ROCK "; cout << setw(13) << right << "P/p:" << " PAPER "; cout << setw(13) << right << "S/s:" << " SCISSORS "; cout << " Please make a selection: "; char temp; cin >> temp; return tolower(temp); }
string generateRand(){ int num = (rand() % 3) + 1; switch(num){ case ROCK: return "Rock"; case PAPER: return "Paper"; default: return "Scissors" }
} int playerWon(int player, int computer){ if(player == ROCK){ if(computer == ROCK) return 3; else if(computer == PAPER) return 2; return 1; } else if(player == SCISSORS){ if(computer == SCISSORS) return 3; else if(computer == ROCK) return 2; return 1; } if(computer == PAPER) return 3; else if(computer == SCISSORS) return 2; return 1; }
bool playAgain(){ cout << "do you wanna play again?"; char temp; cin >> temp; while (tolower(temp) != 'y' || tolower(temp) != 'n') { cout << "Invalid response entered. Would you like to play again?"; cin >> temp; } switch (tolower(temp)){ case 'y': return true; case 'n': return false; } } string parsePlayer(char temp){ switch(tolower(temp)){ case 'r': return "Rock"; case 'p': return "Paper"; default: return "Scissors" } } #include <iostream> #include <iomanip> #include <string> using namespace std; const int ROCK = 1; const int PAPER = 2; const int SCISSORS = 3;
char userInput(); string generateRand(); string parsePlayer(char temp); int playerWon(int player, int computer); bool playAgain();
int main(){ char player = userInput(); string parsedPlayer = parsePlayer(player); string computer = generateRand(); if(playerWon(player, generateRand()) == 3) cout << "its a TIE "; else if(playerWon(player, generateRand()) == 2) cout << "You lost "; else cout << "You won "; bool done = playAgain(); while(done){ player = userInput(); if(playerWon(player, generateRand()) == 3) cout << "its a TIE "; else if(playerWon(player, generateRand()) == 2) cout << "You lost "; else cout << "You won "; cin.get(); done = playAgain(); } cout << "Thank you "; return 0; }
char userInput(){ cout << "Welcome to the Rock/Paper/Scissors game! " << "Here are your choices: "; cout << setw(13) << right << "R/r:" << " ROCK "; cout << setw(13) << right << "P/p:" << " PAPER "; cout << setw(13) << right << "S/s:" << " SCISSORS "; cout << " Please make a selection: "; char temp; cin >> temp; return tolower(temp); }
string generateRand(){ int num = (rand() % 3) + 1; switch(num){ case ROCK: return "Rock"; case PAPER: return "Paper"; default: return "Scissors" }
} int playerWon(int player, int computer){ if(player == ROCK){ if(computer == ROCK) return 3; else if(computer == PAPER) return 2; return 1; } else if(player == SCISSORS){ if(computer == SCISSORS) return 3; else if(computer == ROCK) return 2; return 1; } if(computer == PAPER) return 3; else if(computer == SCISSORS) return 2; return 1; }
bool playAgain(){ cout << "do you wanna play again?"; char temp; cin >> temp; while (tolower(temp) != 'y' || tolower(temp) != 'n') { cout << "Invalid response entered. Would you like to play again?"; cin >> temp; } switch (tolower(temp)){ case 'y': return true; case 'n': return false; } } string parsePlayer(char temp){ switch(tolower(temp)){ case 'r': return "Rock"; case 'p': return "Paper"; default: return "Scissors" } } elcone to the Rock/Paper Scissors gane! Here are your choices: Rr: ROCK P/p: PAPER S/s: SCISSORS Please nake a selection: r oth you and the conputer chose ROCK. You end in a drau! ould you like to play again? (Y/y for yes. N/n for No> d Invalid response entered. Would you like to play again? (Y/y for yes. Nn forN elcone to the Rock/Paper Scissors gane! Here are your choices: R/r: ROCK P/p: PAPER S/s: SCISSORS Please nake a selection: P Both you and the conputer chose PAPER. You end in a drau! ould you like to play again? Y/y for yes. N/n for No y elcone to the Rock/Paper Scissors game! Here are your choices: R/r: ROCK P/p: PAPER S/s: SCISSORS Please nake a selection: s ou chose SCISSORS. The conputer chose ROCK. ROCK snashes SCISSORS.You lose Sorry uld you like to play again? CY/y for yes. N/n for No> n hank you for playing Rock/Paper Scissors Have a great day! Press any key to continue cone to the Rock/Paper/Scissors gane Here are your choices: R/r: ROCK P/p: PAPER S/s: SCISSORS Please nake a selection: x Invalid selection made. Please try again: Invalid selection made. Please try again: ou chose SCISSORS The conputer chose ROCK ROCK snashes SCISSORS You lose Sorr ould you like to play again? (Y/y for yes. N/n for No y elcone to the Rock/Paper/Scissors game? Here are your choices: R/r: ROCK P/p: PAPER S/: SCISSORS Please nake a selection: S Both you and the conputer chose SCISSORS You end in a draw! ould you like to play again? (Y/y for yes. N/n for No y elcone to the Rock/Paper Scissors gane! Here are your choices: R/r ROCK P/p: PAPER S/s: SCISSORS Please nake a selection: s ou chose SCISSORS. The conputer chose PAPER. SCISSORS cuts paper You vin! uld you like to play again? CY/y for yes. N/n for No> n hank you for playing Rock/Paper Scissors! Have a great day! Press any key to continue
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 3;
int getUserChoice();
int getComputerChoice();
void determineWinner(int, int);
void displayChoice(int);
int main()
{
int userChoice;
int computerChoice;
computerChoice = getComputerChoice();
userChoice = getUserChoice();
while (userChoice != 4)
{
determineWinner(userChoice, computerChoice);
computerChoice = getComputerChoice();
userChoice = getUserChoice();
}
return 0;
}
int getComputerChoice()
{
unsigned seed = time(0);
srand(seed);
int number = 1 + rand() % 3;
// Return the random value.
return number;
}
int getUserChoice()
{
int choice; // To hold a menu choice
// Display the menu.
cout << "Game Menu ";
cout << "--------- ";
cout << "1) Rock ";
cout << "2) Paper ";
cout << "3) Scissors ";
cout << "4) Quit ";
cout << "Enter your choice: ";
cin >> choice;
while (choice < 1 || choice > 4)
{
cout << "Invalid selection. Enter 1, 2, 3, or 4: ";
cin >> choice;
}
// Return the choice.
return choice;
}
void determineWinner(int user, int computer)
{
// Display the choices.
cout << "You selected: ";
displayChoice(user);
cout << "The computer selected: ";
displayChoice(computer);
if (user == ROCK)
{
if (computer == SCISSORS)
cout << "YOU win! Rock smashes scissors. ";
else if (computer == PAPER)
cout << "Computer wins! Paper wraps rock. ";
else
cout << "Tie. No winner. ";
}
else if (user == PAPER)
{
if (computer == SCISSORS)
cout << "Computer wins! Scissors cuts paper. ";
else if (computer == ROCK)
cout << "YOU win! Paper wraps scissors. ";
else
cout << "Tie. No winner. ";
}
else if (user == SCISSORS)
{
if (computer == ROCK)
cout << "Computer win! Rock smashes scissors. ";
else if (computer == PAPER)
cout << "YOU wins! Paper wraps scissors. ";
else
cout << "Tie. No winner. ";
}
else
{
cout << "Error condition detected! ";
}
}
void displayChoice(int choice)
{
if (choice == ROCK)
cout << "Rock ";
else if (choice == PAPER)
cout << "Paper ";
else if (choice == SCISSORS)
cout << "Scissors ";
else
cout << "Error condition detected! ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.