summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-10-09 00:43:53 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-10-09 00:43:53 +0900
commit1316f17dca5ac024c91ba93d0522a073f2c546be (patch)
tree76c808a44641c938cbb9a40396d405bfe5c2c061 /src/common
parentRefactor (diff)
downloadsharkey-1316f17dca5ac024c91ba93d0522a073f2c546be.tar.gz
sharkey-1316f17dca5ac024c91ba93d0522a073f2c546be.tar.bz2
sharkey-1316f17dca5ac024c91ba93d0522a073f2c546be.zip
Improve the othello AI
Diffstat (limited to 'src/common')
-rw-r--r--src/common/othello.ts27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/common/othello.ts b/src/common/othello.ts
index 5f7000019a..15dc571ced 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) {