-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board_controller.cpp
225 lines (201 loc) · 4.4 KB
/
Board_controller.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
//
// Board_controller.cpp
// Nomoku
//
// This class stores board infomation
// and apply operations to the board
//
#include <iostream>
#include "pisqpipe.h"
#include "Logger.h"
#include "Board_controller.h"
Board_controller::Board_controller(int w, int h) :width(w), height(h), count(0), i_w(false), h_r(false)
{
if (width<5 || height < 5){
pipeOut("ERROR Invalid size of the board");
return;
}
if (width > MAX_BOARD_WIDTH){
pipeOut("ERROR Maximal board width is %d", MAX_BOARD_WIDTH);
return;
}
if (height > MAX_BOARD_HEIGHT){
pipeOut("ERROR Maximal board height is %d", MAX_BOARD_HEIGHT);
return;
}
//allocate memory
board = new int*[width];
for (int j = 0; j < width; ++j) {
board[j] = new int[height];
}
}
Board_controller::~Board_controller()
{
//free memory
for (int j = 0; j < width; ++j) {
delete[] board[j];
}
delete[] board;
}
void Board_controller::visualize()
{
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
std::cout << board[i][j];
}
std::cout << '\n';
}
std::cout << '\n';
}
bool Board_controller::is_empty()
{
return (count == 0);
}
bool Board_controller::is_free(int x, int y)
{
return (board[x][y] == B_FREE);
}
bool Board_controller::is_in_range(int x,int y)
{
return (x >= 0 && y >= 0 && x < width && y < height);
}
bool Board_controller::have_result()
{
return h_r;
}
bool Board_controller::is_winner()
{
return i_w;
}
void Board_controller::move_self(int x, int y)
{
if (is_in_range(x,y) && is_free(x, y))
{
++count;
board[x][y] = B_MY_MOVE;
}
else{
pipeOut("ERROR my move [%d,%d]", x, y);
logEvent(LOG_ERROR, "Invalid move at %d,%d, not empty or out of range\n", x, y);
}
}
void Board_controller::move_oppo(int x, int y)
{
if (is_in_range(x, y) && is_free(x, y))
{
++count;
board[x][y] = B_OP_MOVE;
}
else{
pipeOut("ERROR opponents's move [%d,%d]", x, y);
logEvent(LOG_ERROR, "Invalid move at %d,%d, not empty or out of range\n", x, y);
}
}
void Board_controller::move_win(int x, int y)
{
h_r = true;
if (is_in_range(x, y) && is_free(x, y))
{
board[x][y] = B_WIN_MOVE;
}
else{
pipeOut("ERROR winning move [%d,%d]", x, y);
logEvent(LOG_ERROR, "Invalid winning move at %d,%d, already empty or out of range\n", x, y);
}
}
int Board_controller::take_back(int x, int y)
{
if (is_in_range(x, y) && !is_free(x, y))
{
--count;
board[x][y] = B_FREE;
return 0;
}
else
{
logEvent(LOG_ERROR, "Invalid takeback at %d,%d, already empty or out of range\n", x, y);
return 2;
}
}
int Board_controller::getWidth()
{
return width;
}
int Board_controller::getHeight()
{
return height;
}
void Board_controller::setWidth(int w)
{
logEvent(LOG_WARNING, "At current version,change the board size on the fly is not supported\n");
width = w;
}
void Board_controller::setHeight(int h)
{
logEvent(LOG_WARNING, "At current version,change the board size on the fly is not supported\n");
height = h;
}
void Board_controller::mark_edge_blocks()
{
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
if (board[i][j] == B_MY_MOVE || board[i][j] == B_OP_MOVE)
{
expand(i, j);
}
}
}
}
void Board_controller::expand(int x, int y)
{
int up_tag, down_tag, left_tag, right_tag;
//if tag is ture then it's a valid move, otherwise it's invalid
up_tag = (x - 1 >= 0);
left_tag = (y - 1 >= 0);
down_tag = (x + 1 < width);
right_tag = (y + 1 < height);
//expand the blocks at the edge of existing moves
if (up_tag && is_free(x - 1, y))
board[x - 1][y] = B_TO_EXPAND;
if (down_tag && is_free(x + 1, y))
board[x + 1][y] = B_TO_EXPAND;
if (left_tag && is_free(x, y - 1))
board[x][y - 1] = B_TO_EXPAND;
if (right_tag && is_free(x, y + 1))
board[x][y + 1] = B_TO_EXPAND;
if (up_tag && left_tag && is_free(x - 1, y - 1))
board[x - 1][y - 1] = B_TO_EXPAND;
if (up_tag && right_tag && is_free(x - 1, y + 1))
board[x - 1][y + 1] = B_TO_EXPAND;
if (down_tag && left_tag && is_free(x + 1, y - 1))
board[x + 1][y - 1] = B_TO_EXPAND;
if (down_tag && right_tag && is_free(x + 1, y + 1))
board[x + 1][y + 1] = B_TO_EXPAND;
}
void Board_controller::clear_expand_marks()
{
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
if (board[i][j] == B_TO_EXPAND) {
board[i][j] = B_FREE;
}
}
}
}
void Board_controller::clear_board()
{
count = 0;
i_w = false;
h_r = false;
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
board[i][j] = B_FREE;
}
}
}