feat(catch): port OpenSpiel 2.0 struct architecture to Catch game#1552
Open
shivansh023023 wants to merge 1 commit into
Open
feat(catch): port OpenSpiel 2.0 struct architecture to Catch game#1552shivansh023023 wants to merge 1 commit into
shivansh023023 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Title:
feat(catch): Migrate environment to JSON-interchangeable state structsDescription:
Overview
Following up on the recent updates for the upcoming OpenSpiel 2.0 release, this PR migrates the
catchenvironment to the new JSON-interchangeable struct architecture. This mirrors the design patterns recently established intic_tac_toeandconnect_four.Because
catchis 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.hCatchStructContents(POD struct withball_row,ball_col,paddle_col), annotated withNLOHMANN_DEFINE_TYPE_INTRUSIVEto enable native JSON serialization.CatchStateStructandCatchObservationStructutilizing theSPIEL_DEFINE_STRUCTmacro.CatchActionStructwith a semanticdirectionstring field via theSPIEL_STRUCT_BOILERPLATEmacro.CatchStateconstructor to support seamless JSON round-trips.ToStruct(),ToObservationStruct(),ActionToStruct(), andStructToActions().NewInitialState(CatchStateStruct)andNewInitialState(nlohmann::json)overloads toCatchGame, utilizingusing Game::NewInitialStateto maintain base overload visibility.absl/types/optional.h,nlohmann/json.hpp, etc.).catch.ccCatchState(game, CatchStateStruct)constructor with strict boundary and state validation (e.g., pre-chance vs. post-chance states, grid bounds).ToStruct()to map internal game variables to the JSON-ready POD struct.ToObservationStruct(), which directly delegates toToJson()(as the full state constitutes the observation in Catch).ActionIdToDirection()andDirectionToActionId()) to map discrete action IDs to semantic direction strings forActionToStruct()andStructToActions().Verification
catch_test).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.