is_checked() -> is_attacked(). Fix checking bug/refactored is_attacked.

master
Tait Hoyem 5 years ago
parent da57a72a2e
commit 25ddada744

@ -97,16 +97,17 @@ void get_poss_of(PieceType pt, std::array<PieceType, 120>* board, std::vector<in
} }
} }
bool is_checked(int pos, std::array<PieceType, 120> board){ bool is_attacked(int pos, std::array<PieceType, 120> board){
PieceType ptt = board[pos]; PieceType ptt = board[pos];
Color pc = get_color(ptt); Color pc = get_color(ptt);
auto other_pieces = pc==Color::WHITE?Pieces::BLACK:Pieces::WHITE; auto other_pieces = pc==Color::WHITE?Pieces::BLACK:Pieces::WHITE;
for (PieceType pt : other_pieces){ for (PieceType opposite_color_piece : other_pieces){
for (int pos_of_other : get_poss_of(pt, &board)) { PieceType same_color_piece = rev_color(opposite_color_piece);
for (int possible_takes : get_all_moves(pos_of_other, board, false)){ std::vector<int> moves;
if (get_to_sq(possible_takes) == pos) { get_all_moves_as_if(pos, same_color_piece, &board, &moves, false);
return true; for (int reverse_move : moves){
} if (get_captured_pc(reverse_move) == opposite_color_piece){
return true;
} }
} }
} }
@ -118,7 +119,7 @@ void add_checked_flags(PieceType pt, std::array<PieceType, 120> *board, std::vec
int other_king_pos = get_pos_of(other_king, board); int other_king_pos = get_pos_of(other_king, board);
for (auto move_pn=pns->begin(); move_pn!=pns->end();){ for (auto move_pn=pns->begin(); move_pn!=pns->end();){
auto moved_board = dumb_move(*move_pn, *board); auto moved_board = dumb_move(*move_pn, *board);
if (is_checked(other_king_pos, moved_board)){ if (is_attacked(other_king_pos, moved_board)){
*move_pn |= (1 << 25); *move_pn |= (1 << 25);
} }
++move_pn; ++move_pn;
@ -148,8 +149,8 @@ void filter_checked_moves(PieceType pt, std::array<PieceType, 120> *board, std::
int full_move = make_move(get_from_sq(*p_pn), get_to_sq(*p_pn)); int full_move = make_move(get_from_sq(*p_pn), get_to_sq(*p_pn));
auto right_board = dumb_move(right_move, *board); auto right_board = dumb_move(right_move, *board);
auto full_board = dumb_move(full_move, *board); auto full_board = dumb_move(full_move, *board);
if (is_checked(get_to_sq(*p_pn)+1, right_board) || if (is_attacked(get_to_sq(*p_pn)+1, right_board) ||
is_checked(get_to_sq(*p_pn), full_board)){ is_attacked(get_to_sq(*p_pn), full_board)){
p_pn = pns->erase(p_pn); p_pn = pns->erase(p_pn);
} else { } else {
++p_pn; ++p_pn;
@ -161,8 +162,8 @@ void filter_checked_moves(PieceType pt, std::array<PieceType, 120> *board, std::
int full_move = make_move(get_from_sq(*p_pn), get_to_sq(*p_pn)); int full_move = make_move(get_from_sq(*p_pn), get_to_sq(*p_pn));
auto left_board = dumb_move(left_move, *board); auto left_board = dumb_move(left_move, *board);
auto full_board = dumb_move(full_move, *board); auto full_board = dumb_move(full_move, *board);
if (is_checked(get_to_sq(*p_pn)-1, left_board) || if (is_attacked(get_to_sq(*p_pn)-1, left_board) ||
is_checked(get_to_sq(*p_pn), full_board)){ is_attacked(get_to_sq(*p_pn), full_board)){
p_pn = pns->erase(p_pn); p_pn = pns->erase(p_pn);
} else { } else {
++p_pn; ++p_pn;
@ -174,7 +175,7 @@ void filter_checked_moves(PieceType pt, std::array<PieceType, 120> *board, std::
// remove all castle moves // remove all castle moves
if ((pt == PieceType::W_KING || if ((pt == PieceType::W_KING ||
pt == PieceType::B_KING) && pt == PieceType::B_KING) &&
is_checked(get_from_sq(*p_pn), *board)){ is_attacked(get_from_sq(*p_pn), *board)){
remove_all_castles = true; remove_all_castles = true;
} }
// Make move // Make move
@ -185,7 +186,7 @@ void filter_checked_moves(PieceType pt, std::array<PieceType, 120> *board, std::
my_king_pos = get_to_sq(*p_pn); my_king_pos = get_to_sq(*p_pn);
} }
if (is_checked(my_king_pos, moved_board)){ if (is_attacked(my_king_pos, moved_board)){
p_pn = pns->erase(p_pn); p_pn = pns->erase(p_pn);
} else { } else {
++p_pn; ++p_pn;

@ -57,7 +57,7 @@ std::vector<int> get_all_moves(int pos, std::array<PieceType, 120> board, bool r
std::array<PieceType, 120> dumb_move(int move, std::array<PieceType, 120> board); std::array<PieceType, 120> dumb_move(int move, std::array<PieceType, 120> board);
// Decides if there this piece in position is in check // Decides if there this piece in position is in check
bool is_checked(int pos, std::array<PieceType, 120> board); bool is_attacked(int pos, std::array<PieceType, 120> board);
// Convert move (and board becuase notation needs more info than the move itself) // Convert move (and board becuase notation needs more info than the move itself)
// ...into algbraic notation. // ...into algbraic notation.

@ -119,14 +119,14 @@ TEST_CASE("Test all moves for black in edge cases.", "[get_all_moves][black]"){
CHECK(get_to_squares(get_all_moves(B_PAWN_SIDE1_POS, B_PAWN_SIDE1_BOARD)) == B_PAWN_SIDE1_ALL_MOVES); CHECK(get_to_squares(get_all_moves(B_PAWN_SIDE1_POS, B_PAWN_SIDE1_BOARD)) == B_PAWN_SIDE1_ALL_MOVES);
} }
TEST_CASE("Tests is_king_checked works", "[is_checked]"){ TEST_CASE("Tests is_king_checked works", "[is_attacked]"){
auto king_checked_moves = get_all_moves(KING_CHECK_TEST_POS, KING_CHECK_TEST_BOARD); auto king_checked_moves = get_all_moves(KING_CHECK_TEST_POS, KING_CHECK_TEST_BOARD);
auto rook_checked_moves = get_all_moves(ROOK_CHECK_TEST_POS, KING_CHECK_TEST_BOARD); auto rook_checked_moves = get_all_moves(ROOK_CHECK_TEST_POS, KING_CHECK_TEST_BOARD);
CHECK(get_to_squares(king_checked_moves) == KING_CHECK_TEST_MOVES); CHECK(get_to_squares(king_checked_moves) == KING_CHECK_TEST_MOVES);
CHECK(get_to_squares(rook_checked_moves) == KING_CHECK_ROOK_MOVES); CHECK(get_to_squares(rook_checked_moves) == KING_CHECK_ROOK_MOVES);
CHECK(is_checked(KING_CHECK_TEST_POS, KING_CHECK_TEST_BOARD)); CHECK(is_attacked(KING_CHECK_TEST_POS, KING_CHECK_TEST_BOARD));
CHECK(is_checked(BLACK_CHECK_POS1, BLACK_CHECK_BOARD1)); CHECK(is_attacked(BLACK_CHECK_POS1, BLACK_CHECK_BOARD1));
CHECK(is_checked(BLACK_CHECK_POS2, BLACK_CHECK_BOARD2)); CHECK(is_attacked(BLACK_CHECK_POS2, BLACK_CHECK_BOARD2));
} }
TEST_CASE("Test that moves that put own king in check are not returned", "[get_all_moves]"){ TEST_CASE("Test that moves that put own king in check are not returned", "[get_all_moves]"){
@ -161,11 +161,14 @@ TEST_CASE("Test that the captures moves are returned", "[get_all_moves]"){
auto rook_moves = get_all_moves(ROOK_BLOCKED1_POS, ROOK_BLOCKED1_BOARD); auto rook_moves = get_all_moves(ROOK_BLOCKED1_POS, ROOK_BLOCKED1_BOARD);
auto pawn_moves = get_all_moves(PAWN_DIAG_TEST1_POS, PAWN_DIAG_TEST1_BOARD); auto pawn_moves = get_all_moves(PAWN_DIAG_TEST1_POS, PAWN_DIAG_TEST1_BOARD);
auto king_moves = get_all_moves(KING_CHECK_TEST_POS, KING_CHECK_TEST_BOARD); auto king_moves = get_all_moves(KING_CHECK_TEST_POS, KING_CHECK_TEST_BOARD);
auto rook_check_moves = get_all_moves(ROOK_CHECK_MOVED_POS, ROOK_CHECK_MOVED_BOARD);
CHECK(get_captured_pieces(knight_moves) == KNIGHT_BLOCKED1_CAPTS); CHECK(get_captured_pieces(knight_moves) == KNIGHT_BLOCKED1_CAPTS);
CHECK(get_captured_pieces(bishop_moves) == BISHOP_BLOCKED1_CAPTS); CHECK(get_captured_pieces(bishop_moves) == BISHOP_BLOCKED1_CAPTS);
CHECK(get_captured_pieces(rook_moves) == ROOK_BLOCKED1_CAPTS); CHECK(get_captured_pieces(rook_moves) == ROOK_BLOCKED1_CAPTS);
CHECK(get_captured_pieces(pawn_moves) == PAWN_DIAG_TEST1_CAPTS); CHECK(get_captured_pieces(pawn_moves) == PAWN_DIAG_TEST1_CAPTS);
CHECK(get_captured_pieces(king_moves) == KING_CHECK_TEST_CAPTS); CHECK(get_captured_pieces(king_moves) == KING_CHECK_TEST_CAPTS);
CHECK(get_captured_pieces(rook_check_moves) == ROOK_CHECK_MOVED_CAPTURES);
} }
TEST_CASE("Test that being blocked, stops moves from generating", "[get_all_moves]"){ TEST_CASE("Test that being blocked, stops moves from generating", "[get_all_moves]"){
@ -288,8 +291,8 @@ TEST_CASE("Tests for check on square of queenside capture", "[get_all_moves]"){
CHECK(get_notations(cannot_queenside4, CASTLE_CHECK4_BOARD) == CASTLE_CHECK4_NOTATION); CHECK(get_notations(cannot_queenside4, CASTLE_CHECK4_BOARD) == CASTLE_CHECK4_NOTATION);
} }
TEST_CASE("Test that king check detection is working correctly.", "[is_checked]"){ TEST_CASE("Test that king check detection is working correctly.", "[is_attacked]"){
CHECK(is_checked(ROOK_CHECK_KING_POS, ROOK_CHECK_MOVED_BOARD)); CHECK(is_attacked(ROOK_CHECK_KING_POS, ROOK_CHECK_MOVED_BOARD));
} }
TEST_CASE("Test for add_checked_flags is working correctly.", "[get_all_moves][add_checked_flags]"){ TEST_CASE("Test for add_checked_flags is working correctly.", "[get_all_moves][add_checked_flags]"){

@ -874,7 +874,8 @@ const std::vector<std::string> CASTLE_CHECK4_NOTATION = {
// This is a check that moves are being marked as "check moves" poperly // This is a check that moves are being marked as "check moves" poperly
const int ROOK_CHECK_POS = H3; const int ROOK_CHECK_POS = H3;
const int ROOK_CHECK_KING_POS = H3; const int ROOK_CHECK_MOVED_POS = H1;
const int ROOK_CHECK_KING_POS = G1;
const std::array<PieceType, 120> ROOK_CHECK_BOARD = { const std::array<PieceType, 120> ROOK_CHECK_BOARD = {
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, INV,
@ -906,6 +907,12 @@ const std::array<PieceType, 120> ROOK_CHECK_MOVED_BOARD = {
const std::vector<std::string> ROOK_CHECK_NOTATION = { const std::vector<std::string> ROOK_CHECK_NOTATION = {
"Rh2", "Rh1+" "Rh2", "Rh1+"
}; };
const std::vector<int> ROOK_CHECK_MOVED_MOVES = {
H3, H2, G1
};
const std::vector<int> ROOK_CHECK_MOVED_CAPTURES = {
W_KING, NONE, NONE
};
const std::vector<int> ROOK_CHECK_FLAGS = { const std::vector<int> ROOK_CHECK_FLAGS = {
0, 1 0, 1
}; };

Loading…
Cancel
Save