#include #include "chess.h" // --------------------------------------------- // Checkmates // --------------------------------------------- void test_checkmate_black_queen_rook() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 'k' }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 'r', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == true); } void test_checkmate_black_queen_bishop() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 'k' }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 'b', 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == true); } void test_checkmate_black_queen_pawn() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 'k' }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 'p', 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == true); } void test_checkmate_black_queen_king() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 'k', 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == true); } void test_checkmate_black_queen_knight() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 'n', 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == true); } void test_checkmate_black_queen_rooks() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'r', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 'n', 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'r', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == true); } void test_checkmate_black_pawns() { Game* game = new Game(); const char board[8][8] = { { 0x20, 'r', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 'r', 0x20, 0x20, 0x20, 0x20 }, { 'K', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 'p', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 'p', 'n', 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == true); } void test_checkmate_black_kings_protects() { Game* game = new Game(); // This tackles the issue on: https://github.com/jeromevonk/chess_console/issues/2 // The king helps in the checkmate const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'k', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 'r' }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == true); } // --------------------------------------------- // Not checkmate // --------------------------------------------- void test_no_checkmate_king_can_take() { Game* game = new Game(); // This tackles the issue on: https://github.com/jeromevonk/chess_console/issues/4 // The king is able to take a queen that is not protected const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 'r', 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == false); } void test_no_checkmate_rook_can_take() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 0x20, 'r', 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 'R', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == false); } void test_no_checkmate_pawn_can_take() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'P', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 0x20, 'r', 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == false); } void test_no_checkmate_knight_can_take() { Game* game = new Game(); const char board[8][8] = { { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 'K', 'q', 0x20, 'r', 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 'N', 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, }; // Set board game->setBoard(board); // Test assert(game->isCheckMate() == false); } int main() { // ------------------------------------------------------------- // Checkmates! // Test moves made with black pieces // Because by default the game is assumes it is white's turn // ------------------------------------------------------------- test_checkmate_black_queen_rook(); test_checkmate_black_queen_bishop(); test_checkmate_black_queen_pawn(); test_checkmate_black_queen_king(); test_checkmate_black_queen_knight(); test_checkmate_black_queen_rooks(); test_checkmate_black_pawns(); test_checkmate_black_kings_protects(); // ------------------------------------------------------------- // NOT checkmates! // Test moves made with black pieces // Because by default the game is assumes it is white's turn // ------------------------------------------------------------- test_no_checkmate_king_can_take(); test_no_checkmate_rook_can_take(); test_no_checkmate_pawn_can_take(); test_no_checkmate_knight_can_take(); // If the program reaches this point, all tests have passed return 0; }