2018-03-12 11:49:54 -05:00
|
|
|
/**
|
|
|
|
* true ... 黒
|
|
|
|
* false ... 白
|
|
|
|
*/
|
|
|
|
export type Color = boolean;
|
|
|
|
const BLACK = true;
|
|
|
|
const WHITE = false;
|
|
|
|
|
2018-03-08 02:57:57 -06:00
|
|
|
export type MapPixel = 'null' | 'empty';
|
|
|
|
|
|
|
|
export type Options = {
|
|
|
|
isLlotheo: boolean;
|
2018-03-10 03:22:54 -06:00
|
|
|
canPutEverywhere: boolean;
|
2018-03-10 06:23:00 -06:00
|
|
|
loopedBoard: boolean;
|
2018-03-08 02:57:57 -06:00
|
|
|
};
|
|
|
|
|
2018-03-12 11:49:54 -05:00
|
|
|
export type Undo = {
|
|
|
|
/**
|
|
|
|
* 色
|
|
|
|
*/
|
|
|
|
color: Color,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* どこに打ったか
|
|
|
|
*/
|
|
|
|
pos: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 反転した石の位置の配列
|
|
|
|
*/
|
|
|
|
effects: number[];
|
2018-03-12 13:22:40 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ターン
|
|
|
|
*/
|
|
|
|
turn: Color;
|
2018-03-12 11:49:54 -05:00
|
|
|
};
|
|
|
|
|
2018-03-08 02:57:57 -06:00
|
|
|
/**
|
2018-06-16 18:10:54 -05:00
|
|
|
* リバーシエンジン
|
2018-03-08 02:57:57 -06:00
|
|
|
*/
|
2018-06-16 18:10:54 -05:00
|
|
|
export default class Reversi {
|
2018-03-09 03:11:10 -06:00
|
|
|
public map: MapPixel[];
|
|
|
|
public mapWidth: number;
|
|
|
|
public mapHeight: number;
|
2018-03-08 02:57:57 -06:00
|
|
|
public board: Color[];
|
2018-03-12 11:49:54 -05:00
|
|
|
public turn: Color = BLACK;
|
2018-03-08 02:57:57 -06:00
|
|
|
public opts: Options;
|
|
|
|
|
2018-03-09 03:11:10 -06:00
|
|
|
public prevPos = -1;
|
2018-03-12 11:49:54 -05:00
|
|
|
public prevColor: Color = null;
|
2018-03-08 02:57:57 -06:00
|
|
|
|
2018-03-15 00:09:38 -05:00
|
|
|
private logs: Undo[] = [];
|
|
|
|
|
2018-03-08 02:57:57 -06:00
|
|
|
/**
|
|
|
|
* ゲームを初期化します
|
|
|
|
*/
|
2018-03-09 03:11:10 -06:00
|
|
|
constructor(map: string[], opts: Options) {
|
2018-03-12 11:49:54 -05:00
|
|
|
//#region binds
|
|
|
|
this.put = this.put.bind(this);
|
|
|
|
//#endregion
|
|
|
|
|
2018-03-10 03:22:54 -06:00
|
|
|
//#region Options
|
2018-03-08 02:57:57 -06:00
|
|
|
this.opts = opts;
|
2018-03-10 03:22:54 -06:00
|
|
|
if (this.opts.isLlotheo == null) this.opts.isLlotheo = false;
|
|
|
|
if (this.opts.canPutEverywhere == null) this.opts.canPutEverywhere = false;
|
2018-03-10 06:23:00 -06:00
|
|
|
if (this.opts.loopedBoard == null) this.opts.loopedBoard = false;
|
2018-03-10 03:22:54 -06:00
|
|
|
//#endregion
|
2018-03-08 02:57:57 -06:00
|
|
|
|
2018-03-10 03:22:54 -06:00
|
|
|
//#region Parse map data
|
2018-03-09 03:11:10 -06:00
|
|
|
this.mapWidth = map[0].length;
|
|
|
|
this.mapHeight = map.length;
|
|
|
|
const mapData = map.join('');
|
|
|
|
|
|
|
|
this.board = mapData.split('').map(d => {
|
2018-03-08 02:57:57 -06:00
|
|
|
if (d == '-') return null;
|
2018-03-12 11:49:54 -05:00
|
|
|
if (d == 'b') return BLACK;
|
|
|
|
if (d == 'w') return WHITE;
|
2018-03-08 02:57:57 -06:00
|
|
|
return undefined;
|
|
|
|
});
|
2018-03-10 03:22:54 -06:00
|
|
|
|
2018-03-09 03:11:10 -06:00
|
|
|
this.map = mapData.split('').map(d => {
|
2018-03-08 02:57:57 -06:00
|
|
|
if (d == '-' || d == 'b' || d == 'w') return 'empty';
|
|
|
|
return 'null';
|
|
|
|
});
|
2018-03-10 03:22:54 -06:00
|
|
|
//#endregion
|
2018-03-08 02:57:57 -06:00
|
|
|
|
2018-03-09 22:07:17 -06:00
|
|
|
// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
|
2018-03-12 11:49:54 -05:00
|
|
|
if (this.canPutSomewhere(BLACK).length == 0) {
|
|
|
|
if (this.canPutSomewhere(WHITE).length == 0) {
|
2018-03-09 22:07:17 -06:00
|
|
|
this.turn = null;
|
|
|
|
} else {
|
2018-03-12 11:49:54 -05:00
|
|
|
this.turn = WHITE;
|
2018-03-09 22:07:17 -06:00
|
|
|
}
|
|
|
|
}
|
2018-03-08 02:57:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 黒石の数
|
|
|
|
*/
|
|
|
|
public get blackCount() {
|
2018-03-12 11:49:54 -05:00
|
|
|
return this.board.filter(x => x === BLACK).length;
|
2018-03-08 02:57:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 白石の数
|
|
|
|
*/
|
|
|
|
public get whiteCount() {
|
2018-03-12 11:49:54 -05:00
|
|
|
return this.board.filter(x => x === WHITE).length;
|
2018-03-08 02:57:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 黒石の比率
|
|
|
|
*/
|
|
|
|
public get blackP() {
|
2018-03-09 21:48:41 -06:00
|
|
|
if (this.blackCount == 0 && this.whiteCount == 0) return 0;
|
2018-03-08 02:57:57 -06:00
|
|
|
return this.blackCount / (this.blackCount + this.whiteCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 白石の比率
|
|
|
|
*/
|
|
|
|
public get whiteP() {
|
2018-03-09 21:48:41 -06:00
|
|
|
if (this.blackCount == 0 && this.whiteCount == 0) return 0;
|
2018-03-08 02:57:57 -06:00
|
|
|
return this.whiteCount / (this.blackCount + this.whiteCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public transformPosToXy(pos: number): number[] {
|
2018-03-09 03:11:10 -06:00
|
|
|
const x = pos % this.mapWidth;
|
2018-03-09 12:01:01 -06:00
|
|
|
const y = Math.floor(pos / this.mapWidth);
|
2018-03-08 02:57:57 -06:00
|
|
|
return [x, y];
|
|
|
|
}
|
|
|
|
|
|
|
|
public transformXyToPos(x: number, y: number): number {
|
2018-03-09 12:01:01 -06:00
|
|
|
return x + (y * this.mapWidth);
|
2018-03-08 02:57:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定のマスに石を打ちます
|
|
|
|
* @param color 石の色
|
|
|
|
* @param pos 位置
|
|
|
|
*/
|
2018-03-15 00:09:38 -05:00
|
|
|
public put(color: Color, pos: number) {
|
2018-03-08 02:57:57 -06:00
|
|
|
this.prevPos = pos;
|
2018-03-12 11:49:54 -05:00
|
|
|
this.prevColor = color;
|
2018-03-12 13:22:40 -05:00
|
|
|
|
|
|
|
this.board[pos] = color;
|
2018-03-08 02:57:57 -06:00
|
|
|
|
|
|
|
// 反転させられる石を取得
|
2018-03-12 11:49:54 -05:00
|
|
|
const effects = this.effects(color, pos);
|
2018-03-08 02:57:57 -06:00
|
|
|
|
|
|
|
// 反転させる
|
2018-03-12 13:22:40 -05:00
|
|
|
for (const pos of effects) {
|
|
|
|
this.board[pos] = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
const turn = this.turn;
|
2018-03-08 02:57:57 -06:00
|
|
|
|
2018-03-15 00:09:38 -05:00
|
|
|
this.logs.push({
|
2018-03-12 11:49:54 -05:00
|
|
|
color,
|
|
|
|
pos,
|
2018-03-12 13:22:40 -05:00
|
|
|
effects,
|
|
|
|
turn
|
2018-03-15 00:09:38 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.calcTurn();
|
2018-03-12 11:49:54 -05:00
|
|
|
}
|
2018-03-08 02:57:57 -06:00
|
|
|
|
2018-03-12 11:49:54 -05:00
|
|
|
private calcTurn() {
|
2018-03-08 02:57:57 -06:00
|
|
|
// ターン計算
|
2018-03-12 13:22:40 -05:00
|
|
|
if (this.canPutSomewhere(!this.prevColor).length > 0) {
|
|
|
|
this.turn = !this.prevColor;
|
2018-03-12 11:49:54 -05:00
|
|
|
} else if (this.canPutSomewhere(this.prevColor).length > 0) {
|
2018-03-12 13:22:40 -05:00
|
|
|
this.turn = this.prevColor;
|
2018-03-08 02:57:57 -06:00
|
|
|
} else {
|
|
|
|
this.turn = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 00:09:38 -05:00
|
|
|
public undo() {
|
|
|
|
const undo = this.logs.pop();
|
2018-03-12 11:49:54 -05:00
|
|
|
this.prevColor = undo.color;
|
|
|
|
this.prevPos = undo.pos;
|
2018-03-12 13:22:40 -05:00
|
|
|
this.board[undo.pos] = null;
|
2018-03-12 11:49:54 -05:00
|
|
|
for (const pos of undo.effects) {
|
|
|
|
const color = this.board[pos];
|
2018-03-12 13:22:40 -05:00
|
|
|
this.board[pos] = !color;
|
2018-03-12 11:49:54 -05:00
|
|
|
}
|
2018-03-12 13:22:40 -05:00
|
|
|
this.turn = undo.turn;
|
2018-03-08 02:57:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定した位置のマップデータのマスを取得します
|
|
|
|
* @param pos 位置
|
|
|
|
*/
|
|
|
|
public mapDataGet(pos: number): MapPixel {
|
2018-03-11 07:48:16 -05:00
|
|
|
const [x, y] = this.transformPosToXy(pos);
|
|
|
|
if (x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight) return 'null';
|
2018-03-09 03:11:10 -06:00
|
|
|
return this.map[pos];
|
2018-03-08 02:57:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 打つことができる場所を取得します
|
|
|
|
*/
|
|
|
|
public canPutSomewhere(color: Color): number[] {
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
this.board.forEach((x, i) => {
|
|
|
|
if (this.canPut(color, i)) result.push(i);
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-10 03:22:54 -06:00
|
|
|
* 指定のマスに石を打つことができるかどうかを取得します
|
2018-03-08 02:57:57 -06:00
|
|
|
* @param color 自分の色
|
|
|
|
* @param pos 位置
|
|
|
|
*/
|
|
|
|
public canPut(color: Color, pos: number): boolean {
|
|
|
|
// 既に石が置いてある場所には打てない
|
2018-03-12 13:22:40 -05:00
|
|
|
if (this.board[pos] !== null) return false;
|
2018-03-10 03:22:54 -06:00
|
|
|
|
|
|
|
if (this.opts.canPutEverywhere) {
|
|
|
|
// 挟んでなくても置けるモード
|
|
|
|
return this.mapDataGet(pos) == 'empty';
|
|
|
|
} else {
|
|
|
|
// 相手の石を1つでも反転させられるか
|
|
|
|
return this.effects(color, pos).length !== 0;
|
|
|
|
}
|
2018-03-08 02:57:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定のマスに石を置いた時の、反転させられる石を取得します
|
|
|
|
* @param color 自分の色
|
|
|
|
* @param pos 位置
|
|
|
|
*/
|
2018-03-12 11:49:54 -05:00
|
|
|
public effects(color: Color, pos: number): number[] {
|
|
|
|
const enemyColor = !color;
|
2018-03-10 06:23:00 -06:00
|
|
|
|
|
|
|
// ひっくり返せる石(の位置)リスト
|
2018-03-08 02:57:57 -06:00
|
|
|
let stones = [];
|
|
|
|
|
2018-03-10 06:23:00 -06:00
|
|
|
const initPos = pos;
|
|
|
|
|
|
|
|
// 走査
|
2018-03-08 02:57:57 -06:00
|
|
|
const iterate = (fn: (i: number) => number[]) => {
|
|
|
|
let i = 1;
|
|
|
|
const found = [];
|
2018-03-10 06:23:00 -06:00
|
|
|
|
2018-03-08 02:57:57 -06:00
|
|
|
while (true) {
|
2018-03-10 06:23:00 -06:00
|
|
|
let [x, y] = fn(i);
|
|
|
|
|
|
|
|
// 座標が指し示す位置がボード外に出たとき
|
|
|
|
if (this.opts.loopedBoard) {
|
2018-03-12 11:49:54 -05:00
|
|
|
if (x < 0 ) x = this.mapWidth - ((-x) % this.mapWidth);
|
2018-03-10 06:48:20 -06:00
|
|
|
if (y < 0 ) y = this.mapHeight - ((-y) % this.mapHeight);
|
|
|
|
if (x >= this.mapWidth ) x = x % this.mapWidth;
|
|
|
|
if (y >= this.mapHeight) y = y % this.mapHeight;
|
|
|
|
|
|
|
|
// for debug
|
|
|
|
//if (x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight) {
|
|
|
|
// console.log(x, y);
|
|
|
|
//}
|
2018-03-10 06:23:00 -06:00
|
|
|
|
|
|
|
// 一周して自分に帰ってきたら
|
2018-03-10 07:23:14 -06:00
|
|
|
if (this.transformXyToPos(x, y) == initPos) {
|
|
|
|
// ↓のコメントアウトを外すと、「現時点で自分の石が隣接していないが、
|
|
|
|
// そこに置いたとするとループして最終的に挟んだことになる」というケースを有効化します。(Test4のマップで違いが分かります)
|
|
|
|
// このケースを有効にした方が良いのか無効にした方が良いのか判断がつかなかったためとりあえず無効としておきます
|
|
|
|
// (あと無効な方がゲームとしておもしろそうだった)
|
2018-03-10 11:17:41 -06:00
|
|
|
stones = stones.concat(found);
|
2018-03-10 07:23:14 -06:00
|
|
|
break;
|
|
|
|
}
|
2018-03-10 06:23:00 -06:00
|
|
|
} else {
|
|
|
|
if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight) break;
|
|
|
|
}
|
|
|
|
|
2018-03-08 02:57:57 -06:00
|
|
|
const pos = this.transformXyToPos(x, y);
|
2018-03-10 06:23:00 -06:00
|
|
|
|
|
|
|
//#region 「配置不能」マスに当たった場合走査終了
|
2018-03-08 02:57:57 -06:00
|
|
|
const pixel = this.mapDataGet(pos);
|
|
|
|
if (pixel == 'null') break;
|
2018-03-10 06:23:00 -06:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
// 石取得
|
2018-03-12 13:22:40 -05:00
|
|
|
const stone = this.board[pos];
|
2018-03-10 06:23:00 -06:00
|
|
|
|
|
|
|
// 石が置かれていないマスなら走査終了
|
2018-03-12 11:49:54 -05:00
|
|
|
if (stone === null) break;
|
2018-03-10 06:23:00 -06:00
|
|
|
|
|
|
|
// 相手の石なら「ひっくり返せるかもリスト」に入れておく
|
2018-03-12 11:49:54 -05:00
|
|
|
if (stone === enemyColor) found.push(pos);
|
2018-03-10 06:23:00 -06:00
|
|
|
|
|
|
|
// 自分の石なら「ひっくり返せるかもリスト」を「ひっくり返せるリスト」に入れ、走査終了
|
2018-03-12 11:49:54 -05:00
|
|
|
if (stone === color) {
|
2018-03-08 02:57:57 -06:00
|
|
|
stones = stones.concat(found);
|
|
|
|
break;
|
|
|
|
}
|
2018-03-12 11:49:54 -05:00
|
|
|
|
2018-03-08 02:57:57 -06:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-10 06:23:00 -06:00
|
|
|
const [x, y] = this.transformPosToXy(pos);
|
|
|
|
|
2018-03-08 02:57:57 -06:00
|
|
|
iterate(i => [x , y - i]); // 上
|
|
|
|
iterate(i => [x + i, y - i]); // 右上
|
|
|
|
iterate(i => [x + i, y ]); // 右
|
|
|
|
iterate(i => [x + i, y + i]); // 右下
|
|
|
|
iterate(i => [x , y + i]); // 下
|
|
|
|
iterate(i => [x - i, y + i]); // 左下
|
|
|
|
iterate(i => [x - i, y ]); // 左
|
|
|
|
iterate(i => [x - i, y - i]); // 左上
|
|
|
|
|
|
|
|
return stones;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ゲームが終了したか否か
|
|
|
|
*/
|
|
|
|
public get isEnded(): boolean {
|
|
|
|
return this.turn === null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ゲームの勝者 (null = 引き分け)
|
|
|
|
*/
|
|
|
|
public get winner(): Color {
|
|
|
|
if (!this.isEnded) return undefined;
|
|
|
|
|
|
|
|
if (this.blackCount == this.whiteCount) return null;
|
|
|
|
|
|
|
|
if (this.opts.isLlotheo) {
|
2018-03-12 11:49:54 -05:00
|
|
|
return this.blackCount > this.whiteCount ? WHITE : BLACK;
|
2018-03-08 02:57:57 -06:00
|
|
|
} else {
|
2018-03-12 11:49:54 -05:00
|
|
|
return this.blackCount > this.whiteCount ? BLACK : WHITE;
|
2018-03-08 02:57:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|