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[];
/**
*
*/
turn: Color;
};
/**
@ -128,51 +133,49 @@ export default class Othello {
return x + (y * this.mapWidth);
}
/**
*
* @param color
* @param pos
*/
private write(color: Color, pos: number) {
this.board[pos] = color;
}
/**
*
* @param color
* @param pos
*/
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.prevColor = color;
this.write(color, pos);
this.board[pos] = color;
// 反転させられる石を取得
const effects = this.effects(color, pos);
// 反転させる
effects.forEach(pos => {
this.write(color, pos);
});
for (const pos of effects) {
this.board[pos] = color;
}
const turn = this.turn;
this.calcTurn();
return {
color,
pos,
effects
effects,
turn
};
}
private calcTurn() {
// ターン計算
const opColor = this.prevColor === BLACK ? WHITE : BLACK;
if (this.canPutSomewhere(opColor).length > 0) {
this.turn = this.prevColor === BLACK ? WHITE : BLACK;
if (this.canPutSomewhere(!this.prevColor).length > 0) {
this.turn = !this.prevColor;
} else if (this.canPutSomewhere(this.prevColor).length > 0) {
this.turn = this.prevColor === BLACK ? BLACK : WHITE;
this.turn = this.prevColor;
} else {
this.turn = null;
}
@ -181,20 +184,12 @@ export default class Othello {
public undo(undo: Undo) {
this.prevColor = undo.color;
this.prevPos = undo.pos;
this.write(null, undo.pos);
this.board[undo.pos] = null;
for (const pos of undo.effects) {
const color = this.board[pos];
this.write(!color, pos);
this.board[pos] = !color;
}
this.calcTurn();
}
/**
*
* @param pos
*/
public get(pos: number) {
return this.board[pos];
this.turn = undo.turn;
}
/**
@ -227,7 +222,7 @@ export default class Othello {
*/
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) {
// 挟んでなくても置けるモード
@ -292,7 +287,7 @@ export default class Othello {
//#endregion
// 石取得
const stone = this.get(pos);
const stone = this.board[pos];
// 石が置かれていないマスなら走査終了
if (stone === null) break;