This commit is contained in:
syuilo
2018-03-08 17:57:57 +09:00
parent 08c6c8b917
commit 155012846d
12 changed files with 956 additions and 457 deletions

View File

@@ -1,325 +0,0 @@
const BOARD_SIZE = 8;
export default class Othello {
public board: Array<'black' | 'white'>;
public stats: Array<{
b: number;
w: number;
}> = [];
/**
* ゲームを初期化します
*/
constructor() {
this.board = [
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, 'white', 'black', null, null, null,
null, null, null, 'black', 'white', null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null
];
this.stats.push({
b: 0.5,
w: 0.5
});
}
public prevPos = -1;
public get blackCount() {
return this.board.filter(s => s == 'black').length;
}
public get whiteCount() {
return this.board.filter(s => s == 'white').length;
}
public get blackP() {
return this.blackCount / (this.blackCount + this.whiteCount);
}
public get whiteP() {
return this.whiteCount / (this.blackCount + this.whiteCount);
}
public setByNumber(color, n) {
const ps = this.getPattern(color);
this.set2(color, ps[n][0], ps[n][1]);
}
private write(color, x, y) {
const pos = x + (y * 8);
this.board[pos] = color;
}
/**
* 石を配置します
*/
public set2(color, x, y) {
this.prevPos = x + (y * 8);
this.write(color, x, y);
const reverses = this.getReverse(color, x, y);
reverses.forEach(r => {
switch (r[0]) {
case 0: // 上
for (let c = 0, _y = y - 1; c < r[1]; c++, _y--) {
this.write(color, x, _y);
}
break;
case 1: // 右上
for (let c = 0, i = 1; c < r[1]; c++, i++) {
this.write(color, x + i, y - i);
}
break;
case 2: // 右
for (let c = 0, _x = x + 1; c < r[1]; c++, _x++) {
this.write(color, _x, y);
}
break;
case 3: // 右下
for (let c = 0, i = 1; c < r[1]; c++, i++) {
this.write(color, x + i, y + i);
}
break;
case 4: // 下
for (let c = 0, _y = y + 1; c < r[1]; c++, _y++) {
this.write(color, x, _y);
}
break;
case 5: // 左下
for (let c = 0, i = 1; c < r[1]; c++, i++) {
this.write(color, x - i, y + i);
}
break;
case 6: // 左
for (let c = 0, _x = x - 1; c < r[1]; c++, _x--) {
this.write(color, _x, y);
}
break;
case 7: // 左上
for (let c = 0, i = 1; c < r[1]; c++, i++) {
this.write(color, x - i, y - i);
}
break;
}
});
this.stats.push({
b: this.blackP,
w: this.whiteP
});
}
public set(color, pos) {
const x = pos % BOARD_SIZE;
const y = Math.floor(pos / BOARD_SIZE);
this.set2(color, x, y);
}
public get(x, y) {
const pos = x + (y * 8);
return this.board[pos];
}
/**
* 打つことができる場所を取得します
*/
public getPattern(myColor): number[][] {
const result = [];
this.board.forEach((stone, i) => {
if (stone != null) return;
const x = i % BOARD_SIZE;
const y = Math.floor(i / BOARD_SIZE);
if (this.canReverse2(myColor, x, y)) result.push([x, y]);
});
return result;
}
/**
* 指定の位置に石を打つことができるかどうか(相手の石を1つでも反転させられるか)を取得します
*/
public canReverse2(myColor, x, y): boolean {
return this.canReverse(myColor, x + (y * 8));
}
public canReverse(myColor, pos): boolean {
if (this.board[pos] != null) return false;
const x = pos % BOARD_SIZE;
const y = Math.floor(pos / BOARD_SIZE);
return this.getReverse(myColor, x, y) !== null;
}
private getReverse(myColor, targetx, targety): number[] {
const opponentColor = myColor == 'black' ? 'white' : 'black';
const createIterater = () => {
let opponentStoneFound = false;
let breaked = false;
return (x, y): any => {
if (breaked) {
return;
} else if (this.get(x, y) == myColor && opponentStoneFound) {
return true;
} else if (this.get(x, y) == myColor && !opponentStoneFound) {
breaked = true;
} else if (this.get(x, y) == opponentColor) {
opponentStoneFound = true;
} else {
breaked = true;
}
};
};
const res = [];
let iterate;
// 上
iterate = createIterater();
for (let c = 0, y = targety - 1; y >= 0; c++, y--) {
if (iterate(targetx, y)) {
res.push([0, c]);
break;
}
}
// 右上
iterate = createIterater();
for (let c = 0, i = 1; i <= Math.min(BOARD_SIZE - targetx, targety); c++, i++) {
if (iterate(targetx + i, targety - i)) {
res.push([1, c]);
break;
}
}
// 右
iterate = createIterater();
for (let c = 0, x = targetx + 1; x < BOARD_SIZE; c++, x++) {
if (iterate(x, targety)) {
res.push([2, c]);
break;
}
}
// 右下
iterate = createIterater();
for (let c = 0, i = 1; i < Math.min(BOARD_SIZE - targetx, BOARD_SIZE - targety); c++, i++) {
if (iterate(targetx + i, targety + i)) {
res.push([3, c]);
break;
}
}
// 下
iterate = createIterater();
for (let c = 0, y = targety + 1; y < BOARD_SIZE; c++, y++) {
if (iterate(targetx, y)) {
res.push([4, c]);
break;
}
}
// 左下
iterate = createIterater();
for (let c = 0, i = 1; i <= Math.min(targetx, BOARD_SIZE - targety); c++, i++) {
if (iterate(targetx - i, targety + i)) {
res.push([5, c]);
break;
}
}
// 左
iterate = createIterater();
for (let c = 0, x = targetx - 1; x >= 0; c++, x--) {
if (iterate(x, targety)) {
res.push([6, c]);
break;
}
}
// 左上
iterate = createIterater();
for (let c = 0, i = 1; i <= Math.min(targetx, targety); c++, i++) {
if (iterate(targetx - i, targety - i)) {
res.push([7, c]);
break;
}
}
return res.length === 0 ? null : res;
}
public toString(): string {
//return this.board.map(row => row.map(state => state === 'black' ? '●' : state === 'white' ? '○' : '┼').join('')).join('\n');
//return this.board.map(row => row.map(state => state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : '🔹').join('')).join('\n');
return 'wip';
}
public toPatternString(color): string {
//const num = ['', '', '', '', '', '', '', '', '', ''];
/*const num = ['0⃣', '1⃣', '2⃣', '3⃣', '4⃣', '5⃣', '6⃣', '7⃣', '8⃣', '9⃣', '🔟', '🍏', '🍎', '🍐', '🍊', '🍋', '🍌', '🍉', '🍇', '🍓', '🍈', '🍒', '🍑', '🍍'];
const pattern = this.getPattern(color);
return this.board.map((row, y) => row.map((state, x) => {
const i = pattern.findIndex(p => p[0] == x && p[1] == y);
//return state === 'black' ? '●' : state === 'white' ? '○' : i != -1 ? num[i] : '┼';
return state === 'black' ? '⚫️' : state === 'white' ? '⚪️' : i != -1 ? num[i] : '🔹';
}).join('')).join('\n');*/
return 'wip';
}
}
export function ai(color: string, othello: Othello) {
const opponentColor = color == 'black' ? 'white' : 'black';
function think() {
// 打てる場所を取得
const ps = othello.getPattern(color);
if (ps.length > 0) { // 打てる場所がある場合
// 角を取得
const corners = ps.filter(p =>
// 左上
(p[0] == 0 && p[1] == 0) ||
// 右上
(p[0] == (BOARD_SIZE - 1) && p[1] == 0) ||
// 右下
(p[0] == (BOARD_SIZE - 1) && p[1] == (BOARD_SIZE - 1)) ||
// 左下
(p[0] == 0 && p[1] == (BOARD_SIZE - 1))
);
if (corners.length > 0) { // どこかしらの角に打てる場合
// 打てる角からランダムに選択して打つ
const p = corners[Math.floor(Math.random() * corners.length)];
othello.set(color, p[0], p[1]);
} else { // 打てる角がない場合
// 打てる場所からランダムに選択して打つ
const p = ps[Math.floor(Math.random() * ps.length)];
othello.set(color, p[0], p[1]);
}
// 相手の打つ場所がない場合続けてAIのターン
if (othello.getPattern(opponentColor).length === 0) {
think();
}
}
}
think();
}

42
src/common/othello/ai.ts Normal file
View File

@@ -0,0 +1,42 @@
import Othello, { Color } from './core';
export function ai(color: Color, othello: Othello) {
//const opponentColor = color == 'black' ? 'white' : 'black';
/* wip
function think() {
// 打てる場所を取得
const ps = othello.canPutSomewhere(color);
if (ps.length > 0) { // 打てる場所がある場合
// 角を取得
const corners = ps.filter(p =>
// 左上
(p[0] == 0 && p[1] == 0) ||
// 右上
(p[0] == (BOARD_SIZE - 1) && p[1] == 0) ||
// 右下
(p[0] == (BOARD_SIZE - 1) && p[1] == (BOARD_SIZE - 1)) ||
// 左下
(p[0] == 0 && p[1] == (BOARD_SIZE - 1))
);
if (corners.length > 0) { // どこかしらの角に打てる場合
// 打てる角からランダムに選択して打つ
const p = corners[Math.floor(Math.random() * corners.length)];
othello.set(color, p[0], p[1]);
} else { // 打てる角がない場合
// 打てる場所からランダムに選択して打つ
const p = ps[Math.floor(Math.random() * ps.length)];
othello.set(color, p[0], p[1]);
}
// 相手の打つ場所がない場合続けてAIのターン
if (othello.getPattern(opponentColor).length === 0) {
think();
}
}
}
think();*/
}

239
src/common/othello/core.ts Normal file
View File

@@ -0,0 +1,239 @@
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';
}
}
}

217
src/common/othello/maps.ts Normal file
View File

@@ -0,0 +1,217 @@
/**
* 組み込みマップ定義
*
* データ値:
* (スペース) ... マス無し
* - ... マス
* b ... 初期配置される黒石
* w ... 初期配置される白石
*/
export type Map = {
name?: string;
size: number;
data: string;
};
export const fourfour: Map = {
name: '4x4',
size: 4,
data:
'----' +
'-wb-' +
'-bw-' +
'----'
};
export const sixsix: Map = {
name: '6x6',
size: 6,
data:
'------' +
'------' +
'--wb--' +
'--bw--' +
'------' +
'------'
};
export const eighteight: Map = {
name: '8x8',
size: 8,
data:
'--------' +
'--------' +
'--------' +
'---wb---' +
'---bw---' +
'--------' +
'--------' +
'--------'
};
export const roundedEighteight: Map = {
name: '8x8 rounded',
size: 8,
data:
' ------ ' +
'--------' +
'--------' +
'---wb---' +
'---bw---' +
'--------' +
'--------' +
' ------ '
};
export const roundedEighteight2: Map = {
name: '8x8 rounded 2',
size: 8,
data:
' ---- ' +
' ------ ' +
'--------' +
'---wb---' +
'---bw---' +
'--------' +
' ------ ' +
' ---- '
};
export const eighteightWithNotch: Map = {
name: '8x8 with notch',
size: 8,
data:
'--- ---' +
'--------' +
'--------' +
' --wb-- ' +
' --bw-- ' +
'--------' +
'--------' +
'--- ---'
};
export const eighteightWithSomeHoles: Map = {
name: '8x8 with some holes',
size: 8,
data:
'--- ----' +
'----- --' +
'-- -----' +
'---wb---' +
'---bw- -' +
' -------' +
'--- ----' +
'--------'
};
export const sixeight: Map = {
name: '6x8',
size: 8,
data:
' ------ ' +
' ------ ' +
' ------ ' +
' --wb-- ' +
' --bw-- ' +
' ------ ' +
' ------ ' +
' ------ '
};
export const tenthtenth: Map = {
name: '10x10',
size: 10,
data:
'----------' +
'----------' +
'----------' +
'----------' +
'----wb----' +
'----bw----' +
'----------' +
'----------' +
'----------' +
'----------'
};
export const hole: Map = {
name: 'hole',
size: 10,
data:
'----------' +
'----------' +
'--wb--wb--' +
'--bw--bw--' +
'---- ----' +
'---- ----' +
'--wb--wb--' +
'--bw--bw--' +
'----------' +
'----------'
};
export const spark: Map = {
name: 'spark',
size: 10,
data:
' - - ' +
'----------' +
' -------- ' +
' -------- ' +
' ---wb--- ' +
' ---bw--- ' +
' -------- ' +
' -------- ' +
'----------' +
' - - '
};
export const islands: Map = {
name: 'islands',
size: 10,
data:
'-------- ' +
'---wb--- ' +
'---bw--- ' +
'-------- ' +
' - - ' +
' - - ' +
' --------' +
' ---bw---' +
' ---wb---' +
' --------'
};
export const grid: Map = {
name: 'grid',
size: 10,
data:
'----------' +
'- - -- - -' +
'----------' +
'- - -- - -' +
'----wb----' +
'----bw----' +
'- - -- - -' +
'----------' +
'- - -- - -' +
'----------'
};
export const iphonex: Map = {
name: 'iPhone X',
size: 10,
data:
' -- -- ' +
' -------- ' +
' -------- ' +
' -------- ' +
' ---wb--- ' +
' ---bw--- ' +
' -------- ' +
' -------- ' +
' -------- ' +
' ------ '
};