0

Openenv Connect 4 RL Env (Community)

Fresh

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.

Type
RL Env
License
bsd-3-clause
Published
Nov 2025

Cite

Notes

Only stored in your browser.

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