From 00458d7b95022e1daa604a59d236678ac33b3154 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Fri, 9 Dec 2022 00:08:46 -0800 Subject: added: castling moves rook. found workaround using make_move function. introduced castling related bug, unknown cause --- chess.c | 9 +++------ chesshelpers.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/chess.c b/chess.c index 1e2fe5f..0671430 100644 --- a/chess.c +++ b/chess.c @@ -378,20 +378,17 @@ vectorp_t get_king_moves(board_t board, vector_t position, int color) { //check that the squares are free vector_t s1 = init_vector(3, color == 0 ? 7 : 0); vector_t s2 = init_vector(2, color == 0 ? 7 : 0); - vector_t s3 = init_vector(1, color == 0 ? 7 : 0); - if(is_empty(board, s1) && is_empty(board, s2) && is_empty(board, s3)) { + if(is_empty(board, s1) && is_empty(board, s2)) { //check that the squares are not threatened board_t tempBoard1 = make_move(clone_board(board), position, s1); board_t tempBoard2 = make_move(clone_board(board), position, s2); - board_t tempBoard3 = make_move(clone_board(board), position, s3); - if(!is_in_check(tempBoard1, color) && !is_in_check(tempBoard2, color) && !is_in_check(tempBoard3, color)) { + if(!is_in_check(tempBoard1, color) && !is_in_check(tempBoard2, color)) { //add the move - moves[moveCount] = s3; + moves[moveCount] = s2; moveCount++; } free(tempBoard1.pieces); free(tempBoard2.pieces); - free(tempBoard3.pieces); } } } diff --git a/chesshelpers.c b/chesshelpers.c index d0464fb..2e046c9 100644 --- a/chesshelpers.c +++ b/chesshelpers.c @@ -23,6 +23,13 @@ board_t make_move(board_t board, vector_t origin, vector_t end) { if(piece == king) { board.whiteCanCastleQueenside = 0; board.whiteCanCastleKingside = 0; + //if the king moved farther than it normally could, move must have been castling. move the rook as well + if(origin.mx == 4 && end.mx == 6) { + board = make_move(board, init_vector(7, 7), init_vector(5, 7)); + } + else if(origin.mx == 4 && end.mx == 2) { + board = make_move(board, init_vector(0, 7), init_vector(3, 7)); + } } //white kingside rook moved else if(origin.mx == 7 && origin.my == 7) { @@ -36,6 +43,13 @@ board_t make_move(board_t board, vector_t origin, vector_t end) { else if(piece == king + 6) { board.blackCanCastleQueenside = 0; board.blackCanCastleKingside = 0; + //if the king moved farther than it normally could, move must have been castling. move the rook as well + if(origin.mx == 4 && end.mx == 6) { + board = make_move(board, init_vector(7, 0), init_vector(5, 0)); + } + else if(origin.mx == 4 && end.mx == 2) { + board = make_move(board, init_vector(0, 0), init_vector(3, 0)); + } } //black kingside rook moved else if(origin.mx == 7 && origin.my == 0) { -- cgit v1.3.1