From 8653b86cae4c9879a9ba8fedc43c141667c93a2d Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 7 Sep 2022 23:09:56 +0300 Subject: fix: only allow castling with rooks --- src/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index c855a6e..3df8e2b 100644 --- a/src/main.c +++ b/src/main.c @@ -255,13 +255,13 @@ int validate_move(Move move, int color, int* board) { // Handle castling if ((piece & NO_COLOR) == KING) { if (piece % 2 == WHITE && move.origin == 4) { - if (move.destination == 2) { + if (move.destination == 2 && board[0] == (ROOK | WHITE)) { if (list_moves_with_destination(NULL, board, color ^ 1, move.origin)) return -5; for (int i = 1; i < 4; i++) { if (board[i] != EMPTY || list_moves_with_destination(NULL, board, color ^ 1, i)) return -5; } return 0; - } else if (move.destination == 6) { + } else if (move.destination == 6 && board[7] == (ROOK | WHITE)) { if (list_moves_with_destination(NULL, board, color ^ 1, move.origin)) return -5; for (int i = 5; i < 7; i++) { if (board[i] != EMPTY || list_moves_with_destination(NULL, board, color ^ 1, i)) return -5; @@ -269,13 +269,13 @@ int validate_move(Move move, int color, int* board) { return 0; } } else if (piece % 2 == BLACK && move.origin == 116) { - if (move.destination == 114) { + if (move.destination == 114 && board[112] == (ROOK | BLACK)) { if (list_moves_with_destination(NULL, board, color ^ 1, move.origin)) return -5; - for (int i = 114; i < 116; i++) { + for (int i = 113; i < 116; i++) { if (board[i] != EMPTY || list_moves_with_destination(NULL, board, color ^ 1, i)) return -5; } return 0; - } else if (move.destination == 118) { + } else if (move.destination == 118 && board[119] == (ROOK | BLACK)) { if (list_moves_with_destination(NULL, board, color ^ 1, move.origin)) return -5; for (int i = 117; i < 119; i++) { if (board[i] != EMPTY || list_moves_with_destination(NULL, board, color ^ 1, i)) return -5; -- cgit v1.2.3