summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-08-31 04:55:21 +0300
committereug-vs <eugene@eug-vs.xyz>2022-08-31 04:55:21 +0300
commit4a99286f1bb9213467cf12f787d993dde2727764 (patch)
treed335959c24ac08fbafee4036f94420aea1e9b7a9
parente90c9419df5ac9bdd831551713c1fee9f92a750f (diff)
downloadc-chess-4a99286f1bb9213467cf12f787d993dde2727764.tar.gz
feat: use long for zobrist seeds
-rw-r--r--src/main.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/main.c b/src/main.c
index deb7d96..81b751f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -12,14 +12,14 @@
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
-void generate_zobrist_seed(int* seed) {
+void generate_zobrist_seed(long* seed) {
for (int i = 0; i < MAX_ZOBRIST_SEEDS; i++) {
seed[i] = rand();
}
}
-int zobrist_hash(int* seed, int* board, int color_to_move) {
- int hash = 1;
+int zobrist_hash(long* seed, int* board, int color_to_move) {
+ long hash = 1;
// Last feature means white to move
if (color_to_move == WHITE) hash ^= seed[MAX_ZOBRIST_SEEDS - 1];
@@ -37,7 +37,7 @@ int zobrist_hash(int* seed, int* board, int color_to_move) {
return hash;
}
-int apply_move_zobrist(Move move, int* board, int* zobrist_seed, int hash) {
+long apply_move_zobrist(Move move, int* board, long* zobrist_seed, long hash) {
int piece = board[move.origin];
int target_piece = board[move.destination];
@@ -474,12 +474,12 @@ int compare_moves(const void* a, const void* b) {
return ((Move*)b)->value - ((Move*)a)->value;
}
-int sort_moves(Move* moves, int moves_count, int* board, int hash, int* transposition_table, int* zobrist_seed) {
+int sort_moves(Move* moves, int moves_count, int* board, long hash, int* transposition_table, long* zobrist_seed) {
int cache_hits = 0;
for (int i = 0; i < moves_count; i++) {
int color = board[moves[i].origin] % 2;
// Lookup move in transpoition table
- int move_hash = apply_move_zobrist(moves[i], board, zobrist_seed, hash);
+ long move_hash = apply_move_zobrist(moves[i], board, zobrist_seed, hash);
if (transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE]) {
moves[i].value = transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE] * (color == WHITE ? 1 : -1);
cache_hits++;
@@ -501,7 +501,7 @@ int sort_moves(Move* moves, int moves_count, int* board, int hash, int* transpos
* Alpha is the best value for maximizer (white)
* Beta is the best value for minimizer (black)
*/
-Move find_best_move(int* board, int color, int depth, int alpha, int beta, int* metrics, int hash, int* transposition_table, int* zobrist_seed) {
+Move find_best_move(int* board, int color, int depth, int alpha, int beta, int* metrics, long hash, int* transposition_table, long* zobrist_seed) {
int is_maximizer = (color == 0);
Move best_move = { -100, -100, 0 };
@@ -512,13 +512,12 @@ Move find_best_move(int* board, int color, int depth, int alpha, int beta, int*
Move available_moves[MAX_AVAILABLE_MOVES];
int available_moves_count = list_available_moves(available_moves, board, color);
int cache_hits = sort_moves(available_moves, available_moves_count, board, hash, transposition_table, zobrist_seed);
- if (depth == 0 && cache_hits && (double)cache_hits / (double)available_moves_count > 0.50) printf("More than 50 percent cache hits (%i)\n", cache_hits);
for (int i = 0; i < available_moves_count; i++) {
*metrics += 1;
Move move = available_moves[i];
- int move_hash = apply_move_zobrist(move, board, zobrist_seed, hash);
+ long move_hash = apply_move_zobrist(move, board, zobrist_seed, hash);
int captured_piece = apply_move(move, board);
move.value = depth < MAX_DEPTH
@@ -549,9 +548,9 @@ int main() {
int board[128];
parse_FEN(DEFAULT_FEN, board);
- int zobrist_seed[MAX_ZOBRIST_SEEDS];
+ long zobrist_seed[MAX_ZOBRIST_SEEDS];
generate_zobrist_seed(zobrist_seed);
- int hash = zobrist_hash(zobrist_seed, board, color);
+ long hash = zobrist_hash(zobrist_seed, board, color);
int transposition_table[TRANSPOSITION_TABLE_SIZE];
@@ -560,7 +559,7 @@ int main() {
print_board(board);
while (1) {
- printf("Zobrist hash: %i\n", hash);
+ printf("Zobrist hash: %lo\n", hash);
if (color == PLAYER) {
printf("Enter a move for %s:\n", COLORS[color]);