Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | /** * Rithmomachia (Battle of Numbers) - Type Definitions * * This module uses Zod schemas as the single source of truth for types. * TypeScript types are inferred from Zod schemas using z.infer<>. */ import { z } from 'zod' // ============================================================================ // Piece Types // ============================================================================ export const PieceTypeSchema = z.enum(['C', 'T', 'S', 'P']) // Circle, Triangle, Square, Pyramid export type PieceType = z.infer<typeof PieceTypeSchema> export const ColorSchema = z.enum(['W', 'B']) // White, Black export type Color = z.infer<typeof ColorSchema> export const PieceSchema = z.object({ id: z.string(), // stable UUID (e.g., "W_C_01") color: ColorSchema, type: PieceTypeSchema, value: z.number().optional(), // for C/T/S always present pyramidFaces: z.array(z.number()).optional(), // for P only (length 4) activePyramidFace: z.number().nullable().optional(), // last chosen face for logging/captures square: z.string(), // "A1".."P8" captured: z.boolean(), }) export type Piece = z.infer<typeof PieceSchema> // ============================================================================ // Relations // ============================================================================ export const RelationKindSchema = z.enum([ 'EQUAL', // a == b 'MULTIPLE', // a % b == 0 'DIVISOR', // b % a == 0 'SUM', // a + h == b or b + h == a 'DIFF', // |a - h| == b or |b - h| == a 'PRODUCT', // a * h == b or b * h == a 'RATIO', // a * r == b or b * r == a (r = helper value) ]) export type RelationKind = z.infer<typeof RelationKindSchema> export const CaptureContextSchema = z.object({ relation: RelationKindSchema, moverPieceId: z.string(), targetPieceId: z.string(), helperPieceId: z.string().optional(), // required for SUM/DIFF/PRODUCT/RATIO moverFaceUsed: z.number().nullable().optional(), // if mover was a Pyramid }) export type CaptureContext = z.infer<typeof CaptureContextSchema> export const AmbushContextSchema = z.object({ relation: RelationKindSchema, enemyPieceId: z.string(), helper1Id: z.string(), helper2Id: z.string(), // two helpers for ambush }) export type AmbushContext = z.infer<typeof AmbushContextSchema> // ============================================================================ // Harmony // ============================================================================ export const HarmonyTypeSchema = z.enum(['ARITH', 'GEOM', 'HARM']) export type HarmonyType = z.infer<typeof HarmonyTypeSchema> export const HarmonyDeclarationSchema = z.object({ by: ColorSchema, pieceIds: z.array(z.string()), // exactly 3 for classical three-piece proportions type: HarmonyTypeSchema, params: z.object({ a: z.string().optional(), // first value in proportion (A-M-B structure) m: z.string().optional(), // middle value in proportion b: z.string().optional(), // last value in proportion }), declaredAtPly: z.number(), }) export type HarmonyDeclaration = z.infer<typeof HarmonyDeclarationSchema> // ============================================================================ // Move Records // ============================================================================ export const WinConditionSchema = z.enum([ 'HARMONY', 'EXHAUSTION', 'RESIGNATION', 'POINTS', 'AGREEMENT', 'REPETITION', 'FIFTY', ]) export const GameResultSchema = z.enum(['ONGOING', 'WINS_W', 'WINS_B', 'DRAW']) export const MoveRecordSchema = z.object({ ply: z.number(), color: ColorSchema, from: z.string(), // e.g., "C2" to: z.string(), // e.g., "C6" pieceId: z.string(), pyramidFaceUsed: z.number().nullable().optional(), capture: CaptureContextSchema.nullable().optional(), ambush: AmbushContextSchema.nullable().optional(), harmonyDeclared: HarmonyDeclarationSchema.nullable().optional(), pointsCapturedThisMove: z.number().optional(), // if point scoring is on fenLikeHash: z.string().optional(), // for repetition detection noProgressCount: z.number().optional(), // for 50-move rule resultAfter: GameResultSchema.optional(), }) export type MoveRecord = z.infer<typeof MoveRecordSchema> // ============================================================================ // Game Configuration // ============================================================================ export const RithmomachiaConfigSchema = z.object({ // Rule toggles pointWinEnabled: z.boolean(), // default: false pointWinThreshold: z.number(), // default: 30 repetitionRule: z.boolean(), // default: true fiftyMoveRule: z.boolean(), // default: true allowAnySetOnRecheck: z.boolean(), // default: true (harmony revalidation) // Optional time controls (not implemented in v1) timeControlMs: z.number().nullable(), // Player assignments (null = auto-assign) whitePlayerId: z.string().nullable().optional(), // default: null (auto-assign first active player) blackPlayerId: z.string().nullable().optional(), // default: null (auto-assign second active player) }) export type RithmomachiaConfig = z.infer<typeof RithmomachiaConfigSchema> & { [key: string]: unknown // Index signature for GameConfig constraint } // ============================================================================ // Game Phase // ============================================================================ export const GamePhaseSchema = z.enum(['setup', 'playing', 'results']) export type GamePhase = z.infer<typeof GamePhaseSchema> // ============================================================================ // Game State // ============================================================================ export const RithmomachiaStateSchema = z.object({ // Configuration (stored in state per arcade pattern) pointWinEnabled: z.boolean(), pointWinThreshold: z.number(), repetitionRule: z.boolean(), fiftyMoveRule: z.boolean(), allowAnySetOnRecheck: z.boolean(), timeControlMs: z.number().nullable(), whitePlayerId: z.string().nullable().optional(), blackPlayerId: z.string().nullable().optional(), // Game phase gamePhase: GamePhaseSchema, // Board dimensions boardCols: z.number(), // 16 boardRows: z.number(), // 8 // Current turn turn: ColorSchema, // 'W' or 'B' // Pieces (key = piece.id) pieces: z.record(z.string(), PieceSchema), // Captured pieces capturedPieces: z.object({ W: z.array(PieceSchema), B: z.array(PieceSchema), }), // Move history history: z.array(MoveRecordSchema), // Pending harmony (declared last turn, awaiting validation) pendingHarmony: HarmonyDeclarationSchema.nullable(), // Draw/repetition tracking noProgressCount: z.number(), // for 50-move rule stateHashes: z.array(z.string()), // Zobrist hashes for repetition detection // Victory state winner: ColorSchema.nullable(), winCondition: WinConditionSchema.nullable(), // Points (if enabled by config) pointsCaptured: z .object({ W: z.number(), B: z.number(), }) .optional(), }) export type RithmomachiaState = z.infer<typeof RithmomachiaStateSchema> // ============================================================================ // Game Moves // ============================================================================ export type RithmomachiaMove = | { type: 'START_GAME' playerId: string userId: string timestamp: number data: { playerColor: Color activePlayers: string[] } } | { type: 'MOVE' playerId: string userId: string timestamp: number data: { from: string to: string pieceId: string pyramidFaceUsed?: number | null capture?: Omit<CaptureContext, 'moverPieceId' | 'targetPieceId'> & { targetPieceId: string } ambush?: AmbushContext } } | { type: 'DECLARE_HARMONY' playerId: string userId: string timestamp: number data: { pieceIds: string[] harmonyType: HarmonyType params: HarmonyDeclaration['params'] } } | { type: 'RESIGN' playerId: string userId: string timestamp: number data: Record<string, never> } | { type: 'OFFER_DRAW' playerId: string userId: string timestamp: number data: Record<string, never> } | { type: 'ACCEPT_DRAW' playerId: string userId: string timestamp: number data: Record<string, never> } | { type: 'CLAIM_REPETITION' playerId: string userId: string timestamp: number data: Record<string, never> } | { type: 'CLAIM_FIFTY_MOVE' playerId: string userId: string timestamp: number data: Record<string, never> } | { type: 'SET_CONFIG' playerId: string userId: string timestamp: number data: { field: string value: unknown } } | { type: 'RESET_GAME' playerId: string userId: string timestamp: number data: Record<string, never> } | { type: 'GO_TO_SETUP' playerId: string userId: string timestamp: number data: Record<string, never> } // ============================================================================ // Helper Types & Constants // ============================================================================ // Square notation helpers export type File = | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' export type Rank = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 export type Square = `${File}${Rank}` // Board boundaries export const WHITE_HALF_ROWS = [1, 2, 3, 4] as const export const BLACK_HALF_ROWS = [5, 6, 7, 8] as const // Point values for pieces export const PIECE_POINTS: Record<PieceType, number> = { C: 1, // Circle T: 2, // Triangle S: 3, // Square P: 5, // Pyramid } // Utility: check if square is in enemy half export function isInEnemyHalf(square: string, color: Color): boolean { const rank = Number.parseInt(square[1], 10) if (color === 'W') { return (BLACK_HALF_ROWS as readonly number[]).includes(rank) } return (WHITE_HALF_ROWS as readonly number[]).includes(rank) } // Utility: parse square notation export function parseSquare(square: string): { file: number; rank: number } { const file = square.charCodeAt(0) - 65 // A=0, B=1, ..., P=15 const rank = Number.parseInt(square[1], 10) // 1-8 return { file, rank } } // Utility: create square notation export function makeSquare(file: number, rank: number): string { return `${String.fromCharCode(65 + file)}${rank}` } // Utility: get opponent color export function opponentColor(color: Color): Color { return color === 'W' ? 'B' : 'W' } |