Skip to main content

A simple Tetris engine for OpenAI Gym

Project description

gym-simpletetris

Simple Tetris is a simple Tetris environment built entirely in Python. Several options are provided for modifying the environment and its reward system.

Reinforcement learning agent playing Tetris

Installation

pip

The preferred installation of gym-simpletetris is via pip:

pip install gym-simpletetris

Clone the project

The code is easy to read and modify. If you wish to modify the package to your needs, then follow these instructions:

  1. Fork the project
  2. Download it using:
git clone https://github.com/<YOUR-USERNAME>/gym-simpletetris
  1. Make your changes
  2. Install it using pip:
cd gym-simpletetris
pip install -e ./

Usage

You can create an environment using gym.make and supplying the environment id.

import gym
import gym_simpletetris

env = gym.make('SimpleTetris-v0')
obs = env.reset()

episode = 0
while episode < 10:
    action = env.action_space.sample()
    obs, reward, done, info = env.step(action)
    
    if done:
        print(f"Episode {episode + 1} has finished.")
        episode += 1
        obs = env.reset()

env.close()

Environment

Several options are provided to modify the environment. Arguments can be used at the creation of the environment:

import gym
import gym_simpletetris

env = gym.make('SimpleTetris-v0',
               height=20,                       # Height of Tetris grid
               width=10,                        # Width of Tetris grid
               obs_type='ram',                  # ram | grayscale | rgb
               extend_dims=False,               # Extend grayscale dimensions
               render_mode='rgb_array',         # Only supports rgb_array
               reward_step=False,               # see reward table
               penalise_height=False,           # see reward table
               penalise_height_increase=False,  # see reward table
               advanced_clears=False,           # see reward table
               high_scoring=False               # see reward table
               )

If using an observation type of grayscale or rgb then the environment will be as an array of size 84 x 84. If using grayscale, then the grid can be returned as 84 x 84 or extended to 84 x 84 x 1 if entend_dims is set to True. The rgb array will always be returned as 84 x 84 x 3.

Note: The environment only supports rendering as rgb_array at this time but a human render option is planned in a future update.

Reward Table

Option Description
reward_step Adds a reward of +1 for every time step that does not include a line clear or end of game.
penalise_height Penalises the height of the current Tetris tower at every time step. A negative reward equal to the height of the current tower is added at every time step.
penalise_height_increase Penalises an increase in the height of the Tetris tower. At each time step, if the height increases then a negative reward equal to the increase is given.
advanced_clears Changes the rewards for clearing lines to be similar to the modern Tetris system. The new rewards are 100 for a single line clear, 250 for a double line clear, 750 for a triple line clear and 3000 for a Tetris (quadruple line clear).
high_scoring Changes the reward given for each line clear to 1000.

Note: penalise_height and penalise_height_increase cannot be used together. If both are enabled then penalise_height will be used.

info dictionary

The info dictionary returned by each step includes the following keys:

Key Type Description
time int The time passed (no. of steps) since the start of the current game
current_piece string The letter representing the current piece
score int The score of the current game
lines_cleared int The number of lines cleared in the current game
statistics dict The number of Tetris pieces dispatched by type

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

gym_simpletetris-0.0.2.tar.gz (7.4 kB view details)

Uploaded Source

File details

Details for the file gym_simpletetris-0.0.2.tar.gz.

File metadata

  • Download URL: gym_simpletetris-0.0.2.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.13

File hashes

Hashes for gym_simpletetris-0.0.2.tar.gz
Algorithm Hash digest
SHA256 db8bb6a8c7eaa4ae6c72e5a6efa07de344cc51a10f7a76ae3f1d5fd025622441
MD5 105db501e7f5dce23f8ef3b9fcc7ea8f
BLAKE2b-256 76f8b1fff24b3a5ab41d5756e43d2b3bdfcfcbc1d8f9d30f78757d533184ee52

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