-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.cpp
424 lines (363 loc) · 12.6 KB
/
3.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
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
/**
* ohhi.cpp
*
* EECS 183, Winter 2015
* Project 3: 0h h1
*
* Ezra Lesser, Michelle Gurevich
* elesser, mgur
*
* Builds a boardgame, checks inputs for the game, and solves game if prompted.
*/
#include <iostream>
#include <cctype>
#include "utility.h"
#include "ohhi.h"
///////////////////////////////////////
// UTILITY FUNCTIONS //////////////////
///////////////////////////////////////
int count_unknown_squares(const int board[MAX_SIZE][MAX_SIZE], int size) {
int count = 0;
// counts the number of unknown squares
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (board[i][j] == UNKNOWN) {
++count;
}
}
}
return count;
}
///////////////////////////////////////
// VALIDITY CHECKS ////////////////////
///////////////////////////////////////
bool row_has_no_threes_of_color(const int board[MAX_SIZE][MAX_SIZE],
int size, int row, int color) {
bool no_threes = true;
int num_in_row = 0;
// counts total amount of a color in row, resets if other color appears
const int MAX_OF_COLOR = 2;
for (int i = 0; i < size; ++i) {
if (board[row][i] == color) {
++num_in_row;
if (num_in_row > MAX_OF_COLOR) {
no_threes = false;
}
}
else {
num_in_row = 0;
}
}
return no_threes;
}
bool col_has_no_threes_of_color(const int board[MAX_SIZE][MAX_SIZE],
int size, int col, int color) {
bool no_threes = true;
int num_in_row = 0;
// counts total amount of a color in column, resets if other color appears
const int MAX_OF_COLOR = 2;
for (int i = 0; i < size; ++i) {
if (board[i][col] == color) {
++num_in_row;
if (num_in_row > MAX_OF_COLOR) {
no_threes = false;
}
}
else {
num_in_row = 0;
}
}
return no_threes;
}
bool board_has_no_threes(const int board[MAX_SIZE][MAX_SIZE], int size) {
bool no_threes = true;
for (int i = 0; i < size; ++i) {
// checks that board does not contain three of a color in row
if (!row_has_no_threes_of_color(board, size, i, RED)) {
no_threes = false;
}
else if (!row_has_no_threes_of_color(board, size, i, BLUE)) {
no_threes = false;
}
// checks that board does not contain three of a color in column
else if (!col_has_no_threes_of_color(board, size, i, RED)) {
no_threes = false;
}
else if (!col_has_no_threes_of_color(board, size, i, BLUE)) {
no_threes = false;
}
}
return no_threes;
}
bool rows_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size, int row1, int row2) {
bool different = false;
// checks that each row contains different combinations of colors
for (int i = 0; i < size; ++i) {
if ((board[row1][i] != board[row2][i])
|| (board[row1][i] == 0)
|| (board[row2][i] == 0)) {
different = true;
}
}
return different;
}
bool cols_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size, int col1, int col2) {
bool different = false;
// checks that each column contains different combinations of colors
for (int i = 0; i < size; ++i) {
if ((board[i][col1] != board[i][col2])
|| (board[i][col1] == 0)
|| (board[i][col2] == 0)) {
different = true;
}
}
return different;
}
bool board_has_no_duplicates(const int board[MAX_SIZE][MAX_SIZE], int size) {
bool no_duplicates = true;
for (int i = 0; i < size - 1; ++i) {
for (int j = i + 1; j < size; ++j) {
// checks that there do not exist duplicate column combinations
if (!(cols_are_different(board, size, i, j))) {
no_duplicates = false;
}
// checks that there do not exist duplicate row combinations
if (!(rows_are_different(board, size, i, j))) {
no_duplicates = false;
}
}
}
return no_duplicates;
}
///////////////////////////////////////
// SOLVING FUNCTIONS //////////////////
///////////////////////////////////////
void solve_three_in_a_row(int board[MAX_SIZE][MAX_SIZE],
int size, int row, bool announce) {
for (int i = 0; i < size - 1; ++i) {
if (board[row][i] == board[row][i + 1] && board[row][i] != UNKNOWN) {
int color = board[row][i];
int opposite_color = 0;
if (color == RED) {
opposite_color = BLUE;
}
else {
opposite_color = RED;
}
// makes third entry in row opposite color to preceding two entries
if ((i - 1) >= 0 && board[row][i - 1] == UNKNOWN) {
mark_square_as(board, size, row, i - 1,
opposite_color, announce);
}
// makes third entry in row opposite color to following two entries
if ((i + 2) < size && board[row][i + 2] == UNKNOWN) {
mark_square_as(board, size, row, i + 2,
opposite_color, announce);
}
}
// makes middle entry in row opposite color to surrounding two entries
if (i + 2 < size && board[row][i] == board[row][i + 2] &&
board[row][i] != UNKNOWN) {
int color = board[row][i];
int opposite_color = 0;
if (color == RED) {
opposite_color = BLUE;
}
else {
opposite_color = RED;
}
if (board[row][i + 1] == UNKNOWN) {
mark_square_as(board, size, row, i + 1,
opposite_color, announce);
}
}
}
return;
}
void solve_three_in_a_column(int board[MAX_SIZE][MAX_SIZE],
int size, int col, bool announce) {
for (int i = 0; i < size - 1; ++i) {
if (board[i][col] == board[i + 1][col] && board[i][col] != UNKNOWN) {
int color = board[i][col];
int opposite_color = 0;
if (color == RED) {
opposite_color = BLUE;
}
else {
opposite_color = RED;
}
// makes third entry in col opposite color to preceding two entries
if ((i - 1) >= 0 && board[i - 1][col] == UNKNOWN) {
mark_square_as(board, size, i - 1, col,
opposite_color, announce);
}
// makes third entry in col opposite color to following two entries
if ((i + 2) < size && board[i + 2][col] == UNKNOWN) {
mark_square_as(board, size, i + 2, col,
opposite_color, announce);
}
}
// makes middle entry in col opposite color to surrounding two entries
if (i + 2 < size && board[i][col] == board[i + 2][col] &&
board[i][col] != UNKNOWN) {
int color = board[i][col];
int opposite_color = 0;
if (color == RED) {
opposite_color = BLUE;
}
else {
opposite_color = RED;
}
if (board[i + 1][col] == UNKNOWN) {
mark_square_as(board, size, i + 1, col,
opposite_color, announce);
}
}
}
return;
}
void solve_balance_row(int board[MAX_SIZE][MAX_SIZE],
int size, int row, bool announce) {
int count_red = 0;
int count_blue = 0;
for (int i = 0; i < size; ++i) {
if (board[row][i] == RED) {
++count_red;
}
else if (board[row][i] == BLUE) {
++count_blue;
}
}
// checks that half of row is red so it can make rest of row blue
if (count_red == size / 2) {
for (int i = 0; i < size; ++i) {
if (board[row][i] == UNKNOWN) {
mark_square_as(board, size, row, i, BLUE, announce);
}
}
}
// checks that half of row is a blue so it can make rest of row red
else if (count_blue == size / 2) {
for (int i = 0; i < size; ++i) {
if (board[row][i] == UNKNOWN) {
mark_square_as(board, size, row, i, RED, announce);
}
}
}
return;
}
void solve_balance_column(int board[MAX_SIZE][MAX_SIZE],
int size, int col, bool announce) {
int count_red = 0;
int count_blue = 0;
for (int i = 0; i < size; ++i) {
if (board[i][col] == RED) {
++count_red;
}
else if (board[i][col] == BLUE) {
++count_blue;
}
}
// checks that half of column is red so it can make rest of column blue
if (count_red == size / 2) {
for (int i = 0; i < size; ++i) {
if (board[i][col] == UNKNOWN) {
mark_square_as(board, size, i, col, BLUE, announce);
}
}
}
// checks that half of column is blue so it can make rest of column red
else if (count_blue == size / 2) {
for (int i = 0; i < size; ++i) {
if (board[i][col] == UNKNOWN) {
mark_square_as(board, size, i, col, RED, announce);
}
}
}
return;
}
///////////////////////////////////////
// GAMEPLAY FUNCTIONS /////////////////
///////////////////////////////////////
bool board_is_solved(const int board[MAX_SIZE][MAX_SIZE], int size) {
if (count_unknown_squares(board, size) != 0) {
return false;
}
if (!board_is_valid(board, size)) {
return false;
}
return true;
}
bool check_valid_input(int size, int row_input, char col_input,
char color_char, int &row, int &col) {
// checks that row input is a valid one
if (row_input >= 1 && row_input <= size) {
row = row_input - 1;
}
else {
cout << "Sorry, that's not a valid input." << endl;
return false;
}
// checks that column input is a valid one
if (col_input >= 'A' && col_input <= ('A' + size - 1)) {
col = col_input - 'A';
}
else if (col_input >= 'a' && col_input <= ('a' + size - 1)) {
col = col_input - 'a';
}
else {
cout << "Sorry, that's not a valid input." << endl;
return false;
}
// lowercase letters are higher than uppercase equivalents
const int CONVERT = 'a' - 'A';
if (!(color_char == RED_LETTER || color_char == BLUE_LETTER ||
color_char == UNKNOWN_LETTER || color_char == RED_LETTER + CONVERT ||
color_char == BLUE_LETTER + CONVERT ||
color_char == UNKNOWN_LETTER + CONVERT)) {
cout << "Sorry, that's not a valid input." << endl;
return false;
}
return true;
}
bool check_valid_move(const int original_board[MAX_SIZE][MAX_SIZE],
const int current_board[MAX_SIZE][MAX_SIZE],
int size, int row, int col, int color) {
// reponds if player tries to change original squares
if (original_board[row][col] != UNKNOWN) {
cout << "Sorry, original squares cannot be changed." << endl;
return false;
}
int new_board[MAX_SIZE][MAX_SIZE];
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
new_board[i][j] = current_board[i][j];
}
}
// responds if a move violates a rule
new_board[row][col] = color;
if (board_is_valid(new_board, size)) {
return true;
}
else {
cout << "Sorry, that move violates a rule." << endl;
return false;
}
}
///////////////////////////////////////
// S'MORE FUNCTIONS ///////////////////
///////////////////////////////////////
void solve_lookahead_row(int board[MAX_SIZE][MAX_SIZE],
int size,
int row,
bool announce) {
// your code here
}
void solve_lookahead_column(int board[MAX_SIZE][MAX_SIZE],
int size,
int col,
bool announce) {
// your code here
}