-
Notifications
You must be signed in to change notification settings - Fork 2
/
square_control_test.go
285 lines (235 loc) · 8.02 KB
/
square_control_test.go
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package chess_engine
import (
"strconv"
"strings"
"testing"
)
func expectQueenFromAt(t *testing.T, unit SquareControl, from, at Position) {
if unit.Get(White, at).Count() == 0 {
t.Errorf("Expecting white queen in piece vector at %s", at)
}
if unit.Get(White, at).ToPositions()[0] != from {
t.Errorf("Expecting %s got %v", from, unit.Get(White, at).ToPositions())
}
}
func Test_SquareControl(t *testing.T) {
board := NewBoard()
board[E4] = WhiteQueen
unit := NewSquareControl()
unit.addPiece(WhiteQueen, E4, board)
positions := E4.GetPieceMoves(WhiteQueen)
for _, pos := range positions {
if unit.Get(White, pos).Count() != 1 {
t.Errorf("Expecting an attack on %s, got %d", pos, unit.Get(White, pos).Count())
}
expectQueenFromAt(t, unit, E4, pos)
}
}
func Test_SquareControl_king_is_ignored(t *testing.T) {
board := NewBoard()
board[E4] = WhiteQueen
board[E2] = BlackKing
unit := NewSquareControl()
unit.addPiece(WhiteQueen, E4, board)
positions := E4.GetPieceMoves(WhiteQueen)
for _, pos := range positions {
if unit.Get(White, pos).Count() != 1 {
t.Errorf("Expecting an attack on %s", pos)
}
expectQueenFromAt(t, unit, E4, pos)
}
}
func Test_SquareControl_own_king_is_not_ignored(t *testing.T) {
board := NewBoard()
board[E6] = WhiteQueen
board[E3] = WhiteKing
unit := NewSquareControl()
unit.addPiece(WhiteQueen, E6, board)
unit.addPiece(WhiteKing, E3, board)
positions := E6.GetPieceMoves(WhiteQueen)
for _, pos := range positions {
if pos == E1 || pos == E2 {
if unit.Get(White, pos).Count() > 1 {
t.Errorf("Expecting no attacks on %s", pos)
}
continue
}
if unit.Get(White, pos).Count() == 0 {
t.Errorf("Expecting an attack on %s", pos)
}
expectQueenFromAt(t, unit, E6, pos)
}
}
func Test_SquareControl_ApplyMove(t *testing.T) {
board := NewBoard()
board[E6] = WhiteQueen
board[E3] = WhiteKing
orig := NewSquareControl()
orig.addPiece(WhiteQueen, E6, board)
orig.addPiece(WhiteKing, E3, board)
board.ApplyMove(E3, D3)
unit := orig.ApplyMove(NewMove(E3, D3), WhiteKing, NoPiece, board, NoPosition)
positions := E6.GetPieceMoves(WhiteQueen)
for _, pos := range positions {
if unit.Get(White, pos).Count() == 0 {
t.Errorf("Expecting an attack on %s", pos)
}
expectQueenFromAt(t, unit, E6, pos)
}
board.ApplyMove(D3, E3)
unit = unit.ApplyMove(NewMove(D3, E3), WhiteKing, NoPiece, board, NoPosition)
if unit.Get(White, C3).Count() != 0 {
t.Errorf("Expecting old king attacks to be removed")
}
for _, pos := range positions {
if pos == E1 || pos == E2 {
if unit.Get(White, pos).Count() > 1 {
t.Errorf("Expecting no attacks on %s, got %v", pos, unit.Get(White, pos).ToPositions())
}
continue
}
if orig.Get(White, pos).Count() != unit.Get(White, pos).Count() {
t.Fatalf("expecting same amount of attacks again on %s", pos)
} else if unit.Get(White, pos).Count() == 0 {
t.Errorf("Expecting an attack on %s", pos)
} else if unit.Get(White, pos).Count() == 0 {
t.Errorf("Expecting white queen in piece vector for %s", pos)
} else if unit.Get(White, pos).ToPositions()[0] != E6 {
t.Errorf("Expecting e6 got %s", unit.Get(White, pos).ToPositions()[0])
}
}
}
func Test_SquareControl_extends_previously_blocked_pieces(t *testing.T) {
cases := [][]string{
// Rook on same rank should be extended after pawn move
[]string{"4K3/8/8/8/1r2P3/8/8/4k3 w - - 0 1", "e4e5", "f4:1", "g4:1", "h4:1"},
// Queen on same rank should be extended after pawn move
[]string{"4K3/8/8/8/1q2P3/8/8/4k3 w - - 0 1", "e4e5", "f4:1", "g4:1", "h4:1"},
// Bishop on same rank shouldn't be extended after pawn move
[]string{"4K3/8/8/8/1b2P3/8/8/4k3 w - - 0 1", "e4e5", "f4:0", "g4:0", "h4:0"},
// Knight on same rank shouldn't be extended after pawn move
[]string{"4K3/8/8/8/1n2P3/8/8/4k3 w - - 0 1", "e4e5", "f4:0", "g4:0", "h4:0"},
// King on same rank shouldn't be extended after pawn move
[]string{"4K3/8/8/8/1k2P3/8/8/4k3 w - - 0 1", "e4e5", "f4:0", "g4:0", "h4:0"},
// Pawn on same rank shouldn't be extended after pawn move
[]string{"4K3/8/8/8/1p2P3/8/8/4k3 w - - 0 1", "e4e5", "f4:0", "g4:0", "h4:0"},
// Rook on same file should be shortened
[]string{"1K6/4r3/8/8/4P3/8/8/4k3 w - - 0 1", "e4e5", "e5:1", "e4:0"},
// Queen on same file should be shortened
[]string{"1K6/4q3/8/8/4P3/8/8/4k3 w - - 0 1", "e4e5", "e5:1", "e4:0"},
}
for _, testCase := range cases {
fen, move, updatedSquares := testCase[0], testCase[1], testCase[2:]
game, err := ParseFEN(fen)
if err != nil {
t.Fatalf("Invalid FEN %s", fen)
}
unit := NewSquareControlFromBoard(game.Board)
applyMove := MustParseMove(move)
movingPiece := game.Board[applyMove.From]
capturedPiece := game.Board[applyMove.To]
game.Board.ApplyMove(applyMove.From, applyMove.To)
updatedUnit := unit.ApplyMove(applyMove, movingPiece, capturedPiece, game.Board, NoPosition)
for _, square := range updatedSquares {
sqParts := strings.Split(square, ":")
sq := MustParsePosition(sqParts[0])
expected, _ := strconv.Atoi(sqParts[1])
old := unit.GetAttacksOnSquare(Black, sq)
new := updatedUnit.GetAttacksOnSquare(Black, sq)
if len(new) != expected {
t.Errorf("Expecting %d attacks on %s; got %s, was %s", expected, square, new, old)
}
}
}
}
func Test_SquareControl_ApplyMove_captures(t *testing.T) {
board := NewBoard()
board[E6] = WhiteQueen
board[E5] = BlackKing
orig := NewSquareControl()
orig.addPiece(WhiteQueen, E6, board)
orig.addPiece(BlackKing, E5, board)
board.ApplyMove(E5, E6)
unit := orig.ApplyMove(NewMove(E5, E6), BlackKing, WhiteQueen, board, NoPosition)
positions := E6.GetPieceMoves(WhiteQueen)
for _, pos := range positions {
if unit.Get(White, pos).Count() != 0 {
t.Errorf("Expecting white position to be removed in %s, got %v", pos, unit.Get(White, pos).ToPositions())
}
if orig.Get(White, pos).Count() == 0 {
t.Errorf("Expecting white queen in piece vector for %s, got %v", pos, orig.Get(White, pos).ToPositions())
}
}
}
func Test_SquareControl_white_pawn(t *testing.T) {
board := NewBoard()
board[E4] = WhitePawn
unit := NewSquareControl()
unit.addPiece(WhitePawn, E4, board)
positions := E4.GetPawnAttacks(White)
for _, pos := range positions {
if unit.Get(White, pos).Count() != 1 {
t.Errorf("Expecting an attack on %s", pos)
}
if unit.Get(White, pos).Count() != 1 {
t.Errorf("Expecting white pawn in piece vector")
}
if unit.Get(White, pos).ToPositions()[0] != E4 {
t.Errorf("Expecting e4 got %v", unit.Get(White, pos).ToPositions())
}
}
}
func Test_SquareControl_black_pawn(t *testing.T) {
board := NewBoard()
board[E3] = BlackPawn
unit := NewSquareControl()
unit.addPiece(BlackPawn, E3, board)
positions := E3.GetPawnAttacks(Black)
for _, pos := range positions {
if unit.Get(Black, pos).Count() != 1 {
t.Errorf("Expecting an attack on %s", pos)
}
if unit.Get(Black, pos).Count() != 1 {
t.Errorf("Expecting black pawn in piece vector")
}
if unit.Get(Black, pos).ToPositions()[0] != E3 {
t.Errorf("Expecting e3 got %v", unit.Get(Black, pos).ToPositions())
}
}
}
func Test_SquareControl_GetAttacksOnSquare(t *testing.T) {
board := NewBoard()
board[E5] = BlackPawn
pieces := NewPiecePositions()
pieces.AddPosition(BlackPawn, E5)
unit := NewSquareControl()
unit.addPiece(BlackPawn, E5, board)
attacks := unit.GetAttacksOnSquare(Black, D4)
if unit.Get(Black, D4).Count() != 1 {
t.Errorf("Supposed to have an attack")
}
if len(attacks) != 1 {
t.Errorf("Expecting one attack, got %v", attacks)
}
if attacks[0].From != E5 {
t.Errorf("Expecting attack from e5, got %v", attacks)
}
}
func Test_SquareControl_GetPinnedPieces(t *testing.T) {
board := NewBoard()
pieces := NewPiecePositions()
pieces.AddPosition(BlackKing, E1)
pieces.AddPosition(BlackPawn, D2)
pieces.AddPosition(WhiteQueen, B4)
board[E1] = BlackKing
board[D2] = BlackPawn
board[B4] = WhiteQueen
unit := NewSquareControl()
for _, pos := range []Position{E1, D2, B4} {
unit.addPiece(board[pos], pos, board)
}
pinned := unit.GetPinnedPieces(board, Black, E1)
if len(pinned) != 1 {
t.Errorf("Supposed to have a pinned piece")
}
}