aboutsummaryrefslogtreecommitdiff
path: root/include/negamax.c
diff options
context:
space:
mode:
authortslil clingman <tslil@posteo.de>2021-01-25 22:23:38 -0500
committertslil <tslil@posteo.de>2026-08-28 19:37:41 +0100
commitc69c764a2202d745a3672a92bcb40ea180bd9740 (patch)
treeab159882d749d352af9c3f3b64513aad8a0e6a37 /include/negamax.c
parent8418ddf3187ec0f526ef5d8dbb0689fc786fac92 (diff)
Still bugs...
Diffstat (limited to 'include/negamax.c')
-rw-r--r--include/negamax.c56
1 files changed, 41 insertions, 15 deletions
diff --git a/include/negamax.c b/include/negamax.c
index 43fa5f6..abb4e27 100644
--- a/include/negamax.c
+++ b/include/negamax.c
@@ -113,35 +113,61 @@ static enum WIN_TYPE w;
static float
negamax(const uint8_t cur_depth, float alpha, float beta,
const float colour) {
+ float value = -infty;
- if (cur_depth == 0) return colour*cnn1986_evaluate_black_win();
- float value = -infty;
+ data_t bd[5*5];
+ colour_stack_t bc[25];
action_list_t *list = action_list_generate();
if (list == NULL) return value; // ???
for (action_node_t *node=list->head; node!=NULL; node=node->next) {
+
+ for (int k=0; k<25; k++) { bd[k]=celldat[k]; bc[k]=colours[k]; }
action_take(node);
- // Somebody won?
+ // Compute the value of the node
if (ply >= 2*board_size - 2 && (w = check_win()) < 0xFF) {
- if (w == WIN_ROAD_BLACK || w == WIN_FLAT_BLACK) value = colour*infty;
- else if (w == WIN_DRAW) value = 0; // Draw is fixed at neutral
- else value = -colour*infty;
- if (value > 0) {
- // Always take the win for ourselves
- action_undo(node);
- if (cur_depth == negamax_search_depth) {
- action_to_ptn(node, negamax_ptn);
- }
- goto prune;
- }
- } else {
+ float winnings = -colour*infty;
+ // Check win if far enough into the game
+ if (w == WIN_ROAD_BLACK || w == WIN_FLAT_BLACK) winnings = colour*infty;
+ else if (w == WIN_DRAW) winnings = 0; // Draw is fixed at neutral
+ value = fmax(value, winnings);
+ } else if (cur_depth > 1) {
+ // If nobody won, or too early, recurse if not a leaf
value = fmax(value, -negamax(cur_depth - 1, -beta, -alpha, -colour));
+ } else {
+ // Recursion would take us to a leaf, evaluate
+ value = fmax(value, colour*cnn1986_evaluate_black_win());
}
action_undo(node);
+ for (int k=0; k<25; k++) {
+ if (bd[k]!=celldat[k]) {
+ printf("Depth %d %.3f %.3f %d: { .type = %s, .loc = 0x%02X, .data0 = 0x%02X, .data1 = 0x%02X} DIFF 0x%02X \n",
+ cur_depth,
+ alpha,
+ beta,
+ ply,
+ (node->type == A_PLACE) ? "A_PLACE" : "A_MOVE",
+ node->loc,
+ node->data0,
+ node->data1,
+ k);
+ puts("Before:");
+ for (int j=0; j<25;j++) printf("0x%02X ",bd[j]);
+ putchar('\n');
+ for (int j=0; j<25;j++) printf("0x%02X ",bc[j]);
+ putchar('\n');
+ puts("After:");
+ for (int j=0; j<25;j++) printf("0x%02X ",celldat[j]);
+ putchar('\n');
+ for (int j=0; j<25;j++) printf("0x%02X ",colours[j]);
+ putchar('\n');
+ exit(1);
+ }
+ }
negamax_display_progress(cur_depth, list->length);