summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-08-18 09:58:52 +0300
committereug-vs <eugene@eug-vs.xyz>2022-08-18 10:08:21 +0300
commita389dd0562699e36c196d69c578a1f1c373a12ab (patch)
treeba4aaddf8b68d979bef8f49f3ccd0785ab9091bd
parentca2fc17834207b501b66d5c713889de029da27d6 (diff)
downloadc-chess-a389dd0562699e36c196d69c578a1f1c373a12ab.tar.gz
feat: use board coverage in metrics
-rw-r--r--src/config.h2
-rw-r--r--src/main.c31
-rw-r--r--src/pieces.h2
3 files changed, 31 insertions, 4 deletions
diff --git a/src/config.h b/src/config.h
index 5da6a21..1cadbc1 100644
--- a/src/config.h
+++ b/src/config.h
@@ -1,4 +1,4 @@
#define BOARD_SIZE 8
#define DEFAULT_FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
-#define MAX_DEPTH 5
+#define MAX_DEPTH 3
#define INFINITY 1000000
diff --git a/src/main.c b/src/main.c
index b18c7ea..edb553b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -261,6 +261,33 @@ int compute_material_advantage(int* board, int color) {
return counter;
}
+int compute_coverage(int* board, int color) {
+ int counter = 0;
+ 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) {
+ counter++;
+ }
+ }
+ }
+ }
+ }
+ }
+ return counter;
+}
+
+int compute_score(int* board) {
+ int coverage_score = compute_coverage(board, WHITE) - compute_coverage(board, BLACK);
+ return compute_material_advantage(board, WHITE) + coverage_score;
+}
+
// Alpha-beta pruning
// Value here is white material advantage over black
// Alpha is the best value for maximizer (white)
@@ -294,7 +321,7 @@ int find_best_move(int* board, int color, int depth, int alpha, int beta) {
int value = depth < MAX_DEPTH
? find_best_move(fake_board, 1 - color, depth + 1, alpha, beta)
- : compute_material_advantage(fake_board, 0); // Always for maximizer (white)
+ : compute_score(fake_board);
if (is_maximizer) {
if (value > best_value) {
@@ -341,7 +368,7 @@ int main() {
int color = 0;
while (1) {
- printf("Current board advantage is %i\n", compute_material_advantage(board, 0));
+ printf("Current score is %i\n", compute_score(board));
find_best_move(board, color, 0, -INFINITY, +INFINITY);
printf("Enter a move for %s:\n", COLORS[color]);
input_move(move);
diff --git a/src/pieces.h b/src/pieces.h
index a88626a..f975190 100644
--- a/src/pieces.h
+++ b/src/pieces.h
@@ -33,7 +33,7 @@ int knightMoves[] = { 33, 31, 18, 14, -33, -31, -18, -14, 0 };
int kingMoves[] = { 17, 16, 15, 1, -1, -15, -16, -17, 0 };
int pawnMoves[] = { 16, 0 };
-int blackPawnMoves[] = { 16, 0 };
+int blackPawnMoves[] = { -16, 0 };
int newPawnMoves[] = { 32, 16, 0 };
int newBlackPawnMoves[] = { -32, -16, 0 };