Skip to content

Commit

Permalink
リバーシ 復活
Browse files Browse the repository at this point in the history
  • Loading branch information
remitocat committed Feb 2, 2022
1 parent 0e16b73 commit 571f0cd
Show file tree
Hide file tree
Showing 38 changed files with 5,224 additions and 3 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
### Changes
- Room機能が削除されました
- 後日別リポジトリとして復活予定です
- リバーシ機能が削除されました
- 後日別リポジトリとして復活予定です
- Chat UIが削除されました
- ノートに添付できるファイルの数が16に増えました
- カスタム絵文字にSVGを指定した場合、PNGに変換されて表示されるようになりました
Expand Down
38 changes: 38 additions & 0 deletions locales/ja-JP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ uploadFromUrlDescription: "アップロードしたいファイルのURL"
uploadFromUrlRequested: "アップロードをリクエストしました"
uploadFromUrlMayTakeTime: "アップロードが完了するまで時間がかかる場合があります。"
explore: "みつける"
games: "Misskey Games"
messageRead: "既読"
noMoreHistory: "これより過去の履歴はありません"
startMessaging: "チャットを開始"
Expand Down Expand Up @@ -674,6 +675,7 @@ emailVerified: "メールアドレスが確認されました"
noteFavoritesCount: "お気に入りノートの数"
pageLikesCount: "Pageにいいねした数"
pageLikedCount: "Pageにいいねされた数"
reversiCount: "リバーシの対局数"
contact: "連絡先"
useSystemFont: "システムのデフォルトのフォントを使う"
clips: "クリップ"
Expand Down Expand Up @@ -962,6 +964,40 @@ _mfm:
rotate: "回転"
rotateDescription: "指定した角度で回転させます。"

_reversi:
reversi: "リバーシ"
gameSettings: "対局の設定"
chooseBoard: "ボードを選択"
blackOrWhite: "先行/後攻"
blackIs: "{name}が黒(先行)"
rules: "ルール"
botSettings: "Botのオプション"
thisGameIsStartedSoon: "対局は数秒後に開始されます"
waitingForOther: "相手の準備が完了するのを待っています"
waitingForMe: "あなたの準備が完了するのを待っています"
waitingBoth: "準備してください"
ready: "準備完了"
cancelReady: "準備を再開"
opponentTurn: "相手のターンです"
myTurn: "あなたのターンです"
turnOf: "{name}のターンです"
pastTurnOf: "{name}のターン"
surrender: "投了"
surrendered: "投了により"
drawn: "引き分け"
won: "{name}の勝ち"
black: ""
white: ""
total: "合計"
turnCount: "{count}ターン目"
myGames: "自分の対局"
allGames: "みんなの対局"
ended: "終了"
playing: "対局中"
isLlotheo: "石の少ない方が勝ち(ロセオ)"
loopedMap: "ループマップ"
canPutEverywhere: "どこでも置けるモード"

_instanceTicker:
none: "表示しない"
remote: "リモートユーザーに表示"
Expand Down Expand Up @@ -1089,6 +1125,8 @@ _sfx:
chatBg: "チャット(バックグラウンド)"
antenna: "アンテナ受信"
channel: "チャンネル通知"
reversiPutBlack: "リバーシ: 黒が打ったとき"
reversiPutWhite: "リバーシ: 白が打ったとき"

_ago:
unknown: ""
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/db/postgre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import { Signin } from '@/models/entities/signin';
import { AuthSession } from '@/models/entities/auth-session';
import { FollowRequest } from '@/models/entities/follow-request';
import { Emoji } from '@/models/entities/emoji';
import { ReversiGame } from '@/models/entities/games/reversi/game';
import { ReversiMatching } from '@/models/entities/games/reversi/matching';
import { UserNotePining } from '@/models/entities/user-note-pining';
import { Poll } from '@/models/entities/poll';
import { UserKeypair } from '@/models/entities/user-keypair';
Expand Down Expand Up @@ -164,6 +166,8 @@ export const entities = [
AntennaNote,
PromoNote,
PromoRead,
ReversiGame,
ReversiMatching,
Relay,
MutedNote,
Channel,
Expand Down
263 changes: 263 additions & 0 deletions packages/backend/src/games/reversi/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
import { count, concat } from '@/prelude/array';

// MISSKEY REVERSI ENGINE

/**
* true ... 黒
* false ... 白
*/
export type Color = boolean;
const BLACK = true;
const WHITE = false;

export type MapPixel = 'null' | 'empty';

export type Options = {
isLlotheo: boolean;
canPutEverywhere: boolean;
loopedBoard: boolean;
};

export type Undo = {
/**
* 色
*/
color: Color;

/**
* どこに打ったか
*/
pos: number;

/**
* 反転した石の位置の配列
*/
effects: number[];

/**
* ターン
*/
turn: Color | null;
};

/**
* リバーシエンジン
*/
export default class Reversi {
public map: MapPixel[];
public mapWidth: number;
public mapHeight: number;
public board: (Color | null | undefined)[];
public turn: Color | null = BLACK;
public opts: Options;

public prevPos = -1;
public prevColor: Color | null = null;

private logs: Undo[] = [];

/**
* ゲームを初期化します
*/
constructor(map: string[], opts: Options) {
//#region binds
this.put = this.put.bind(this);
//#endregion

//#region Options
this.opts = opts;
if (this.opts.isLlotheo == null) this.opts.isLlotheo = false;
if (this.opts.canPutEverywhere == null) this.opts.canPutEverywhere = false;
if (this.opts.loopedBoard == null) this.opts.loopedBoard = false;
//#endregion

//#region Parse map data
this.mapWidth = map[0].length;
this.mapHeight = map.length;
const mapData = map.join('');

this.board = mapData.split('').map(d => d === '-' ? null : d === 'b' ? BLACK : d === 'w' ? WHITE : undefined);

this.map = mapData.split('').map(d => d === '-' || d === 'b' || d === 'w' ? 'empty' : 'null');
//#endregion

// ゲームが始まった時点で片方の色の石しかないか、始まった時点で勝敗が決定するようなマップの場合がある
if (!this.canPutSomewhere(BLACK)) this.turn = this.canPutSomewhere(WHITE) ? WHITE : null;
}

/**
* 黒石の数
*/
public get blackCount() {
return count(BLACK, this.board);
}

/**
* 白石の数
*/
public get whiteCount() {
return count(WHITE, this.board);
}

public transformPosToXy(pos: number): number[] {
const x = pos % this.mapWidth;
const y = Math.floor(pos / this.mapWidth);
return [x, y];
}

public transformXyToPos(x: number, y: number): number {
return x + (y * this.mapWidth);
}

/**
* 指定のマスに石を打ちます
* @param color 石の色
* @param pos 位置
*/
public put(color: Color, pos: number) {
this.prevPos = pos;
this.prevColor = color;

this.board[pos] = color;

// 反転させられる石を取得
const effects = this.effects(color, pos);

// 反転させる
for (const pos of effects) {
this.board[pos] = color;
}

const turn = this.turn;

this.logs.push({
color,
pos,
effects,
turn,
});

this.calcTurn();
}

private calcTurn() {
// ターン計算
this.turn =
this.canPutSomewhere(!this.prevColor) ? !this.prevColor :
this.canPutSomewhere(this.prevColor!) ? this.prevColor :
null;
}

public undo() {
const undo = this.logs.pop()!;
this.prevColor = undo.color;
this.prevPos = undo.pos;
this.board[undo.pos] = null;
for (const pos of undo.effects) {
const color = this.board[pos];
this.board[pos] = !color;
}
this.turn = undo.turn;
}

/**
* 指定した位置のマップデータのマスを取得します
* @param pos 位置
*/
public mapDataGet(pos: number): MapPixel {
const [x, y] = this.transformPosToXy(pos);
return x < 0 || y < 0 || x >= this.mapWidth || y >= this.mapHeight ? 'null' : this.map[pos];
}

/**
* 打つことができる場所を取得します
*/
public puttablePlaces(color: Color): number[] {
return Array.from(this.board.keys()).filter(i => this.canPut(color, i));
}

/**
* 打つことができる場所があるかどうかを取得します
*/
public canPutSomewhere(color: Color): boolean {
return this.puttablePlaces(color).length > 0;
}

/**
* 指定のマスに石を打つことができるかどうかを取得します
* @param color 自分の色
* @param pos 位置
*/
public canPut(color: Color, pos: number): boolean {
return (
this.board[pos] !== null ? false : // 既に石が置いてある場所には打てない
this.opts.canPutEverywhere ? this.mapDataGet(pos) == 'empty' : // 挟んでなくても置けるモード
this.effects(color, pos).length !== 0); // 相手の石を1つでも反転させられるか
}

/**
* 指定のマスに石を置いた時の、反転させられる石を取得します
* @param color 自分の色
* @param initPos 位置
*/
public effects(color: Color, initPos: number): number[] {
const enemyColor = !color;

const diffVectors: [number, number][] = [
[ 0, -1], // 上
[ +1, -1], // 右上
[ +1, 0], // 右
[ +1, +1], // 右下
[ 0, +1], // 下
[ -1, +1], // 左下
[ -1, 0], // 左
[ -1, -1], // 左上
];

const effectsInLine = ([dx, dy]: [number, number]): number[] => {
const nextPos = (x: number, y: number): [number, number] => [x + dx, y + dy];

const found: number[] = []; // 挟めるかもしれない相手の石を入れておく配列
let [x, y] = this.transformPosToXy(initPos);
while (true) {
[x, y] = nextPos(x, y);

// 座標が指し示す位置がボード外に出たとき
if (this.opts.loopedBoard && this.transformXyToPos(
(x = ((x % this.mapWidth) + this.mapWidth) % this.mapWidth),
(y = ((y % this.mapHeight) + this.mapHeight) % this.mapHeight)) === initPos) {
// 盤面の境界でループし、自分が石を置く位置に戻ってきたとき、挟めるようにしている (ref: Test4のマップ)
return found;
} else if (x === -1 || y === -1 || x === this.mapWidth || y === this.mapHeight) {
return []; // 挟めないことが確定 (盤面外に到達)
}

const pos = this.transformXyToPos(x, y);
if (this.mapDataGet(pos) === 'null') return []; // 挟めないことが確定 (配置不可能なマスに到達)
const stone = this.board[pos];
if (stone === null) return []; // 挟めないことが確定 (石が置かれていないマスに到達)
if (stone === enemyColor) found.push(pos); // 挟めるかもしれない (相手の石を発見)
if (stone === color) return found; // 挟めることが確定 (対となる自分の石を発見)
}
};

return concat(diffVectors.map(effectsInLine));
}

/**
* ゲームが終了したか否か
*/
public get isEnded(): boolean {
return this.turn === null;
}

/**
* ゲームの勝者 (null = 引き分け)
*/
public get winner(): Color | null {
return this.isEnded ?
this.blackCount == this.whiteCount ? null :
this.opts.isLlotheo === this.blackCount > this.whiteCount ? WHITE : BLACK :
undefined as never;
}
}
Loading

0 comments on commit 571f0cd

Please sign in to comment.