-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.cpp
52 lines (51 loc) · 1.16 KB
/
eval.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
#include "eval.hpp"
int eval(const Board& board) {
if(board.moves_hist.empty())
return 0;
int c = board.moves_hist.back(), r = board.empty_column[board.moves_hist.back()];
char p = board.board[r][c];
bool down = false;
if(r < 3) {
down = true;
for(int i = r; i < board.HEIGHT && i-r < 4; i++) {
if(board.board[i][c] != p)
down = false;
}
if(down)
return 1;
}
int hor = -1;
for(int i = c; i >= 0 && board.board[r][i] == p; i--) {
hor++;
if(hor == 4)
return 1;
}
for(int i = c; i < board.WIDTH && board.board[r][i] == p; i++) {
hor++;
if(hor == 4)
return 1;
}
int backslash = -1;
for(int i = 0; c-i >= 0 && r-i >= 0 && board.board[r-i][c-i] == p; i++) {
backslash++;
if(backslash == 4)
return 1;
}
for(int i = 0; c+i < board.WIDTH && r+i < board.HEIGHT && board.board[r+i][c+i] == p; i++) {
backslash++;
if(backslash == 4)
return 1;
}
int slash = -1;
for(int i = 0; c-i >= 0 && r+i < board.HEIGHT && board.board[r+i][c-i] == p; i++) {
slash++;
if(slash == 4)
return 1;
}
for(int i = 0; c+i < board.WIDTH && r-i >= 0 && board.board[r-i][c+i] == p; i++) {
slash++;
if(slash == 4)
return 1;
}
return 0;
}