c# - Negamax for simple addition game -
i'm trying implement negamax simple game players alternate adding 1 or 2 running sum. player increases total 21 wins.
i'm using pseudocode here: https://en.wikipedia.org/wiki/negamax#negamax_base_algorithm
the human player moves first computer should win adding number makes total congruent 0 mod 3.
i'm not doing dynamic move generation. comparing negamax score adding 1 running sum negamax score adding 2 running sum.
int total = 0; console.writeline("the current total " + total); while (total < 21) { console.writeline("add 1 or 2?"); total += convert.toint32(console.readline()); console.writeline("you increased total " + total); if (total == 21) { console.writeline("you win"); break; } if (negamax(total + 1, 1) > negamax(total + 2, 1)) total++; else total += 2; console.writeline("computer increased total " + total); if (total == 21) { console.writeline("computer wins"); break; } }
the negamax function:
static int negamax(int total, int color) { if (total == 21) { return color * 100; } int bestvalue = -100; (int = 1; <= 2; i++) { if (total + <= 21) { int v = -1 * negamax(total + i, -1 * color); bestvalue = max(bestvalue, v); } } return bestvalue; }
max method:
static int max(int a, int b) { if (a > b) return a; return b; }
not sure why ai adding 2 every time.
the static evaluation function incorrect.
https://en.wikipedia.org/wiki/negamax#negamax_base_algorithm negamax node's return value heuristic score from point of view of node's current player.
if (total == 21), it's loss node's current player. negamax return must -100. there other code errors too, such when total 22.
Comments
Post a Comment