Refactor reversi engine (#2633)

* Refactor reversi engine

* Add puttablePlaces
This commit is contained in:
Aya Morisawa
2018-09-06 03:02:52 +09:00
committed by syuilo
parent 1cc183ecdb
commit 498094b3c7

View File

@@ -1,4 +1,4 @@
import { count } from "../../prelude/array"; import { count, countIf } from "../../prelude/array";
// MISSKEY REVERSI ENGINE // MISSKEY REVERSI ENGINE
@@ -90,8 +90,8 @@ export default class Reversi {
//#endregion //#endregion
// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある // ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
if (this.canPutSomewhere(BLACK).length == 0) { if (!this.canPutSomewhere(BLACK)) {
if (this.canPutSomewhere(WHITE).length == 0) { if (!this.canPutSomewhere(WHITE)) {
this.turn = null; this.turn = null;
} else { } else {
this.turn = WHITE; this.turn = WHITE;
@@ -172,9 +172,9 @@ export default class Reversi {
private calcTurn() { private calcTurn() {
// ターン計算 // ターン計算
if (this.canPutSomewhere(!this.prevColor).length > 0) { if (this.canPutSomewhere(!this.prevColor)) {
this.turn = !this.prevColor; this.turn = !this.prevColor;
} else if (this.canPutSomewhere(this.prevColor).length > 0) { } else if (this.canPutSomewhere(this.prevColor)) {
this.turn = this.prevColor; this.turn = this.prevColor;
} else { } else {
this.turn = null; this.turn = null;
@@ -206,10 +206,17 @@ export default class Reversi {
/** /**
* 打つことができる場所を取得します * 打つことができる場所を取得します
*/ */
public canPutSomewhere(color: Color): number[] { public puttablePlaces(color: Color): number[] {
return Array.from(this.board.keys()).filter(i => this.canPut(color, i)); return Array.from(this.board.keys()).filter(i => this.canPut(color, i));
} }
/**
* 打つことができる場所があるかどうかを取得します
*/
public canPutSomewhere(color: Color): boolean {
return this.puttablePlaces(color).length > 0;
}
/** /**
* 指定のマスに石を打つことができるかどうかを取得します * 指定のマスに石を打つことができるかどうかを取得します
* @param color 自分の色 * @param color 自分の色