Skip to content

refactor(env): replace visualizer's 600-line elif chain with dispatch - #24

Merged
NorbertRop merged 1 commit into
mainfrom
refactor/visualizer-dispatch
May 26, 2026
Merged

NorbertRop merged 1 commit into
mainfrom
refactor/visualizer-dispatch

Conversation

@NorbertRop

Copy link
Copy Markdown
Contributor

Summary

`src/jaxpot/env/visualizer.py`'s `_set_config_by_state` was a single ~600-line `if/elif` chain over 19 different `env_id`s. Every branch followed the same shape — set 3 dimensions + `_make_dwg_group` + two-arm `ColorSet` for dark vs light theme — but the code was open-coded 19 times.

This PR replaces the chain with a dict-dispatch over per-env config builder functions. File net: 1001 → 895 lines. No behavior change beyond fixing a latent color-theme bug (see below).

Latent bug fixed

Most pre-existing branches had this color-theme check:

```python
if (self.config["COLOR_THEME"] is None and self.config["COLOR_THEME"] == "dark") or self.config["COLOR_THEME"] == "dark":
```

The first clause `(x is None and x == "dark")` is structurally always `False` (a `None` value can't equal `"dark"`). Effective behavior was `theme == "dark"` — the project's own envs (`liars_dice`, `phantom_ttt`, `dark_hex`) already used the clean form. The refactor unifies all 19 envs on a single `_pick_color_set(theme, dark_cs, light_cs)` helper.

What changed

File Change
`src/jaxpot/env/visualizer.py` Add `_pick_color_set` helper + `_EnvVisConfig` dataclass + 19 `config<env_id>` builder functions + `_ENV_CONFIGS` dispatch dict. Replace the 600-line `set_config_by_state` body with a 15-line lookup + dispatch + dict update. `Visualizer` class API and all module-level `save*` functions unchanged.
`tests/test_visualizer_dispatch.py` NEW — 6 tests pinning dispatch surface, structural validity of the four project-trained env builders, three-theme SVG round-trip for tic_tac_toe, and the unknown-env_id error path.

What did NOT change

  • `Visualizer` class public API.
  • `save_svg` / `save_svg_animation` / `save_gif` module-level functions.
  • Caller code in `env/quoridor`, `env/phantom_ttt`, `env/dark_hex`, `env/liars_dice` — same `Visualizer(color_theme=..., scale=...).get_dwg(...)` API.
  • Color palettes per env / theme (each `ColorSet` literal carries the exact same values as before).
  • Lazy import pattern for the heavy `pgx._src.dwg.*` modules.

Test plan

  • `uv run pytest tests/` — 232 passed, 2 skipped (was 226 + 6 new).
  • `tests/test_visualizer_dispatch.py` — all 6 pass, including the three-theme round-trip and the unknown-env error path.

🤖 Generated with Claude Code

_set_config_by_state was a single ~600-line if/elif chain over 19
env_ids, each branch setting GRID_SIZE/BOARD_WIDTH/BOARD_HEIGHT, the
env-specific _make_dwg_group, and dark vs light ColorSets. The chain
duplicated a buggy color-theme check across most branches
(``(theme is None and theme == 'dark') or theme == 'dark'`` — the
first clause is always False since None != 'dark'; effectively
``theme == 'dark'``).

Replace with:
- One _EnvVisConfig dataclass holding the resolved per-env parameters.
- A _pick_color_set helper that takes (theme, dark_cs, light_cs) and
  picks one; dead is-None check is gone.
- 21 _config_<env_id> module-level functions (one per env, two go
  variants share _config_go), each returning _EnvVisConfig. pgx-import
  remains lazy per builder.
- _ENV_CONFIGS dispatch table mapping env_id -> builder.
- _set_config_by_state shrinks from ~600 lines to ~15 lines of lookup
  + dispatch + dict update.

File net: 1001 -> 895 lines. The Visualizer class API is unchanged;
its four callers (env.quoridor, env.phantom_ttt, env.dark_hex,
env.liars_dice) need no updates.

New tests/test_visualizer_dispatch.py pins the dispatch surface:
covered env_ids, structural validity of the four project-trained env
builders, three-theme round-trip through Visualizer.get_dwg for
tic_tac_toe, and the unknown-env_id error path. 6 new tests.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@NorbertRop
NorbertRop merged commit d74b2a4 into main May 26, 2026
1 of 2 checks passed
@NorbertRop
NorbertRop deleted the refactor/visualizer-dispatch branch May 26, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant