Deterministic game engine

reverse-optcgsim

Year

2025-2026

project

02

cover screen2025-2026
reverse-optcgsim

Overview

A Python port of the OPTCG simulator used for replay debugging and AI experiments: deterministic game state, real card assets, MCTS/PPO/AlphaZero runs and diagnostic plots.

PythonPyTorchRich TUIPygameReplay paritypytest

Technical details

Parity

Training starts with a deterministic engine

Problem

An AlphaZero run is meaningless if the Python simulator can drift from the Unity/C# rules engine. A single wrong branch order in combat, targeting or effect resolution would teach the model a game that does not exist.

Resolution

The Python engine is treated as a parity port first and an AI substrate second. The C# behaviour remains the source of truth, method names and state fields are mirrored, RNG is centralized, and every training layer consumes the engine through public replayable state.

System flow

  1. 01The decompiled Unity behaviour is documented into phase-by-phase engine notes before Python code is written.
  2. 02Seeded matches, recorder/player logs and scenario tests replay the same inputs against deterministic snapshots.
  3. 03Only after the replay and parity harness is green does the ML layer receive the state through OptcgEnv.

Implementation notes

EngineRng is the single randomness surface; rogue random imports are blocked by tests.

Replay snapshots use canonical JSON and parity scenarios cover blockers, replacements, costs, power, silence and DON flows.

Verb coverage guards V3 and legacy effect fields so new card mechanics cannot disappear silently.

Overview01
Training terminal, diagnostics and replay viewer in one capture.

Training terminal, diagnostics and replay viewer in one capture.

Replay proof02
Replay viewer rendered from a real trace, with board state and card assets.

Replay viewer rendered from a real trace, with board state and card assets.

Replay clip03

Short replay viewer capture showing turn state, legal actions, board state and the replay log.

Training diagnostics04
Diagnostic graph generated from an AlphaZero run metrics.csv.

Diagnostic graph generated from an AlphaZero run metrics.csv.

Problem

A rules, RNG or replay mistake is enough to train the model on a game that does not exist.

Approach

Treat the engine as a parity port first: documented C# behaviours, deterministic snapshots, recorder/player logs and match visualization.

Result

A stable environment for testing MCTS/AlphaZero, checking replays and quickly locating engine or training divergences.

Next2025-2026

OPTCG community SaaS

Sharedeck

A One Piece TCG web app with deck publishing, profiles, collections, trade lists, realtime chat, tournaments and a browser match simulator connected to a Python rules engine.

Laravel 13InertiaVue 3TypeScript
OPTCG community SaaS2025-2026
Sharedeck