-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
254 lines (218 loc) · 5.35 KB
/
board.cpp
File metadata and controls
254 lines (218 loc) · 5.35 KB
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
#include <ncurses.h>
#include <algorithm>
#include "board.hpp"
#include "cell.hpp"
#include "color.hpp"
#include "colormanager.hpp"
#include "print.hpp"
#include "cellcolor.hpp"
namespace roadagain
{
const Point Board::START(1, 1);
const Point Board::END(START.y + ROW * 2, START.x + COL * 3);
const Point Board::D[] = {
Point(-1, -1),
Point(-1, 0),
Point(-1, 1),
Point( 0, -1),
Point( 0, 1),
Point( 1, -1),
Point( 1, 0),
Point( 1, 1),
};
Board::Board() : black_(DEFAULT_STONE / 2), white_(DEFAULT_STONE / 2)
{
matrix_ = new CellColor*[ROW]();
for (int i = 0; i < ROW; i++){
matrix_[i] = new CellColor[COL]();
}
matrix_[ROW / 2 - 1][COL / 2 - 1] = CellColor::WHITE;
matrix_[ROW / 2][COL / 2] = CellColor::WHITE;
matrix_[ROW / 2 - 1][COL / 2] = CellColor::BLACK;
matrix_[ROW / 2][COL / 2 - 1] = CellColor::BLACK;
}
Board::Board(const Board& board) : black_(board.black()), white_(board.white())
{
matrix_ = new CellColor*[ROW]();
for (int i = 0; i < ROW; i++){
matrix_[i] = new CellColor[COL]();
}
board.copy_matrix(matrix_);
}
Board::~Board()
{
for (int i = 0; i < ROW; i++){
delete[] matrix_[i];
}
delete[] matrix_;
}
void Board::print(const Point& p) const
{
ColorManager::instance().change_color(ColorManager::BOARD);
for (int i = 0; i < ROW * 2 + 1; i++){
for (int j = 0; j < COL * 3 + 1; j++){
move(START.y + p.y + i, START.x + p.x + j);
if (i % 2 == 0){
addch(j % 3 == 0 ? '+' : '-');
}
else {
if (j % 3 == 0){
addch('|');
}
else {
print_stone(Cell(i / 2, j / 3, matrix_[i / 2][j / 3]), false);
}
}
}
}
}
void Board::put(const Cell& cell, bool print_flag)
{
matrix_[cell.point.y][cell.point.x] = cell.color;
if (print_flag){
print_stone(cell, false);
}
reverse(cell, print_flag);
update_counter();
}
void Board::reverse(const Cell& cell, bool print_flag)
{
for (const Point& d : D){
reverse(cell, d, print_flag);
}
}
void Board::reverse(const Cell& cell, const Point& d, bool print_flag)
{
int cnt = 0;
Cell c = cell;
c.point += d;
while (in_board(c.point) && matrix_[c.point.y][c.point.x] == cell.color.reversed()){
cnt++;
c.point += d;
}
if (not in_board(c.point) || matrix_[c.point.y][c.point.x].empty()){
return;
}
while (cnt-- > 0){
c.point -= d;
matrix_[c.point.y][c.point.x].reverse();
if (print_flag){
print_stone(c, false);
}
}
}
int Board::reverse_num(const Cell& cell) const
{
int cnt = 0;
for (const Point& d : D){
cnt += reverse_num(cell, d);
}
return (cnt);
}
int Board::reverse_num(const Cell& cell, const Point& d) const
{
int cnt = 0;
Cell c = cell;
c.point += d;
while (in_board(c.point) && matrix_[c.point.y][c.point.x] == cell.color.reversed()){
cnt++;
c.point += d;
}
if (not in_board(c.point) || matrix_[c.point.y][c.point.x].empty()){
return (0);
}
return (cnt);
}
int Board::count_neighbor(const Point& p, const CellColor& stone)
{
int cnt = 0;
for (const Point& d : D){
Point tmp = p + d;
if (not in_board(tmp)){
continue;
}
if (matrix_[tmp.y][tmp.x] == stone){
cnt++;
}
}
return (cnt);
}
int Board::black() const
{
return (black_);
}
int Board::white() const
{
return (white_);
}
void Board::copy_matrix(CellColor** matrix) const
{
for (int i = 0; i < COL; i++){
for (int j = 0; j < ROW; j++){
matrix[i][j] = matrix_[i][j];
}
}
}
CellColor Board::winner() const
{
if (black_ > white_){
return (CellColor::BLACK);
}
else if (white_ > black_){
return (CellColor::WHITE);
}
else {
return (CellColor::EMPTY);
}
}
bool Board::in_board(const Point& p) const
{
return (0 <= p.y && p.y < ROW && 0 <= p.x && p.x < COL);
}
bool Board::empty(const Point& p) const
{
return (matrix_[p.y][p.x].empty());
}
void Board::update_counter()
{
black_ = white_ = 0;
for (int i = 0; i < Board::ROW; i++){
black_ += std::count(matrix_[i], matrix_[i] + Board::COL, CellColor::BLACK);
white_ += std::count(matrix_[i], matrix_[i] + Board::COL, CellColor::WHITE);
}
}
bool Board::can_put(const CellColor& stone) const
{
for (int i = 0; i < ROW; i++){
for (int j = 0; j < COL; j++){
if (can_put(Cell(i, j, stone))){
return (true);
}
}
}
return (false);
}
bool Board::can_put(const Cell& cell) const
{
if (not matrix_[cell.point.y][cell.point.x].empty()){
return (false);
}
for (const Point& d : D){
if (can_put(cell, d)){
return (true);
}
}
return (false);
}
bool Board::can_put(const Cell& cell, const Point& d) const
{
bool can_reverse = false;
Cell c = cell;
c.point += d;
while (in_board(c.point) && matrix_[c.point.y][c.point.x] == cell.color.reversed()){
can_reverse = true;
c.point += d;
}
return (in_board(c.point) && can_reverse && matrix_[c.point.y][c.point.x] == cell.color);
}
} // namespace roadagain