-
Notifications
You must be signed in to change notification settings - Fork 0
/
att_br.cpp
159 lines (145 loc) · 5.07 KB
/
att_br.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
#include <vector>
#include <utility>
//returns a pair <direction, coordinate> where direction is North, Northeast, etc (clockwise)
//and coordinate is in the form x*8 + y
std::vector< std::pair<int, int> > get_directions(int board[][8], int x, int y, int color) {
std::vector< std::pair<int, int> > directions;
if(board[x][y]) {
return directions;
}
//north
if((y < 6) && (board[x][y+1] == -color)) {
for(int i=y+2; i < 8; i++) {
if(!board[x][i]) {
break;
} else if(board[x][i] == color) {
directions.push_back(std::make_pair<int, int>(0, x*8+i));
break;
}
}
}
//northeast
if((y < 6) && (x < 6) && (board[x+1][y+1] == -color)) {
for(int i=0; (x+i+2 < 8) && (y+i+2 < 8); i++) {
if(!board[x+i+2][y+i+2]) {
break;
} else if(board[x+i+2][y+i+2] == color) {
directions.push_back(std::make_pair<int, int>(1, (x+i+2)*8+(y+i+2)));
break;
}
}
}
//east
if((x < 6) && (board[x+1][y] == -color)) {
for(int i=x+2; i < 8; i++) {
if(!board[i][y]) {
break;
} else if(board[i][y] == color) {
directions.push_back(std::make_pair<int, int>(2, i*8+y));
break;
}
}
}
//southeast
if((y > 1) && (x < 6) && (board[x+1][y-1] == -color)) {
for(int i=0; (x+i+2 < 8) && (y-i-2 >= 0); i++) {
if(!board[x+i+2][y-i-2]) {
break;
} else if(board[x+i+2][y-i-2] == color) {
directions.push_back(std::make_pair<int, int>(3, (x+i+2)*8+(y-i-2)));
break;
}
}
}
//south
if((y > 1) && (board[x][y-1] == -color)) {
for(int i=y-2; i >= 0; i--) {
if(!board[x][i]) {
break;
} else if(board[x][i] == color) {
directions.push_back(std::make_pair<int, int>(4, x*8+i));
break;
}
}
}
//southwest
if((y > 1) && (x > 1) && (board[x-1][y-1] == -color)) {
for(int i=0; (x-i-2 >= 0) && (y-i-2 >= 0); i++) {
if(!board[x-i-2][y-i-2]) {
break;
} else if(board[x-i-2][y-i-2] == color) {
directions.push_back(std::make_pair<int, int>(5, (x-i-2)*8+(y-i-2)));
break;
}
}
}
int validp (int move) {
if ((move >= 11) && (move <= 88) && (move%10 >= 1) && (move%10 <= 8))
return 1;
else return 0;
}
/* findbracketingpiece is called from wouldflip (below).
findbracketingpiece starts from a *square* that is occupied
by a *player*'s opponent, moves in a direction, *dir*, past
all opponent pieces, until a square occupied by the *player*
is found. If a square occupied by *player* is not found before
hitting an EMPTY or OUTER square, then 0 is returned (i.e., no
bracketing piece found). For example, if the current board is
1 2 3 4 5 6 7 8
10 . . . . . . . .
20 . . . . . . . .
30 . . . . . . . .
40 . . b b b . . .
50 . . w w w b . .
60 . . . . . w . .
70 . . . . . . . .
80 . . . . . . . .
then findbracketingpiece(66, BLACK, board, -11) will return 44
findbracketingpiece(53, BLACK, board, 1) will return 56
findbracketingpiece(55, BLACK, board, -9) will return 0
*/
int findbracketingpiece(int square, int player, int * board, int dir) {
while (board[square] == opponent(player)) square = square + dir;
if (board[square] == player) return square;
else return 0;
}
/* wouldflip is called by legalmove (below). Give a *move* (square)
to which a player is considering moving, wouldflip returns
the square that brackets opponent pieces (along with the sqaure
under consideration, or 0 if no such bracketing square exists
*/
int wouldflip (int move, int player, int * board, int dir) {
int c;
c = move + dir;
if (board[c] == opponent(player))
return findbracketingpiece(c+dir, player, board, dir);
else return 0;
}
/* A "legal" move is a valid move, but it is also a move/location
that will bracket a sequence of the opposing player's pieces,
thus flipping at least one opponent piece. legalp considers a
move/square and seaches in all directions for at least one bracketing
square. The function returns 1 if at least one bracketing square
is found, and 0 otherwise.
*/
int legalp (int move, int player, int * board) {
int i;
if (!validp(move)) return 0;
if (board[move]==EMPTY) {
i=0;
while (i<=7 && !wouldflip(move, player, board, ALLDIRECTIONS[i])) i++;
if (i==8) return 0; else return 1;
}
else return 0;
}
//west
if((x > 1) && (board[x-1][y] == -color)) {
for(int i=x-2; i >= 0; i--) {
if(!board[i][y]) {
break;
} else if(board[i][y] == color) {
directions.push_back(std::make_pair<int, int>(6, i*8+y));
break;
}
}
}