Skip to main content

OpenAI Gym Environment for 2048 extended functionality

Project description

https://badge.fury.io/py/gym-2048-extended.svg

This package implements the classic grid game 2048 for OpenAI gym environment.

Summarizing my changes to the repo Activated Geek Gym 2048:

  • changed the requirements.txt to be more flexible

  • added the method is_action_possible

  • added different reward schemes

  • added the render mode dict

  • added game.py a PyGame that uses the GymEnvironment from this repo

Install

Pip

pip install gym-2048-extended

From cloned repository

python setup.py install

Environment(s)

The package currently contains two environments

  • Tiny2048-v0: A 2 x 2 grid game.

  • 2048-v0: The standard 4 x 4 grid game.

I only checked the 4 x 4 grid game, the other one might not work.

Attributes

  • Observation: All observations are n x n numpy arrays representing the grid. The array is 0 for empty locations and numbered 2, 4, 8, ... wherever the tiles are placed.

  • Actions: There are four actions defined by integers.
    • LEFT = 0

    • UP = 1

    • RIGHT = 2

    • DOWN = 3

  • Reward: Reward is the total score obtained by merging any potential tiles for a given action. Score obtained by merging two tiles is simply the sum of values of those two tiles.

Rendering

Currently 2 rendering modes are implemented

  • basic print rendering (mode='human')

  • dict rendering (mode='dict') returns a dictionary with the board state

Usage

PyGame Interactive Demo

game.py provides a PyGame implementation of the game. Use the arrow keys to play, q and n can be used to quit the game or restart it.

The game serves as a demo, the different reward schemes and step function can be explored.

python gym_2048/game.py
pygame.png

Basic Demo

Here is a sample rollout of the game which follows the same API as OpenAI gym.Env.

import gym_2048
import gym


if __name__ == '__main__':
  env = gym.make('2048-extended-v2')
  env.seed(42)

  env.reset()
  env.render()

  done = False
  moves = 0
  while not done:
    action = env.np_random.choice(range(4), 1).item()
    next_state, reward, done, _, info = env.step(action)
    moves += 1

    print('Next Action: "{}"\n\nReward: {}'.format(
      gym_2048.Base2048Env.ACTION_STRING[action], reward))
    env.render()

  print('\nTotal Moves: {}'.format(moves))

NOTE: Top level import gym_2048 is needed to ensure registration with Gym.

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-2048-extended-1.9.tar.gz (83.9 kB view details)

Uploaded Source

Built Distribution

gym_2048_extended-1.9-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file gym-2048-extended-1.9.tar.gz.

File metadata

  • Download URL: gym-2048-extended-1.9.tar.gz
  • Upload date:
  • Size: 83.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for gym-2048-extended-1.9.tar.gz
Algorithm Hash digest
SHA256 f48f2ca6dc848334e57f4a24e43cd9697495957ad8e0965bd80414c88d35d541
MD5 777519f403a01b76ec957f791b8c0fb0
BLAKE2b-256 a3d2eae38f2b3103ea727213faf582151f679af474c374549bfa8e9071d73f03

See more details on using hashes here.

File details

Details for the file gym_2048_extended-1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for gym_2048_extended-1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 dec83e34fb4a98cbf6ef98ca35d3762be57e795e08c1bd14a4bbd88db24aab73
MD5 badc7988e7d2904de8f8fc6cb1c2a640
BLAKE2b-256 c2443a9622a513f6bb436de904c1416f3cd125cc4e76fba2e7d38f869becd7e3

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