From 1316f17dca5ac024c91ba93d0522a073f2c546be Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 9 Oct 2017 00:43:53 +0900 Subject: [PATCH] Improve the othello AI --- src/common/othello.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/common/othello.ts b/src/common/othello.ts index 5f7000019..15dc571ce 100644 --- a/src/common/othello.ts +++ b/src/common/othello.ts @@ -229,10 +229,31 @@ export function ai(color: string, othello: Othello) { const opponentColor = color == 'black' ? 'white' : 'black'; function think() { + // 打てる場所を取得 const ps = othello.getPattern(color); - if (ps.length > 0) { - const p = ps[Math.floor(Math.random() * ps.length)]; - othello.set(color, p[0], p[1]); + + if (ps.length > 0) { // 打てる場所がある場合 + // 角を取得 + const corners = ps.filter(p => + // 左上 + (p[0] == 0 && p[1] == 0) || + // 右上 + (p[0] == 7 && p[1] == 0) || + // 右下 + (p[0] == 7 && p[1] == 7) || + // 左下 + (p[0] == 0 && p[1] == 7) + ); + + if (corners.length > 0) { // どこかしらの角に打てる場合 + // 打てる角からランダムに選択して打つ + const p = corners[Math.floor(Math.random() * corners.length)]; + othello.set(color, p[0], p[1]); + } else { // 打てる角がない場合 + // 打てる場所からランダムに選択して打つ + const p = ps[Math.floor(Math.random() * ps.length)]; + othello.set(color, p[0], p[1]); + } // 相手の打つ場所がない場合続けてAIのターン if (othello.getPattern(opponentColor).length === 0) {