-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_board.cc
195 lines (171 loc) · 5.67 KB
/
test_board.cc
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
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <functional>
#include "Board.hh"
const board_array START_BOARD = {
'R','N','B','Q','K','B','N','R',
'P','P','P','P','P','P','P','P',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r'
};
const board_array E4 = {
'R','N','B','Q','K','B','N','R',
'P','P','P','P',' ','P','P','P',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ','P',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r'
};
const board_array ExD5 = {
'R','N','B','Q','K','B','N','R',
'P','P','P','P',' ','P','P','P',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ','P',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
'p','p','p',' ','p','p','p','p',
'r','n','b','q','k','b','n','r'
};
const board_array EN_PASSANT = {
'R','N','B','Q','K','B','N','R',
'P','P','P','P',' ','P','P','P',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ','P',' ',' ',' ',
'p','p','p',' ',' ','p','p','p',
'r','n','b','q','k','b','n','r'
};
bool vec_contains(std::vector<Move> v, const Move m){
auto lamb = [&m](Move const &m2){return m.start == m2.start && m.end == m2.end; };
return std::any_of(v.begin(), v.end(), lamb) > 0;
}
auto b = std::make_unique<Board>();
TEST_CASE( "board setup" ){
REQUIRE( b->get_past_moves().size() == 0 );
REQUIRE( b->get_board() == START_BOARD );
REQUIRE( !b->game_over() );
REQUIRE( b->is_white_turn() );
}
TEST_CASE( "initial move calculation" ){
REQUIRE( b->get_legal_moves(true).size() == 20 );
REQUIRE( b->get_legal_moves(false).size() == 20 );
}
TEST_CASE( "moves update board state" ){
b->play_move({"e2","e4"});
REQUIRE( b->get_board() == E4);
REQUIRE( !b->is_white_turn() );
b->play_move({"d7","d5"});
REQUIRE( b->get_past_moves().size() == 2 );
}
TEST_CASE( "captures happen" ){
REQUIRE( vec_contains(b->get_legal_moves(true), {"e4","d5"}) );
b->play_move({"e4","d5"});
REQUIRE( b->get_board() == ExD5 );
REQUIRE( !vec_contains(b->get_legal_moves(false), {"d5","d4"}) );
}
TEST_CASE( "en passant" ){
b->play_move({"e7","e5"});
REQUIRE( vec_contains(b->get_legal_moves(true), {"d5","e6"}) );
b->play_move({"d5","e6"});
}
TEST_CASE ( "bishops" ){
REQUIRE( vec_contains(b->get_legal_moves(false), {"c8","d7"}) );
b->play_move({"c8", "d7"});
b->play_move({"f1","a6"});
}
TEST_CASE ( "knights" ) {
REQUIRE( vec_contains(b->get_legal_moves(false), {"b8", "c6"}) );
b->play_move({"b8", "c6"});
b->play_move({"g1","f3"});
}
TEST_CASE ( "queens" ) {
REQUIRE( vec_contains(b->get_legal_moves(false), {"d8","h4"}) );
b->play_move({"d8","h4"});
b->play_move({"d1","e2"});
REQUIRE( vec_contains(b->get_legal_moves(false), {"h4","a4"}) );
REQUIRE( vec_contains(b->get_legal_moves(false), {"h4","h5"}) );
}
TEST_CASE ( "check" ) {
b->play_move({"h4","f2"});
REQUIRE( b->get_legal_moves(true).size() == 3);
b->play_move({"e2","f2"});
}
TEST_CASE ( "castling" ) {
REQUIRE( vec_contains(b->get_legal_moves(false), {"e8","c8"}) );
b->play_move({"b7","b6"});
b->play_move({"e1","g1"});
// can't castle into check
REQUIRE( !vec_contains(b->get_legal_moves(false), {"e8","c8"}) );
b->play_move({"h7","h6"});
b->play_move({"a6","b5"});
b->play_move({"h6","h5"});
b->play_move({"e6","e7"});
// can't castle through check
REQUIRE( !vec_contains(b->get_legal_moves(false), {"e8","c8"}) );
b->play_move({"h5","h4"});
}
TEST_CASE( "promoting" ) {
REQUIRE( vec_contains(b->get_legal_moves(true), {"e7","f8=Q"}) );
REQUIRE( vec_contains(b->get_legal_moves(true), {"e7","f8=B"}) );
REQUIRE( vec_contains(b->get_legal_moves(true), {"e7","f8=N"}) );
REQUIRE( vec_contains(b->get_legal_moves(true), {"e7","f8=R"}) );
b->play_move({"e7","f8=Q"});
}
TEST_CASE( "castling pt 2" ) {
// Can't castle out of check
REQUIRE( !vec_contains(b->get_legal_moves(false), {"e8","c8"}) );
b->play_move({"e8","f8"});
b->play_move({"b2","b3"});
// Can't castle once have moved king
REQUIRE( !vec_contains(b->get_legal_moves(false), {"f8","c8"}) );
b->play_move({"a7","a6"});
}
TEST_CASE( "checkmate" ) {
b->play_move({"f1","e1"});
b->play_move({"h4","h3"});
b->play_move({"c1","a3"});
b->play_move({"c6","b4"});
b->play_move({"a3","b4"});
b->play_move({"c7","c5"});
b->play_move({"b4","c5"});
b->play_move({"b6","c5"});
b->play_move({"f2","c5"});
b->play_move({"g8","e7"});
b->play_move({"c5","e7"});
b->play_move({"f8","g8"});
b->play_move({"f3","g5"});
b->play_move({"a8","e8"});
b->play_move({"e7","e8"});
b->play_move({"d7","e8"});
b->play_move({"e1","e8"});
REQUIRE( b->game_over() );
REQUIRE( b->white_wins() );
}
TEST_CASE( "reset" ) {
b->reset();
REQUIRE( b->get_past_moves().size() == 0 );
REQUIRE( b->get_board() == START_BOARD );
REQUIRE( !b->game_over() );
REQUIRE( b->is_white_turn() );
}
TEST_CASE( "threefold repetition" ) {
b->play_move({"g1","f3"});
b->play_move({"g8","f6"});
b->play_move({"f3","g1"});
b->play_move({"f6","g8"});
b->play_move({"g1","f3"});
b->play_move({"g8","f6"});
b->play_move({"f3","g1"});
b->play_move({"f6","g8"});
REQUIRE( b->game_over() );
REQUIRE( !b->white_wins() );
REQUIRE( !b->black_wins() );
b->reset();
}