Skip to content

feat(catch): port OpenSpiel 2.0 struct architecture to Catch game#1552

Open
shivansh023023 wants to merge 1 commit into
google-deepmind:masterfrom
shivansh023023:feature-catch-structs
Open

feat(catch): port OpenSpiel 2.0 struct architecture to Catch game#1552
shivansh023023 wants to merge 1 commit into
google-deepmind:masterfrom
shivansh023023:feature-catch-structs

Conversation

@shivansh023023

@shivansh023023 shivansh023023 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Title:

feat(catch): Migrate environment to JSON-interchangeable state structs

Description:

Overview
Following up on the recent updates for the upcoming OpenSpiel 2.0 release, this PR migrates the catch environment to the new JSON-interchangeable struct architecture. This mirrors the design patterns recently established in tic_tac_toe and connect_four.

Because catch is a straightforward, perfect-information, single-player game, it serves as a clean, minimal environment to further validate this new architectural layer before scaling it to more complex multi-agent games.

Key Architectural Changes

catch.h

  • Introduced CatchStructContents (POD struct with ball_row, ball_col, paddle_col), annotated with NLOHMANN_DEFINE_TYPE_INTRUSIVE to enable native JSON serialization.
  • Declared CatchStateStruct and CatchObservationStruct utilizing the SPIEL_DEFINE_STRUCT macro.
  • Declared CatchActionStruct with a semantic direction string field via the SPIEL_STRUCT_BOILERPLATE macro.
  • Added a struct-accepting CatchState constructor to support seamless JSON round-trips.
  • Overrode necessary virtual methods: ToStruct(), ToObservationStruct(), ActionToStruct(), and StructToActions().
  • Added NewInitialState(CatchStateStruct) and NewInitialState(nlohmann::json) overloads to CatchGame, utilizing using Game::NewInitialState to maintain base overload visibility.
  • Updated includes to support the new types (absl/types/optional.h, nlohmann/json.hpp, etc.).

catch.cc

  • Implemented the CatchState(game, CatchStateStruct) constructor with strict boundary and state validation (e.g., pre-chance vs. post-chance states, grid bounds).
  • Implemented ToStruct() to map internal game variables to the JSON-ready POD struct.
  • Implemented ToObservationStruct(), which directly delegates to ToJson() (as the full state constitutes the observation in Catch).
  • Built translation helpers (ActionIdToDirection() and DirectionToActionId()) to map discrete action IDs to semantic direction strings for ActionToStruct() and StructToActions().

Verification

  • ✅ Native C++ engine tests pass (catch_test).
  • ✅ Python frontend bindings and environment tests pass (python/tests/catch_test.py).

@lanctot , I hope this helps accelerate the migration effort for the 2.0 release! I am happy to help port this architecture to other environments next. Please let me know if you have a specific priority list of games you'd like targeted.

Implements the JSON-interchangeable struct layer for the Catch environment,
mirroring the pattern established in tic_tac_toe and connect_four.

Changes in catch.h:
- Add CatchStructContents POD struct with ball_row, ball_col, paddle_col
  fields; annotated with NLOHMANN_DEFINE_TYPE_INTRUSIVE for automatic
  JSON serialisation.
- Declare CatchStateStruct (inherits StateStruct) and
  CatchObservationStruct (inherits ObservationStruct) via
  SPIEL_DEFINE_STRUCT macro.
- Declare CatchActionStruct (inherits ActionStruct) with a semantic
  'direction' string field, via SPIEL_STRUCT_BOILERPLATE macro.
- Add struct-accepting CatchState constructor for JSON round-trips.
- Override ToStruct(), ToObservationStruct(), ActionToStruct(), and
  StructToActions() on CatchState.
- Add NewInitialState(CatchStateStruct) and
  NewInitialState(nlohmann::json) overloads on CatchGame; add
  'using Game::NewInitialState' to keep the base overload visible.
- Add new includes: absl/types/optional.h, absl/types/span.h,
  nlohmann/json.hpp, game_parameters.h.

Changes in catch.cc:
- Implement CatchState(game, CatchStateStruct) constructor with full
  validation (pre-chance vs post-chance state, row/column bounds).
- Implement ToStruct(): serialises ball_row, ball_col, paddle_col.
- Implement ToObservationStruct(): delegates to ToJson() (full state
  is the observation in this perfect-information, single-player game).
- Implement ActionToStruct(): maps action id -> direction string.
- Implement StructToActions(): maps direction string -> action id.
- Extract helpers ActionIdToDirection() and DirectionToActionId().
- Add absl/strings/str_format.h and nlohmann/json.hpp includes.
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