-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
216 lines (201 loc) · 6.3 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
/* Copyright 2018, Forrest Timour */
#include <cassert>
#include <stack>
#include <vector>
#include <iostream>
#include <algorithm>
#include "game.hpp"
#include "solve.hpp"
/* Game class functions */
Game::Game(unsigned int size, Tile state[]):
numTiles(0), size(size), rowB(size, 0), colB(size, 0), rowW(size, 0), colW(size, 0)
{
Tile temp;
board = new Tile*[size];
for (unsigned int r = 0; r < size; r++) {
board[r] = new Tile[size];
for (unsigned int c = 0; c < size; c++) {
temp = state[size*r+c];
board[r][c] = temp;
if (temp == B) {
numTiles++;
rowB[r]++;
colB[c]++;
} else if (temp == W) {
numTiles++;
rowW[r]++;
colW[c]++;
}
}
}
}
Game::~Game() {
for (unsigned int i = 0; i < size; i++)
delete[] board[i];
delete[] board;
}
bool Game::verifySolved() {
return numTiles == size*size && checkrep();
}
std::ostream & operator<<(std::ostream &os, Game const &game) {
for (unsigned int r = 0; r < game.size; r++) {
os << " ";
for (unsigned int c = 0; c < game.size; c++) {
if (game.board[r][c] == B)
os << "b";
else if (game.board[r][c] == W)
os << "w";
else
os << "-";
}
os << std::endl;
}
return os;
}
void Game::apply(const Move &m) {
board[m.row][m.col] = m.color;
auto &row = (m.color == B)? rowB : rowW;
auto &col = (m.color == B)? colB : colW;
row[m.row]++;
col[m.col]++;
numTiles++;
}
void Game::undo(const Move &m) {
assert(m.color == board[m.row][m.col]);
auto &row = (m.color == B)? rowB : rowW;
auto &col = (m.color == B)? colB : colW;
board[m.row][m.col] = E;
row[m.row]--;
col[m.col]--;
numTiles--;
}
bool Game::checkrep() {
// check number of each color per row/col
for (unsigned int i = 0; i < size; i++) {
if (rowB[i] > size/2) return false;
if (colB[i] > size/2) return false;
if (rowW[i] > size/2) return false;
if (colW[i] > size/2) return false;
}
// check row count
unsigned int count = 0;
for (unsigned int r = 0; r < size; r++) {
unsigned int rowCountB = 0;
unsigned int rowCountW = 0;
for (unsigned int c = 0; c < size; c++) {
if (board[r][c] == B) {
count++;
rowCountB++;
}
if (board[r][c] == W) {
count++;
rowCountW++;
}
}
if (rowB[r] != rowCountB || rowW[r] != rowCountW) return false;
}
// check total count
if (count != numTiles) return false;
// check col count
for (unsigned int c = 0; c < size; c++) {
unsigned int colCountB = 0;
unsigned int colCountW = 0;
for (unsigned int r = 0; r < size; r++) {
if (board[r][c] == B) {
colCountB++;
}
if (board[r][c] == W) {
colCountW++;
}
}
if (colB[c] != colCountB || colW[c] != colCountW) return false;
}
return true;
}
std::vector<Move> *Game::generateMoves() {
std::vector<Move> *moves = new std::vector<Move>();
moves->reserve(2*(size*size-numTiles));
for (unsigned int r = 0; r < size; r++) {
for (unsigned int c = 0; c < size; c++) {
if (board[r][c] != E)
continue;
if (isValid(B, r, c))
moves->push_back(Move(B, r, c));
if (isValid(W, r, c))
moves->push_back(Move(W, r, c));
}
}
return moves;
}
bool Game::isWinnable() {
// check for rows/cols with too many tiles of a color
for (unsigned int i = 0; i < size; i++)
if (rowB[i] > size/2 || rowW[i] > size/2 ||
colB[i] > size/2 || colW[i] > size/2 ) { return false; }
// check for empty tiles with no valid moves
for (unsigned int r = 0; r < size; r++) {
for (unsigned int c = 0; c < size; c++) {
if (board[r][c] == E && !isValid(W, r, c) && !isValid(B, r, c))
return false;
}
}
// check for three in a row/col
for (unsigned int i = 0; i < size; i++) {
unsigned count[] = { 0, 0 }; // rowCount, colCount
Tile t[] = { E, E };
for (unsigned int j = 0; j < size; j++) {
if (auto color = board[i][j]; color == E) {
t[0] = E;
count[0] = 0;
} else {
if (t[0] == color) {
count[0]++;
if (count[0] > 2) return false;
} else {
t[0] = color;
count[0] = 1;
};
}
if (auto color = board[j][i]; color == E) {
t[1] = E;
count[1] = 0;
} else {
if (t[1] == color) {
count[1]++;
if (count[1] > 2) return false;
} else {
t[1] = color;
count[1] = 1;
}
}
}
}
return true;
}
bool Game::isValid(Tile color, unsigned int r, unsigned int c) {
if (r >= size || c >= size || color == E) return false;
auto const &row = (color == B)? rowB : rowW;
auto const &col = (color == B)? colB : colW;
// check for row and col saturation
if (row[r] >= size/2 || col[c] >= size/2) return false;
// check for triple in row
unsigned int count = 1;
if (r > 0 && board[r-1][c] == color)
count += (r > 1 && board[r-2][c] == color)? 2 : 1;
if (r < size-1 && board[r+1][c] == color)
count += (r < size-2 && board[r+2][c] == color)? 2 : 1;
if (count > 2) return false;
// check for triple in col
count = 1;
if (c > 0 && board[r][c-1] == color)
count += (c > 1 && board[r][c-2] == color)? 2 : 1;
if (c < size-1 && board[r][c+1] == color)
count += (c < size-2 && board[r][c+2] == color)? 2 : 1;
return count <= 2;
}
/* Move class functions */
// Constructors/Destructor/Operators
Move::Move(Tile color, unsigned int row, unsigned int col)
: color(color), row(row), col(col) {}
Move::Move(const Move &o): color(o.color), row(o.row), col(o.col) {}
Move::~Move() {}