enhance(reversi): tweak reversi

This commit is contained in:
syuilo
2024-01-20 13:14:46 +09:00
parent f86d0186d2
commit b9a81edae5
16 changed files with 225 additions and 131 deletions

View File

@@ -1,10 +1,22 @@
{
"type": "module",
"name": "misskey-reversi",
"version": "0.0.1",
"main": "./built/index.js",
"types": "./built/index.d.ts",
"exports": {
".": {
"import": "./built/esm/index.js",
"types": "./built/dts/index.d.ts"
},
"./*": {
"import": "./built/esm/*",
"types": "./built/dts/*"
}
},
"scripts": {
"build": "tsc",
"build": "npm run ts",
"ts": "npm run ts-esm && npm run ts-dts",
"ts-esm": "tsc --outDir built/esm",
"ts-dts": "tsc --outDir built/dts --declaration true --emitDeclarationOnly true --declarationMap true",
"watch": "nodemon -w src -e ts,js,cjs,mjs,json --exec \"pnpm run build\"",
"eslint": "eslint . --ext .js,.jsx,.ts,.tsx",
"typecheck": "tsc --noEmit",
@@ -16,6 +28,7 @@
"@typescript-eslint/eslint-plugin": "6.19.0",
"@typescript-eslint/parser": "6.19.0",
"eslint": "8.56.0",
"nodemon": "3.0.2",
"typescript": "5.3.3"
},
"files": [

View File

@@ -46,7 +46,7 @@ export class Game {
constructor(map: string[], opts: Options) {
//#region binds
this.put = this.put.bind(this);
this.putStone = this.putStone.bind(this);
//#endregion
//#region Options
@@ -88,7 +88,10 @@ export class Game {
return x + (y * this.mapWidth);
}
public put(color: Color, pos: number) {
public putStone(pos: number) {
const color = this.turn;
if (color == null) return;
this.prevPos = pos;
this.prevColor = color;

View File

@@ -3,10 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Game } from './game.js';
export {
Game,
};
export { Game } from './game.js';
export * as Serializer from './serializer.js';
export * as maps from './maps.js';

View File

@@ -0,0 +1,98 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Game } from './game.js';
export type Log = {
time: number;
player: boolean;
operation: 'put';
pos: number;
};
export type SerializedLog = number[];
export function serializeLogs(logs: Log[]) {
const _logs: number[][] = [];
for (let i = 0; i < logs.length; i++) {
const log = logs[i];
const timeDelta = i === 0 ? log.time : log.time - logs[i - 1].time;
switch (log.operation) {
case 'put':
_logs.push([timeDelta, log.player ? 1 : 0, 0, log.pos]);
break;
//case 'surrender':
// _logs.push([timeDelta, log.player, 1]);
// break;
}
}
return _logs;
}
export function deserializeLogs(logs: SerializedLog[]) {
const _logs: Log[] = [];
let time = 0;
for (const log of logs) {
const timeDelta = log[0];
time += timeDelta;
const player = log[1];
const operation = log[2];
switch (operation) {
case 0:
_logs.push({
time,
player: player === 1,
operation: 'put',
pos: log[3],
});
break;
//case 1:
// _logs.push({
// time,
// player: player === 1,
// operation: 'surrender',
// });
// break;
}
}
return _logs;
}
export function restoreGame(env: {
map: string[];
isLlotheo: boolean;
canPutEverywhere: boolean;
loopedBoard: boolean;
logs: SerializedLog[];
}) {
const logs = deserializeLogs(env.logs);
const game = new Game(env.map, {
isLlotheo: env.isLlotheo,
canPutEverywhere: env.canPutEverywhere,
loopedBoard: env.loopedBoard,
});
for (const log of logs) {
switch (log.operation) {
case 'put':
game.putStone(log.pos);
break;
//case 'surrender':
// game.surrender(log.player);
// break;
}
}
return game;
}