This commit is contained in:
syuilo 2017-10-09 00:23:17 +09:00
parent e6474cf239
commit 06a4cb93df

View file

@ -1,14 +1,10 @@
import * as EventEmitter from 'events'; export default class Othello {
export default class Othello extends EventEmitter {
public board: Array<Array<'black' | 'white'>>; public board: Array<Array<'black' | 'white'>>;
/** /**
* *
*/ */
constructor() { constructor() {
super();
this.board = [ this.board = [
[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, null, null, null, null, null],
@ -26,11 +22,15 @@ export default class Othello extends EventEmitter {
this.set(color, ps[n][0], ps[n][1]); this.set(color, ps[n][0], ps[n][1]);
} }
private write(color, x, y) {
this.board[y][x] = color;
}
/** /**
* *
*/ */
public set(color, x, y) { public set(color, x, y) {
this.board[y][x] = color; this.write(color, x, y);
const reverses = this.getReverse(color, x, y); const reverses = this.getReverse(color, x, y);
@ -38,55 +38,53 @@ export default class Othello extends EventEmitter {
switch (r[0]) { switch (r[0]) {
case 0: // 上 case 0: // 上
for (let c = 0, _y = y - 1; c < r[1]; c++, _y--) { for (let c = 0, _y = y - 1; c < r[1]; c++, _y--) {
this.board[_y][x] = color; this.write(color, x, _y);
} }
break; break;
case 1: // 右上 case 1: // 右上
for (let c = 0, i = 1; c < r[1]; c++, i++) { for (let c = 0, i = 1; c < r[1]; c++, i++) {
this.board[y - i][x + i] = color; this.write(color, x + i, y - i);
} }
break; break;
case 2: // 右 case 2: // 右
for (let c = 0, _x = x + 1; c < r[1]; c++, _x++) { for (let c = 0, _x = x + 1; c < r[1]; c++, _x++) {
this.board[y][_x] = color; this.write(color, _x, y);
} }
break; break;
case 3: // 右下 case 3: // 右下
for (let c = 0, i = 1; c < r[1]; c++, i++) { for (let c = 0, i = 1; c < r[1]; c++, i++) {
this.board[y + i][x + i] = color; this.write(color, x + i, y + i);
} }
break; break;
case 4: // 下 case 4: // 下
for (let c = 0, _y = y + 1; c < r[1]; c++, _y++) { for (let c = 0, _y = y + 1; c < r[1]; c++, _y++) {
this.board[_y][x] = color; this.write(color, x, _y);
} }
break; break;
case 5: // 左下 case 5: // 左下
for (let c = 0, i = 1; c < r[1]; c++, i++) { for (let c = 0, i = 1; c < r[1]; c++, i++) {
this.board[y + i][x - i] = color; this.write(color, x - i, y + i);
} }
break; break;
case 6: // 左 case 6: // 左
for (let c = 0, _x = x - 1; c < r[1]; c++, _x--) { for (let c = 0, _x = x - 1; c < r[1]; c++, _x--) {
this.board[y][_x] = color; this.write(color, _x, y);
} }
break; break;
case 7: // 左上 case 7: // 左上
for (let c = 0, i = 1; c < r[1]; c++, i++) { for (let c = 0, i = 1; c < r[1]; c++, i++) {
this.board[y - i][x - i] = color; this.write(color, x - i, y - i);
} }
break; break;
} }
}); });
this.emit('set:' + color, x, y);
} }
/** /**
@ -226,40 +224,7 @@ export default class Othello extends EventEmitter {
}).join('')).join('\n'); }).join('')).join('\n');
} }
} }
/*
export class Ai {
private othello: Othello;
private color: string;
private opponentColor: string;
constructor(color: string, othello: Othello) {
this.othello = othello;
this.color = color;
this.opponentColor = this.color == 'black' ? 'white' : 'black';
this.othello.on('set:' + this.opponentColor, () => {
this.turn();
});
if (this.color == 'black') {
this.turn();
}
}
public turn() {
const ps = this.othello.getPattern(this.color);
if (ps.length > 0) {
const p = ps[Math.floor(Math.random() * ps.length)];
this.othello.set(this.color, p[0], p[1]);
// 相手の打つ場所がない場合続けてAIのターン
if (this.othello.getPattern(this.opponentColor).length === 0) {
this.turn();
}
}
}
}
*/
export function ai(color: string, othello: Othello) { export function ai(color: string, othello: Othello) {
const opponentColor = color == 'black' ? 'white' : 'black'; const opponentColor = color == 'black' ? 'white' : 'black';