I have a special function assert_invariants() that checks whether the position is legally valid. There are 16 assert() statements inside that function, and it is called inside every constructor and inside make_move(). In debug mode this checks that I am always in a valid position. See below for the code. Note that disjoint(a,b) means !(a & b), using bitwise-AND.
Code: Select all
void assert_invariants() const noexcept
{
assert(squares_v<board_type> == (pieces(occup_c) | pieces(empty_c)));
assert(pieces(occup_c) == (pieces(black_c) | pieces(white_c)));
assert(pieces(occup_c) == (pieces(pawns_c) | pieces(kings_c)));
assert(pieces(black_c) == (pieces(black_c, pawns_c) | pieces(black_c, kings_c)));
assert(pieces(white_c) == (pieces(white_c, pawns_c) | pieces(white_c, kings_c)));
assert(pieces(pawns_c) == (pieces(black_c, pawns_c) | pieces(white_c, pawns_c)));
assert(pieces(kings_c) == (pieces(black_c, kings_c) | pieces(white_c, kings_c)));
assert(disjoint(pieces(occup_c), pieces(empty_c)));
assert(disjoint(pieces(black_c), pieces(white_c)));
assert(disjoint(pieces(pawns_c), pieces(kings_c)));
assert(disjoint(pieces(black_c, pawns_c), pieces(black_c, kings_c)));
assert(disjoint(pieces(white_c, pawns_c), pieces(white_c, kings_c)));
assert(disjoint(pieces(black_c, pawns_c), pieces(white_c, pawns_c)));
assert(disjoint(pieces(black_c, kings_c), pieces(white_c, kings_c)));
assert(disjoint(pieces(black_c, pawns_c), promotion_v<board_type, black_>));
assert(disjoint(pieces(white_c, pawns_c), promotion_v<board_type, white_>));
}