Spaces:
Sleeping
Sleeping
| /** | |
| * TypeScript Game Validation - Output game results as JSON | |
| * | |
| * Reads TGN files and outputs final board state + territory for comparison | |
| */ | |
| import { TrigoGame } from "@inc/trigo/game.js"; | |
| import { calculateTerritory } from "@inc/trigo/gameUtils.js"; | |
| import * as fs from "fs"; | |
| import * as path from "path"; | |
| interface GameResult { | |
| tgnFile: string; | |
| boardShape: { x: number; y: number; z: number }; | |
| moveCount: number; | |
| board: number[][][]; // 3D array: board[x][y][z] = stone type | |
| territory: { | |
| black: number; | |
| white: number; | |
| neutral: number; | |
| }; | |
| error?: string; | |
| } | |
| function processGame(tgnFilePath: string): GameResult { | |
| try { | |
| const tgnContent = fs.readFileSync(tgnFilePath, "utf-8"); | |
| // Parse TGN | |
| const game = TrigoGame.fromTGN(tgnContent); | |
| // Get final state | |
| const board = game.getBoard(); | |
| const shape = game.getShape(); | |
| const history = game.getHistory(); | |
| // Calculate territory | |
| const territory = calculateTerritory(board, shape); | |
| return { | |
| tgnFile: path.basename(tgnFilePath), | |
| boardShape: shape, | |
| moveCount: history.length, | |
| board, | |
| territory: { | |
| black: territory.black, | |
| white: territory.white, | |
| neutral: territory.neutral | |
| } | |
| }; | |
| } catch (error) { | |
| return { | |
| tgnFile: path.basename(tgnFilePath), | |
| boardShape: { x: 0, y: 0, z: 0 }, | |
| moveCount: 0, | |
| board: [], | |
| territory: { black: 0, white: 0, neutral: 0 }, | |
| error: error instanceof Error ? error.message : String(error) | |
| }; | |
| } | |
| } | |
| function main() { | |
| const args = process.argv.slice(2); | |
| if (args.length === 0) { | |
| console.error("Usage: validateGameResults.ts <tgn_file> [<tgn_file> ...]"); | |
| process.exit(1); | |
| } | |
| const results: GameResult[] = []; | |
| for (const tgnFile of args) { | |
| if (!fs.existsSync(tgnFile)) { | |
| console.error(`File not found: ${tgnFile}`); | |
| results.push({ | |
| tgnFile: path.basename(tgnFile), | |
| boardShape: { x: 0, y: 0, z: 0 }, | |
| moveCount: 0, | |
| board: [], | |
| territory: { black: 0, white: 0, neutral: 0 }, | |
| error: "File not found" | |
| }); | |
| continue; | |
| } | |
| const result = processGame(tgnFile); | |
| results.push(result); | |
| } | |
| // Output as JSON | |
| console.log(JSON.stringify(results, null, 2)); | |
| } | |
| main(); | |