Skip to main content

Tetris (NES) for OpenAI Gym

Project description

gym-tetris

BuildStatus PackageVersion PythonVersion Stable Format License

An OpenAI Gym environment for Tetris on The Nintendo Entertainment System (NES) based on the nes-py emulator.

Installation

The preferred installation of gym-tetris is from pip:

pip install gym-tetris

Usage

Python

You must import gym_tetris before trying to make an environment. This is because gym environments are registered at runtime. By default, gym_tetris environments use the full NES action space of 256 discrete actions. To constrain this, gym_tetris.actions provides an action list called MOVEMENT (20 discrete actions) for the nes_py.wrappers.JoypadSpace wrapper. There is also SIMPLE_MOVEMENT with a reduced action space (6 actions). For exact details, see gym_tetris/actions.py.

from nes_py.wrappers import JoypadSpace
import gym_tetris
from gym_tetris.actions import MOVEMENT

env = gym_tetris.make('TetrisA-v0')
env = JoypadSpace(env, MOVEMENT)

done = True
for step in range(5000):
    if done:
        state = env.reset()
    state, reward, done, info = env.step(env.action_space.sample())
    env.render()

env.close()

NOTE: gym_tetris.make is just an alias to gym.make for convenience.

NOTE: remove calls to render in training code for a nontrivial speedup.

Command Line

gym_tetris features a command line interface for playing environments using either the keyboard, or uniform random movement.

gym_tetris -e <environment ID> -m <`human` or `random`>

Environments

There are two game modes define in NES Tetris, namely, A-type and B-type. A-type is the standard endurance Tetris game and B-type is an arcade style mode where the agent must clear a certain number of lines to win. There are three potential reward streams: (1) the change in score, (2) the change in number of lines cleared, and (3) a penalty for an increase in board height. The table below defines the available environments in terms of the game mode (i.e., A-type or B-type) and the rewards applied.

Environment Game Mode reward score reward lines penalize height
TetrisA-v0 A-type
TetrisA-v1 A-type
TetrisA-v2 A-type
TetrisA-v3 A-type
TetrisB-v0 B-type
TetrisB-v1 B-type
TetrisB-v2 B-type
TetrisB-v3 B-type

info dictionary

The info dictionary returned by the step method contains the following keys:

Key Type Description
current_piece str the current piece as a string
number_of_lines int the number of cleared lines in [0, 999]
score int the current score of the game in [0, 999999]
next_piece str the next piece on deck as a string
statistics dict the number of tetriminos dispatched (by type)
board_height int the height of the board in [0, 20]

Citation

Please cite gym-tetris if you use it in your research.

@misc{gym-tetris,
  author = {Christian Kauten},
  howpublished = {GitHub},
  title = {{Tetris (NES)} for {OpenAI Gym}},
  URL = {https://github.com/Kautenja/gym-tetris},
  year = {2019},
}

References

The following references contributed to the construction of this project.

  1. Tetris (NES): RAM Map. Data Crystal ROM Hacking.
  2. Tetris: Memory Addresses. NES Hacker.
  3. Applying Artificial Intelligence to Nintendo Tetris. MeatFighter.

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_tetris-3.0.3.tar.gz (36.9 kB view details)

Uploaded Source

Built Distribution

gym_tetris-3.0.3-py3-none-any.whl (34.6 kB view details)

Uploaded Python 3

File details

Details for the file gym_tetris-3.0.3.tar.gz.

File metadata

  • Download URL: gym_tetris-3.0.3.tar.gz
  • Upload date:
  • Size: 36.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.7

File hashes

Hashes for gym_tetris-3.0.3.tar.gz
Algorithm Hash digest
SHA256 77d33e58d75a46cedb0c56dfef556516f345e79f69c52e0bca2f08d637e6443e
MD5 b435e866cd60392b90e722b5b30b93bb
BLAKE2b-256 e70d9746e7fd69c55446057d3fa05bdd064d80a008ee0fa348eb6f21f253f7c2

See more details on using hashes here.

File details

Details for the file gym_tetris-3.0.3-py3-none-any.whl.

File metadata

  • Download URL: gym_tetris-3.0.3-py3-none-any.whl
  • Upload date:
  • Size: 34.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.7

File hashes

Hashes for gym_tetris-3.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2826bfe1b285c9ac5336e69071e7898ad5dd194392146c2d780cd547eff00d27
MD5 dd8d358d12739c45df223c91428bddcd
BLAKE2b-256 c52c4549b8e44400d71d428caabd05b0c99525bb158d9f6cd4a7a8c65d27a899

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