-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA15rockpaperscissors.cpp
115 lines (87 loc) · 2.45 KB
/
A15rockpaperscissors.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
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
// Rock, Paper, Scissors game
// By Emily Dayanghirang
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void displayMenu();
int getUserInput();
int generateComputerChoice();
void displayChoice(int,int);
void selectWinner(int, int);
int main()
{
int userInput, compChoice;
do
{
displayMenu();
userInput = getUserInput();
compChoice = generateComputerChoice();
displayChoice(userInput, compChoice);
selectWinner(userInput, compChoice);
}while(userInput != 4);
return 0;
}
void displayMenu()
{
cout << "\nThis is the game of rock, paper, scissors.\n"
<< "***The program repeats until "
<< "you select 4 to Quit the program.\n\n"
<< "Choose from the following choices:\n"
<< "1. Rock\n"
<< "2. Paper\n"
<< "3. Scissors\n"
<< "4. Quit\n";
}
int getUserInput()
{
int userInput;
do
{
cout << "\nEnter your choice (1 - 4): ";
cin >> userInput;
if(userInput < 1 || userInput > 4)
cout << "\nChoose from the numbers 1-4 only.\n";
}while(userInput < 1 || userInput > 4);
cout << endl;
return userInput;
}
int generateComputerChoice()
{
const int UPPER_LIMIT = 3;
const int LOWER_LIMIT = 1;
const int RANGE = UPPER_LIMIT - LOWER_LIMIT + 1;
int compChoice;
srand(time(0));
compChoice = rand() % RANGE + LOWER_LIMIT;
return compChoice;
}
void displayChoice(int userInput, int compChoice)
{
cout << "The user chose " << userInput << ".\n"
<< "The computer chose " << compChoice << ".\n";
}
void selectWinner(int userInput, int compChoice)
{
if(compChoice == 1 && userInput == 1)
cout << "Tie.\n";
else if(compChoice == 1 && userInput == 2)
cout << "You won!\n";
else if(compChoice == 1 && userInput == 3)
cout << "You lost!\n";
else if(compChoice == 2 && userInput == 1)
cout << "You lost!\n";
else if(compChoice == 2 && userInput == 2)
cout << "Tie.\n";
else if(compChoice == 2 && userInput == 3)
cout << "You won!\n";
else if(compChoice == 3 && userInput == 1)
cout << "You won!\n";
else if(compChoice == 3 && userInput == 2)
cout << "You lost!\n";
else if(compChoice == 3 && userInput == 3)
cout << "Tie.\n";
else
cout << "You have decided to quit the program.\n"
<< "The game stops here. Goodbye.\n";
}