-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxim.cpp
More file actions
429 lines (395 loc) · 13.7 KB
/
Maxim.cpp
File metadata and controls
429 lines (395 loc) · 13.7 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// Written on 2018/5/24.
//
#include "Maxim.h"
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cassert>
#include <fstream>
#define random(a, b) (rand() % (b-a+1) + a)
using std::cout;
using std::endl;
using std::cin;
// GAMEPLAY
// the game is played on a 7 * 7 grid, each grid having a number initialized randomly from 0 to 9.
// the central grid is initialized to 0, and Player 1 starts with it marking an "O" on it.
// then Player 2 chooses a grid that's either on the same row or the same column as
// the grid just chosen, adds the number on that grid to his/her score, and marking an "X" on it.
// Then the two players take turns to choose grids until all grids are chosen.
// The player with a higher total score wins.
// if one-player mode is chosen, computer is Player 1.
Maxim::Maxim() {
int mode, sqL, difficulty;
cout << "Welcome to Maxim! Enter Mode--\n"
"0, Zero-player; \n"
"1, One-Player; \n"
"2, Two players: \n";
cin >> mode;
assert(mode == 0 or mode == 1 or mode == 2);
cout << "\nChoose grid length "
"\n(must be 3, 5, 7, or 9): \n";
cin >> sqL;
assert(sqL > 2 and sqL < 10 and sqL % 2 == 1);
switch (mode) {
case 0: {
MaximZeroPlayer m0(sqL, 4);
break;
} case 1: {
cout << "\nChoose difficulty (Must be "
"\nan even number less than grid size): \n";
cin >> difficulty;
assert(difficulty % 2 == 0 && difficulty < sqL);
MaximOnePlayer m1(sqL, difficulty);
break;
} case 2: {
MaximTwoPlayers m2(sqL);
}
}
}
MaximBase::MaximBase(int squareLength) :
curRow(squareLength / 2), curCol(squareLength / 2), curPlayer(true), score1(0), score2(0) {
srand((unsigned) time(nullptr));
for (int i = 0; i < MAXIMLENGTH; i++) {
for (int j = 0; j < MAXIMLENGTH; j++) {
numbers[i][j] = random(0, 9);
}
}
numbers[curRow][curCol] = 0; // set central grid to be 0
for (int i = 0; i < MAXIMLENGTH; i++) {
for (int j = 0; j < MAXIMLENGTH; j++) {
states[i][j] = EMPTY;
}
}
states[curRow][curCol] = P1;
//while (play()); // fixme: is this undefined?
}
void MaximBase::mark(int row, int col) {
states[row][col] = curPlayer ? P2 : P1;
if (!curPlayer) score1 += numbers[row][col];
else score2 += numbers[row][col];
}
// when displaying, to use Cartesian coordinates,
// the row and column are swapped and flipped
void MaximBase::display() const {
cout << "^ y-axis" << endl;
for (int i = squareLength - 1; i >= 0; i--) {
for (int j = 0; j < squareLength; j++) {
if (states[j][i] == EMPTY) {
cout << "|" << numbers[j][i];
} else if (states[j][i] == P1) {
cout << "|-";
} else {
cout << "| ";
}
}
cout << "|";
if (i > 0) {
cout << endl;
} else {
cout << "-> x-axis\n";
}
}
cout << "Current Grid: " << curRow << "," << curCol << "; Current Player: " << curPlayer + 1;
cout << "\nScores--Player 1: " << score1 << "; Player 2: " << score2 << endl;
}
//void Maxim::mark2(int row, int col) {
// states[row][col] = P2;
// score2 += numbers[row][col];
//}
// ************************************ play() methods
bool MaximZeroPlayer::play() {
computerChooses();
display();
curPlayer = not curPlayer; // flip turns
return not gameOver();
}
bool MaximOnePlayer::play() {
if (curPlayer) {
playerChooses();
} else {
computerChooses();
}
display();
curPlayer = not curPlayer; // flip turns
return not gameOver();
}
bool MaximTwoPlayers::play() {
playerChooses();
display();
curPlayer = not curPlayer; // flip turns
return not gameOver();
}
//
void MaximAuto::computerChooses() {
int coord = chooseGrid(curRow, curCol, difficulty);
curRow = coord / 100;
curCol = coord % 100;
mark(curRow, curCol);
}
void MaximBase::playerChooses() {
int row, col;
bool valid = false;
while (!valid) {
cout << "\nPlayer " << curPlayer + 1 << ", enter your next move: ";
cin >> row >> col;
if (isValid(row, col)) {
valid = true;
} else {
cout << "Invalid input; try again!\n";
}
}
curRow = row;
curCol = col; // assign inputs
mark(curRow, curCol);
cout << endl;
}
// tests whether the grid coordinates the user enters is valid
// the user's grid should be on the same row or column as the current grid
// if all grids on the row or column are occupied, the user can choose from any remaining grid
bool MaximBase::isValid(int row, int col) {
if (row >= squareLength || row < 0 || col >= squareLength || col < 0)
return false;
bool hasEmpty = false; // whether there are empty grids on the same row/col
for (int i = 0; i < squareLength; i++) {
if (states[curRow][i] == EMPTY) hasEmpty = true;
}
for (int i = 0; i < squareLength; i++) {
if (states[i][curCol] == EMPTY) hasEmpty = true;
}
if (hasEmpty) {
return (row == curRow || col == curCol) && states[row][col] == EMPTY;
} else {
return states[row][col] == EMPTY;
}
}
// helper recursive method for chooseGridHard()
// returns the total net points after a certain iteration of optimized plays,
// not including the point of the first grid that's passed from outside
// iter is the number of iterations (n-ply) for how deep the algorithm goes; should be even
int MaximAuto::calcPoints(int row, int col, int iter) {
if (iter == 0) {
return 0;
} else {
states[row][col] = P1;
int coord = chooseGridEasy(row, col);
if (coord == -1) {
states[row][col] = EMPTY;
return 0;
}
int nextRow = coord / 100, nextCol = coord % 100;
// if it's the other player's hypothesized turn (iter % 2 == 0),
// the score should be subtracted (== 0 because player 1 plays the last round, when iter == 1)
int score = numbers[nextRow][nextCol] * (iter % 2 == 0 ? -1 : 1);
//cout << iter << " " << nextRow << " " << nextCol << " " << score << endl;
score += calcPoints(nextRow, nextCol, iter - 1); // recursive call
states[row][col] = EMPTY;
return score;
}
}
// iter is the number of iterations (n-ply) for how deep the algorithm goes; should be even
int MaximAuto::chooseGrid(int row, int col, int iter) { // modified from chooseGridEasy()
int NONE = -1000;
int score = 0, curBiggest = NONE;
int ansRow = row, ansCol = col;
for (int i = 0; i < squareLength; i++) {
if (states[row][i] == EMPTY) {
score = numbers[row][i] + calcPoints(row, i, iter);
//cout << row << " " << i << " " << score << endl;
if (score > curBiggest) {
curBiggest = score;
ansCol = i; // row already == curRow
}
}
}
for (int i = 0; i < squareLength; i++) {
if (states[i][col] == EMPTY) {
score = numbers[i][col] + calcPoints(i, col, iter);
//cout << i << " " << col << " " << score << endl;
if (score > curBiggest) {
curBiggest = score;
ansCol = col; // resets column chosen
ansRow = i;
}
}
}
if (curBiggest == NONE) { // there are no empty grids on same row/col
for (int i = 0; i < squareLength; i++) {
for (int j = 0; j < squareLength; j++) {
if (states[i][j] == EMPTY) {
score = numbers[i][j] + calcPoints(i, j, iter);
//cout << i << " " << j << " " << score << endl;
if (score > curBiggest) {
curBiggest = score;
ansRow = i;
ansCol = j;
}
}
}
}
}
assert(curBiggest != NONE);
return ansRow * 100 + ansCol;
}
// easy algorithm: one-ply (search for the largest number available)
// if game is over, return -1 (should not happen);
// else returns a three-digit number; the first digit is row, last is col
int MaximAuto::chooseGridEasy(int row, int col) {
int curBiggest = -1;
int ansRow = row, ansCol = col;
for (int i = 0; i < squareLength; i++) {
if (states[row][i] == EMPTY && numbers[row][i] > curBiggest) {
curBiggest = numbers[row][i];
ansCol = i; // row already == curRow
}
}
for (int i = 0; i < squareLength; i++) {
if (states[i][col] == EMPTY && numbers[i][col] > curBiggest) {
curBiggest = numbers[i][col];
ansCol = col; // resets column chosen
ansRow = i;
}
}
if (curBiggest == -1) { // there are no empty grids on same row/col
for (int i = 0; i < squareLength; i++) {
for (int j = 0; j < squareLength; j++) {
if (states[i][j] == EMPTY && numbers[i][j] > curBiggest) {
curBiggest = numbers[i][j];
ansRow = i;
ansCol = j;
}
}
}
}
if (curBiggest == -1)
return -1;
else
return ansRow * 100 + ansCol;
}
bool MaximBase::gameOver() const {
for (int i = 0; i < squareLength; i++) {
for (int j = 0; j < squareLength; j++) {
if (states[i][j] == EMPTY) return false;
}
}
return true;
}
void MaximBase::gameEnd() const { // to add bookmark, use ctrl + shift + num
cout << "GAME OVER!";
if (score1 != score2)
cout << " Player " << (score1 > score2 ? 1 : 2) << " wins!\n";
else
cout << " It's a tie!\n";
}
void MaximBase::outputResult(std::ofstream &file) const {
file << "Player 1 score: " << score1 << ";\tPlayer 2 score: " << score2 <<
";\tDifference: " << score1 - score2 << endl;
}
// mode 0: player 1 is hard, player 2 is easy
// mode 1: 2-player; mode 2: one-player easy; mode 3: one-player hard
//void Maxim::play() {
// if ((mode != 1 && !curPlayer) || mode == 0) { // the computer plays
// int coord;
// if (mode == 2 || (mode == 0 && curPlayer))
// coord = chooseGridEasy(curRow, curCol);
// else
// coord = chooseGridHard(curRow, curCol, 4);
// curRow = coord / 100; curCol = coord % 100;
// mark(curRow, curCol);
// display();
// } else {
// int row, col;
// bool valid = false;
// while (!valid) {
// cout << "\nPlayer " << curPlayer+1 << ", enter your next move: ";
// cin >> row >> col;
// if (isValid(row, col)) {
// valid = true;
// } else {
// cout << "Invalid input; try again!\n";
// }
// }
// curRow = row; curCol = col; // assign inputs
// mark(curRow, curCol);
// display();
// cout << endl;
// }
// curPlayer = !curPlayer; // flip turns
//}
/*Point MaximBase::chooseGrid(int row, int col, int iter) {
int score = 0, curBiggest = -1000;
Point point(-1, -1);
for (int i = 0; i < squareLength; i++) {
if (states[row][i] == EMPTY) {
score = numbers[row][i] + calcPoints(row, i, iter);
//cout << row << " " << i << " " << score << endl;
if (score > curBiggest) {
curBiggest = score;
point.col = i; // row already == curRow
}
}
}
for (int i = 0; i < squareLength; i++) {
if (states[i][col] == EMPTY) {
score = numbers[i][col] + calcPoints(i, col, iter);
//cout << i << " " << col << " " << score << endl;
if (score > curBiggest) {
curBiggest = score;
point.col = col; // resets column chosen
point.row = i;
}
}
}
if (curBiggest == -1000) { // there are no empty grids on same row/col
for (int i = 0; i < squareLength; i++) {
for (int j = 0; j < squareLength; j++) {
if (states[i][j] == EMPTY) {
score = numbers[i][j] + calcPoints(i, j, iter);
//cout << i << " " << j << " " << score << endl;
if (score > curBiggest) {
curBiggest = score;
point.row = i; point.col = j;
}
}
}
}
}
assert(curBiggest != -1000);
return point;
}*/
//void Maxim::play() {
// if ((mode != 1 && !curPlayer) || mode == 0) { // the computer plays
// int coord;
// if (mode == 2 || (mode == 0 && curPlayer))
// coord = chooseGridEasy(curRow, curCol);
// else
// coord = chooseGridHard(curRow, curCol, 4);
// curRow = coord / 100; curCol = coord % 100;
// if (mode == 0 && curPlayer)
// mark2(curRow, curCol);
// else
// mark1(curRow, curCol);
// display();
// } else {
// int row, col;
// bool valid = false;
// while (!valid) {
// cout << "\nPlayer " << curPlayer+1 << ", enter your next move: ";
// cin >> row >> col;
// if (isValid(row, col)) {
// valid = true;
// } else {
// cout << "Invalid input; try again!\n";
// }
// }
// curRow = row; curCol = col; // assign inputs
// if (!curPlayer) { // current player is 1
// mark1(curRow, curCol);
// } else {
// mark2(curRow, curCol);
// }
// display();
// cout << endl;
// }
// curPlayer = !curPlayer; // flip turns
//}