From 25ddada7447f61f967f32d1cb0e72297838e9e89 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <44244401+TTWNO@users.noreply.github.com> Date: Sun, 28 Apr 2019 16:41:36 +0000 Subject: [PATCH] is_checked() -> is_attacked(). Fix checking bug/refactored is_attacked. --- src/functions.cpp | 29 +++++++++++++++-------------- src/functions.h | 2 +- tests/main.cpp | 15 +++++++++------ tests/valid_moves.h | 9 ++++++++- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/functions.cpp b/src/functions.cpp index 95cf9aa..def07ac 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -97,16 +97,17 @@ void get_poss_of(PieceType pt, std::array* board, std::vector board){ +bool is_attacked(int pos, std::array board){ PieceType ptt = board[pos]; Color pc = get_color(ptt); auto other_pieces = pc==Color::WHITE?Pieces::BLACK:Pieces::WHITE; - for (PieceType pt : other_pieces){ - for (int pos_of_other : get_poss_of(pt, &board)) { - for (int possible_takes : get_all_moves(pos_of_other, board, false)){ - if (get_to_sq(possible_takes) == pos) { - return true; - } + for (PieceType opposite_color_piece : other_pieces){ + PieceType same_color_piece = rev_color(opposite_color_piece); + std::vector moves; + get_all_moves_as_if(pos, same_color_piece, &board, &moves, false); + 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 *board, std::vec int other_king_pos = get_pos_of(other_king, board); for (auto move_pn=pns->begin(); move_pn!=pns->end();){ 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; @@ -148,8 +149,8 @@ void filter_checked_moves(PieceType pt, std::array *board, std:: int full_move = make_move(get_from_sq(*p_pn), get_to_sq(*p_pn)); auto right_board = dumb_move(right_move, *board); auto full_board = dumb_move(full_move, *board); - if (is_checked(get_to_sq(*p_pn)+1, right_board) || - is_checked(get_to_sq(*p_pn), full_board)){ + if (is_attacked(get_to_sq(*p_pn)+1, right_board) || + is_attacked(get_to_sq(*p_pn), full_board)){ p_pn = pns->erase(p_pn); } else { ++p_pn; @@ -161,8 +162,8 @@ void filter_checked_moves(PieceType pt, std::array *board, std:: int full_move = make_move(get_from_sq(*p_pn), get_to_sq(*p_pn)); auto left_board = dumb_move(left_move, *board); auto full_board = dumb_move(full_move, *board); - if (is_checked(get_to_sq(*p_pn)-1, left_board) || - is_checked(get_to_sq(*p_pn), full_board)){ + if (is_attacked(get_to_sq(*p_pn)-1, left_board) || + is_attacked(get_to_sq(*p_pn), full_board)){ p_pn = pns->erase(p_pn); } else { ++p_pn; @@ -174,7 +175,7 @@ void filter_checked_moves(PieceType pt, std::array *board, std:: // remove all castle moves if ((pt == PieceType::W_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; } // Make move @@ -185,7 +186,7 @@ void filter_checked_moves(PieceType pt, std::array *board, std:: 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); } else { ++p_pn; diff --git a/src/functions.h b/src/functions.h index 92e6836..e0b385c 100644 --- a/src/functions.h +++ b/src/functions.h @@ -57,7 +57,7 @@ std::vector get_all_moves(int pos, std::array board, bool r std::array dumb_move(int move, std::array board); // Decides if there this piece in position is in check -bool is_checked(int pos, std::array board); +bool is_attacked(int pos, std::array board); // Convert move (and board becuase notation needs more info than the move itself) // ...into algbraic notation. diff --git a/tests/main.cpp b/tests/main.cpp index 69d1e76..e828eb2 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -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); } -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 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(rook_checked_moves) == KING_CHECK_ROOK_MOVES); - CHECK(is_checked(KING_CHECK_TEST_POS, KING_CHECK_TEST_BOARD)); - CHECK(is_checked(BLACK_CHECK_POS1, BLACK_CHECK_BOARD1)); - CHECK(is_checked(BLACK_CHECK_POS2, BLACK_CHECK_BOARD2)); + CHECK(is_attacked(KING_CHECK_TEST_POS, KING_CHECK_TEST_BOARD)); + CHECK(is_attacked(BLACK_CHECK_POS1, BLACK_CHECK_BOARD1)); + 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]"){ @@ -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 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 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(bishop_moves) == BISHOP_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(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]"){ @@ -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); } -TEST_CASE("Test that king check detection is working correctly.", "[is_checked]"){ - CHECK(is_checked(ROOK_CHECK_KING_POS, ROOK_CHECK_MOVED_BOARD)); +TEST_CASE("Test that king check detection is working correctly.", "[is_attacked]"){ + 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]"){ diff --git a/tests/valid_moves.h b/tests/valid_moves.h index 7f6acec..339d98b 100644 --- a/tests/valid_moves.h +++ b/tests/valid_moves.h @@ -874,7 +874,8 @@ const std::vector CASTLE_CHECK4_NOTATION = { // This is a check that moves are being marked as "check moves" poperly 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 ROOK_CHECK_BOARD = { 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 ROOK_CHECK_MOVED_BOARD = { const std::vector ROOK_CHECK_NOTATION = { "Rh2", "Rh1+" }; +const std::vector ROOK_CHECK_MOVED_MOVES = { + H3, H2, G1 +}; +const std::vector ROOK_CHECK_MOVED_CAPTURES = { + W_KING, NONE, NONE +}; const std::vector ROOK_CHECK_FLAGS = { 0, 1 };