/
ohhi.cpp
285 lines (243 loc) · 7.66 KB
/
ohhi.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
/**
* ohhi.cpp
*
* EECS 183, Winter 2015
* Project 3: 0h h1
*
* John Paul Imperial, Jacob Rich
* jpimp, jacobjdr
*
* <#A description of the project here#>
*/
#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 unknown = 0;
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++)
if (board[x][y] == 0) {
unknown++;
}
return unknown;
}
///////////////////////////////////////
// VALIDITY CHECKS ////////////////////
///////////////////////////////////////
bool row_has_no_threes_of_color(const int board[MAX_SIZE][MAX_SIZE],
int size,
int row,
int color) {
const int oneSpace = 1;
const int twoSpace = 2;
for (int i = 0; i < size - twoSpace; i++)
if (board[row][i] == board[row][i + oneSpace] &&
board[row][i + oneSpace] == board[row][i + twoSpace]
&& board[row][i] == color) {
return false;
}
return true;
}
bool col_has_no_threes_of_color(const int board[MAX_SIZE][MAX_SIZE],
int size,
int col,
int color) {
const int oneSpace = 1;
const int twoSpace = 2;
for (int i = 0; i < size - twoSpace; i++)
if (board[i][col] == board[i + oneSpace][col] &&
board[i + oneSpace][col] == board[i + twoSpace][col]
&& board[i][col] == color) {
return false;
}
return true;
}
bool board_has_no_threes(const int board[MAX_SIZE][MAX_SIZE], int size) {
for (int i = 0; i < size; i++)
if (!row_has_no_threes_of_color(board, size, i, BLUE) ||
!col_has_no_threes_of_color(board, size, i, BLUE) ||
!row_has_no_threes_of_color(board, size, i, RED) ||
!col_has_no_threes_of_color(board, size, i, RED)) {
return false;
}
return true;
}
bool rows_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size,
int row1,
int row2) {
for (int col = 0; col < size; col++)
if (board[row1][col] == 0 || board[row2][col] == 0 ||
board[row1][col] != board[row2][col]) {
return true;
}
return false;
}
bool cols_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size,
int col1,
int col2) {
for (int row = 0; row < size; row++)
if (board[row][col1] == 0 || board[row][col2] == 0 ||
board[row][col1] != board[row][col2]) {
return true;
}
return false;
}
bool board_has_no_duplicates(const int board[MAX_SIZE][MAX_SIZE], int size) {
for (int x = 0; x < size; x++) {
for (int y = x + 1; y < size; y++) {
if (!rows_are_different(board, size, x, y)) {
return false;
}
else if (!cols_are_different(board, size, x, y)) {
return false;
}
}
}
return true;
}
///////////////////////////////////////
// SOLVING FUNCTIONS //////////////////
///////////////////////////////////////
void solve_three_in_a_row(int board[MAX_SIZE][MAX_SIZE],
int size,
int row,
bool announce) {
for (int col = 0; col < size; col++) {
if (board[row][col] == board[row][col + 1] && board[row][col] != UNKNOWN) {
board[row][col + 2] = opposite_color(board[row][col]);
if (col > 0) {
board[row][col - 1] = opposite_color(board[row][col]);
}
}
}
for (int col = 0; col < size; col++) {
if (board[row][col] == board[row][col + 2] && board[row][col] != UNKNOWN) {
board[row][col + 1] = opposite_color(board[row][col]);
}
}
}
void solve_three_in_a_column(int board[MAX_SIZE][MAX_SIZE],
int size,
int col,
bool announce) {
for (int col = 0; col < size; col++) {
if (board[row][col] == board[row][col + 1] && board[row][col] != UNKNOWN) {
board[row][col + 2] = opposite_color(board[row][col]);
if (col > 0) {
board[row][col - 1] = opposite_color(board[row][col]);
}
}
}
for (int col = 0; col < size; col++) {
if (board[row][col] == board[row][col + 2] && board[row][col] != UNKNOWN) {
board[row][col + 1] = opposite_color(board[row][col]);
}
}
}
void solve_balance_row(int board[MAX_SIZE][MAX_SIZE],
int size,
int row,
bool announce) {
int num_red = 0;
int num_blue = 0;
for (int col = 0; col < size; col++) {
if (board[row][col] == RED) {
num_red++;
}
else if (board[row][col] == BLUE) {
num_blue++;
}
}
//if num red tiles is exactly half row, mark unknown squares as blue
//go through row, if element is UNKNOWN, mark as blue
//vice versa for blue
if (num_red == size / 2) {
for (int col = 0; col < size; col++) {
if (board[row][col] == UNKNOWN) {
mark_square_as(board, size, row, col, BLUE, announce);
cout << "Square has been modified." << endl;
}
}
}
if (num_blue == size / 2) {
for (int col = 0; col < size; col++) {
if (board[row][col] == UNKNOWN) {
mark_square_as(board, size, row, col, RED, announce);
cout << "Square has been modified." << endl;
}
}
}
}
void solve_balance_column(int board[MAX_SIZE][MAX_SIZE],
int size,
int col,
bool announce) {
int num_red = 0;
int num_blue = 0;
for (int row = 0; row < size; row++) {
if (board[row][col] == RED) {
num_red++;
}
else if (board[row][col] == BLUE) {
num_blue++;
}
}
//if num red tiles is exactly half col, mark unknown squares as blue
//go through col, if element is UNKNOWN, mark as blue
//vice versa for blue
if (num_red == size / 2) {
for (int row = 0; row < size; row++) {
if (board[row][col] == UNKNOWN) {
mark_square_as(board, size, row, col, BLUE, announce);
cout << "Square has been modified." << endl;
}
}
}
if (num_blue == size / 2) {
for (int row = 0; row < size; row++) {
if (board[row][col] == UNKNOWN) {
mark_square_as(board, size, row, col, RED, announce);
cout << "Square has been modified." << endl;
}
}
}
}
///////////////////////////////////////
// GAMEPLAY FUNCTIONS /////////////////
///////////////////////////////////////
bool board_is_solved(const int board[MAX_SIZE][MAX_SIZE], int size) {
// your code here
return false;
}
bool check_valid_input(int size, int row_input, char col_input,
char color_char, int &row, int &col) {
// your code here
return false;
}
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) {
// your code here
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
}