OpenEnv documentation

Connect4 Environment

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.4.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Connect4 Environment

A classic Connect Four board game environment for training agents on turn-based strategy with a 6×7 grid. Players alternate dropping pieces into columns, aiming to connect four in a row horizontally, vertically, or diagonally.

Quick Start

import asyncio
from connect4_env import Connect4Action, Connect4Env

async def main():
    async with Connect4Env(base_url="http://localhost:8000") as client:
        obs = await client.reset()
        print(f"Board: {obs.board}")
        print(f"Legal moves: {obs.legal_actions}")

        # Drop a piece in column 3
        result = await client.step(Connect4Action(column=3))
        print(f"Reward: {result.reward}, Done: {result.done}")

asyncio.run(main())

Action Space

FieldTypeDescription
columnintColumn index (0–6) where the piece will be dropped

Invalid moves (out-of-range or full column) result in a reward of -1 and end the episode.

Observation Space

FieldTypeDescription
boardlist[list[int]]6×7 grid — 1 = current player, -1 = opponent, 0 = empty
legal_actionslist[int]Column indices that are valid moves

Rewards

OutcomeReward
Win (4 in a row)+1.0
Draw (board full)0.0
Invalid move-1.0
Otherwise0.0

State

FieldTypeDescription
episode_idstrUnique ID for the current game
boardlist[list[int]]Current board state
next_playerintWhose turn it is (1 or -1)
step_countintNumber of steps taken
Update on GitHub