This commit is contained in:
syuilo 2018-03-15 14:09:38 +09:00
parent 1a8d6e0a25
commit c662726259
2 changed files with 16 additions and 19 deletions

View file

@ -135,7 +135,7 @@ function onGameStarted(g) {
} }
function onSet(x) { function onSet(x) {
o.put(x.color, x.pos, true); o.put(x.color, x.pos);
if (x.next === botColor) { if (x.next === botColor) {
think(); think();
@ -157,17 +157,17 @@ function think() {
*/ */
const dive = (o: Othello, pos: number, alpha = -Infinity, beta = Infinity, depth = 0): number => { const dive = (o: Othello, pos: number, alpha = -Infinity, beta = Infinity, depth = 0): number => {
// 試し打ち // 試し打ち
const undo = o.put(o.turn, pos, true); o.put(o.turn, pos);
const key = o.board.toString(); const key = o.board.toString();
let cache = db[key]; let cache = db[key];
if (cache) { if (cache) {
if (alpha >= cache.upper) { if (alpha >= cache.upper) {
o.undo(undo); o.undo();
return cache.upper; return cache.upper;
} }
if (beta <= cache.lower) { if (beta <= cache.lower) {
o.undo(undo); o.undo();
return cache.lower; return cache.lower;
} }
alpha = Math.max(alpha, cache.lower); alpha = Math.max(alpha, cache.lower);
@ -199,7 +199,7 @@ function think() {
} }
// 巻き戻し // 巻き戻し
o.undo(undo); o.undo();
// 接待なら自分が負けた方が高スコア // 接待なら自分が負けた方が高スコア
return isSettai return isSettai
@ -225,7 +225,7 @@ function think() {
}); });
// 巻き戻し // 巻き戻し
o.undo(undo); o.undo();
// ロセオならスコアを反転 // ロセオならスコアを反転
if (game.settings.is_llotheo) score = -score; if (game.settings.is_llotheo) score = -score;
@ -257,7 +257,7 @@ function think() {
} }
// 巻き戻し // 巻き戻し
o.undo(undo); o.undo();
if (value <= alpha) { if (value <= alpha) {
cache.upper = value; cache.upper = value;

View file

@ -50,6 +50,8 @@ export default class Othello {
public prevPos = -1; public prevPos = -1;
public prevColor: Color = null; public prevColor: Color = null;
private logs: Undo[] = [];
/** /**
* *
*/ */
@ -138,13 +140,7 @@ export default class Othello {
* @param color * @param color
* @param pos * @param pos
*/ */
public put(color: Color, pos: number, fast = false): Undo { public put(color: Color, pos: number) {
if (!fast && !this.canPut(color, pos)) {
console.warn('can not put this position:', pos, color);
console.warn(this.board);
return null;
}
this.prevPos = pos; this.prevPos = pos;
this.prevColor = color; this.prevColor = color;
@ -160,14 +156,14 @@ export default class Othello {
const turn = this.turn; const turn = this.turn;
this.calcTurn(); this.logs.push({
return {
color, color,
pos, pos,
effects, effects,
turn turn
}; });
this.calcTurn();
} }
private calcTurn() { private calcTurn() {
@ -181,7 +177,8 @@ export default class Othello {
} }
} }
public undo(undo: Undo) { public undo() {
const undo = this.logs.pop();
this.prevColor = undo.color; this.prevColor = undo.color;
this.prevPos = undo.pos; this.prevPos = undo.pos;
this.board[undo.pos] = null; this.board[undo.pos] = null;