-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rules_possibles.ml
273 lines (209 loc) · 8.88 KB
/
test_rules_possibles.ml
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
open Chesstypes
open Chessmodel
open Rules
open Demoboards
open Util
open Game
(*
Test rules.ml functions that have to do with finding all movements that are possible for pieces to make
(Inverse Rule Calculations)
*)
TEST_MODULE "possible_movements - simple board" = struct
let board_1 = make_empty_board()
(* black pieces *)
let _ = add_piece_to_board (board_1) ("8", "c") ("R1") (Black) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("7", "e") ("P1") (Black) ("Pawn") (Pawn)
let _ = add_piece_to_board (board_1) ("8", "f") ("K1") (Black) ("Knight") (Knight)
(* white pieces *)
let _ = add_piece_to_board (board_1) ("4", "f") ("B1") (White) ("Bishop") (Bishop)
let _ = add_piece_to_board (board_1) ("3", "a") ("Q" ) (White) ("Queen") (Queen)
let _ = add_piece_to_board (board_1) ("2", "d") ("R1") (White) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("3", "e") ("R2") (White) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("4", "c") ("P1") (White) ("Pawn") (Pawn)
let _ = add_piece_to_board (board_1) ("3", "g") ("P4") (White) ("Pawn") (Pawn)
(*
test board
------- Black ---------
a b c d e f g h
8 -- -- R1 -- -- K1 -- -- <-- all black pieces
7 -- -- -- -- P1 -- -- -- <-- all black pieces
6 -- -- -- -- -- -- -- --
5 -- -- -- -- -- -- -- --
4 -- -- P1 -- -- B1 -- -- <-- all white pieces
3 Q1 -- -- -- R2 -- P4 -- <-- all white pieces
2 -- -- -- R1 -- -- -- -- <-- all white pieces
1 -- -- -- -- -- -- -- --
------- White ---------
*)
let g = {make_empty_game() with board=board_1}
(* -- Black Pawn 1 -- *)
let mvmts = (possible_movements pawn_black_1 g)
let expected = [
((pawn_black_1,("7","e"),("6","e")),Basic);
((pawn_black_1,("7","e"),("5","e")),Basic)
]
TEST = (List.length mvmts) = 2
TEST = List.for_all (fun x -> List.mem x mvmts) expected
(* -- White Queen -- *)
let mvmts = (possible_movements queen_white_1 g)
(* check a subset of the possible moves *)
let expected = [
((queen_white_1,("3","a"),("8","a")),Basic);
((queen_white_1,("3","a"),("1","a")),Basic);
((queen_white_1,("3","a"),("7","e")),Capture);
((queen_white_1,("3","a"),("3","d")),Basic)
]
TEST = (List.length mvmts) = 7+2+3+4
TEST = List.for_all (fun x -> List.mem x mvmts) expected
(* -- Black Rook 1 -- *)
let mvmts = (possible_movements rook_black_1 g)
(* check a subset(or maybe all) of the possible moves *)
let expected = [
((rook_black_1,("8","c"),("8","a")),Basic);
((rook_black_1,("8","c"),("8","b")),Basic);
((rook_black_1,("8","c"),("8","d")),Basic);
((rook_black_1,("8","c"),("8","e")),Basic);
((rook_black_1,("8","c"),("7","c")),Basic);
((rook_black_1,("8","c"),("6","c")),Basic);
((rook_black_1,("8","c"),("5","c")),Basic);
((rook_black_1,("8","c"),("4","c")),Capture);
]
TEST = (List.length mvmts) = 8
TEST = List.for_all (fun x -> List.mem x mvmts) expected
(* White Bishop 1 *)
let mvmts = (possible_movements bishop_white_1 g)
TEST = (List.length mvmts)=6
end
TEST_MODULE "possible_movements - full board" = struct
(*
------- Black ---------
R2 K2 B2 K Q B1 K1 R1
P8 P7 P6 P5 P4 P3 P2 P1
-- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
P1 P2 P3 P4 P5 P6 P7 P8
R1 K1 B1 Q K B2 K2 R2
------- White ---------
*)
(* --- full_board --- *)
let board_full = make_init_board()
let g = {make_empty_game() with board=board_full}
(* Pawns - 2 initial moves *)
let mvmts = (possible_movements pawn_white_1 g)
TEST = (List.length mvmts)=2
let mvmts = (possible_movements pawn_black_1 g)
TEST = (List.length mvmts)=2
let mvmts = (possible_movements pawn_white_3 g)
TEST = (List.length mvmts)=2
(* Queens - 0 initial moves *)
let mvmts = (possible_movements queen_white_1 g)
TEST = (List.length mvmts)=0
let mvmts = (possible_movements queen_black_1 g)
TEST = (List.length mvmts)=0
(* Knights - 0 initial moves *)
let mvmts = (possible_movements knight_white_1 g)
TEST = (List.length mvmts)=2
let mvmts = (possible_movements knight_white_2 g)
TEST = (List.length mvmts)=2
(* Bishops - 0 initial moves *)
let mvmts = (possible_movements bishop_white_1 g)
TEST = (List.length mvmts)=0
end
TEST_MODULE "pieces_capturable_by_moves - simple" = struct
let board_1 = make_empty_board()
(* black pieces *)
let _ = add_piece_to_board (board_1) ("8", "c") ("R1") (Black) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("7", "e") ("P1") (Black) ("Pawn") (Pawn)
let _ = add_piece_to_board (board_1) ("8", "f") ("K1") (Black) ("Knight") (Knight)
(* white pieces *)
let _ = add_piece_to_board (board_1) ("4", "f") ("B1") (White) ("Bishop") (Bishop)
let _ = add_piece_to_board (board_1) ("3", "a") ("Q" ) (White) ("Queen") (Queen)
let _ = add_piece_to_board (board_1) ("2", "d") ("R1") (White) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("3", "e") ("R2") (White) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("4", "c") ("P1") (White) ("Pawn") (Pawn)
let _ = add_piece_to_board (board_1) ("3", "g") ("P4") (White) ("Pawn") (Pawn)
(*
test board
------- Black ---------
a b c d e f g h
8 -- -- R1 -- -- K1 -- -- <-- all black pieces
7 -- -- -- -- P1 -- -- -- <-- all black pieces
6 -- -- -- -- -- -- -- --
5 -- -- -- -- -- -- -- --
4 -- -- P1 -- -- B1 -- -- <-- all white pieces
3 Q1 -- -- -- R2 -- P4 -- <-- all white pieces
2 -- -- -- R1 -- -- -- -- <-- all white pieces
1 -- -- -- -- -- -- -- --
------- White ---------
*)
let g = {make_empty_game() with board=board_1}
(* -- White Queen as Attacker -- *)
let mvmts = (possible_movements queen_white_1 g)
let vulnerable_pcs = pieces_capturable_by_moves mvmts board_1
let expected_pcs = [pawn_black_1]
TEST = (List.length vulnerable_pcs) = 1
TEST = List.for_all (fun x -> List.mem x vulnerable_pcs) expected_pcs
(* -- White Rook 2 as Attacker -- *)
let mvmts = (possible_movements rook_white_2 g)
let vulnerable_pcs = pieces_capturable_by_moves mvmts board_1
let expected_pcs = [pawn_black_1]
TEST = (List.length vulnerable_pcs) = 1
TEST = List.for_all (fun x -> List.mem x vulnerable_pcs) expected_pcs
(* let mvmts = possible_movements rook_white_1 g
let vulnerable_pcs = pieces_capturable_by_moves mvmts board_1
TEST = (List.length vulnerable_pcs)=1 *)
end
TEST_MODULE "pieces_capturable_by_moves - complex" = struct
let board_1 = make_empty_board()
(* black pieces *)
let _ = add_piece_to_board (board_1) ("8", "c") ("R1") (Black) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("5", "g") ("R2") (Black) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("7", "e") ("P1") (Black) ("Pawn") (Pawn)
let _ = add_piece_to_board (board_1) ("7", "a") ("P2") (Black) ("Pawn") (Pawn)
let _ = add_piece_to_board (board_1) ("8", "f") ("K1") (Black) ("Knight") (Knight)
let _ = add_piece_to_board (board_1) ("5", "d") ("K2") (Black) ("Knight") (Knight)
(* white pieces *)
let _ = add_piece_to_board (board_1) ("4", "f") ("B1") (White) ("Bishop") (Bishop)
let _ = add_piece_to_board (board_1) ("3", "h") ("B2") (White) ("Bishop") (Bishop)
let _ = add_piece_to_board (board_1) ("3", "a") ("Q" ) (White) ("Queen") (Queen)
let _ = add_piece_to_board (board_1) ("2", "d") ("R1") (White) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("3", "e") ("R2") (White) ("Rook") (Rook)
let _ = add_piece_to_board (board_1) ("4", "c") ("P1") (White) ("Pawn") (Pawn)
let _ = add_piece_to_board (board_1) ("3", "g") ("P4") (White) ("Pawn") (Pawn)
(*
test board
------- Black ---------
a b c d e f g h
8 -- -- R1 -- -- K1 -- -- <-- all black pieces
7 P2 -- -- -- P1 -- -- -- <-- all black pieces
6 -- -- -- -- -- -- -- --
5 -- -- -- K2 -- -- R2 -- <-- all black pieces
4 -- -- P1 -- -- B1 -- -- <-- all white pieces
3 Q1 -- -- -- R2 -- P4 B2 <-- all white pieces
2 -- -- -- R1 -- -- -- -- <-- all white pieces
1 -- -- -- -- -- -- -- --
------- White ---------
*)
let g = {make_empty_game() with board=board_1}
(* -- Black Knight 2 as Attacker -- *)
let mvmts = (possible_movements knight_black_2 g)
let vulnerable_pcs = pieces_capturable_by_moves mvmts board_1
let expected_pcs = [bishop_white_1;rook_white_2]
TEST = (List.length vulnerable_pcs) = 2
TEST = List.for_all (fun x -> List.mem x vulnerable_pcs) expected_pcs
(* -- White Bishop 2 as Attacker -- *)
let mvmts = (possible_movements bishop_white_2 g)
let vulnerable_pcs = pieces_capturable_by_moves mvmts board_1
let expected_pcs = [rook_black_1]
TEST = (List.length vulnerable_pcs) = 1
TEST = List.for_all (fun x -> List.mem x vulnerable_pcs) expected_pcs
(* -- White Queen as Attacker -- *)
let mvmts = (possible_movements queen_white_1 g)
let vulnerable_pcs = pieces_capturable_by_moves mvmts board_1
let expected_pcs = [pawn_black_2;pawn_black_1]
TEST = (List.length vulnerable_pcs) = 2
TEST = List.for_all (fun x -> List.mem x vulnerable_pcs) expected_pcs
end
Pa_ounit_lib.Runtime.summarize ()