Add minesweeper env and example
Summary
This PR adds a new Minesweeper game environment to OpenEnv, providing a classic grid-based puzzle game for reinforcement learning agents. The environment challenges agents to reveal all non-mine cells without triggering any mines, using strategies based on number clues and flag placement.
Motivation
Minesweeper is a well-known logical puzzle that requires:
- Strategic thinking and planning
- Risk assessment and decision-making under uncertainty
- Multi-step reasoning based on partial information
- Balance between exploration (revealing cells) and exploitation (using known information)
This environment is valuable for:
- Testing RL agents on logical reasoning tasks
- Evaluating agents' ability to handle uncertainty and risk
- Benchmarking decision-making in partially observable environments
- Educational purposes and demonstrations
Changes
Core Implementation
- Environment Logic: Full Minesweeper game implementation with configurable grid size and mine count
- Action Space: Two action types:
reveal: Uncover a cell to see its valueflag: Place/remove flag on suspected mine locations
- Observation Space: 2D grid showing:
- Unrevealed cells (-1)
- Number clues (0-8) indicating adjacent mines
- Flagged cells ('F')
- Mines ('*', only shown on game over)
- Reward System:
- +1.0 for revealing safe cells
- +0.5 for correctly flagging mines
- -10.0 for hitting mines (game over)
- Small penalties for invalid actions
- Game States: ONGOING, WON, LOST
Features
- Recursive Reveal: Automatically reveals adjacent cells when a cell with zero adjacent mines is uncovered
- State Management: Full game state tracking with episode IDs and step counts
- HTTP API: FastAPI-based server for remote environment access
- Type Safety: Pydantic models for all actions and observations
Docker Support
- Multi-stage Dockerfile based on
openenv-baseimage - Build script (
build_docker.sh) for easy local development - Health check endpoint for container orchestration
- Optimized layer caching for faster builds
Testing
- Comprehensive test suite with 13 test cases covering:
- Server setup and initialization
- Action execution (reveal, flag, toggle)
- Edge cases (invalid positions, already revealed cells)
- Game state transitions
- Reset functionality
- HTTP serialization handling
- All tests passing ✅
Documentation
- Detailed README with:
- Quick start guide
- Action and observation specifications
- Reward structure
- Configuration options
- Usage examples
- Docker instructions
- Example agent script demonstrating full gameplay
CI/CD Integration
- Added to GitHub Actions build matrix
- Automated Docker image building for linux/amd64 and linux/arm64
- Automated publishing to GitHub Container Registry
Files Changed
New Files (14 files, 1,552 insertions)
Environment Core:
src/envs/minesweeper_env/__init__.py- Module exports and public APIsrc/envs/minesweeper_env/client.py- HTTP client for remote environment accesssrc/envs/minesweeper_env/models.py- Pydantic models for actions, observations, and statesrc/envs/minesweeper_env/openenv.yaml- Environment metadata and configurationsrc/envs/minesweeper_env/pyproject.toml- Package dependencies and build configuration
Server Implementation:
src/envs/minesweeper_env/server/__init__.py- Server module exportssrc/envs/minesweeper_env/server/app.py- FastAPI application and HTTP endpointssrc/envs/minesweeper_env/server/minesweeper_environment.py- Core game logic and environment implementation
Docker & Build:
src/envs/minesweeper_env/server/Dockerfile- Multi-stage container image definitionsrc/envs/minesweeper_env/server/build_docker.sh- Build script for local development
Documentation & Examples:
src/envs/minesweeper_env/README.md- Comprehensive environment documentationexamples/minesweeper_agent.py- Example agent demonstrating environment usage
Testing:
tests/envs/test_minesweeper_env.py- Test suite with 13 test cases
Modified Files (1 file)
CI/CD:
.github/workflows/docker-build.yml- Addedminesweeper-envto build matrix for automated Docker image building
Usage Example
from envs.minesweeper_env import MinesweeperAction, MinesweeperEnv
# Create environment from Docker image
env = MinesweeperEnv.from_docker_image("minesweeper-env:latest")
try:
# Reset
result = env.reset()
print(f"Board: {result.observation.board_height}x{result.observation.board_width}")
print(f"Mines: {result.observation.num_mines}")
# Reveal a cell
result = env.step(MinesweeperAction(row=2, col=2, action_type="reveal"))
# Place a flag
result = env.step(MinesweeperAction(row=0, col=0, action_type="flag"))
finally:
env.close()
Testing
Run the test suite:
python tests/envs/test_minesweeper_env.py
Build and run the Docker image:
./build_docker.sh latest
docker run -p 8000:8000 minesweeper-env:latest
Hi @acharyaanusha!
Thank you for your pull request.
We require contributors to sign our Contributor License Agreement, and yours needs attention.
You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.
Process
In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.
Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.
If you have received this in error or have any questions, please contact us at [email protected]. Thanks!
@Darktex fixed all the claude code comments. Should be ready for another review
@greptile
Greptile Summary
This PR adds a complete Minesweeper environment implementation following OpenEnv patterns, including core game logic, client/server architecture, Docker support, comprehensive tests, and example agent code.
Key Changes:
- Implemented full Minesweeper game with reveal/flag actions, recursive cell revealing, and win/loss conditions
- Created WebSocket-based client (
MinesweeperEnv) and FastAPI server following OpenEnv architecture - Added Docker support with multi-stage build and health checks
- Integrated into CI/CD pipeline for automated image building
- Provided comprehensive test suite (13 tests) and example agent demonstrating strategy
- Included detailed documentation with action space, reward structure, and usage examples
Issues Found:
- Critical: Dockerfile CMD uses incorrect module path
minesweeper.server.app:appthat will fail at runtime (should beserver.app:app) - Logic bug: In
_reveal_cell, the cell is added to_revealed_cellson line 185 before calling_reveal_recursive, which adds it again on line 207, causing redundant operations - Test design: Tests start a new server for each test method instead of sharing one instance, causing port conflicts and slow test execution
Strengths:
- Clean separation of client/server following OpenEnv invariants
- Well-structured Pydantic models with proper validation
- Good documentation and example code
- Follows established patterns from other environments
Confidence Score: 2/5
- This PR has critical issues that will prevent the Docker container from starting
- Score reflects a critical Dockerfile bug that will cause runtime failure when the container starts. The incorrect module path in the CMD directive means the container will immediately crash. Additionally, there's a logic bug in the cell reveal function and test architecture issues that need addressing.
- Pay close attention to
src/envs/minesweeper_env/server/Dockerfile(incorrect CMD path will break container startup) andsrc/envs/minesweeper_env/server/minesweeper_environment.py(redundant cell addition logic)
Important Files Changed
| Filename | Overview |
|---|---|
| src/envs/minesweeper_env/server/minesweeper_environment.py | Core game logic with recursive reveal bug and redundant cell additions |
| src/envs/minesweeper_env/client.py | Clean client implementation following OpenEnv patterns correctly |
| src/envs/minesweeper_env/server/app.py | Minimal FastAPI app setup following standard pattern |
| tests/envs/test_minesweeper_env.py | Comprehensive test coverage but with test dependencies and server management issues |
| src/envs/minesweeper_env/server/Dockerfile | Dockerfile with incorrect CMD module path that will fail at runtime |
Sequence Diagram
sequenceDiagram
participant Agent
participant Client as MinesweeperEnv<br/>(Client)
participant Server as FastAPI Server
participant Env as MinesweeperEnvironment
Note over Agent,Env: Connection Establishment
Agent->>Client: MinesweeperEnv(base_url)
Client->>Server: WebSocket connect
Server->>Env: Create environment instance
Server-->>Client: Connection established
Note over Agent,Env: Reset Episode
Agent->>Client: reset()
Client->>Server: WS: reset message
Server->>Env: reset()
Env->>Env: Place mines randomly
Env->>Env: Compute mine counts
Env-->>Server: MinesweeperObservation<br/>(board=-1, status=ONGOING)
Server-->>Client: WS: observation response
Client-->>Agent: StepResult(observation)
Note over Agent,Env: Game Loop - Reveal Action
Agent->>Client: step(MinesweeperAction<br/>row=2, col=2, reveal)
Client->>Server: WS: step message with action
Server->>Env: step(action)
Env->>Env: Validate position
Env->>Env: _reveal_cell(2,2)
alt Cell is mine
Env->>Env: Set status=LOST
Env->>Env: Add to revealed_cells
Env-->>Server: Observation(reward=-10.0, done=true)
else Cell is safe
Env->>Env: _reveal_recursive(2,2)
Env->>Env: Add to revealed_cells
alt mine_count == 0
Env->>Env: Recursively reveal neighbors
end
Env->>Env: _check_win_condition()
Env-->>Server: Observation(reward=1.0, done=false)
end
Server-->>Client: WS: observation response
Client-->>Agent: StepResult(observation, reward, done)
Note over Agent,Env: Game Loop - Flag Action
Agent->>Client: step(MinesweeperAction<br/>row=0, col=0, flag)
Client->>Server: WS: step message with action
Server->>Env: step(action)
Env->>Env: _toggle_flag(0,0)
alt Cell already flagged
Env->>Env: Remove from flags
Env-->>Server: Observation(reward=0.0)
else Cell not flagged
Env->>Env: Add to flags
alt Cell is mine
Env-->>Server: Observation(reward=0.5)
else Cell not mine
Env-->>Server: Observation(reward=0.0)
end
end
Server-->>Client: WS: observation response
Client-->>Agent: StepResult(observation, reward, done)
Note over Agent,Env: Session Cleanup
Agent->>Client: close()
Client->>Server: WS: close message
Server->>Env: Cleanup resources
Server-->>Client: Connection closed
@Darktex updated based on greptile comments
Looks good @acharyaanusha. Have you also deployed it to the HF hub, and updated the environments page
Looks good @acharyaanusha. Have you also deployed it to the HF hub, and updated the environments page
@burtenshaw No I haven't but I could do it and let you know
@burtenshaw : pushed it on HF as well https://huggingface.co/spaces/anushaacharya/minesweeper
@Darktex : ready for final review!
@burtenshaw ready for another review
@Darktex hey addressed all the comments. ready for re-review