An OpenAI Gymnasium Environment Connect X Game with GUI. ConnectX is a game for two players that is based on the well-known Connect 4. The goal is to place X coins in a row, column, or diagonal on a board with dimensions M by N.
Project description
ConnectX Game Environment
This repository contains the implementation of a Connect Game Environment using OpenAI's Gym and Pygame for rendering. The environment supports various game modes like Connect 4 and includes features like GUI display, avatar support for players, and different modes of player interaction (human, random).
Table of Contents
Description
The Connect Game Environment is a customizable and interactive environment for simulating Connect-style games. It leverages the OpenAI Gym interface for standard reinforcement learning interactions and uses Pygame for graphical rendering. The environment allows for different configurations of the board size and connect length and includes support for different player types, including human players and random agents.
Installation
To use this environment, you need to have Python installed. You can install the necessary packages using pip
:
pip install gymconnectx
Usage
import gymconnectx
def run_game_env():
env = gymconnectx.gym.make('gymconnectx/ConnectGameEnv',
connect=4,
width=7,
height=6,
reward_winner=1,
reward_loser=-1,
reward_living=0,
reward_draw=0.5,
reward_hell=-0.5,
max_steps=100,
delay=100,
square_size=100,
avatar_player_1='img_cat.png',
avatar_player_2='img_dog.png')
env.reset()
while not env.is_done and env.current_step < env.max_steps:
try:
move = env.set_players(player_1_mode='random', player_2_mode='random')
observations, rewards, done, _, info = env.step(move)
env.render(mode='terminal_display')
env.render(mode='gui_update_display')
print(f'Observation: {observations}')
print(f"Step: {env.current_step}, "
f"Move: {move}, "
f"Rewards: {rewards}, "
f"Done: {done}, "
f"Info: {info}")
print(env.get_game_status())
if done:
break
else:
env.current_step += 1
except Exception as e:
print(f"An error occurred: {str(e)}")
break
if __name__ == "__main__":
run_game_env()
Setting Player Modes
You can set different player modes using the set_players method. Here are some examples:
Example 1: Both Players Make Random Moves
move = env.set_players(player_1_mode='random', player_2_mode='random')
Example 2: Player 1 Makes Moves Through the Terminal, Player 2 Makes Random Moves
move = env.set_players(player_1_mode='human_terminal', player_2_mode='random')
Example 3: Player 1 Makes Moves Through the GUI, Player 2 Makes Random Moves
move = env.set_players(player_1_mode='human_gui', player_2_mode='random')
Example 4: Player 1 Makes Moves Through the GUI, Player 2 Follows a Custom Policy
if env.get_current_player() == 1:
move = env.set_players(player_1_mode='human_gui')
else:
move = env.get_action_random() # Add your policy here
Example 5: Both Players Make Moves Through the GUI
move = env.set_players(player_1_mode='human_gui', player_2_mode='human_gui')
ConnectGameEnv Class
The ConnectGameEnv
class is designed to simulate a Connect 4-like game environment, allowing customization through various parameters to accommodate different rules and interfaces. Below are the parameters it accepts:
Please use this bibtex if you want to cite this repository in your publications:
@misc{pytorchaaac,
author = {Sholichin, Fauzi},
title = {ConnectX Game Environment},
year = {2024},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/fauzisho/GymConnectX}},
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gymconnectx-2.0.0.tar.gz
.
File metadata
- Download URL: gymconnectx-2.0.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 082dba2a1dcb2f78f8d4dba8fea6d8da8e552b7c97f46dc9e3af343eda688a15 |
|
MD5 | 2d52201dd42f39a2ff08d434e6865b6e |
|
BLAKE2b-256 | 9806071f43573d5d1b7b5214e14f941368bc2c32249a8654a6ab6a5df72bc166 |
File details
Details for the file gymconnectx-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: gymconnectx-2.0.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3648dbc7c1a6df1156ba4085ace29aef129dadc61a0fcbfe9c04561a031b3ef |
|
MD5 | d558fc946d904dc53921196296218766 |
|
BLAKE2b-256 | 1a221ee58b790a7bb3aff4ae7a6ba3b71bc62f208c2ca9bc72aee82629d1beda |