-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoBangBoard.cpp
72 lines (59 loc) · 1.66 KB
/
GoBangBoard.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
#include "GoBangBoard.h"
#include <cstring>
GoBangBoard::GoBangBoard() {
}
void GoBangBoard::initboard() {
memset(board, 0, sizeof(board));
}
void GoBangBoard::putPiece(int x, int y, int id) {
board[y][x] = id;
}
void GoBangBoard::takePiece(int x, int y) {
board[y][x] = No_Player;
}
void GoBangBoard::setBlackPlayerID(int player_id) {
this->black_player_id = player_id;
}
int GoBangBoard::getBlackPlayerID() const {
return this->black_player_id;
}
int GoBangBoard::getBoardPositionID(int x, int y) const {
return board[y][x];
}
bool GoBangBoard::isInside(int r, int c) const {
return r >= 0 && r < Grid_num && c >= 0 && c < Grid_num;
}
bool GoBangBoard::isInside(const Point& c) const {
return c.x >= 0 && c.x < Grid_num && c.y >= 0 && c.y < Grid_num;
}
bool GoBangBoard::isAvaliable(const Point& c) const {
return c.x >= 0 && c.x < Grid_num && c.y >= 0 && c.y < Grid_num && board[c.y][c.x] == No_Player;
}
bool GoBangBoard::win(const Point& move, const int id) {
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, -1, 1, 1, -1, 1, -1};
int r, c, r0 = move.y, c0 = move.x, cnt;
bool is_win = false;
for(int i = 0; i < 8; i++) {
cnt = 1;
r = r0 + dr[i];
c = c0 + dc[i];
while(isInside(r, c) && board[r][c] == id) {
cnt++;
r += dr[i];
c += dc[i];
}
r = r0 - dr[i];
c = c0 - dc[i];
while(isInside(r, c) && board[r][c] == id) {
cnt++;
r -= dr[i];
c -= dc[i];
}
if(cnt >= 5) {
is_win = true;
break;
}
}
return is_win;
}