Refactor Reversi (#3584)

* Update core.ts

* Update core.ts

* Create functional-syntax.ts

* Update core.ts

* Update functional-syntax.ts

* Update core.ts

* Delete functional-syntax.ts
This commit is contained in:
Acid Chicken (硫酸鶏) 2018-12-17 19:10:38 +09:00 committed by Aya Morisawa
parent 48dc56e834
commit 82d721d60b

View file

@ -76,27 +76,14 @@ export default class Reversi {
this.mapHeight = map.length; this.mapHeight = map.length;
const mapData = map.join(''); const mapData = map.join('');
this.board = mapData.split('').map(d => { this.board = mapData.split('').map(d => d === '-' ? null : d === 'b' ? BLACK : d === 'w' ? WHITE : undefined);
if (d == '-') return null;
if (d == 'b') return BLACK;
if (d == 'w') return WHITE;
return undefined;
});
this.map = mapData.split('').map(d => { this.map = mapData.split('').map(d => d === '-' || d === 'b' || d === 'w' ? 'empty' : 'null');
if (d == '-' || d == 'b' || d == 'w') return 'empty';
return 'null';
});
//#endregion //#endregion
// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある // ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
if (!this.canPutSomewhere(BLACK)) { if (!this.canPutSomewhere(BLACK))
if (!this.canPutSomewhere(WHITE)) { this.turn = this.canPutSomewhere(WHITE) ? WHITE : null;
this.turn = null;
} else {
this.turn = WHITE;
}
}
} }
/** /**
@ -117,16 +104,14 @@ export default class Reversi {
* *
*/ */
public get blackP() { public get blackP() {
if (this.blackCount == 0 && this.whiteCount == 0) return 0; return this.blackCount == 0 && this.whiteCount == 0 ? 0 : this.blackCount / (this.blackCount + this.whiteCount);
return this.blackCount / (this.blackCount + this.whiteCount);
} }
/** /**
* *
*/ */
public get whiteP() { public get whiteP() {
if (this.blackCount == 0 && this.whiteCount == 0) return 0; return this.blackCount == 0 && this.whiteCount == 0 ? 0 : this.whiteCount / (this.blackCount + this.whiteCount);
return this.whiteCount / (this.blackCount + this.whiteCount);
} }
public transformPosToXy(pos: number): number[] { public transformPosToXy(pos: number): number[] {
@ -172,13 +157,10 @@ export default class Reversi {
private calcTurn() { private calcTurn() {
// ターン計算 // ターン計算
if (this.canPutSomewhere(!this.prevColor)) { this.turn =
this.turn = !this.prevColor; this.canPutSomewhere(!this.prevColor) ? !this.prevColor :
} else if (this.canPutSomewhere(this.prevColor)) { this.canPutSomewhere(this.prevColor) ? this.prevColor :
this.turn = this.prevColor; null;
} else {
this.turn = null;
}
} }
public undo() { public undo() {
@ -199,8 +181,7 @@ export default class Reversi {
*/ */
public mapDataGet(pos: number): MapPixel { public mapDataGet(pos: number): MapPixel {
const [x, y] = this.transformPosToXy(pos); const [x, y] = this.transformPosToXy(pos);
if (x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight) return 'null'; return x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight ? 'null' : this.map[pos];
return this.map[pos];
} }
/** /**
@ -223,16 +204,10 @@ export default class Reversi {
* @param pos * @param pos
*/ */
public canPut(color: Color, pos: number): boolean { public canPut(color: Color, pos: number): boolean {
// 既に石が置いてある場所には打てない return (
if (this.board[pos] !== null) return false; this.board[pos] !== null ? false : // 既に石が置いてある場所には打てない
this.opts.canPutEverywhere ? this.mapDataGet(pos) == 'empty' : // 挟んでなくても置けるモード
if (this.opts.canPutEverywhere) { this.effects(color, pos).length !== 0); // 相手の石を1つでも反転させられるか
// 挟んでなくても置けるモード
return this.mapDataGet(pos) == 'empty';
} else {
// 相手の石を1つでも反転させられるか
return this.effects(color, pos).length !== 0;
}
} }
/** /**
@ -263,19 +238,13 @@ export default class Reversi {
[x, y] = nextPos(x, y); [x, y] = nextPos(x, y);
// 座標が指し示す位置がボード外に出たとき // 座標が指し示す位置がボード外に出たとき
if (this.opts.loopedBoard) { if (this.opts.loopedBoard && this.transformXyToPos(
x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth; (x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth),
y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight; (y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight)) == initPos)
if (this.transformXyToPos(x, y) == initPos) {
// 盤面の境界でループし、自分が石を置く位置に戻ってきたとき、挟めるようにしている (ref: Test4のマップ) // 盤面の境界でループし、自分が石を置く位置に戻ってきたとき、挟めるようにしている (ref: Test4のマップ)
return found; return found;
} else if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight)
} else { return []; // 挟めないことが確定 (盤面外に到達)
if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight) {
return []; // 挟めないことが確定 (盤面外に到達)
}
}
const pos = this.transformXyToPos(x, y); const pos = this.transformXyToPos(x, y);
if (this.mapDataGet(pos) === 'null') return []; // 挟めないことが確定 (配置不可能なマスに到達) if (this.mapDataGet(pos) === 'null') return []; // 挟めないことが確定 (配置不可能なマスに到達)
@ -300,14 +269,9 @@ export default class Reversi {
* (null = ) * (null = )
*/ */
public get winner(): Color { public get winner(): Color {
if (!this.isEnded) return undefined; return this.isEnded ?
this.blackCount == this.whiteCount ? null :
if (this.blackCount == this.whiteCount) return null; this.opts.isLlotheo === this.blackCount > this.whiteCount ? WHITE : BLACK :
undefined;
if (this.opts.isLlotheo) {
return this.blackCount > this.whiteCount ? WHITE : BLACK;
} else {
return this.blackCount > this.whiteCount ? BLACK : WHITE;
}
} }
} }