1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// Write a program that lets the user play a game of Rock, Paper, Scissors against the computer.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;
//Show menu choices
void showMenu()
{
cout << "R - Rock" << endl;
cout << "P - Paper" << endl;
cout << "S - Scissors" << endl;
cout << "E - Exit" << endl;
}
//Get computer's choice
char getComp()
{
//Default value
char compChoice = 'X';
int choice = rand() % 3;
if (choice == 0)
compChoice = 'R';
else if (choice == 1)
compChoice = 'P';
else if (choice == 2)
compChoice = 'S';
return compChoice;
}
//Get user's choice
char getUser()
{
//Default choice
char userChoice = 'X';
cout << "Please select your choice: " << endl;
cin >> userChoice;
return userChoice;
}
//Generate decision
void game(char user, char comp)
{
if (user == comp)
cout << "Tie!" << endl;
else if (user == 'R' || 'r' && comp == 'P' || 'p')
cout << "Paper beats rock, you lose!" << endl;
else if (user == 'R' || 'r' && comp == 'S' || 's')
cout << "Rock beats scissors, you win!";
else if (user == 'P' || 'p' && comp == 'S' || 's')
cout << "Scissors beats paper, you lose!" << endl;
else if (user == 'P' || 'p' && comp == 'R' || 'r')
cout << "Paper beats rock, you win!" << endl;
else if (user == 'S' || 's' && comp == 'R' || 'r')
cout << "Rock beats scissors, you lose!";
else if (user == 'S' || 's' && comp == 'P' || 'p')
cout << "Scissors beats paper, you win!";
}
int main()
{
//Seed random number generator
unsigned seed = time(0);
srand(seed);
//Declare variables, default values
char user = 'X',
comp = 'X',
playAgain = 'X';
//Loop of program
do
{
//Show menu choice
showMenu();
//Get computer's choice
getComp();
//Get user's choice
getUser();
if (user == 'Q' || user == 'q')
exit(0);
//Input validation
if (user != 'R' || user != 'r', user != 'P' || user != 'p', user != 'S' || user != 's', user != 'Q' || user != 'q')
cout << endl;
cout << "Please select a valid choice." << endl;
//Display choices
switch (user)
{
case 'R': case 'r':
cout << "You chose rock" << endl;
case 'P': case 'p':
cout << "You chose paper" << endl;
case 'S': case 's':
cout << "You chose scissors" << endl;
}
switch (comp)
{
case 'R':
cout << "Computer chose rock" << endl;
case 'P':
cout << "Computer chose paper" << endl;
case 'S':
cout << "Computer chose scissors" << endl;
}
//Determine winner
game(user, comp);
//Play again?
cout << "Do you want to play again? Choose 'Y' for yes, and 'N' for no" << endl;
cin >> playAgain;
if (playAgain != 'Y' || playAgain != 'y', playAgain != 'N' || playAgain != 'n')
cout << "Invalid choice, please choose a valid choice" << endl;
}
while (playAgain == 'Y' || playAgain == 'y');
return 0;
}
|