-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
246 lines (237 loc) · 5.88 KB
/
main.cpp
File metadata and controls
246 lines (237 loc) · 5.88 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* ------------------------------------------------
prog1RockPaperScissors.cpp
Program #1: Play the game of Paper / Rock / Scissors against the computer.
Class: CS 141, Spring 2019. Tues 9am lab
Author: *** Farooq Syed ***
----------------------------------------------------
*/
#include <iostream> // For cin and cout
#include <cstdlib> // For rand()
#include <cctype> // For uppercase
// #include <ctime> Since its predetermined
using namespace std;
const int ROCK = 0; // Sets rocks value to 0
const int PAPER = 1; // Sets paper value to 1
const int SCISSORS = 2; // Sets scissors value to 2
const int USER_POINT = 1; // Gives a point if user wins
const int COMPUTER_POINT = -USER_POINT; // Gives a point to computer and takes away a point from user if computer wins
const int TIE = 0; // Gives 0 points if no one wins
//-----------------------------------------------------------------------------------------
int main()
{
// Display Instructions
cout << "CS 141 Program #1: Rock/Paper/Scissors\n"
<< endl
<< "Welcome to the game of Rock/Paper/Scissors where you play against\n"
<< "the computer. On each move the computer will choose R, P, or S, then\n"
<< "the user will be prompted for their choice, and then the score will\n"
<< "be updated. P beats R, R beats S, and S beats P. The score starts\n"
<< "at 0. Add one if the person wins, subtract one if the computer wins.\n"
<< "The game ends if the score reaches -5 or + 5.\n"
<< "User input of 'x' or 'X' causes the game to exit.\n"
<< endl
<< "Here we go!\n"
<< endl;
int computerChoiceVersion;
cout << "Select how the computer chooses its move: \n"
<< "1. Always choose Rock\n"
<< "2. Random guess\n"
<< "3. Random guess with graphical score\n"
<< "Enter your choice: ";
cin >> computerChoiceVersion;
cout << endl;
if (computerChoiceVersion < 1 && computerChoiceVersion > 3)
{
return 0;
}
// srand(time(NULL)); no seed required?
int computerNumber;
int userNumber;
int moveNumber = 0;
int gameScore = 0;
char userInput;
// Main loop should go here, with the indented sections below inside the main loop
// ...
while (gameScore != -5 && gameScore != 5)
{
// Get the computer choice
// ...
if (computerChoiceVersion == 1) // Always computer chooses rock
{
computerNumber = ROCK;
}
else
{
computerNumber = rand() % 3;
}
bool isValidInput = false;
while (!isValidInput)
{
// Prompt for and get the user's choice
// ...
// Increment move number
// ...
cout << ++moveNumber << ". "
<< "Your move: "; // My move
cin >> userInput;
if (isalpha(userInput))
{
userInput = toupper(userInput);
}
if (userInput == 'R')
{
userNumber = ROCK;
isValidInput = true;
}
else if (userInput == 'P')
{
userNumber = PAPER;
isValidInput = true;
}
else if (userInput == 'S')
{
userNumber = SCISSORS;
isValidInput = true;
}
else if (userInput == 'X')
{
isValidInput = true;
cout << "Exiting program..."
<< endl;
if (gameScore == 0)
{
cout << "Tie game!";
}
else if (gameScore < 0)
{
cout << "Computer wins!";
}
else if (gameScore > 0)
{
cout << "User wins!";
}
cout << endl
<< endl
<< "Ending program..."
<< endl;
return 0;
}
else
{
isValidInput = false;
}
}
int winner = TIE;
if (userNumber == computerNumber)
{
winner = TIE;
gameScore += TIE;
}
// Switch statement for rock paper and scissors points
switch (userNumber)
{
case ROCK:
switch (computerNumber)
{
case PAPER:
winner = COMPUTER_POINT;
break;
case SCISSORS:
winner = USER_POINT;
break;
}
break;
case PAPER:
switch (computerNumber)
{
case ROCK:
winner = USER_POINT;
break;
case SCISSORS:
winner = COMPUTER_POINT;
break;
}
break;
case SCISSORS:
switch (computerNumber)
{
case PAPER:
winner = USER_POINT;
break;
case ROCK:
winner = COMPUTER_POINT;
break;
}
break;
}
cout << " ";
// Display computer choice
// ...
switch (computerNumber)
{
case ROCK:
cout << "Computer chose R";
break;
case PAPER:
cout << "Computer chose P";
break;
case SCISSORS:
cout << "Computer chose S";
break;
}
cout << endl
<< " ";
// Update score, and then display it
// ...
if (winner == USER_POINT)
{
gameScore += USER_POINT;
cout << "User's point.";
}
else if (winner == COMPUTER_POINT)
{
gameScore += COMPUTER_POINT;
cout << "Computer's point.";
}
else
{
cout << "Tie.";
}
// Shows the graph and the calculations
if (computerChoiceVersion == 3)
{
int dotAmounts = (3 * (gameScore + 5)) + 2;
cout << endl
<< " -5 -4 -3 -2 -1 0 1 2 3 4 5" << endl
<< " ----------------------------------" << endl;
cout << " " << string(dotAmounts, '.') << "^";
}
else
{
cout << " Score: "
<< gameScore;
}
cout << endl
<< endl;
}
cout //<< "Exiting program..."
<< endl;
if (gameScore == 0)
{
cout << "Tie game!";
}
else if (gameScore < 0)
{
cout << "Computer wins!";
}
else if (gameScore > 0)
{
cout << "User wins!";
}
cout << endl
<< endl
<< "Ending program..."
<< endl;
return 0;
} //end main()
// Keep C++ happy