-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
332 lines (247 loc) · 7.27 KB
/
game.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <iostream>
#include <vector>
#include <random>
using namespace std;
/* Class created to store in an object information of the
score after each pass. This will improve the efficiency of
the program*/
class pathScores {
private:
int number;
int turn;
int score;
static int totalScores;
public:
pathScores(int number, int turn, int score) {
this->number = number;
this->turn = turn;
this->score = score;
totalScores++;
}
int getNumber() {
return number;
}
int getTurn() {
return turn;
}
int getScore() {
return score;
}
};
int pathScores::totalScores = 0;
// Function prototypes
int traverse (int n, int turn);
int max (int scoreX, int scoreY, int scoreZ);
void determineWinner (int turn);
int playerTurn (int turn, int number);
int computerTurn (int turn, int number);
void switchTurn(int& turn);
void playerVsComputer();
void computerVsComputer();
int randomNumber();
// Global variable to store the scores from each path
vector<pathScores> scoreVector;
int main() {
system("clear");
// Version 1 of the game Player vs Computer
playerVsComputer();
/* Version 2 of the game Computer vs Computer
* NOTE: Uncomment the code below to test this version of the game!
*/
// computerVsComputer();
return 0;
}
/* Function to generate a random number ranging from [1, 99]
* NOTE: Again the mt19937 was used to generate a more distributed random numbers
*/
int randomNumber() {
int number;
random_device rand_dev;
mt19937 generator(rand_dev());
uniform_int_distribution<int> distribution(1, 99);
number = distribution(generator);
return number;
}
// Function for version 1 of the game (Player vs Computer)
void playerVsComputer() {
int number;
int turn;
cout << "The starting number is: ";
number = randomNumber();
cout << number << endl << endl;
while (number != 0) {
turn = 1;
int playerOption = playerTurn(turn, number);
cout << "Player 1 option is " << playerOption << endl;
number -= playerOption;
cout << "Remaining after player 1 turn is " << number << endl << endl;
if (number != 0) {
turn = 2;
int computerOption = computerTurn(turn, number);
number -= computerOption;
cout << "Remaining after player " << turn << " turn is " << number << endl << endl;
}
}
determineWinner(turn);
}
// Function for version 2 of the game (Computer vs Computer)
void computerVsComputer() {
int number;
int turn;
cout << "Insert a starting number: ";
cin >> number;
cout << endl;
while (number != 0) {
turn = 1;
int computer_1_Option = computerTurn(turn, number);
number -= computer_1_Option;
cout << "Remaining after player " << turn << " turn is " << number << endl << endl;
if (number != 0) {
turn = 2;
int computerOption = computerTurn(turn, number);
number -= computerOption;
cout << "Remaining after player " << turn << " turn is " << number << endl << endl;
}
}
determineWinner(turn);
}
/* Function that allows the player to input the option desired
* It validates if the user input is 4, 3, or 1. It also makes sure that
* player cannot input anything other than those 3 numbers. Furthermore,
* it doesn't let the player to subtract more of the remaining quantity
*/
int playerTurn (int turn, int number) {
int playerValue = 0;
turn = 1;
cout << "Insert number to subtract (4, 3, or 1): ";
cin >> playerValue;
cout << endl;
while ((playerValue > number) || ((playerValue != 4) && (playerValue != 3) && (playerValue != 1))) {
if ((playerValue != 4) && (playerValue != 3) && (playerValue != 1)) {
cout << "Please try again! Insert number to subtract (4, 3, or 1): ";
cin.clear();
cin.ignore();
cin >> playerValue;
cout << endl;
}
else if (playerValue > number) {
cout << "Invalid subtraction. Select a smaller number (4, 3, or 1): ";
cin.clear();
cin.ignore();
cin >> playerValue;
cout << endl;
}
}
return playerValue;
}
/* Function that works like the AI of the game. It checks if the remaining of the number
* is 4, 3, or 1. If it falls in any of those cases, the computer automatically wins the game.
* Otherwise, it will calculate recursively the best option to take in order to win the game.
*/
int computerTurn (int turn, int number) {
int pathX;
int pathY;
int pathZ;
int bestOption;
if (number - 4 == 0) {
cout << "Player " << turn << " inserted: " << 4 << endl;
return 4;
}
else if (number - 3 == 0) {
cout << "Player " << turn << " inserted: " << 3 << endl;
return 3;
}
else if (number - 1 == 0) {
cout << "Player " << turn << " inserted: " << 1 << endl;
return 1;
}
pathZ = traverse(number - 1, turn);
pathY = traverse(number - 3, turn);
pathX = traverse(number - 4, turn);
bestOption = max(pathX, pathY, pathZ);
cout << "Player " << turn << " inserted: " << bestOption << endl;
return bestOption;
}
/* Recursive funtion to calculate each possible path. This function was developed
* by using dynamic programing to save intermediate values and avoid repetition.
* Each intermediate value is saved in a vector that stores objects name pathScores.
* This object contains 3 fields, which are number, turn (to distinguish the score for the players),
* and the score of the path. By saving intermediate values, the program efficiency increases and
* avoid running out of memory.
*/
int traverse (int n, int turn) {
int compScore = 0;
int pathX = 0;
int pathY = 0;
int pathZ = 0;
if (n == 0 && turn == 2) {
compScore++;
return compScore;
}
else if (n == 0 && turn == 1){
compScore--;
return compScore;
}
if (scoreVector.size() != 0) {
for (int i = 0; i < scoreVector.size(); i++) {
if (scoreVector.at(i).getNumber() == n && scoreVector.at(i).getTurn() == turn) {
return scoreVector.at(i).getScore();
}
}
}
switchTurn(turn);
if ((n - 4) >= 0) {
pathX += traverse (n - 4, turn);
scoreVector.push_back(pathScores(n - 4, turn, pathX));
compScore = 0;
}
if ((n - 3) >= 0) {
pathY += traverse (n - 3, turn);
scoreVector.push_back(pathScores(n - 3, turn, pathY));
compScore = 0;
}
if ((n - 1) >= 0) {
pathZ += traverse (n - 1, turn);
scoreVector.push_back(pathScores(n - 1, turn, pathZ));
compScore = 0;
}
return pathX + pathY + pathZ;
}
// Function that changes the turn when the current player finishes his move
void switchTurn(int& turn) {
if (turn == 2) {
turn = 1;
}
else {
turn = 2;
}
}
// Function that determines who wins the game
void determineWinner (int turn) {
if (turn == 1) {
cout << "---------------------------------------------" << endl;
cout << "Player " << turn << " wins the game" << endl;
cout << "---------------------------------------------" << endl << endl;
}
else {
cout << "---------------------------------------------" << endl;
cout << "Player " << turn << " wins the game" << endl;
cout << "---------------------------------------------" << endl << endl;
}
}
/* Function that returns the option that the computer will use to subtract
* the number based on the score of each path. This will take the path that
* has the highest score, which represents the highest chance for the computer
* to win
*/
int max (int scoreX, int scoreY, int scoreZ) {
if ((scoreX >= scoreY) && (scoreX >= scoreZ) && (scoreX != 0)) {
return 4;
}
else if ((scoreY >= scoreX) && (scoreY >= scoreZ) && (scoreY != 0)) {
return 3;
}
else {
return 1;
}
}