-
Notifications
You must be signed in to change notification settings - Fork 0
/
graded_quiz2.cpp
89 lines (71 loc) · 2.38 KB
/
graded_quiz2.cpp
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
#include <iostream>
// Constant variables
const char ROCK = 'r';
const char PAPER = 'p';
const char SCISSORS = 's';
using namespace std;
char getComputerOption() {
srand(time(0));
// Random number
int num = rand() % 3 + 1;
if(num==1) return 'r';
if(num==2) return 'p';
if(num==3) return 's';
}
char getUserOption() {
char c;
cout << "Rock, Paper and Scissors Game!" << endl;
cout << "Choose one of the following options" << endl;
cout << "-----------------------------------" << endl;
cout << "(r) for rock " << endl << "(p) for paper" << endl << "(s) for scissors " << endl;
cin >> c;
while (c!='r' && c!='p' && c!='s' )
{
cout << "Please enter one of the following options only. " << endl;
cout << "(r) for rock " << endl << "(p) for paper" << endl << "(s) for scissors " << endl;
cin >> c;
}
return c;
}
void showSelectedOption(char option) {
if (option == 'r') cout << "Rock" << endl;
if (option == 'p') cout << "Paper" << endl;
if (option == 's') cout << "Scissors" << endl;
}
void chooseWinner(char uChoice, char cChoice) {
if (uChoice == ROCK && cChoice == PAPER) {
cout << "Computer Wins! Paper wraps Rock."<< endl;
}
else if (uChoice == PAPER && cChoice == SCISSORS) {
cout << "Computer Wins! Scissors cut Paper."<< endl;
}
else if (uChoice == SCISSORS && cChoice == ROCK) {
cout << "Computer Wins! Rock smashes Scissors."<< endl;
}
else if (uChoice == ROCK && cChoice == SCISSORS) {
cout << "You Win! Paper wraps Rock."<< endl;
}
else if (uChoice == PAPER && cChoice == ROCK) {
cout << "You Win! Paper wraps Rock."<< endl;
}
else if (uChoice == SCISSORS && cChoice == PAPER) {
cout << "You Win! Scissors cut Paper."<< endl;
}
else{
cout << "Tie. Play again win the Game." << endl;
}
}
int main() {
//User's choice
char uChoice;
//Compter's choice
char cChoice;
uChoice = getUserOption();
cout << "Your choice is: "<< endl;
showSelectedOption(uChoice);
cout << "Computer's choice is: "<< endl;
cChoice = getComputerOption();
showSelectedOption(cChoice);
chooseWinner(uChoice, cChoice);
return 0;
}