Spaces:
Sleeping
Sleeping
File size: 2,192 Bytes
15f353f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #!/usr/bin/env node
/**
* 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();
|