From da57a72a2e791e767e798516ea619b05561d18ad Mon Sep 17 00:00:00 2001 From: Tait Hoyem <44244401+TTWNO@users.noreply.github.com> Date: Sun, 28 Apr 2019 15:20:11 +0000 Subject: [PATCH] Try to track down a bug with checking kings. Add tests. --- src/chess.cpp | 22 ++++++++++++++++------ tests/main.cpp | 14 +++++++++++++- tests/test_functions.cpp | 7 +++++++ tests/test_functions.h | 1 + tests/valid_moves.h | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/chess.cpp b/src/chess.cpp index 552dd07..bec4398 100644 --- a/src/chess.cpp +++ b/src/chess.cpp @@ -49,6 +49,7 @@ int main(){ // Gets all moves for color who's turn it is. get_all_moves_for_pieces(my_pieces, &my_board, &all_moves, en_passant_square, castle_perms); + cout << "Castle perms: " << castle_perms << endl; print_board(my_board); // Gets a string from cin called input string input; @@ -99,20 +100,29 @@ int main(){ if (moving_piece == W_ROOK){ if (moving_from_pos == Position::A1 && (castle_perms & CastlePerms::WQS == 1)){ - castle_perms - CastlePerms::WQS; + castle_perms -= CastlePerms::WQS; } else if (moving_from_pos == Position::H1 && (castle_perms & CastlePerms::WKS == 1)){ - castle_perms - CastlePerms::WKS; + castle_perms -= CastlePerms::WKS; } } else if (moving_piece == B_ROOK){ if (moving_from_pos == Position::H8 && (castle_perms & CastlePerms::BKS == 1)){ - castle_perms - CastlePerms::BKS; + castle_perms -= CastlePerms::BKS; } else if (moving_from_pos == Position::A8 && (castle_perms & CastlePerms::BQS == 1)){ - castle_perms - CastlePerms::BQS; - } - + castle_perms -= CastlePerms::BQS; + } + } + // Removes castle perms after castling + if (is_white(moving_piece) && + get_castle_flag(move_to_exec)){ + castle_perms -= CastlePerms::WQS; + castle_perms -= CastlePerms::WKS; + } else if (is_black(moving_piece) && + get_castle_flag(move_to_exec)) { + castle_perms -= CastlePerms::BQS; + castle_perms -= CastlePerms::BKS; } // This will keep the en passant sqaure for one whole turn. if (reset_en_passant){ diff --git a/tests/main.cpp b/tests/main.cpp index 1a2660a..69d1e76 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -129,7 +129,7 @@ TEST_CASE("Tests is_king_checked works", "[is_checked]"){ CHECK(is_checked(BLACK_CHECK_POS2, BLACK_CHECK_BOARD2)); } -TEST_CASE("Test that moves that put 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]"){ CHECK(get_to_squares(get_all_moves(ROOK_CHECK_TEST_POS, ROOK_CHECK_TEST_BOARD)) == ROOK_CHECK_TEST_MOVES); CHECK(get_to_squares(get_all_moves(PAWN_CHECK_TEST_POS, PAWN_CHECK_TEST_BOARD)) == PAWN_CHECK_TEST_MOVES); auto pawn_diag_moves = get_all_moves(PAWN_DIAG_TEST1_POS, PAWN_DIAG_TEST1_BOARD); @@ -287,3 +287,15 @@ TEST_CASE("Tests for check on square of queenside capture", "[get_all_moves]"){ CHECK(get_notations(cannot_queenside3, CASTLE_CHECK3_BOARD) == CASTLE_CHECK3_NOTATION); 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 for add_checked_flags is working correctly.", "[get_all_moves][add_checked_flags]"){ + auto rook_checked_flags = get_all_moves(ROOK_CHECK_POS, ROOK_CHECK_BOARD); + + CHECK(get_notations(rook_checked_flags, ROOK_CHECK_BOARD) == ROOK_CHECK_NOTATION); + CHECK(get_checked_flags(rook_checked_flags) == ROOK_CHECK_FLAGS); +} + diff --git a/tests/test_functions.cpp b/tests/test_functions.cpp index e36f434..125882a 100644 --- a/tests/test_functions.cpp +++ b/tests/test_functions.cpp @@ -55,6 +55,13 @@ std::vector get_castle_flags(std::vector moves){ } return transformed; } +std::vector get_checked_flags(std::vector moves){ + std::vector transformed; + for (int mv : moves){ + transformed.push_back(get_check_flag(mv)); + } + return transformed; +} std::vector get_notations(std::vector moves, std::array board){ std::vector notations; for (int move : moves){ diff --git a/tests/test_functions.h b/tests/test_functions.h index 50311a9..0c217c0 100644 --- a/tests/test_functions.h +++ b/tests/test_functions.h @@ -12,5 +12,6 @@ std::vector get_promoted_pieces(std::vector moves); std::vector get_en_passant_flags(std::vector moves); std::vector get_pawn_start_flags(std::vector moves); std::vector get_castle_flags(std::vector moves); +std::vector get_checked_flags(std::vector moves); std::vector get_notations(std::vector moves, std::array board); #endif diff --git a/tests/valid_moves.h b/tests/valid_moves.h index 7180729..7f6acec 100644 --- a/tests/valid_moves.h +++ b/tests/valid_moves.h @@ -871,3 +871,42 @@ const std::array CASTLE_CHECK4_BOARD = { const std::vector CASTLE_CHECK4_NOTATION = { "Kf8" }; + +// 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 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, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, B_PAWN, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, B_PAWN, B_ROOK, INV, + INV, NONE, NONE, NONE, NONE, NONE, W_PAWN, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, W_ROOK, W_KING, NONE, INV, + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV +}; +const std::array ROOK_CHECK_MOVED_BOARD = { + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, B_PAWN, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, B_PAWN, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, W_PAWN, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, W_ROOK, W_KING, B_ROOK, INV, + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV +}; +const std::vector ROOK_CHECK_NOTATION = { + "Rh2", "Rh1+" +}; +const std::vector ROOK_CHECK_FLAGS = { + 0, 1 +}; +