-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGomokuBoard.cpp
262 lines (252 loc) · 6.89 KB
/
GomokuBoard.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
#include "GomokuBoard.h"
#include<exception>
#include<string>
#include<iostream>
#include <pybind11/pybind11.h>
using namespace std;
namespace py = pybind11;
using namespace pybind11::literals;
GomokuBoard::GomokuBoard(unsigned int n, unsigned int n_in_row, unsigned int moveCacheNum)
:n(n),n_in_row(n_in_row),moveCache(moveCacheNum,NullSite),board(n,vector<PieceType>(n,PieceType::EMPTY)){}
GomokuBoard::BoardState GomokuBoard::getBoardState()
{
if (moveCache.back() == NullSite) //空棋盘未终止状态
return BoardState::NO_END;
vector<pair<int, int>> dirs{ {0,1},{1,1},{1,0},{1,-1},{0,-1} };
int lastRow = moveCache.back() / n;
int lastCol = moveCache.back() % n;
for (auto dir : dirs)
{
int sum = 1;
int curRow = lastRow + dir.first;
int curCol = lastCol + dir.second;
for (int i = 0; i < n_in_row-1; ++i)
{
if (curRow < 0 || curRow >= n || curCol < 0 || curCol >= n || (board[curRow][curCol] != board[lastRow][lastCol]))
break;
++sum;
curRow = curRow + dir.first;
curCol = curCol + dir.second;
}
if(sum>=n_in_row)
return board[lastRow][lastCol] == PieceType::BLACK ? BoardState::BLACK_WIN : BoardState::WHITE_WIN;
curRow = lastRow - dir.first;
curCol = lastCol - dir.second;
for (int i = 0; i < n_in_row - 1; ++i)
{
if (curRow < 0 || curRow >= n || curCol < 0 || curCol >= n || (board[curRow][curCol] != board[lastRow][lastCol]))
break;
++sum;
curRow = curRow - dir.first;
curCol = curCol - dir.second;
}
if (sum >= n_in_row)
return board[lastRow][lastCol] == PieceType::BLACK ? BoardState::BLACK_WIN : BoardState::WHITE_WIN;
}
if (board_cnt == n * n)
return BoardState::DRAW;
else
return BoardState::NO_END;
}
vector<unsigned int> GomokuBoard::getAvailableMove()
{
vector<unsigned int> availableMove(n*n,0);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (board[i][j] == PieceType::EMPTY)
availableMove[i * n + j] = 1;
}
}
return availableMove;
}
void GomokuBoard::executeMove(unsigned int action)
{
unsigned int row = action / n, col = action % n;
if (row < n && col < n && board[row][col] == PieceType::EMPTY)
{
board[row][col] = curColor;
moveCache.pop_front();
moveCache.push_back(action);
changeCurColor();
++board_cnt;
}
else
throw exception("illegal row or col");
}
torch::Tensor GomokuBoard::getBoardData()
{
/*添加近几手黑白棋子局面x,y*/
int index = moveCache.size();
torch::Tensor x = torch::zeros({1, n,n }, torch::dtype(torch::kFloat32));
torch::Tensor y = torch::zeros({1, n,n }, torch::dtype(torch::kFloat32));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (board[i][j] == curColor)
x[0][i][j] = 1;
else if (board[i][j] == anotherColor(curColor))
y[0][i][j] = 1;
}
}
torch::Tensor data = torch::cat({ x,y },0);
while (index)
{
int move1 = moveCache[--index];
if (move1 != NullSite)
y[0][move1 / n][move1 % n] = 0;
int move2 = moveCache[--index];
if (move2 != NullSite)
x[0][move2 / n][move2 % n] = 0;
data = torch::cat({ data,x,y },0);
}
/*添加当前落子方的局面矩阵C*/
if (curColor == PieceType::BLACK)
data = torch::cat({ data,torch::ones({1,n,n},torch::dtype(torch::kFloat32)) }, 0);
else
data = torch::cat({ data,torch::zeros({1,n,n},torch::dtype(torch::kFloat32)) }, 0);
int channels = moveCache.size() + 3;
data = data.reshape({ 1,channels,n,n });
return data;
}
GomokuBoard::BoardData GomokuBoard::getBoardData_py()
{
/*添加近几手黑白棋子局面x,y*/
int index = moveCache.size();
vector<vector<int>> x(n, vector<int>(n, 0));
vector<vector<int>> y(n, vector<int>(n, 0));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (board[i][j] == curColor)
x[i][j] = 1;
else if (board[i][j] == anotherColor(curColor))
y[i][j] = 1;
}
}
BoardData data{ x,y };
while (index)
{
int move1 = moveCache[--index];
if (move1 != NullSite)
y[move1 / n][move1 % n] = 0;
int move2 = moveCache[--index];
if (move2 != NullSite)
x[move2 / n][move2 % n] = 0;
data.push_back(x);
data.push_back(y);
}
/*添加当前落子方的局面矩阵C*/
if (curColor == PieceType::BLACK)
data.emplace_back(vector<vector<int>>(n, vector<int>(n, 1)));
else
data.emplace_back(vector<vector<int>>(n, vector<int>(n, 0)));
return data;
}
void GomokuBoard::display()
{
cout << endl << " ABCDEFGHIJKLMNO" << endl;
vector<string> digits = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
for (int i = 0; i < n; ++i)
{
cout << digits[i];
for (int j = 0; j < n; ++j)
{
if (i * n + j == moveCache.back())
{
cout << "◎";
continue;
}
if (board[i][j] == PieceType::EMPTY)
{
if (i == 0)
{
if (j == 0)
cout << "┏ ";
else if (j == n - 1)
cout << "┓ ";
else
cout << "┯ ";
}
else if (i == n - 1)
{
if (j == 0)
cout << "┗ ";
else if (j == n - 1)
cout << "┛ ";
else
cout << "┷ ";
}
else if (j == 0)
cout << "┠ ";
else if (j == n - 1)
cout << "┨ ";
else
cout << "┼ ";
}
else
cout << (board[i][j] == PieceType::BLACK ? "○" : "●");
}
cout << endl;
}
cout << endl;
}
void GomokuBoard::display_py()
{
py::print(u"\n A B C D E F G H I J K L M N O");
vector<const char16_t*> digits = { u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", u"9", u"a", u"b", u"c", u"d", u"e", u"f" };
for (int i = 0; i < n; ++i)
{
py::print(digits[i], "end"_a = "");
for (int j = 0; j < n; ++j)
{
if (i * n + j == moveCache.back())
{
py::print(u"◎ ", "end"_a = "");
continue;
}
if (board[i][j] == PieceType::EMPTY)
{
if (i == 0)
{
if (j == 0)
py::print(u"┏ ", "end"_a = "");
else if (j == n - 1)
py::print(u"┓ ", "end"_a = "");
else
py::print(u"┯ ", "end"_a = "");
}
else if (i == n - 1)
{
if (j == 0)
py::print(u"┗ ", "end"_a = "");
else if (j == n - 1)
py::print(u"┛ ", "end"_a = "");
else
py::print(u"┷ ", "end"_a = "");
}
else if (j == 0)
py::print(u"┠ ", "end"_a = "");
else if (j == n - 1)
py::print(u"┨ ", "end"_a = "");
else
py::print(u"┼ ", "end"_a = "");
}
else
py::print(board[i][j] == PieceType::BLACK ? u"○ " : u"● ", "end"_a = "");
}
py::print("");
}
py::print("");
}
void GomokuBoard::changeCurColor()
{
curColor = anotherColor(curColor);
}
inline GomokuBoard::PieceType GomokuBoard::anotherColor(PieceType color)
{
return color == PieceType::BLACK ? PieceType::WHITE : PieceType::BLACK;
}