Sharkey/src/common/othello/core.ts

299 lines
7.1 KiB
TypeScript
Raw Normal View History

2018-03-08 10:57:57 +02:00
export type Color = 'black' | 'white';
export type MapPixel = 'null' | 'empty';
export type Options = {
isLlotheo: boolean;
2018-03-10 11:22:54 +02:00
canPutEverywhere: boolean;
2018-03-10 14:23:00 +02:00
loopedBoard: boolean;
2018-03-08 10:57:57 +02:00
};
/**
*
*/
export default class Othello {
2018-03-09 11:11:10 +02:00
public map: MapPixel[];
public mapWidth: number;
public mapHeight: number;
2018-03-08 10:57:57 +02:00
public board: Color[];
public turn: Color = 'black';
public opts: Options;
2018-03-09 11:11:10 +02:00
public prevPos = -1;
2018-03-08 10:57:57 +02:00
public stats: Array<{
b: number;
w: number;
}>;
/**
*
*/
2018-03-09 11:11:10 +02:00
constructor(map: string[], opts: Options) {
2018-03-10 11:22:54 +02:00
//#region Options
2018-03-08 10:57:57 +02:00
this.opts = opts;
2018-03-10 11:22:54 +02:00
if (this.opts.isLlotheo == null) this.opts.isLlotheo = false;
if (this.opts.canPutEverywhere == null) this.opts.canPutEverywhere = false;
2018-03-10 14:23:00 +02:00
if (this.opts.loopedBoard == null) this.opts.loopedBoard = false;
2018-03-10 11:22:54 +02:00
//#endregion
2018-03-08 10:57:57 +02:00
2018-03-10 11:22:54 +02:00
//#region Parse map data
2018-03-09 11:11:10 +02:00
this.mapWidth = map[0].length;
this.mapHeight = map.length;
const mapData = map.join('');
this.board = mapData.split('').map(d => {
2018-03-08 10:57:57 +02:00
if (d == '-') return null;
if (d == 'b') return 'black';
if (d == 'w') return 'white';
return undefined;
});
2018-03-10 11:22:54 +02:00
2018-03-09 11:11:10 +02:00
this.map = mapData.split('').map(d => {
2018-03-08 10:57:57 +02:00
if (d == '-' || d == 'b' || d == 'w') return 'empty';
return 'null';
});
2018-03-10 11:22:54 +02:00
//#endregion
2018-03-08 10:57:57 +02:00
// Init stats
this.stats = [{
b: this.blackP,
w: this.whiteP
}];
2018-03-10 06:07:17 +02:00
// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
if (this.canPutSomewhere('black').length == 0) {
if (this.canPutSomewhere('white').length == 0) {
this.turn = null;
} else {
this.turn = 'white';
}
}
2018-03-08 10:57:57 +02:00
}
/**
*
*/
public get blackCount() {
return this.board.filter(x => x == 'black').length;
}
/**
*
*/
public get whiteCount() {
return this.board.filter(x => x == 'white').length;
}
/**
*
*/
public get blackP() {
2018-03-10 05:48:41 +02:00
if (this.blackCount == 0 && this.whiteCount == 0) return 0;
2018-03-08 10:57:57 +02:00
return this.blackCount / (this.blackCount + this.whiteCount);
}
/**
*
*/
public get whiteP() {
2018-03-10 05:48:41 +02:00
if (this.blackCount == 0 && this.whiteCount == 0) return 0;
2018-03-08 10:57:57 +02:00
return this.whiteCount / (this.blackCount + this.whiteCount);
}
public transformPosToXy(pos: number): number[] {
2018-03-09 11:11:10 +02:00
const x = pos % this.mapWidth;
2018-03-09 20:01:01 +02:00
const y = Math.floor(pos / this.mapWidth);
2018-03-08 10:57:57 +02:00
return [x, y];
}
public transformXyToPos(x: number, y: number): number {
2018-03-09 20:01:01 +02:00
return x + (y * this.mapWidth);
2018-03-08 10:57:57 +02:00
}
/**
*
* @param color
* @param pos
*/
private write(color: Color, pos: number) {
this.board[pos] = color;
}
/**
*
* @param color
* @param pos
*/
public put(color: Color, pos: number) {
if (!this.canPut(color, pos)) return;
this.prevPos = pos;
this.write(color, pos);
// 反転させられる石を取得
const reverses = this.effects(color, pos);
// 反転させる
reverses.forEach(pos => {
this.write(color, pos);
});
this.stats.push({
b: this.blackP,
w: this.whiteP
});
// ターン計算
const opColor = color == 'black' ? 'white' : 'black';
if (this.canPutSomewhere(opColor).length > 0) {
this.turn = color == 'black' ? 'white' : 'black';
} else if (this.canPutSomewhere(color).length > 0) {
this.turn = color == 'black' ? 'black' : 'white';
} else {
this.turn = null;
}
}
/**
*
* @param pos
*/
public get(pos: number) {
return this.board[pos];
}
/**
*
* @param pos
*/
public mapDataGet(pos: number): MapPixel {
2018-03-09 11:11:10 +02:00
if (pos < 0 || pos >= this.map.length) return 'null';
return this.map[pos];
2018-03-08 10:57:57 +02: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 11:22:54 +02:00
*
2018-03-08 10:57:57 +02:00
* @param color
* @param pos
*/
public canPut(color: Color, pos: number): boolean {
// 既に石が置いてある場所には打てない
if (this.get(pos) !== null) return false;
2018-03-10 11:22:54 +02:00
if (this.opts.canPutEverywhere) {
// 挟んでなくても置けるモード
return this.mapDataGet(pos) == 'empty';
} else {
// 相手の石を1つでも反転させられるか
return this.effects(color, pos).length !== 0;
}
2018-03-08 10:57:57 +02:00
}
/**
*
* @param color
* @param pos
*/
private effects(color: Color, pos: number): number[] {
const enemyColor = color == 'black' ? 'white' : 'black';
2018-03-10 14:23:00 +02:00
// ひっくり返せる石(の位置)リスト
2018-03-08 10:57:57 +02:00
let stones = [];
2018-03-10 14:23:00 +02:00
const initPos = pos;
// 走査
2018-03-08 10:57:57 +02:00
const iterate = (fn: (i: number) => number[]) => {
let i = 1;
const found = [];
2018-03-10 14:23:00 +02:00
2018-03-08 10:57:57 +02:00
while (true) {
2018-03-10 14:23:00 +02:00
let [x, y] = fn(i);
// 座標が指し示す位置がボード外に出たとき
if (this.opts.loopedBoard) {
if (x < 0 ) x = this.mapWidth - (-x);
if (y < 0 ) y = this.mapHeight - (-y);
if (x >= this.mapWidth ) x = x - this.mapWidth;
if (y >= this.mapHeight) y = y - this.mapHeight;
// 一周して自分に帰ってきたら
if (this.transformXyToPos(x, y) == initPos) break;
} else {
if (x == -1 || y == -1 || x == this.mapWidth || y == this.mapHeight) break;
}
2018-03-08 10:57:57 +02:00
const pos = this.transformXyToPos(x, y);
2018-03-10 14:23:00 +02:00
//#region 「配置不能」マスに当たった場合走査終了
2018-03-08 10:57:57 +02:00
const pixel = this.mapDataGet(pos);
if (pixel == 'null') break;
2018-03-10 14:23:00 +02:00
//#endregion
// 石取得
2018-03-08 10:57:57 +02:00
const stone = this.get(pos);
2018-03-10 14:23:00 +02:00
// 石が置かれていないマスなら走査終了
2018-03-08 10:57:57 +02:00
if (stone == null) break;
2018-03-10 14:23:00 +02:00
// 相手の石なら「ひっくり返せるかもリスト」に入れておく
2018-03-08 10:57:57 +02:00
if (stone == enemyColor) found.push(pos);
2018-03-10 14:23:00 +02:00
// 自分の石なら「ひっくり返せるかもリスト」を「ひっくり返せるリスト」に入れ、走査終了
2018-03-08 10:57:57 +02:00
if (stone == color) {
stones = stones.concat(found);
break;
}
i++;
}
};
2018-03-10 14:23:00 +02:00
const [x, y] = this.transformPosToXy(pos);
2018-03-08 10:57:57 +02: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) {
return this.blackCount > this.whiteCount ? 'white' : 'black';
} else {
return this.blackCount > this.whiteCount ? 'black' : 'white';
}
}
}