diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 09:11:23 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 14:20:34 +0300 |
commit | d6f49b139b42b9e6ff4fdaba3427972a4c07dd93 (patch) | |
tree | fc8205e8c740a2b3f16b64330bada53af837cfb8 | |
parent | d3c719b48c8fa73a5b281b54df265cfe597c5b86 (diff) | |
download | c-chess-d6f49b139b42b9e6ff4fdaba3427972a4c07dd93.tar.gz |
feat: dont allow castle under check
-rw-r--r-- | src/main.c | 43 |
1 files changed, 29 insertions, 14 deletions
@@ -158,6 +158,8 @@ Move input_move() { return move; }; +int list_moves_with_destination(Move* moves, int* board, int color, int destination); + int validate_move(Move move, int color, int* board) { // Null move means player is checkmated if (move.origin == -100 && move.destination == -100) return INFINITY; @@ -224,25 +226,29 @@ int validate_move(Move move, int color, int* board) { if ((piece & NO_COLOR) == KING) { if (piece % 2 == WHITE && move.origin == 4) { if (move.destination == 2) { + if (list_moves_with_destination(NULL, board, color ^ 1, move.origin)) return -5; for (int i = 1; i < 4; i++) { - if (board[i] != EMPTY) return -5; + if (board[i] != EMPTY || list_moves_with_destination(NULL, board, color ^ 1, i)) return -5; } return 0; } else if (move.destination == 6) { + if (list_moves_with_destination(NULL, board, color ^ 1, move.origin)) return -5; for (int i = 5; i < 7; i++) { - if (board[i] != EMPTY) return -5; + if (board[i] != EMPTY || list_moves_with_destination(NULL, board, color ^ 1, i)) return -5; } return 0; } } else if (piece % 2 == BLACK && move.origin == 116) { if (move.destination == 114) { + if (list_moves_with_destination(NULL, board, color ^ 1, move.origin)) return -5; for (int i = 114; i < 116; i++) { - if (board[i] != EMPTY) return -5; + if (board[i] != EMPTY || list_moves_with_destination(NULL, board, color ^ 1, i)) return -5; } return 0; } else if (move.destination == 118) { + if (list_moves_with_destination(NULL, board, color ^ 1, move.origin)) return -5; for (int i = 117; i < 119; i++) { - if (board[i] != EMPTY) return -5; + if (board[i] != EMPTY || list_moves_with_destination(NULL, board, color ^ 1, i)) return -5; } return 0; } @@ -407,7 +413,7 @@ int compute_material_advantage(int* board, int color) { return counter; } -int list_available_moves(Move* moves, int* board, int color) { +int list_moves_with_destination(Move* moves, int* board, int color, int destination) { int moves_count = 0; for (int rank = 7; rank >= 0; rank--) { @@ -415,15 +421,10 @@ int list_available_moves(Move* moves, int* board, int color) { int index = rank * 16 + file; int piece = board[index]; if (piece != EMPTY && (piece % 2) == color) { - for (int rank_destination = 7; rank_destination >= 0; rank_destination--) { - for (int file_destination = 0; file_destination < 8; file_destination++) { - int index_destination = rank_destination * 16 + file_destination; - Move move = { index, index_destination, 0 }; - if (validate_move(move, color, board) >= 0) { - if (moves) moves[moves_count] = move; - moves_count++; - } - } + Move move = { index, destination, 0 }; + if (validate_move(move, color, board) >= 0) { + if (moves) moves[moves_count] = move; + moves_count++; } } } @@ -432,6 +433,20 @@ int list_available_moves(Move* moves, int* board, int color) { return moves_count; } +int list_available_moves(Move* moves, int* board, int color) { + int moves_count = 0; + + for (int rank = 7; rank >= 0; rank--) { + for (int file = 0; file < 8; file++) { + int destination = rank * 16 + file; + Move* next_ptr = moves ? moves + moves_count : NULL; + moves_count += list_moves_with_destination(next_ptr, board, color, destination); + } + } + + return moves_count; +} + // Count doubled, blocked and isolated pawns int count_bad_pawns(int* board, int color) { int bad_pawns = 0; |