-
Notifications
You must be signed in to change notification settings - Fork 81
[ENVIRONMENT] Connect4 Environment #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ENVIRONMENT] Connect4 Environment #101
Conversation
|
Hi @GboyeStack-Robotics-ML-Engineer! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn 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 If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
| import numpy as np | ||
| from envs.atari_env import AtariEnv, AtariAction | ||
|
|
||
| # import envs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove debug code?
| print("\nResetting environment...") | ||
| result = env.reset() | ||
| print(f"Screen shape: {result.observation.screen_shape}") | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newlines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new Connect4 environment integration to the OpenEnv framework, implementing a two-player Connect 4 game that can be accessed via HTTP server. This follows the OpenEnv pattern of server-side environment implementation with client-side HTTP access.
- Implements Connect4Environment server with game logic for a 6x7 board
- Provides type-safe client wrapper (Connect4Env) for HTTP communication
- Adds Docker support and GitHub Actions workflow integration
- Includes example script with visualization using matplotlib
Reviewed Changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| src/envs/connect4_env/server/connect4_environment.py | Implements core Connect4 game logic with win detection and state management |
| src/envs/connect4_env/server/app.py | Creates FastAPI app wrapper for the Connect4 environment server |
| src/envs/connect4_env/server/init.py | Exports Connect4Environment for server module |
| src/envs/connect4_env/server/Dockerfile | Defines Docker image for running Connect4 server |
| src/envs/connect4_env/models.py | Defines type-safe Action, Observation, and State models |
| src/envs/connect4_env/client.py | HTTP client for communicating with Connect4 server |
| src/envs/connect4_env/init.py | Main module exports for Connect4 environment |
| examples/connect4.py | Example script demonstrating Connect4 environment usage with visualization |
| examples/atari_simple.py | Modified to use localhost URL instead of Docker image |
| examples/OpenEnv_Tutorial.ipynb | Updated with execution outputs and kernel version |
| .github/workflows/docker-build.yml | Adds Connect4 environment to Docker build workflow |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| CMD curl -f http://localhost:8000/health || exit 1 | ||
|
|
||
| # Run server | ||
| CMD ["uvicorn", "envs.my_env.server.app:app", "--host", "0.0.0.0", "--port", "8000"] No newline at end of file |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CMD directive references 'envs.my_env.server.app:app' but should reference 'envs.connect4_env.server.app:app' to match the actual module path. This will cause the server to fail to start.
| CMD ["uvicorn", "envs.my_env.server.app:app", "--host", "0.0.0.0", "--port", "8000"] | |
| CMD ["uvicorn", "envs.connect4_env.server.app:app", "--host", "0.0.0.0", "--port", "8000"] |
src/envs/connect4_env/__init__.py
Outdated
| This module provides OpenEnv integration for Connect4 2600 games via the | ||
| Arcade Learning Environment (ALE). | ||
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation incorrectly refers to 'Connect4 2600 games via the Arcade Learning Environment (ALE)'. Connect4 is a board game, not an Atari 2600 game. This appears to be copied from the Atari environment template and should be updated to describe Connect4 specifically.
| This module provides OpenEnv integration for Connect4 2600 games via the | |
| Arcade Learning Environment (ALE). | |
| This module provides OpenEnv integration for the classic Connect4 board game. |
src/envs/connect4_env/client.py
Outdated
| >>> print(result.observation.board) | ||
| >>> | ||
| >>> # Take an action | ||
| >>> result = client.step(Connect4Action(action_id=3)) |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example uses 'action_id=3' but Connect4Action expects 'column' as the parameter name according to the model definition. This should be 'Connect4Action(column=3)'.
| >>> result = client.step(Connect4Action(action_id=3)) | |
| >>> result = client.step(Connect4Action(column=3)) |
examples/connect4.py
Outdated
| break | ||
|
|
||
| action_id = int(np.random.choice(result.observation.legal_actions)) | ||
| result = env.step(Connect4Action(action_id)) |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Connect4Action expects a 'column' parameter, not a positional argument. This should be 'Connect4Action(column=action_id)' to match the dataclass definition.
| result = env.step(Connect4Action(action_id)) | |
| result = env.step(Connect4Action(column=action_id)) |
| return ax.patches | ||
|
|
||
| # Create animation | ||
| ani = FuncAnimation(fig, animate_frame, frames=len(frames), |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable ani is not used.
|
|
||
| def step(self, action: Connect4Action): | ||
| col = action.column | ||
| reward = 0.0 |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| from envs.connect4_env import Connect4Action, Connect4Env | ||
|
|
||
|
|
||
| def render_connect4_board(board, ax, player_colors={1: "red", 2: "yellow", -1: "yellow"}, show=True): |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixing implicit and explicit returns may indicate an error, as implicit returns always return None.
src/envs/connect4_env/server/app.py
Outdated
| @@ -0,0 +1,12 @@ | |||
| from core.env_server import create_fastapi_app | |||
| from ..models import Connect4Action, Connect4Observation, Connect4State | |||
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import of 'Connect4State' is not used.
| from ..models import Connect4Action, Connect4Observation, Connect4State | |
| from ..models import Connect4Action, Connect4Observation |
| from .models import Connect4Action, Connect4Observation, Connect4State | ||
|
|
||
| if TYPE_CHECKING: | ||
| from core.containers.runtime import ContainerProvider |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import of 'ContainerProvider' is not used.
| from core.containers.runtime import ContainerProvider |
| @@ -0,0 +1,90 @@ | |||
| import uuid | |||
| import numpy as np | |||
| from core.env_server import Action, Environment, Observation | |||
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import of 'Action' is not used.
Import of 'Observation' is not used.
| from core.env_server import Action, Environment, Observation | |
| from core.env_server import Environment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really good. Just a couple of changes :)
- Can you ask your favorite LLM to write tests for the connect4 logic? I love that we don't have to use a library for it, but I'm always worried that we may have some off-by-one errors here and there...
- Can you address the minor nits that Copilot found?
That's it!!
|
@Darktex I already addressed the minor issues that copilot raised. I have commited this. As for the tests I have included a unittest file under the test folder |
|
Thank you so much!! |
Add Connect4 Environment and Supporting Models to OpenEnv
Summary
This pull request introduces a new Connect4 Environment to the OpenEnv framework, expanding its suite of interactive learning environments.
The Connect4 environment follows OpenEnv’s modular design and integrates seamlessly with the existing
Environment,Action, andObservationinterfaces.🔑 Key Features
🧠 New Environment
resetandsteplifecycle.uuid.⚙️ Model Additions
Connect4Action– Defines valid column selections as agent actions.Connect4State– Represents board state, active player, and step count.Connect4Observation– Encapsulates board view, legal actions, reward signal, and done status.🎮 Gameplay Logic
(vertical, horizontal, diagonal-down, diagonal-up).🧩 Visualization Support
Testing out the env
To test out the environment, open two terminals
On terminal 1:
Run the following from root dir (
OpenEnv/)cd srcThen start the server:
-
python -m envs.connect4_env.server.appOn the terminal 2:
Run
python examples/connect4.pyfrom root directory (OpenEnv/)This is an example of the output:
