aboutsummaryrefslogtreecommitdiff
path: root/include/negamax.c
diff options
context:
space:
mode:
Diffstat (limited to 'include/negamax.c')
-rw-r--r--include/negamax.c62
1 files changed, 2 insertions, 60 deletions
diff --git a/include/negamax.c b/include/negamax.c
index 061b629..cfe2643 100644
--- a/include/negamax.c
+++ b/include/negamax.c
@@ -8,74 +8,15 @@ const float infty = 3.0;
char negamax_ptn[9];
uint8_t negamax_search_depth = 3;
-static uint64_t *zobrist[15];
-
// ===================================================================
// Helpers
// ===================================================================
-static void
-zobrist_free(void);
-
-static int
-zobrist_init(void);
-
-static uint64_t
-zobrist_compute(void);
-
static float
negamax(const uint8_t cur_depth, float alpha, float beta,
const float colour);
// ===================================================================
-// Zobrist hashing
-// ===================================================================
-
-static uint64_t
-zobrist_compute(void) {
- uint64_t hash = 0;
- for (uint8_t l=0; l<board_size*board_size; l++) {
- colour_stack_t c = colours[l];
- const uint8_t count = COUNT_AT(l);
- enum STONE_VARIANT s = STONE_AT(l);
- for (uint8_t h=0; h<15; h++) {
- if (h<count) {
- hash ^= zobrist[h][l*(2*3+1)+(c&1)*3+s];
- c >>= 1;
- }
- }
- }
- return hash;
-}
-
-static int
-zobrist_init(void) {
- for (int k=0; k<15; k++) {
- if (zobrist[k] != NULL) return EXIT_FAILURE;
- }
-
- for (int j=0; j<15; j++) {
- zobrist[j] = malloc(sizeof(uint64_t)*board_size*board_size*(2*3+1));
- for (int k=0; k<board_size*board_size*(2*3+1); k++) {
- XORSHIFT64;
- zobrist[j][k] = RANDOM64;
- }
- }
-
- return EXIT_SUCCESS;
-}
-
-static void
-zobrist_free(void) {
- for (int k=0; k<15; k++) {
- if (zobrist[k] != NULL) {
- free(zobrist[k]);
- zobrist[k] = NULL;
- }
- }
-}
-
-// ===================================================================
// α-β negamax using the cnn1986 evaluation function and transposition
// tables using Zobrist hasing and a treap
// ===================================================================
@@ -119,7 +60,7 @@ negamax(const uint8_t cur_depth, float alpha, float beta,
tt_entry_t *entry = tt_seek(hash);
// CAUTION: >= breaks search stability
- if (entry != NULL && entry->depth == cur_depth) {
+ if (entry != NULL && entry->depth >= cur_depth) {
if (entry->flag == TT_EXACT) {
return entry->value;
} else if (entry->flag == TT_LOWERBOUND && entry->value > alpha) {
@@ -145,6 +86,7 @@ negamax(const uint8_t cur_depth, float alpha, float beta,
for (action_node_t *node=list->head; node!=NULL; node=node->next) {
action_take(node->action);
+
// Compute the value of the node
float node_value;
if (ply >= 2*board_size - 2 && (w = check_win()) < 0xFF) {