Skip to main content

An OpenAI Gym 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).

demo.png

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=7,
                               reward_winner=1,
                               reward_loser=-1,
                               living_reward=0, 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"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:

Parameters

  • 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.
  • living_reward (float): A reward given at each step of the game, applicable to all ongoing games. Default is 0.
  • 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gymconnectx-1.0.4.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

gymconnectx-1.0.4-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file gymconnectx-1.0.4.tar.gz.

File metadata

  • Download URL: gymconnectx-1.0.4.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for gymconnectx-1.0.4.tar.gz
Algorithm Hash digest
SHA256 a94babe05fb87e271522f3aa2ce814d367c796bda08c8aa284a9bdff19fefae5
MD5 498d1fdb588ccc9e3e3e828e10b6a7f4
BLAKE2b-256 8937b797f355e05b9bc71cc75e1402e896a5b9bad5da387401328dcf712bec98

See more details on using hashes here.

File details

Details for the file gymconnectx-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: gymconnectx-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for gymconnectx-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8ce2dd7e6e64ddfe91b1f5288ca794bbe9d43f95e1ffeb7ed33aad17f285f3a5
MD5 7a5034a1b5d39240bfbc5f4c1ad313e7
BLAKE2b-256 feaa143f4aa72d9e0d5e12b3365b3b613afa1337fcd177b313db2ec1c00a8235

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page