This commit is contained in:
syuilo 2018-03-13 03:22:40 +09:00
parent 9359523d0f
commit c7272a4de4
1 changed files with 27 additions and 32 deletions

View File

@ -29,6 +29,11 @@ export type Undo = {
* *
*/ */
effects: number[]; effects: number[];
/**
*
*/
turn: Color;
}; };
/** /**
@ -128,51 +133,49 @@ export default class Othello {
return x + (y * this.mapWidth); return x + (y * this.mapWidth);
} }
/**
*
* @param color
* @param pos
*/
private write(color: Color, pos: number) {
this.board[pos] = color;
}
/** /**
* *
* @param color * @param color
* @param pos * @param pos
*/ */
public put(color: Color, pos: number, fast = false): Undo { public put(color: Color, pos: number, fast = false): Undo {
if (!fast && !this.canPut(color, pos)) return null; if (!fast && !this.canPut(color, pos)) {
console.warn('can not put this position:', pos, color);
console.warn(this.board);
return null;
}
this.prevPos = pos; this.prevPos = pos;
this.prevColor = color; this.prevColor = color;
this.write(color, pos);
this.board[pos] = color;
// 反転させられる石を取得 // 反転させられる石を取得
const effects = this.effects(color, pos); const effects = this.effects(color, pos);
// 反転させる // 反転させる
effects.forEach(pos => { for (const pos of effects) {
this.write(color, pos); this.board[pos] = color;
}); }
const turn = this.turn;
this.calcTurn(); this.calcTurn();
return { return {
color, color,
pos, pos,
effects effects,
turn
}; };
} }
private calcTurn() { private calcTurn() {
// ターン計算 // ターン計算
const opColor = this.prevColor === BLACK ? WHITE : BLACK; if (this.canPutSomewhere(!this.prevColor).length > 0) {
if (this.canPutSomewhere(opColor).length > 0) { this.turn = !this.prevColor;
this.turn = this.prevColor === BLACK ? WHITE : BLACK;
} else if (this.canPutSomewhere(this.prevColor).length > 0) { } else if (this.canPutSomewhere(this.prevColor).length > 0) {
this.turn = this.prevColor === BLACK ? BLACK : WHITE; this.turn = this.prevColor;
} else { } else {
this.turn = null; this.turn = null;
} }
@ -181,20 +184,12 @@ export default class Othello {
public undo(undo: Undo) { public undo(undo: Undo) {
this.prevColor = undo.color; this.prevColor = undo.color;
this.prevPos = undo.pos; this.prevPos = undo.pos;
this.write(null, undo.pos); this.board[undo.pos] = null;
for (const pos of undo.effects) { for (const pos of undo.effects) {
const color = this.board[pos]; const color = this.board[pos];
this.write(!color, pos); this.board[pos] = !color;
} }
this.calcTurn(); this.turn = undo.turn;
}
/**
*
* @param pos
*/
public get(pos: number) {
return this.board[pos];
} }
/** /**
@ -227,7 +222,7 @@ export default class Othello {
*/ */
public canPut(color: Color, pos: number): boolean { public canPut(color: Color, pos: number): boolean {
// 既に石が置いてある場所には打てない // 既に石が置いてある場所には打てない
if (this.get(pos) !== null) return false; if (this.board[pos] !== null) return false;
if (this.opts.canPutEverywhere) { if (this.opts.canPutEverywhere) {
// 挟んでなくても置けるモード // 挟んでなくても置けるモード
@ -292,7 +287,7 @@ export default class Othello {
//#endregion //#endregion
// 石取得 // 石取得
const stone = this.get(pos); const stone = this.board[pos];
// 石が置かれていないマスなら走査終了 // 石が置かれていないマスなら走査終了
if (stone === null) break; if (stone === null) break;