opensi·si

Integration of OpenSpiel games with the OpenEnv framework. OpenSpiel is DeepMind's collection of 70+ game environments for RL research.

OpenSpiel

Integration of OpenSpiel games with the OpenEnv framework. OpenSpiel is DeepMind's collection of 70+ game environments for RL research.

The /web playground on 2048 after some moves: the board and one button per legal move.

Supported Games

This environment supports 6 games across different categories:

The /web playground drawing for the OpenSpiel games, each after a few moves.

Single-Player Games (No Opponent)

  1. Catch - Move horizontally to catch a falling ball
  2. Cliff Walking - Navigate grid without falling off cliff (Sutton & Barto benchmark)
  3. 2048 - Classic tile-merging puzzle game
  4. Blackjack - Simplified blackjack (HIT/STAND only)

Multi-Player Games (with Bot Opponent)

  1. Tic-Tac-Toe - Classic 3x3 game
  2. Kuhn Poker - 2-player simplified poker (game theory benchmark)

Quick Start

The simplest way to use the OpenSpiel environment is through the OpenSpielEnv class:

from openspiel_env import OpenSpielEnv, OpenSpielAction

try:
    # Create environment from Docker image
    env = OpenSpielEnv.from_docker_image("openspiel-env:latest").sync()

    # Reset to start a new episode
    result = env.reset()
    print(f"Initial state: {result.observation.info_state}")
    print(f"Legal actions: {result.observation.legal_actions}")

    # Play until done
    while not result.done:
        action_id = result.observation.legal_actions[0]
        result = env.step(OpenSpielAction(action_id=action_id))
        print(f"Reward: {result.reward}, Done: {result.done}")

finally:
    # Always clean up
    env.close()

That's it! The OpenSpielEnv.from_docker_image() method handles:

  • Starting the Docker container
  • Waiting for the server to be ready
  • Connecting to the environment
  • Container cleanup when you call close()

Building the Docker Image

OpenSpiel requires compilation from C++ source. The Docker build uses a pre-built base image by default to avoid long build times.

From the environment directory (envs/openspiel_env/):

# Uses pre-built base image from GHCR (fast, ~1-2 min)
docker build -t openspiel-env:latest -f server/Dockerfile .

This uses the pre-built ghcr.io/huggingface/openenv-openspiel-base image which already contains compiled OpenSpiel.

Building Your Own Base Image (Optional)

If you need to customize OpenSpiel or can't access the pre-built image:

# Step 1: Build the base image (compiles OpenSpiel, ~30-60 min)
docker build -t openspiel-base:latest -f server/Dockerfile.openspiel-base .

# Step 2: Build the environment using your local base image
docker build -t openspiel-env:latest \
  --build-arg OPENSPIEL_BASE_IMAGE=openspiel-base:latest \
  -f server/Dockerfile .

Running Specific Games

Pick the game and opponent with environment variables:

Variable Default Description
OPENSPIEL_GAME catch catch, tic_tac_toe, kuhn_poker, 2048, blackjack or cliff_walking
OPENSPIEL_AGENT_PLAYER 0 Player ID the agent plays as
OPENSPIEL_OPPONENT_POLICY random Opponent in multi-player games: random, first (first legal action) or last (last legal action)
docker run -p 8000:8000 \
  -e OPENSPIEL_GAME=tic_tac_toe \
  -e OPENSPIEL_OPPONENT_POLICY=first \
  openspiel-env:latest

Deploying to Hugging Face Spaces

From envs/openspiel_env/:

openenv push --repo-id my-org/openspiel-env -e OPENSPIEL_GAME=tic_tac_toe

-e sets the variables above on the Space. See the openenv push reference for all options. The Space serves the web UI at /web, the API docs at /docs and a health check at /health. The default Dockerfile uses the pre-built OpenSpiel base image, so it runs on standard CPU hardware.

Environment Details

Action

OpenSpielAction: Contains the action to take

  • action_id (int) - Action ID to execute
  • game_name (str) - Game name (default: "catch")
  • game_params (Dict) - Optional game parameters

Observation

OpenSpielObservation: Contains the game state

  • info_state (List[float]) - Agent's information state vector
  • legal_actions (List[int]) - Legal action IDs
  • game_phase (str) - "initial", "playing", or "terminal"
  • current_player_id (int) - Current player (-1 for simultaneous)
  • opponent_last_action (Optional[int]) - Last opponent action
  • done (bool) - Whether the episode has ended
  • reward (Optional[float]) - Reward for the last action

State

OpenSpielState: Server-side state snapshot

  • episode_id (str) - Unique identifier for the current episode
  • step_count (int) - Number of steps taken
  • game_name (str) - Game name
  • agent_player (int) - Agent's player ID
  • opponent_policy (str) - Opponent policy name
  • num_players (int) - Total players

Advanced Usage

Connecting to an Existing Server

If you already have an OpenSpiel environment server running:

from openspiel_env import OpenSpielEnv, OpenSpielAction

# Connect to existing server
env = OpenSpielEnv(base_url="http://localhost:8000")

# Use as normal
result = env.reset()
result = env.step(OpenSpielAction(action_id=result.observation.legal_actions[0]))

# Close connection (does NOT stop the server)
env.close()

Connecting to HuggingFace Space

from openspiel_env import OpenSpielEnv, OpenSpielAction

# Connect to remote Space
env = OpenSpielEnv(base_url="https://your-username-openspiel.hf.space")

result = env.reset()
print(f"Game: {result.observation.game_phase}")
print(f"Legal actions: {result.observation.legal_actions}")

result = env.step(OpenSpielAction(action_id=result.observation.legal_actions[0]))
env.close()