Sharkey/src/common/othello/core.ts

240 lines
5.2 KiB
TypeScript
Raw Normal View History

2018-03-08 10:57:57 +02:00
import { Map } from './maps';
export type Color = 'black' | 'white';
export type MapPixel = 'null' | 'empty';
export type Options = {
isLlotheo: boolean;
};
/**
*
*/
export default class Othello {
public map: Map;
public mapData: MapPixel[];
public board: Color[];
public turn: Color = 'black';
public opts: Options;
public stats: Array<{
b: number;
w: number;
}>;
/**
*
*/
constructor(map: Map, opts: Options) {
this.map = map;
this.opts = opts;
// Parse map data
this.board = this.map.data.split('').map(d => {
if (d == '-') return null;
if (d == 'b') return 'black';
if (d == 'w') return 'white';
return undefined;
});
this.mapData = this.map.data.split('').map(d => {
if (d == '-' || d == 'b' || d == 'w') return 'empty';
return 'null';
});
// Init stats
this.stats = [{
b: this.blackP,
w: this.whiteP
}];
}
public prevPos = -1;
/**
*
*/
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() {
return this.blackCount / (this.blackCount + this.whiteCount);
}
/**
*
*/
public get whiteP() {
return this.whiteCount / (this.blackCount + this.whiteCount);
}
public transformPosToXy(pos: number): number[] {
const x = pos % this.map.size;
const y = Math.floor(pos / this.map.size);
return [x, y];
}
public transformXyToPos(x: number, y: number): number {
return x + (y * this.map.size);
}
/**
*
* @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 {
if (pos < 0 || pos >= this.mapData.length) return 'null';
return this.mapData[pos];
}
/**
*
*/
public canPutSomewhere(color: Color): number[] {
const result = [];
this.board.forEach((x, i) => {
if (this.canPut(color, i)) result.push(i);
});
return result;
}
/**
* (1)
* @param color
* @param pos
*/
public canPut(color: Color, pos: number): boolean {
// 既に石が置いてある場所には打てない
if (this.get(pos) !== null) return false;
return this.effects(color, pos).length !== 0;
}
/**
*
* @param color
* @param pos
*/
private effects(color: Color, pos: number): number[] {
const enemyColor = color == 'black' ? 'white' : 'black';
const [x, y] = this.transformPosToXy(pos);
let stones = [];
const iterate = (fn: (i: number) => number[]) => {
let i = 1;
const found = [];
while (true) {
const [x, y] = fn(i);
if (x < 0 || y < 0 || x >= this.map.size || y >= this.map.size) break;
const pos = this.transformXyToPos(x, y);
const pixel = this.mapDataGet(pos);
if (pixel == 'null') break;
const stone = this.get(pos);
if (stone == null) break;
if (stone == enemyColor) found.push(pos);
if (stone == color) {
stones = stones.concat(found);
break;
}
i++;
}
};
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';
}
}
}