オセロで黒白を真理値で表現するように

This commit is contained in:
syuilo 2018-03-13 01:49:54 +09:00
parent 1d62003e08
commit e6f68e0b9e
4 changed files with 98 additions and 57 deletions

View file

@ -25,7 +25,11 @@ export interface IGame {
is_started: boolean; is_started: boolean;
is_ended: boolean; is_ended: boolean;
winner_id: mongo.ObjectID; winner_id: mongo.ObjectID;
logs: any[]; logs: Array<{
at: Date;
color: boolean;
pos: number;
}>;
settings: { settings: {
map: string[]; map: string[];
bw: string | number; bw: string | number;

View file

@ -214,9 +214,9 @@ export default function(request: websocket.request, connection: websocket.connec
if (o.isEnded) { if (o.isEnded) {
let winner; let winner;
if (o.winner == 'black') { if (o.winner === true) {
winner = freshGame.black == 1 ? freshGame.user1_id : freshGame.user2_id; winner = freshGame.black == 1 ? freshGame.user1_id : freshGame.user2_id;
} else if (o.winner == 'white') { } else if (o.winner === false) {
winner = freshGame.black == 1 ? freshGame.user2_id : freshGame.user1_id; winner = freshGame.black == 1 ? freshGame.user2_id : freshGame.user1_id;
} else { } else {
winner = null; winner = null;
@ -263,17 +263,17 @@ export default function(request: websocket.request, connection: websocket.connec
const myColor = const myColor =
(game.user1_id.equals(user._id) && game.black == 1) || (game.user2_id.equals(user._id) && game.black == 2) (game.user1_id.equals(user._id) && game.black == 1) || (game.user2_id.equals(user._id) && game.black == 2)
? 'black' ? true
: 'white'; : false;
if (!o.canPut(myColor, pos)) return; if (!o.canPut(myColor, pos)) return;
o.put(myColor, pos); o.put(myColor, pos);
let winner; let winner;
if (o.isEnded) { if (o.isEnded) {
if (o.winner == 'black') { if (o.winner === true) {
winner = game.black == 1 ? game.user1_id : game.user2_id; winner = game.black == 1 ? game.user1_id : game.user2_id;
} else if (o.winner == 'white') { } else if (o.winner === false) {
winner = game.black == 1 ? game.user2_id : game.user1_id; winner = game.black == 1 ? game.user2_id : game.user1_id;
} else { } else {
winner = null; winner = null;

View file

@ -1,4 +1,11 @@
export type Color = 'black' | 'white'; /**
* true ...
* false ...
*/
export type Color = boolean;
const BLACK = true;
const WHITE = false;
export type MapPixel = 'null' | 'empty'; export type MapPixel = 'null' | 'empty';
export type Options = { export type Options = {
@ -7,6 +14,23 @@ export type Options = {
loopedBoard: boolean; loopedBoard: boolean;
}; };
export type Undo = {
/**
*
*/
color: Color,
/**
*
*/
pos: number;
/**
*
*/
effects: number[];
};
/** /**
* *
*/ */
@ -15,19 +39,20 @@ export default class Othello {
public mapWidth: number; public mapWidth: number;
public mapHeight: number; public mapHeight: number;
public board: Color[]; public board: Color[];
public turn: Color = 'black'; public turn: Color = BLACK;
public opts: Options; public opts: Options;
public prevPos = -1; public prevPos = -1;
public stats: Array<{ public prevColor: Color = null;
b: number;
w: number;
}>;
/** /**
* *
*/ */
constructor(map: string[], opts: Options) { constructor(map: string[], opts: Options) {
//#region binds
this.put = this.put.bind(this);
//#endregion
//#region Options //#region Options
this.opts = opts; this.opts = opts;
if (this.opts.isLlotheo == null) this.opts.isLlotheo = false; if (this.opts.isLlotheo == null) this.opts.isLlotheo = false;
@ -42,8 +67,8 @@ export default class Othello {
this.board = mapData.split('').map(d => { this.board = mapData.split('').map(d => {
if (d == '-') return null; if (d == '-') return null;
if (d == 'b') return 'black'; if (d == 'b') return BLACK;
if (d == 'w') return 'white'; if (d == 'w') return WHITE;
return undefined; return undefined;
}); });
@ -53,18 +78,12 @@ export default class Othello {
}); });
//#endregion //#endregion
// Init stats
this.stats = [{
b: this.blackP,
w: this.whiteP
}];
// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある // ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
if (this.canPutSomewhere('black').length == 0) { if (this.canPutSomewhere(BLACK).length == 0) {
if (this.canPutSomewhere('white').length == 0) { if (this.canPutSomewhere(WHITE).length == 0) {
this.turn = null; this.turn = null;
} else { } else {
this.turn = 'white'; this.turn = WHITE;
} }
} }
} }
@ -73,14 +92,14 @@ export default class Othello {
* *
*/ */
public get blackCount() { public get blackCount() {
return this.board.filter(x => x == 'black').length; return this.board.filter(x => x === BLACK).length;
} }
/** /**
* *
*/ */
public get whiteCount() { public get whiteCount() {
return this.board.filter(x => x == 'white').length; return this.board.filter(x => x === WHITE).length;
} }
/** /**
@ -123,36 +142,53 @@ export default class Othello {
* @param color * @param color
* @param pos * @param pos
*/ */
public put(color: Color, pos: number) { public put(color: Color, pos: number, fast = false): Undo {
if (!this.canPut(color, pos)) return; if (!fast && !this.canPut(color, pos)) return null;
this.prevPos = pos; this.prevPos = pos;
this.prevColor = color;
this.write(color, pos); this.write(color, pos);
// 反転させられる石を取得 // 反転させられる石を取得
const reverses = this.effects(color, pos); const effects = this.effects(color, pos);
// 反転させる // 反転させる
reverses.forEach(pos => { effects.forEach(pos => {
this.write(color, pos); this.write(color, pos);
}); });
this.stats.push({ this.calcTurn();
b: this.blackP,
w: this.whiteP
});
return {
color,
pos,
effects
};
}
private calcTurn() {
// ターン計算 // ターン計算
const opColor = color == 'black' ? 'white' : 'black'; const opColor = this.prevColor === BLACK ? WHITE : BLACK;
if (this.canPutSomewhere(opColor).length > 0) { if (this.canPutSomewhere(opColor).length > 0) {
this.turn = color == 'black' ? 'white' : 'black'; this.turn = this.prevColor === BLACK ? WHITE : BLACK;
} else if (this.canPutSomewhere(color).length > 0) { } else if (this.canPutSomewhere(this.prevColor).length > 0) {
this.turn = color == 'black' ? 'black' : 'white'; this.turn = this.prevColor === BLACK ? BLACK : WHITE;
} else { } else {
this.turn = null; this.turn = null;
} }
} }
public undo(undo: Undo) {
this.prevColor = undo.color;
this.prevPos = undo.pos;
this.write(null, undo.pos);
for (const pos of undo.effects) {
const color = this.board[pos];
this.write(!color, pos);
}
this.calcTurn();
}
/** /**
* *
* @param pos * @param pos
@ -207,8 +243,8 @@ export default class Othello {
* @param color * @param color
* @param pos * @param pos
*/ */
private effects(color: Color, pos: number): number[] { public effects(color: Color, pos: number): number[] {
const enemyColor = color == 'black' ? 'white' : 'black'; const enemyColor = !color;
// ひっくり返せる石(の位置)リスト // ひっくり返せる石(の位置)リスト
let stones = []; let stones = [];
@ -259,16 +295,17 @@ export default class Othello {
const stone = this.get(pos); const stone = this.get(pos);
// 石が置かれていないマスなら走査終了 // 石が置かれていないマスなら走査終了
if (stone == null) break; if (stone === null) break;
// 相手の石なら「ひっくり返せるかもリスト」に入れておく // 相手の石なら「ひっくり返せるかもリスト」に入れておく
if (stone == enemyColor) found.push(pos); if (stone === enemyColor) found.push(pos);
// 自分の石なら「ひっくり返せるかもリスト」を「ひっくり返せるリスト」に入れ、走査終了 // 自分の石なら「ひっくり返せるかもリスト」を「ひっくり返せるリスト」に入れ、走査終了
if (stone == color) { if (stone === color) {
stones = stones.concat(found); stones = stones.concat(found);
break; break;
} }
i++; i++;
} }
}; };
@ -303,9 +340,9 @@ export default class Othello {
if (this.blackCount == this.whiteCount) return null; if (this.blackCount == this.whiteCount) return null;
if (this.opts.isLlotheo) { if (this.opts.isLlotheo) {
return this.blackCount > this.whiteCount ? 'white' : 'black'; return this.blackCount > this.whiteCount ? WHITE : BLACK;
} else { } else {
return this.blackCount > this.whiteCount ? 'black' : 'white'; return this.blackCount > this.whiteCount ? BLACK : WHITE;
} }
} }
} }

View file

@ -60,13 +60,13 @@ export default Vue.extend({
}, },
myColor(): Color { myColor(): Color {
if (!this.iAmPlayer) return null; if (!this.iAmPlayer) return null;
if (this.game.user1_id == (this as any).os.i.id && this.game.black == 1) return 'black'; if (this.game.user1_id == (this as any).os.i.id && this.game.black == 1) return true;
if (this.game.user2_id == (this as any).os.i.id && this.game.black == 2) return 'black'; if (this.game.user2_id == (this as any).os.i.id && this.game.black == 2) return true;
return 'white'; return false;
}, },
opColor(): Color { opColor(): Color {
if (!this.iAmPlayer) return null; if (!this.iAmPlayer) return null;
return this.myColor == 'black' ? 'white' : 'black'; return this.myColor === true ? false : true;
}, },
blackUser(): any { blackUser(): any {
return this.game.black == 1 ? this.game.user1 : this.game.user2; return this.game.black == 1 ? this.game.user1 : this.game.user2;
@ -75,9 +75,9 @@ export default Vue.extend({
return this.game.black == 1 ? this.game.user2 : this.game.user1; return this.game.black == 1 ? this.game.user2 : this.game.user1;
}, },
turnUser(): any { turnUser(): any {
if (this.o.turn == 'black') { if (this.o.turn === true) {
return this.game.black == 1 ? this.game.user1 : this.game.user2; return this.game.black == 1 ? this.game.user1 : this.game.user2;
} else if (this.o.turn == 'white') { } else if (this.o.turn === false) {
return this.game.black == 1 ? this.game.user2 : this.game.user1; return this.game.black == 1 ? this.game.user2 : this.game.user1;
} else { } else {
return null; return null;
@ -99,7 +99,7 @@ export default Vue.extend({
}); });
this.logs.forEach((log, i) => { this.logs.forEach((log, i) => {
if (i < v) { if (i < v) {
this.o.put(log.color, log.pos); this.o.put(log.color, log.pos, true);
} }
}); });
this.$forceUpdate(); this.$forceUpdate();
@ -116,7 +116,7 @@ export default Vue.extend({
}); });
this.game.logs.forEach(log => { this.game.logs.forEach(log => {
this.o.put(log.color, log.pos); this.o.put(log.color, log.pos, true);
}); });
this.logs = this.game.logs; this.logs = this.game.logs;
@ -190,10 +190,10 @@ export default Vue.extend({
checkEnd() { checkEnd() {
this.game.is_ended = this.o.isEnded; this.game.is_ended = this.o.isEnded;
if (this.game.is_ended) { if (this.game.is_ended) {
if (this.o.winner == 'black') { if (this.o.winner === true) {
this.game.winner_id = this.game.black == 1 ? this.game.user1_id : this.game.user2_id; this.game.winner_id = this.game.black == 1 ? this.game.user1_id : this.game.user2_id;
this.game.winner = this.game.black == 1 ? this.game.user1 : this.game.user2; this.game.winner = this.game.black == 1 ? this.game.user1 : this.game.user2;
} else if (this.o.winner == 'white') { } else if (this.o.winner === false) {
this.game.winner_id = this.game.black == 1 ? this.game.user2_id : this.game.user1_id; this.game.winner_id = this.game.black == 1 ? this.game.user2_id : this.game.user1_id;
this.game.winner = this.game.black == 1 ? this.game.user2 : this.game.user1; this.game.winner = this.game.black == 1 ? this.game.user2 : this.game.user1;
} else { } else {
@ -214,7 +214,7 @@ export default Vue.extend({
}); });
this.game.logs.forEach(log => { this.game.logs.forEach(log => {
this.o.put(log.color, log.pos); this.o.put(log.color, log.pos, true);
}); });
this.logs = this.game.logs; this.logs = this.game.logs;