From f8afe2fed4cff27eaacbe4bc11393c0237bec11b Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 18 Aug 2022 04:40:05 +0300 Subject: feat: add board estimates and best moves --- src/main.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index eaded89..338952f 100644 --- a/src/main.c +++ b/src/main.c @@ -155,7 +155,7 @@ int validate_move(int move[2], int color, int* board) { } if (target_piece != EMPTY) break; - if ((piece & NO_COLOR) == PAWN || piece == KNIGHT || piece == KING) break; + if ((piece & NO_COLOR) == PAWN || (piece & NO_COLOR) == KNIGHT || piece == KING) break; } legal_move++; } @@ -181,12 +181,85 @@ int apply_move(int move[2], int* board) { board[origin] = EMPTY; if (target_piece != EMPTY) { - printf("Captured %s\n", pieces[target_piece]); + // printf("Captured %s\n", pieces[target_piece]); } return board[destination]; } +int estimate_board(int* board, int color) { + int counter = 0; + for (int rank = 7; rank >= 0; rank--) { + for (int file = 0; file < 8; file++) { + int piece = board[rank * 16 + file]; + if (piece != EMPTY) { + int sign = (piece % 2 == color) ? 1 : -1; + switch (piece & NO_COLOR) { + case KNIGHT: + counter += 3 * sign; + break; + case BISHOP: + counter += 3 * sign; + break; + case ROOK: + counter += 4 * sign; + break; + case QUEEN: + counter += 9 * sign; + break; + default: + counter += 1 * sign; + break; + } + } + } + } + return counter; +} + +void find_best_move(int* board, int color) { + int fake_board[128]; + int best_estimate = -10000; + int best_move[2]; + + + for (int rank = 7; rank >= 0; rank--) { + for (int file = 0; file < 8; file++) { + 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; + int move[2] = { index, index_destination }; + if (validate_move(move, color, board) >= 0) { + // Generate fake board + for (int r = 7; r >= 0; r--) { + for (int f = 0; f < 8; f++) { + fake_board[r * 16 + f] = board[r * 16 + f]; + } + } + + apply_move(move, fake_board); + int estimate = estimate_board(fake_board, color); + if (estimate > best_estimate) { + best_estimate = estimate; + best_move[0] = move[0]; + best_move[1] = move[1]; + } + } + } + } + } + } + } + + char move_in_notation[] = "xy XY"; + index_to_notation(best_move[0], move_in_notation); + index_to_notation(best_move[1], move_in_notation + 3); + printf("Best move for %i is %s with score %i\n", color, move_in_notation, best_estimate); +} + int main() { int board[128]; parse_FEN(DEFAULT_FEN, board); @@ -197,7 +270,9 @@ int main() { int color = 0; while (1) { + printf("Current board estimate is %i\n", estimate_board(board, color)); printf("Enter a move for %s:\n", COLORS[color]); + find_best_move(board, color); input_move(move); int error = validate_move(move, color, board); -- cgit v1.2.3