2017-10-09 19:52:18 +09:00
|
|
|
|
const BOARD_SIZE = 8;
|
|
|
|
|
|
2017-10-09 00:23:17 +09:00
|
|
|
|
export default class Othello {
|
2018-03-07 17:48:32 +09:00
|
|
|
|
public board: Array<'black' | 'white'>;
|
2017-10-08 03:24:10 +09:00
|
|
|
|
|
2018-03-07 21:57:06 +09:00
|
|
|
|
public stats: Array<{
|
|
|
|
|
b: number;
|
|
|
|
|
w: number;
|
|
|
|
|
}> = [];
|
|
|
|
|
|
2017-10-08 03:24:10 +09:00
|
|
|
|
/**
|
|
|
|
|
* ゲームを初期化します
|
|
|
|
|
*/
|
|
|
|
|
constructor() {
|
|
|
|
|
this.board = [
|
2018-03-07 17:48:32 +09:00
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
|
null, null, null, 'white', 'black', null, null, null,
|
|
|
|
|
null, null, null, 'black', 'white', null, null, null,
|
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
|
null, null, null, null, null, null, null, null,
|
|
|
|
|
null, null, null, null, null, null, null, null
|
2017-10-08 03:24:10 +09:00
|
|
|
|
];
|
2018-03-07 21:57:06 +09:00
|
|
|
|
|
|
|
|
|
this.stats.push({
|
|
|
|
|
b: 0.5,
|
|
|
|
|
w: 0.5
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-07 22:25:26 +09:00
|
|
|
|
public prevPos = -1;
|
|
|
|
|
|
2018-03-07 21:57:06 +09:00
|
|
|
|
public get blackCount() {
|
|
|
|
|
return this.board.filter(s => s == 'black').length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get whiteCount() {
|
|
|
|
|
return this.board.filter(s => s == 'white').length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get blackP() {
|
|
|
|
|
return this.blackCount / (this.blackCount + this.whiteCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get whiteP() {
|
|
|
|
|
return this.whiteCount / (this.blackCount + this.whiteCount);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setByNumber(color, n) {
|
|
|
|
|
const ps = this.getPattern(color);
|
2018-03-07 17:48:32 +09:00
|
|
|
|
this.set2(color, ps[n][0], ps[n][1]);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 00:23:17 +09:00
|
|
|
|
private write(color, x, y) {
|
2018-03-07 17:48:32 +09:00
|
|
|
|
const pos = x + (y * 8);
|
|
|
|
|
this.board[pos] = color;
|
2017-10-09 00:23:17 +09:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 03:24:10 +09:00
|
|
|
|
/**
|
|
|
|
|
* 石を配置します
|
|
|
|
|
*/
|
2018-03-07 17:48:32 +09:00
|
|
|
|
public set2(color, x, y) {
|
2018-03-07 22:25:26 +09:00
|
|
|
|
this.prevPos = x + (y * 8);
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, x, y);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
|
|
|
|
|
const reverses = this.getReverse(color, x, y);
|
|
|
|
|
|
|
|
|
|
reverses.forEach(r => {
|
|
|
|
|
switch (r[0]) {
|
|
|
|
|
case 0: // 上
|
|
|
|
|
for (let c = 0, _y = y - 1; c < r[1]; c++, _y--) {
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, x, _y);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1: // 右上
|
|
|
|
|
for (let c = 0, i = 1; c < r[1]; c++, i++) {
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, x + i, y - i);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 2: // 右
|
|
|
|
|
for (let c = 0, _x = x + 1; c < r[1]; c++, _x++) {
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, _x, y);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3: // 右下
|
|
|
|
|
for (let c = 0, i = 1; c < r[1]; c++, i++) {
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, x + i, y + i);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4: // 下
|
|
|
|
|
for (let c = 0, _y = y + 1; c < r[1]; c++, _y++) {
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, x, _y);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 5: // 左下
|
|
|
|
|
for (let c = 0, i = 1; c < r[1]; c++, i++) {
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, x - i, y + i);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 6: // 左
|
|
|
|
|
for (let c = 0, _x = x - 1; c < r[1]; c++, _x--) {
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, _x, y);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 7: // 左上
|
|
|
|
|
for (let c = 0, i = 1; c < r[1]; c++, i++) {
|
2017-10-09 00:23:17 +09:00
|
|
|
|
this.write(color, x - i, y - i);
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-03-07 21:57:06 +09:00
|
|
|
|
|
|
|
|
|
this.stats.push({
|
|
|
|
|
b: this.blackP,
|
|
|
|
|
w: this.whiteP
|
|
|
|
|
});
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-07 17:48:32 +09:00
|
|
|
|
public set(color, pos) {
|
|
|
|
|
const x = pos % BOARD_SIZE;
|
|
|
|
|
const y = Math.floor(pos / BOARD_SIZE);
|
|
|
|
|
this.set2(color, x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get(x, y) {
|
|
|
|
|
const pos = x + (y * 8);
|
|
|
|
|
return this.board[pos];
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 03:24:10 +09:00
|
|
|
|
/**
|
|
|
|
|
* 打つことができる場所を取得します
|
|
|
|
|
*/
|
|
|
|
|
public getPattern(myColor): number[][] {
|
|
|
|
|
const result = [];
|
2018-03-07 17:48:32 +09:00
|
|
|
|
this.board.forEach((stone, i) => {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
if (stone != null) return;
|
2018-03-07 17:48:32 +09:00
|
|
|
|
const x = i % BOARD_SIZE;
|
|
|
|
|
const y = Math.floor(i / BOARD_SIZE);
|
|
|
|
|
if (this.canReverse2(myColor, x, y)) result.push([x, y]);
|
|
|
|
|
});
|
2017-10-08 03:24:10 +09:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 指定の位置に石を打つことができるかどうか(相手の石を1つでも反転させられるか)を取得します
|
|
|
|
|
*/
|
2018-03-07 21:57:06 +09:00
|
|
|
|
public canReverse2(myColor, x, y): boolean {
|
|
|
|
|
return this.canReverse(myColor, x + (y * 8));
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
2018-03-07 17:48:32 +09:00
|
|
|
|
public canReverse(myColor, pos): boolean {
|
2018-03-07 21:57:06 +09:00
|
|
|
|
if (this.board[pos] != null) return false;
|
2018-03-07 17:48:32 +09:00
|
|
|
|
const x = pos % BOARD_SIZE;
|
|
|
|
|
const y = Math.floor(pos / BOARD_SIZE);
|
|
|
|
|
return this.getReverse(myColor, x, y) !== null;
|
|
|
|
|
}
|
2017-10-08 03:24:10 +09:00
|
|
|
|
|
|
|
|
|
private getReverse(myColor, targetx, targety): number[] {
|
|
|
|
|
const opponentColor = myColor == 'black' ? 'white' : 'black';
|
|
|
|
|
|
|
|
|
|
const createIterater = () => {
|
|
|
|
|
let opponentStoneFound = false;
|
|
|
|
|
let breaked = false;
|
|
|
|
|
return (x, y): any => {
|
|
|
|
|
if (breaked) {
|
|
|
|
|
return;
|
2018-03-07 17:48:32 +09:00
|
|
|
|
} else if (this.get(x, y) == myColor && opponentStoneFound) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
return true;
|
2018-03-07 17:48:32 +09:00
|
|
|
|
} else if (this.get(x, y) == myColor && !opponentStoneFound) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
breaked = true;
|
2018-03-07 17:48:32 +09:00
|
|
|
|
} else if (this.get(x, y) == opponentColor) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
opponentStoneFound = true;
|
|
|
|
|
} else {
|
|
|
|
|
breaked = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const res = [];
|
|
|
|
|
|
|
|
|
|
let iterate;
|
|
|
|
|
|
|
|
|
|
// 上
|
|
|
|
|
iterate = createIterater();
|
|
|
|
|
for (let c = 0, y = targety - 1; y >= 0; c++, y--) {
|
|
|
|
|
if (iterate(targetx, y)) {
|
|
|
|
|
res.push([0, c]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 右上
|
|
|
|
|
iterate = createIterater();
|
2017-10-09 20:12:06 +09:00
|
|
|
|
for (let c = 0, i = 1; i <= Math.min(BOARD_SIZE - targetx, targety); c++, i++) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
if (iterate(targetx + i, targety - i)) {
|
|
|
|
|
res.push([1, c]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 右
|
|
|
|
|
iterate = createIterater();
|
2017-10-09 19:52:18 +09:00
|
|
|
|
for (let c = 0, x = targetx + 1; x < BOARD_SIZE; c++, x++) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
if (iterate(x, targety)) {
|
|
|
|
|
res.push([2, c]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 右下
|
|
|
|
|
iterate = createIterater();
|
2018-03-07 21:57:06 +09:00
|
|
|
|
for (let c = 0, i = 1; i <= Math.min(BOARD_SIZE - targetx, BOARD_SIZE - targety); c++, i++) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
if (iterate(targetx + i, targety + i)) {
|
|
|
|
|
res.push([3, c]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 下
|
|
|
|
|
iterate = createIterater();
|
2017-10-09 19:52:18 +09:00
|
|
|
|
for (let c = 0, y = targety + 1; y < BOARD_SIZE; c++, y++) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
if (iterate(targetx, y)) {
|
|
|
|
|
res.push([4, c]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 左下
|
|
|
|
|
iterate = createIterater();
|
2018-03-07 21:57:06 +09:00
|
|
|
|
for (let c = 0, i = 1; i <= Math.min(targetx, BOARD_SIZE - targety); c++, i++) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
if (iterate(targetx - i, targety + i)) {
|
|
|
|
|
res.push([5, c]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 左
|
|
|
|
|
iterate = createIterater();
|
|
|
|
|
for (let c = 0, x = targetx - 1; x >= 0; c++, x--) {
|
|
|
|
|
if (iterate(x, targety)) {
|
|
|
|
|
res.push([6, c]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 左上
|
|
|
|
|
iterate = createIterater();
|
2017-10-09 20:12:06 +09:00
|
|
|
|
for (let c = 0, i = 1; i <= Math.min(targetx, targety); c++, i++) {
|
2017-10-08 03:24:10 +09:00
|
|
|
|
if (iterate(targetx - i, targety - i)) {
|
|
|
|
|
res.push([7, c]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.length === 0 ? null : res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public toString(): string {
|
2017-10-08 03:56:03 +09:00
|
|
|
|
//return this.board.map(row => row.map(state => state === 'black' ? '●' : state === 'white' ? '○' : '┼').join('')).join('\n');
|
2018-03-07 17:48:32 +09:00
|
|
|
|
//return this.board.map(row => row.map(state => state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : '🔹').join('')).join('\n');
|
|
|
|
|
return 'wip';
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public toPatternString(color): string {
|
2017-10-08 03:56:03 +09:00
|
|
|
|
//const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
2018-03-07 17:48:32 +09:00
|
|
|
|
/*const num = ['0️⃣', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟', '🍏', '🍎', '🍐', '🍊', '🍋', '🍌', '🍉', '🍇', '🍓', '🍈', '🍒', '🍑', '🍍'];
|
2017-10-08 03:24:10 +09:00
|
|
|
|
|
|
|
|
|
const pattern = this.getPattern(color);
|
|
|
|
|
|
|
|
|
|
return this.board.map((row, y) => row.map((state, x) => {
|
|
|
|
|
const i = pattern.findIndex(p => p[0] == x && p[1] == y);
|
2017-10-08 03:56:03 +09:00
|
|
|
|
//return state === 'black' ? '●' : state === 'white' ? '○' : i != -1 ? num[i] : '┼';
|
2017-10-08 03:24:10 +09:00
|
|
|
|
return state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : i != -1 ? num[i] : '🔹';
|
2018-03-07 17:48:32 +09:00
|
|
|
|
}).join('')).join('\n');*/
|
|
|
|
|
|
|
|
|
|
return 'wip';
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ai(color: string, othello: Othello) {
|
|
|
|
|
const opponentColor = color == 'black' ? 'white' : 'black';
|
|
|
|
|
|
|
|
|
|
function think() {
|
2017-10-09 00:43:53 +09:00
|
|
|
|
// 打てる場所を取得
|
2017-10-08 03:24:10 +09:00
|
|
|
|
const ps = othello.getPattern(color);
|
2017-10-09 00:43:53 +09:00
|
|
|
|
|
|
|
|
|
if (ps.length > 0) { // 打てる場所がある場合
|
|
|
|
|
// 角を取得
|
|
|
|
|
const corners = ps.filter(p =>
|
|
|
|
|
// 左上
|
|
|
|
|
(p[0] == 0 && p[1] == 0) ||
|
|
|
|
|
// 右上
|
2017-10-09 19:52:18 +09:00
|
|
|
|
(p[0] == (BOARD_SIZE - 1) && p[1] == 0) ||
|
2017-10-09 00:43:53 +09:00
|
|
|
|
// 右下
|
2017-10-09 19:52:18 +09:00
|
|
|
|
(p[0] == (BOARD_SIZE - 1) && p[1] == (BOARD_SIZE - 1)) ||
|
2017-10-09 00:43:53 +09:00
|
|
|
|
// 左下
|
2017-10-09 19:52:18 +09:00
|
|
|
|
(p[0] == 0 && p[1] == (BOARD_SIZE - 1))
|
2017-10-09 00:43:53 +09:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
}
|
2017-10-08 03:24:10 +09:00
|
|
|
|
|
|
|
|
|
// 相手の打つ場所がない場合続けてAIのターン
|
|
|
|
|
if (othello.getPattern(opponentColor).length === 0) {
|
|
|
|
|
think();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-08 04:13:38 +09:00
|
|
|
|
|
|
|
|
|
think();
|
2017-10-08 03:24:10 +09:00
|
|
|
|
}
|