From c6627262591e298615557dafd8aae37c21822ff7 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 15 Mar 2018 14:09:38 +0900 Subject: [PATCH] Refactor --- src/common/othello/ai/back.ts | 14 +++++++------- src/common/othello/core.ts | 21 +++++++++------------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/common/othello/ai/back.ts b/src/common/othello/ai/back.ts index 9765b7d4e..38a564f55 100644 --- a/src/common/othello/ai/back.ts +++ b/src/common/othello/ai/back.ts @@ -135,7 +135,7 @@ function onGameStarted(g) { } function onSet(x) { - o.put(x.color, x.pos, true); + o.put(x.color, x.pos); if (x.next === botColor) { think(); @@ -157,17 +157,17 @@ function think() { */ 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(); let cache = db[key]; if (cache) { if (alpha >= cache.upper) { - o.undo(undo); + o.undo(); return cache.upper; } if (beta <= cache.lower) { - o.undo(undo); + o.undo(); return cache.lower; } alpha = Math.max(alpha, cache.lower); @@ -199,7 +199,7 @@ function think() { } // 巻き戻し - o.undo(undo); + o.undo(); // 接待なら自分が負けた方が高スコア return isSettai @@ -225,7 +225,7 @@ function think() { }); // 巻き戻し - o.undo(undo); + o.undo(); // ロセオならスコアを反転 if (game.settings.is_llotheo) score = -score; @@ -257,7 +257,7 @@ function think() { } // 巻き戻し - o.undo(undo); + o.undo(); if (value <= alpha) { cache.upper = value; diff --git a/src/common/othello/core.ts b/src/common/othello/core.ts index 5e25578ca..217066d37 100644 --- a/src/common/othello/core.ts +++ b/src/common/othello/core.ts @@ -50,6 +50,8 @@ export default class Othello { public prevPos = -1; public prevColor: Color = null; + private logs: Undo[] = []; + /** * ゲームを初期化します */ @@ -138,13 +140,7 @@ export default class Othello { * @param color 石の色 * @param pos 位置 */ - public put(color: Color, pos: number, fast = false): Undo { - if (!fast && !this.canPut(color, pos)) { - console.warn('can not put this position:', pos, color); - console.warn(this.board); - return null; - } - + public put(color: Color, pos: number) { this.prevPos = pos; this.prevColor = color; @@ -160,14 +156,14 @@ export default class Othello { const turn = this.turn; - this.calcTurn(); - - return { + this.logs.push({ color, pos, effects, turn - }; + }); + + this.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.prevPos = undo.pos; this.board[undo.pos] = null;