From b3636d404715899724753ce727e9d6df87da7a17 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 18 Aug 2022 04:53:47 +0300 Subject: feat: count up to 3 moves ahead --- src/main.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 338952f..644e869 100644 --- a/src/main.c +++ b/src/main.c @@ -187,7 +187,7 @@ int apply_move(int move[2], int* board) { return board[destination]; } -int estimate_board(int* board, int color) { +int compute_advantage(int* board, int color) { int counter = 0; for (int rank = 7; rank >= 0; rank--) { for (int file = 0; file < 8; file++) { @@ -217,10 +217,9 @@ int estimate_board(int* board, int color) { return counter; } -void find_best_move(int* board, int color) { +void find_best_move(int best_move[2], int* board, int color, int depth) { int fake_board[128]; - int best_estimate = -10000; - int best_move[2]; + int best_advantage = -10000; for (int rank = 7; rank >= 0; rank--) { @@ -241,9 +240,17 @@ void find_best_move(int* board, int color) { } apply_move(move, fake_board); - int estimate = estimate_board(fake_board, color); - if (estimate > best_estimate) { - best_estimate = estimate; + + // Try finding a response + if (depth) { + int response_move[2]; + find_best_move(response_move, fake_board, color ^ 1, depth - 1); + apply_move(response_move, fake_board); + } + + int advantage = compute_advantage(fake_board, color); + if (advantage > best_advantage) { + best_advantage = advantage; best_move[0] = move[0]; best_move[1] = move[1]; } @@ -257,7 +264,7 @@ void find_best_move(int* board, int color) { 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); + if (depth == 3) printf("Best move for %i is %s with score %i\n", color, move_in_notation, best_advantage); } int main() { @@ -270,9 +277,9 @@ int main() { int color = 0; while (1) { - printf("Current board estimate is %i\n", estimate_board(board, color)); + printf("Current board advantage is %i\n", compute_advantage(board, color)); + find_best_move(move, board, color, 3); 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