Skip to content

Instantly share code, notes, and snippets.

@syuilo
Last active March 12, 2018 18:40
Show Gist options
  • Save syuilo/a1476653b66c7232dd01ce20b805be38 to your computer and use it in GitHub Desktop.
Save syuilo/a1476653b66c7232dd01ce20b805be38 to your computer and use it in GitHub Desktop.
/**
* αβ法での探索
*/
const dive = (o: Othello, pos: number, alpha = -Infinity, beta = Infinity, depth = 0): number => {
depth++;
const undo = o.put(o.turn, pos, true);
// 勝った
if (o.turn === null) {
const winner = o.winner;
// 勝ちは勝ちでも、より相手の石を少なくした方が美しい勝ちだと判定する
const score = botColor ? 10000 + (o.blackCount * 100) : 10000 + (o.whiteCount * 100);
// 巻き戻し
o.undo(undo);
return winner === botColor ? score : -score;
}
if (depth === maxDepth) {
let score = o.canPutSomewhere(botColor).length;
mathsStrongs.forEach((s, i) => {
s = s * 30;
const stone = o.board[i];
if (stone === botColor) {
score += s;
} else if (stone != null) {
score -= s;
}
});
// 巻き戻し
o.undo(undo);
return score;
} else {
const isNextBotTurn = o.turn === botColor;
const cans = o.canPutSomewhere(o.turn);
// 次のターンのプレイヤーにとって最も良い手を取得
for (const p of cans) {
const score = dive(o, p, alpha, beta, depth);
if (isNextBotTurn) {
alpha = Math.max(alpha, score);
if (alpha >= beta) break;
} else {
beta = Math.min(beta, score);
if (alpha >= beta) break;
}
}
// 巻き戻し
o.undo(undo);
return isNextBotTurn ? alpha : beta;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment