2018-12-19 12:20:25 +00:00
|
|
|
|
import { count, concat } from '../../prelude/array';
|
2018-09-05 17:16:08 +00:00
|
|
|
|
|
2018-07-07 10:01:33 +00:00
|
|
|
|
// MISSKEY REVERSI ENGINE
|
|
|
|
|
|
2018-03-12 16:49:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* true ... 黒
|
|
|
|
|
* false ... 白
|
|
|
|
|
*/
|
|
|
|
|
export type Color = boolean;
|
|
|
|
|
const BLACK = true;
|
|
|
|
|
const WHITE = false;
|
|
|
|
|
|
2018-03-08 08:57:57 +00:00
|
|
|
|
export type MapPixel = 'null' | 'empty';
|
|
|
|
|
|
|
|
|
|
export type Options = {
|
|
|
|
|
isLlotheo: boolean;
|
2018-03-10 09:22:54 +00:00
|
|
|
|
canPutEverywhere: boolean;
|
2018-03-10 12:23:00 +00:00
|
|
|
|
loopedBoard: boolean;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-03-12 16:49:54 +00:00
|
|
|
|
export type Undo = {
|
|
|
|
|
/**
|
|
|
|
|
* 色
|
|
|
|
|
*/
|
2018-07-07 10:01:33 +00:00
|
|
|
|
color: Color;
|
2018-03-12 16:49:54 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* どこに打ったか
|
|
|
|
|
*/
|
|
|
|
|
pos: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 反転した石の位置の配列
|
|
|
|
|
*/
|
|
|
|
|
effects: number[];
|
2018-03-12 18:22:40 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ターン
|
|
|
|
|
*/
|
|
|
|
|
turn: Color;
|
2018-03-12 16:49:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-03-08 08:57:57 +00:00
|
|
|
|
/**
|
2018-06-16 23:10:54 +00:00
|
|
|
|
* リバーシエンジン
|
2018-03-08 08:57:57 +00:00
|
|
|
|
*/
|
2018-06-16 23:10:54 +00:00
|
|
|
|
export default class Reversi {
|
2018-03-09 09:11:10 +00:00
|
|
|
|
public map: MapPixel[];
|
|
|
|
|
public mapWidth: number;
|
|
|
|
|
public mapHeight: number;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
public board: Color[];
|
2018-03-12 16:49:54 +00:00
|
|
|
|
public turn: Color = BLACK;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
public opts: Options;
|
|
|
|
|
|
2018-03-09 09:11:10 +00:00
|
|
|
|
public prevPos = -1;
|
2018-03-12 16:49:54 +00:00
|
|
|
|
public prevColor: Color = null;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
|
2018-03-15 05:09:38 +00:00
|
|
|
|
private logs: Undo[] = [];
|
|
|
|
|
|
2018-03-08 08:57:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* ゲームを初期化します
|
|
|
|
|
*/
|
2018-03-09 09:11:10 +00:00
|
|
|
|
constructor(map: string[], opts: Options) {
|
2018-03-12 16:49:54 +00:00
|
|
|
|
//#region binds
|
|
|
|
|
this.put = this.put.bind(this);
|
|
|
|
|
//#endregion
|
|
|
|
|
|
2018-03-10 09:22:54 +00:00
|
|
|
|
//#region Options
|
2018-03-08 08:57:57 +00:00
|
|
|
|
this.opts = opts;
|
2018-03-10 09:22:54 +00:00
|
|
|
|
if (this.opts.isLlotheo == null) this.opts.isLlotheo = false;
|
|
|
|
|
if (this.opts.canPutEverywhere == null) this.opts.canPutEverywhere = false;
|
2018-03-10 12:23:00 +00:00
|
|
|
|
if (this.opts.loopedBoard == null) this.opts.loopedBoard = false;
|
2018-03-10 09:22:54 +00:00
|
|
|
|
//#endregion
|
2018-03-08 08:57:57 +00:00
|
|
|
|
|
2018-03-10 09:22:54 +00:00
|
|
|
|
//#region Parse map data
|
2018-03-09 09:11:10 +00:00
|
|
|
|
this.mapWidth = map[0].length;
|
|
|
|
|
this.mapHeight = map.length;
|
|
|
|
|
const mapData = map.join('');
|
|
|
|
|
|
2018-12-17 10:10:38 +00:00
|
|
|
|
this.board = mapData.split('').map(d => d === '-' ? null : d === 'b' ? BLACK : d === 'w' ? WHITE : undefined);
|
2018-03-10 09:22:54 +00:00
|
|
|
|
|
2018-12-17 10:10:38 +00:00
|
|
|
|
this.map = mapData.split('').map(d => d === '-' || d === 'b' || d === 'w' ? 'empty' : 'null');
|
2018-03-10 09:22:54 +00:00
|
|
|
|
//#endregion
|
2018-03-08 08:57:57 +00:00
|
|
|
|
|
2018-03-10 04:07:17 +00:00
|
|
|
|
// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
|
2018-12-17 10:10:38 +00:00
|
|
|
|
if (!this.canPutSomewhere(BLACK))
|
|
|
|
|
this.turn = this.canPutSomewhere(WHITE) ? WHITE : null;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 黒石の数
|
|
|
|
|
*/
|
|
|
|
|
public get blackCount() {
|
2018-09-05 17:16:08 +00:00
|
|
|
|
return count(BLACK, this.board);
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 白石の数
|
|
|
|
|
*/
|
|
|
|
|
public get whiteCount() {
|
2018-09-06 10:28:52 +00:00
|
|
|
|
return count(WHITE, this.board);
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public transformPosToXy(pos: number): number[] {
|
2018-03-09 09:11:10 +00:00
|
|
|
|
const x = pos % this.mapWidth;
|
2018-03-09 18:01:01 +00:00
|
|
|
|
const y = Math.floor(pos / this.mapWidth);
|
2018-03-08 08:57:57 +00:00
|
|
|
|
return [x, y];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public transformXyToPos(x: number, y: number): number {
|
2018-03-09 18:01:01 +00:00
|
|
|
|
return x + (y * this.mapWidth);
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 指定のマスに石を打ちます
|
|
|
|
|
* @param color 石の色
|
|
|
|
|
* @param pos 位置
|
|
|
|
|
*/
|
2018-03-15 05:09:38 +00:00
|
|
|
|
public put(color: Color, pos: number) {
|
2018-03-08 08:57:57 +00:00
|
|
|
|
this.prevPos = pos;
|
2018-03-12 16:49:54 +00:00
|
|
|
|
this.prevColor = color;
|
2018-03-12 18:22:40 +00:00
|
|
|
|
|
|
|
|
|
this.board[pos] = color;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
|
|
|
|
|
// 反転させられる石を取得
|
2018-03-12 16:49:54 +00:00
|
|
|
|
const effects = this.effects(color, pos);
|
2018-03-08 08:57:57 +00:00
|
|
|
|
|
|
|
|
|
// 反転させる
|
2018-03-12 18:22:40 +00:00
|
|
|
|
for (const pos of effects) {
|
|
|
|
|
this.board[pos] = color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const turn = this.turn;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
|
2018-03-15 05:09:38 +00:00
|
|
|
|
this.logs.push({
|
2018-03-12 16:49:54 +00:00
|
|
|
|
color,
|
|
|
|
|
pos,
|
2018-03-12 18:22:40 +00:00
|
|
|
|
effects,
|
|
|
|
|
turn
|
2018-03-15 05:09:38 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.calcTurn();
|
2018-03-12 16:49:54 +00:00
|
|
|
|
}
|
2018-03-08 08:57:57 +00:00
|
|
|
|
|
2018-03-12 16:49:54 +00:00
|
|
|
|
private calcTurn() {
|
2018-03-08 08:57:57 +00:00
|
|
|
|
// ターン計算
|
2018-12-17 10:10:38 +00:00
|
|
|
|
this.turn =
|
|
|
|
|
this.canPutSomewhere(!this.prevColor) ? !this.prevColor :
|
|
|
|
|
this.canPutSomewhere(this.prevColor) ? this.prevColor :
|
|
|
|
|
null;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-15 05:09:38 +00:00
|
|
|
|
public undo() {
|
|
|
|
|
const undo = this.logs.pop();
|
2018-03-12 16:49:54 +00:00
|
|
|
|
this.prevColor = undo.color;
|
|
|
|
|
this.prevPos = undo.pos;
|
2018-03-12 18:22:40 +00:00
|
|
|
|
this.board[undo.pos] = null;
|
2018-03-12 16:49:54 +00:00
|
|
|
|
for (const pos of undo.effects) {
|
|
|
|
|
const color = this.board[pos];
|
2018-03-12 18:22:40 +00:00
|
|
|
|
this.board[pos] = !color;
|
2018-03-12 16:49:54 +00:00
|
|
|
|
}
|
2018-03-12 18:22:40 +00:00
|
|
|
|
this.turn = undo.turn;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 指定した位置のマップデータのマスを取得します
|
|
|
|
|
* @param pos 位置
|
|
|
|
|
*/
|
|
|
|
|
public mapDataGet(pos: number): MapPixel {
|
2018-03-11 12:48:16 +00:00
|
|
|
|
const [x, y] = this.transformPosToXy(pos);
|
2018-12-17 10:10:38 +00:00
|
|
|
|
return x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight ? 'null' : this.map[pos];
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 打つことができる場所を取得します
|
|
|
|
|
*/
|
2018-09-05 18:02:52 +00:00
|
|
|
|
public puttablePlaces(color: Color): number[] {
|
2018-09-01 13:09:54 +00:00
|
|
|
|
return Array.from(this.board.keys()).filter(i => this.canPut(color, i));
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 18:02:52 +00:00
|
|
|
|
/**
|
|
|
|
|
* 打つことができる場所があるかどうかを取得します
|
|
|
|
|
*/
|
|
|
|
|
public canPutSomewhere(color: Color): boolean {
|
|
|
|
|
return this.puttablePlaces(color).length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-08 08:57:57 +00:00
|
|
|
|
/**
|
2018-03-10 09:22:54 +00:00
|
|
|
|
* 指定のマスに石を打つことができるかどうかを取得します
|
2018-03-08 08:57:57 +00:00
|
|
|
|
* @param color 自分の色
|
|
|
|
|
* @param pos 位置
|
|
|
|
|
*/
|
|
|
|
|
public canPut(color: Color, pos: number): boolean {
|
2018-12-17 10:10:38 +00:00
|
|
|
|
return (
|
|
|
|
|
this.board[pos] !== null ? false : // 既に石が置いてある場所には打てない
|
|
|
|
|
this.opts.canPutEverywhere ? this.mapDataGet(pos) == 'empty' : // 挟んでなくても置けるモード
|
|
|
|
|
this.effects(color, pos).length !== 0); // 相手の石を1つでも反転させられるか
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 指定のマスに石を置いた時の、反転させられる石を取得します
|
|
|
|
|
* @param color 自分の色
|
2018-09-06 11:06:16 +00:00
|
|
|
|
* @param initPos 位置
|
2018-03-08 08:57:57 +00:00
|
|
|
|
*/
|
2018-09-06 11:06:16 +00:00
|
|
|
|
public effects(color: Color, initPos: number): number[] {
|
2018-03-12 16:49:54 +00:00
|
|
|
|
const enemyColor = !color;
|
2018-03-10 12:23:00 +00:00
|
|
|
|
|
2018-09-06 11:06:16 +00:00
|
|
|
|
const diffVectors: [number, number][] = [
|
|
|
|
|
[ 0, -1], // 上
|
|
|
|
|
[ +1, -1], // 右上
|
|
|
|
|
[ +1, 0], // 右
|
|
|
|
|
[ +1, +1], // 右下
|
|
|
|
|
[ 0, +1], // 下
|
|
|
|
|
[ -1, +1], // 左下
|
|
|
|
|
[ -1, 0], // 左
|
|
|
|
|
[ -1, -1] // 左上
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const effectsInLine = ([dx, dy]: [number, number]): number[] => {
|
|
|
|
|
const nextPos = (x: number, y: number): [number, number] => [x + dx, y + dy];
|
|
|
|
|
|
|
|
|
|
const found: number[] = []; // 挟めるかもしれない相手の石を入れておく配列
|
|
|
|
|
let [x, y] = this.transformPosToXy(initPos);
|
2018-03-08 08:57:57 +00:00
|
|
|
|
while (true) {
|
2018-09-06 15:03:44 +00:00
|
|
|
|
[x, y] = nextPos(x, y);
|
|
|
|
|
|
2018-03-10 12:23:00 +00:00
|
|
|
|
// 座標が指し示す位置がボード外に出たとき
|
2018-12-17 10:10:38 +00:00
|
|
|
|
if (this.opts.loopedBoard && this.transformXyToPos(
|
|
|
|
|
(x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth),
|
|
|
|
|
(y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight)) == initPos)
|
2018-09-06 11:06:16 +00:00
|
|
|
|
// 盤面の境界でループし、自分が石を置く位置に戻ってきたとき、挟めるようにしている (ref: Test4のマップ)
|
2018-12-17 10:10:38 +00:00
|
|
|
|
return found;
|
|
|
|
|
else if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight)
|
|
|
|
|
return []; // 挟めないことが確定 (盤面外に到達)
|
2018-03-10 12:23:00 +00:00
|
|
|
|
|
2018-03-08 08:57:57 +00:00
|
|
|
|
const pos = this.transformXyToPos(x, y);
|
2018-09-06 11:06:16 +00:00
|
|
|
|
if (this.mapDataGet(pos) === 'null') return []; // 挟めないことが確定 (配置不可能なマスに到達)
|
2018-03-12 18:22:40 +00:00
|
|
|
|
const stone = this.board[pos];
|
2018-09-06 11:06:16 +00:00
|
|
|
|
if (stone === null) return []; // 挟めないことが確定 (石が置かれていないマスに到達)
|
|
|
|
|
if (stone === enemyColor) found.push(pos); // 挟めるかもしれない (相手の石を発見)
|
|
|
|
|
if (stone === color) return found; // 挟めることが確定 (対となる自分の石を発見)
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-06 12:31:15 +00:00
|
|
|
|
return concat(diffVectors.map(effectsInLine));
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ゲームが終了したか否か
|
|
|
|
|
*/
|
|
|
|
|
public get isEnded(): boolean {
|
|
|
|
|
return this.turn === null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ゲームの勝者 (null = 引き分け)
|
|
|
|
|
*/
|
|
|
|
|
public get winner(): Color {
|
2018-12-17 10:10:38 +00:00
|
|
|
|
return this.isEnded ?
|
|
|
|
|
this.blackCount == this.whiteCount ? null :
|
|
|
|
|
this.opts.isLlotheo === this.blackCount > this.whiteCount ? WHITE : BLACK :
|
|
|
|
|
undefined;
|
2018-03-08 08:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|