|
| 1 | +include <iostream> |
| 2 | + |
| 3 | +// Constant variables |
| 4 | +const char ROCK = 'r'; |
| 5 | +const char PAPER = 'p'; |
| 6 | +const char SCISSORS = 's'; |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +char getComputerOption() { |
| 11 | + srand(time(0)); |
| 12 | + // Random number |
| 13 | + int num = rand() % 3 + 1; |
| 14 | + |
| 15 | + if(num==1) return 'r'; |
| 16 | + if(num==2) return 'p'; |
| 17 | + if(num==3) return 's'; |
| 18 | +} |
| 19 | + |
| 20 | +char getUserOption() { |
| 21 | + char c; |
| 22 | + cout << "Rock, Paper and Scissors Game!" << endl; |
| 23 | + cout << "Choose one of the following options" << endl; |
| 24 | + cout << "-----------------------------------" << endl; |
| 25 | + cout << "(r) for rock " << endl << "(p) for paper" << endl << "(s) for scissors " << endl; |
| 26 | + cin >> c; |
| 27 | + |
| 28 | + while (c!='r' && c!='p' && c!='s' ) |
| 29 | + { |
| 30 | + cout << "Please enter one of the following options only. " << endl; |
| 31 | + cout << "(r) for rock " << endl << "(p) for paper" << endl << "(s) for scissors " << endl; |
| 32 | + cin >> c; |
| 33 | + } |
| 34 | + |
| 35 | + return c; |
| 36 | +} |
| 37 | + |
| 38 | +void showSelectedOption(char option) { |
| 39 | + if (option == 'r') cout << "Rock" << endl; |
| 40 | + if (option == 'p') cout << "Paper" << endl; |
| 41 | + if (option == 's') cout << "Scissors" << endl; |
| 42 | +} |
| 43 | + |
| 44 | +void chooseWinner(char uChoice, char cChoice) { |
| 45 | + if (uChoice == ROCK && cChoice == PAPER) { |
| 46 | + cout << "Computer Wins! Paper wraps Rock."<< endl; |
| 47 | + } |
| 48 | + else if (uChoice == PAPER && cChoice == SCISSORS) { |
| 49 | + cout << "Computer Wins! Scissors cut Paper."<< endl; |
| 50 | + |
| 51 | + } |
| 52 | + else if (uChoice == SCISSORS && cChoice == ROCK) { |
| 53 | + cout << "Computer Wins! Rock smashes Scissors."<< endl; |
| 54 | + |
| 55 | + } |
| 56 | + else if (uChoice == ROCK && cChoice == SCISSORS) { |
| 57 | + cout << "You Win! Paper wraps Rock."<< endl; |
| 58 | + |
| 59 | + } |
| 60 | + else if (uChoice == PAPER && cChoice == ROCK) { |
| 61 | + cout << "You Win! Paper wraps Rock."<< endl; |
| 62 | + |
| 63 | + } |
| 64 | + else if (uChoice == SCISSORS && cChoice == PAPER) { |
| 65 | + cout << "You Win! Scissors cut Paper."<< endl; |
| 66 | + } |
| 67 | + else{ |
| 68 | + cout << "Tie. Play again win the Game." << endl; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +int main() { |
| 73 | + //User's choice |
| 74 | + char uChoice; |
| 75 | + //Compter's choice |
| 76 | + char cChoice; |
| 77 | + |
| 78 | + uChoice = getUserOption(); |
| 79 | + cout << "Your choice is: "<< endl; |
| 80 | + showSelectedOption(uChoice); |
| 81 | + |
| 82 | + cout << "Computer's choice is: "<< endl; |
| 83 | + cChoice = getComputerOption(); |
| 84 | + showSelectedOption(cChoice); |
| 85 | + |
| 86 | + chooseWinner(uChoice, cChoice); |
| 87 | + |
| 88 | + return 0; |
| 89 | +} |
0 commit comments