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).
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.
To use this environment, you need to have Python installed. You can install the necessary packages using pip
:
pip install gymconnectx
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()
You can set different player modes using the set_players method. Here are some examples:
move = env.set_players(player_1_mode='random', player_2_mode='random')
move = env.set_players(player_1_mode='human_terminal', player_2_mode='random')
move = env.set_players(player_1_mode='human_gui', player_2_mode='random')
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
move = env.set_players(player_1_mode='human_gui', player_2_mode='human_gui')
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:
- connect (int): The number of consecutive tokens required to win. Default is
4
. - width (int): The width of the game board, measured in number of columns. Default is
7
. - height (int): The height of the game board, measured in number of rows. Default is
7
. - reward_winner (float): The reward given to the winner at the end of the game. Default is
1
. - reward_loser (float): The penalty (or negative reward) given to the loser at the end of the game. Default is
-1
. - reward_living (float): A reward given at each step of the game, applicable to all ongoing games. Default is
0
. - reward_draw (float): A reward given to both players in case of a draw. Default is
0
. - reward_hell (float): The reward given to the player if they allow the opponent's chance to win in the next move. Default is
-0.5
. - max_steps (int): The maximum number of steps the game can take before it ends. Default is
100
. - delay (int): Time delay (in milliseconds) between moves, primarily used for GUI purposes. Default is
100
. - square_size (int): Size of the squares in the GUI. Default is
100
. - avatar_player_1 (string): Base64/path format avatar image for player 1. This is used in the GUI.
- avatar_player_2 (string): Base64/path format avatar image for player 2. This is used in the GUI.