summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-08-18 10:27:04 +0300
committereug-vs <eugene@eug-vs.xyz>2022-08-18 10:27:04 +0300
commit5cf1a520f190657cb0d5bb41c4dcc741795536ae (patch)
treeec2dca5a9be3f902460b0fba2e0b5803e1c22c0b /src/main.c
parent3a9cdaa176370edd81653d62812c836f9bb00219 (diff)
downloadc-chess-5cf1a520f190657cb0d5bb41c4dcc741795536ae.tar.gz
feat: add metrics
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index c66ab37..5db2141 100644
--- a/src/main.c
+++ b/src/main.c
@@ -292,7 +292,7 @@ int compute_score(int* board) {
// Value here is white material advantage over black
// Alpha is the best value for maximizer (white)
// Beta is the best value for minimizer (black)
-int find_best_move(int best_move[2], int* board, int color, int depth, int alpha, int beta) {
+int find_best_move(int best_move[2], int* board, int color, int depth, int alpha, int beta, int* metrics) {
int fake_board[128];
int is_maximizer = (color == 0);
int best_value = is_maximizer ? -INFINITY : INFINITY;
@@ -308,6 +308,8 @@ int find_best_move(int best_move[2], int* board, int color, int depth, int alpha
int move[2] = { index, index_destination };
if (validate_move(move, color, board) >= 0) {
+ *metrics += 1;
+
// Generate fake board
for (int r = 7; r >= 0; r--) {
for (int f = 0; f < 8; f++) {
@@ -320,7 +322,7 @@ int find_best_move(int best_move[2], int* board, int color, int depth, int alpha
int dummy[2];
int value = depth < MAX_DEPTH
- ? find_best_move(dummy, fake_board, 1 - color, depth + 1, alpha, beta)
+ ? find_best_move(dummy, fake_board, 1 - color, depth + 1, alpha, beta, metrics)
: compute_score(fake_board);
if (is_maximizer) {
@@ -349,12 +351,6 @@ int find_best_move(int best_move[2], int* board, int color, int depth, int alpha
}
}
- if (depth == 0) {
- 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 (validation: %i) for %s is %s with score %i in #%i moves\n", validate_move(best_move, color, board), COLORS[color], move_in_notation, best_value, MAX_DEPTH);
- }
return best_value;
}
@@ -372,7 +368,16 @@ int main() {
printf("Current score is %i\n", compute_score(board));
printf("Enter a move for %s:\n", COLORS[color]);
input_move(move);
- } else find_best_move(move, board, color, 0, -INFINITY, +INFINITY);
+ } else {
+ printf("Evaluating move for black...\n");
+ int metrics = 0;
+ int value = find_best_move(move, board, color, 0, -INFINITY, +INFINITY, &metrics);
+ char move_in_notation[] = "xy XY";
+ index_to_notation(move[0], move_in_notation);
+ index_to_notation(move[1], move_in_notation + 3);
+ printf("%i positions analyzed\n", metrics);
+ printf("Best move for black is %s with score %i in #%i moves\n", move_in_notation, value, MAX_DEPTH);
+ }
int error = validate_move(move, color, board);
if (error < 0) {