summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ai_mm.cpp37
1 files changed, 12 insertions, 25 deletions
diff --git a/ai_mm.cpp b/ai_mm.cpp
index 8d61e20..4400a84 100644
--- a/ai_mm.cpp
+++ b/ai_mm.cpp
@@ -27,10 +27,9 @@ static int evaluate(const Board &bd, int win) {
return score;
}
-static int minimaxBlack(const Board &bd, int depth, int alpha, int beta);
+static int minimaxBlack(const Board &bd, int depth, int alpha, int beta, int win);
-static int minimaxWhite(const Board &bd, int depth, int alpha, int beta) {
- int win = bd.checkWin();
+static int minimaxWhite(const Board &bd, int depth, int alpha, int beta, int win) {
if (depth == 0 || win != 0) {
return evaluate(bd, win);
}
@@ -39,8 +38,8 @@ static int minimaxWhite(const Board &bd, int depth, int alpha, int beta) {
bd.forEachMove(1, [&bd, depth, &alpha, &beta, &bestValue](Move mv) {
Board bd2 = bd;
- bd2.apply(mv);
- int value = minimaxBlack(bd2, depth - 1, alpha, beta);
+ int win = bd2.applyCW(mv);
+ int value = minimaxBlack(bd2, depth - 1, alpha, beta, win);
// cerr << string(6 - 2 * depth, ' ') << "mmW: " << mv << ": " << value << endl;
bestValue = max(bestValue, value);
alpha = max(alpha, bestValue);
@@ -51,8 +50,7 @@ static int minimaxWhite(const Board &bd, int depth, int alpha, int beta) {
return bestValue;
}
-static int minimaxBlack(const Board &bd, int depth, int alpha, int beta) {
- int win = bd.checkWin();
+static int minimaxBlack(const Board &bd, int depth, int alpha, int beta, int win) {
if (depth == 0 || win != 0) {
return evaluate(bd, win);
}
@@ -61,8 +59,8 @@ static int minimaxBlack(const Board &bd, int depth, int alpha, int beta) {
bd.forEachMove(-1, [&bd, depth, &alpha, &beta, &bestValue](Move mv) {
Board bd2 = bd;
- bd2.apply(mv);
- int value = minimaxWhite(bd2, depth - 1, alpha, beta);
+ int win = bd2.applyCW(mv);
+ int value = minimaxWhite(bd2, depth - 1, alpha, beta, win);
// cerr << string(6 - 2 * depth, ' ') << "mmB: " << mv << ": " << value << endl;
bestValue = min(bestValue, value);
beta = min(beta, bestValue);
@@ -73,23 +71,12 @@ static int minimaxBlack(const Board &bd, int depth, int alpha, int beta) {
return bestValue;
}
-static int minimaxGeneric(const Board &bd, int depth, int alpha, int beta, int player) {
- if (player == 1) return minimaxWhite(bd, depth, alpha, beta);
- else return minimaxBlack(bd, depth, alpha, beta);
+static int minimaxGeneric(const Board &bd, int depth, int alpha, int beta, int win, int player) {
+ if (player == 1) return minimaxWhite(bd, depth, alpha, beta, win);
+ else return minimaxBlack(bd, depth, alpha, beta, win);
}
Move AI::MM::findMove(const Board &bd, int player) {
- /* {
- int pl = player;
- Board bd2 = bd;
- Move mv;
- mv = Move(3, 30); assert(bd2.isValid(mv, pl)); bd2.apply(mv); pl = -pl;
- mv = Move(22, 21); assert(bd2.isValid(mv, pl)); bd2.apply(mv); pl = -pl;
- cerr << bd2 << endl;
- // mv = Move(77, 80); assert(bd2.isValid(mv, pl)); bd2.apply(mv); pl = -pl;
- cerr << "## " << evaluate(bd2, 0) << endl;
- } */
-
Move bestMove(-1, -1);
int bestScore = INT_MIN;
// 'bestScore', and 'score' below, have positive values good for 'player'.
@@ -99,8 +86,8 @@ Move AI::MM::findMove(const Board &bd, int player) {
bd.forEachMove(player, [&bd, player, &bestScore, &bestMove](Move mv) {
Board bd2 = bd;
- bd2.apply(mv);
- int score = player * minimaxGeneric(bd2, mm_depth, INT_MIN, INT_MAX, -player);
+ int win = bd2.applyCW(mv);
+ int score = player * minimaxGeneric(bd2, mm_depth, INT_MIN, INT_MAX, win, -player);
// cerr << "fM: " << mv << ": " << score << endl;
cerr << score << ',';
if (score > bestScore) {