summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-08-18 04:53:47 +0300
committereug-vs <eugene@eug-vs.xyz>2022-08-18 04:53:47 +0300
commitb3636d404715899724753ce727e9d6df87da7a17 (patch)
tree079ce333787097ef1f4e7a3b253441b50d1aa8f3
parentf8afe2fed4cff27eaacbe4bc11393c0237bec11b (diff)
downloadc-chess-b3636d404715899724753ce727e9d6df87da7a17.tar.gz
feat: count up to 3 moves ahead
-rw-r--r--src/main.c27
1 files changed, 17 insertions, 10 deletions
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);