-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board.cpp
358 lines (339 loc) · 8.64 KB
/
Board.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
#include "Board.h"
#include "DataType.h"
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctype.h>
void Board::init(){
blackNum =2, whiteNum = 2, turn = false;
b = new pieceType*[boardSize];
for(int i = 0; i < boardSize; ++i){
b[i] = new pieceType[boardSize];
for(int j = 0;j<boardSize;++j){
b[i][j] = empty;
}
}
b[3][3] = b[4][4] = white;
b[3][4] = b[4][3] = black;
}
Board::Board(const Board& rhs){
boardSize = rhs.boardSize;
blackNum = rhs.blackNum;
whiteNum = rhs.whiteNum;
turn = rhs.turn;
b = new pieceType*[boardSize];
for(int i = 0; i < boardSize; ++i){
b[i] = new pieceType[boardSize];
for(int j = 0;j < boardSize; ++j){
b[i][j] = rhs.b[i][j];
}
}
}
Board& Board::operator=(const Board& rhs){
if(this != &rhs){
boardSize = rhs.boardSize;
blackNum = rhs.blackNum;
whiteNum = rhs.whiteNum;
turn = rhs.turn;
b = new pieceType*[boardSize];
for(int i = 0; i < boardSize; ++i){
b[i] = new pieceType[boardSize];
for(int j = 0;j < boardSize; ++j){
b[i][j] = rhs.b[i][j];
}
}
}
return *this;
}
void Board::freeBoard(){
for(int i = 0; i < boardSize; ++i){
delete[] b[i];
}
delete [] b;
}
void Board::set(int x, int y, pieceType pt){
b[x][y] = pt;
}
pieceType Board::fetch(int x, int y)const{
if(x < DEFAULT_SIZE && x >= 0 && y < DEFAULT_SIZE && y >= 0){
return b[x][y];
}
return invalid;
}
pieceType Board::reverseType(pieceType pt)const{
return pt == black? white: pt == white? black: pt;
}
pieceType Board::turnToPieceType(bool turn)const{
return turn? white: black;
}
bool Board::canFlip(int _x, int _y, pieceType pt)const{ // check if somewhere is available to set piece
if(fetch(_x, _y) != empty){
return false;
}
for(int i = 0; i < boardSize; i++){
int x(_x + Direction[i].x), y(_y + Direction[i].y);
while(fetch(x, y) == reverseType(pt)){
x += Direction[i].x;
y += Direction[i].y;
}
if((x != _x + Direction[i].x || y != _y + Direction[i].y) && (fetch(x, y) == pt)){
return true;
}
}
return false;
}
bool Board::canFlip(int _x, int _y, pieceType pt, int& count) const{ // check if somewhere is available to set piece
bool flag = false;
if(fetch(_x, _y) != empty){
count = 0;
return flag;
}
for(int i = 0; i < boardSize; i++){
int x(_x + Direction[i].x), y(_y + Direction[i].y);
while(fetch(x, y) == reverseType(pt)){
x += Direction[i].x;
y += Direction[i].y;
}
if((x != _x + Direction[i].x || y != _y + Direction[i].y) && (fetch(x, y) == pt)){
count++;
flag = true;
}
}
if(flag){
return true;
}
return false;
}
bool Board::canFlip(int _x, int _y, pieceType pt, std::vector<vec>& v) const{ // check if somewhere is available to set piece
bool flag = false;
if(fetch(_x, _y) != empty){
return flag;
}
for(int i = 0; i < boardSize; i++){
int x(_x + Direction[i].x), y(_y + Direction[i].y);
while(fetch(x, y) == reverseType(pt)){
x += Direction[i].x;
y += Direction[i].y;
}
if((x != _x + Direction[i].x || y != _y + Direction[i].y) && (fetch(x, y) == pt)){
int x_start = _x + Direction[i].x;
int y_start = _y + Direction[i].y;
while(x_start != x || y_start != y ){
//y_start = y_start + Direction[i].y;
v.push_back(vec(x_start, y_start));
x_start += Direction[i].x;
y_start += Direction[i].y;
}
flag = true;
}
}
if(flag){
return true;
}
return false;
}
void Board::flip(int _x, int _y, pieceType pt){
for(int i = 0; i < 8; i++){
int x(_x + Direction[i].x), y(_y + Direction[i].y);
std::vector<vec> mem;
while(fetch(x, y) == reverseType(pt)){
mem.push_back(vec(x, y));
x += Direction[i].x;
y += Direction[i].y;
}
if((x != _x + Direction[i].x || y != _y + Direction[i].y) && (fetch(x, y) == pt)){
for(int index = 0; index < int(mem.size()); ++index){
if(b[mem[index].x][mem[index].y] == reverseType(pt)){
b[mem[index].x][mem[index].y] = pt;
}
else{
fprintf(stderr, "flipping error at (%d, %d)!\n", mem[index].x, mem[index].y);
exit(1);
}
}
}
}
}
void Board::flip(int _x, int _y, pieceType pt, std::vector<vec>& v){
for(int i = 0; i < 8; i++){
int x(_x + Direction[i].x), y(_y + Direction[i].y);
std::vector<vec> mem;
while(fetch(x, y) == reverseType(pt)){
mem.push_back(vec(x, y));
x += Direction[i].x;
y += Direction[i].y;
}
if((x != _x + Direction[i].x || y != _y + Direction[i].y) && (fetch(x, y) == pt)){
for(int index = 0; index < int(mem.size()); ++index){
if(b[mem[index].x][mem[index].y] == reverseType(pt)){
v.push_back(mem[index]);
b[mem[index].x][mem[index].y] = pt;
}
else{
fprintf(stderr, "flipping error at (%d, %d)!\n", mem[index].x, mem[index].y);
exit(1);
}
}
}
}
}
bool Board::canSetPiece(bool color){ //check if one can set piece, used for checkTurn()
pieceType pc = color? white: black;
for(int x = 0; x < DEFAULT_SIZE; x++){
for(int y = 0; y < DEFAULT_SIZE; y++){
if(b[x][y] == empty && canFlip(x, y, pc)){
return true;
}
}
}
return false;
}
void Board::getResult(status& stat){
int blackCount = countColor(black);
int whiteCount = countColor(white);
if( blackCount > whiteCount){
stat.s = blackWin;
stat.black = blackCount;
stat.white = whiteCount;
}
else if(blackCount < whiteCount){
stat.s = whiteWin;
stat.black = blackCount;
stat.white = whiteCount;
}
else{
stat.s = tie;
stat.black = blackCount;
stat.white = whiteCount;
}
}
bool Board::checkEnd(){
return !(canSetPiece(turn) || canSetPiece(!turn));
}
pieceType Board::checkTurn(){
if(canSetPiece(turn)){
return turnToPieceType(turn);
}
else if(canSetPiece(!turn)){
turn = !turn;
return turnToPieceType(turn);
}
else{
std::cerr << "should have won, check error!\n";
exit(1);
}
}
std::vector<vec> Board::setPieces(int x, int y){
b[x][y] = turnToPieceType(turn); //set piece
std::vector<vec> ret;
flip(x, y, turnToPieceType(turn), ret); // flip around
turn = !turn;
return ret;
}
int Board::countColor(pieceType pt)const{
int count = 0;
for(int i = 0; i < DEFAULT_SIZE; ++i){
for(int j = 0; j < DEFAULT_SIZE; ++j){
if(b[i][j] == pt){
count++;
}
}
}
return count;
}
void skipSpace(char* p){
while(isspace(*p)){
p += 1;
}
}
vec Board::inputVec(){
vec def(-1, -1);
char* line = NULL;
//size_t len = 0;
if(turn){
printf("white's turn : please input a vector (devided by ','): ");
}
else{
printf("black's turn : please input a vector (devided by ','): ");
}
/*if(getline(&line, &len, stdin) == -1){
fprintf(stderr, "get line %s error!\n", line);
exit(1);
}*/
char* startp = line;
char* endp;
long num1 = strtol(startp, &endp, 10);
if(endp == startp){
fprintf(stderr, "no first number!\n");
exit(1);
}
skipSpace(endp);
endp++; // skip ','
startp = endp;
long num2 = strtol(startp, &endp, 10);
if(endp == startp){
fprintf(stderr, "no second number!\n");
exit(1);
}
free(line);
def.x = int(num1);
def.y = int(num2);
return def;
}
void Board::takeStep(){
checkTurn();
vec step = inputVec();
if(step == vec(-1, -1)){
fprintf(stderr, "no input vector!\n");
step = inputVec();
}
while(!canFlip(step.x, step.y, turnToPieceType(turn))){
fprintf(stderr, "invalid input (%d, %d)!\n", step.x, step.y);
step = inputVec();
}
setPieces(step.x, step.y);
if(checkEnd()){
if(countColor(black) > countColor(white)){
printf("black: %d\n", countColor(black));
printf("white: %d\n", countColor(white));
printf("black wins!\n");
}
else if(countColor(black) < countColor(white)){
printf("black: %d\n", countColor(black));
printf("white: %d\n", countColor(white));
printf("white wins!\n");
}
else{
printf("black: %d\n", countColor(black));
printf("white: %d\n", countColor(white));
printf("ties!\n");
}
printBoard();
exit(0);
}
else{
printBoard();
}
}
void Board::printBoard()const{
printf("=========================\n| Board |\n=========================\n\n");
for(int i = 0; i < DEFAULT_SIZE; ++i){
printf("%d ", i);
for(int j = 0; j < DEFAULT_SIZE; ++j){
if(b[i][j] == white){
printf("O ");
}
else if(b[i][j] == black){
printf("X ");
}
else{
printf(". ");
}
}
printf("\n\n");
}
printf(" 0 1 2 3 4 5 6 7 \n");
printf("Black: %d\tWhite: %d\n", countColor(black), countColor(white));
printf("=========================\n| Board |\n=========================\n");
}