A pure Python 2048 engine, based on the original game!
Project description
Py2048-Engine
A pure 🐍 , fully type hinted, well documented 2048 Engine that just works.
Installation
First, install Py2048-Engine
.
The easiest method is with PyPi:
$ pip install Py2048-Engine
Alternatively, you can clone the repo:
$ git clone https://github.com/http-samc/2048.py
Getting Started
If you're looking to play the game itself
You can get a quick and intuivite introduction to the engine by using the built-in game tester. First import runTest()
from the package and call it to get started.
>>> from Py2048_Engine.Test import runTest
>>> runTest()
You'll be given a printout of the board, a 4 by 4 grid. All 'empty' spaces have a -
. From here, you are in a loop of being able to move in any direction.
- - - 4
- - - -
- - 2 -
- - - -
Choose a movement:
[1] Left
[2] Right
[3] Up
[4] Down
Choice: 1
Many moves later...
32 - - -
- - 4 -
2 - - -
4 - - -
Eventually, you'll get a printout when you win or lose.
If you want to implement the engine in a project
First, import the game class from Py2048_Engine.Game
. Then, you can create a game like:
from Py2048_Engine.Game import Game
game = Game()
You can get your current board via Game.__repr__()
(pretty printed String) or access the raw board (2D int array) with Game.getBoard()
like:
print(game) # Pretty printed output
board = game.getBoard() # Type: List[List[int]], better for custom applications
You can perform an action on your game board by using Game.left()
, Game.right()
, Game.up()
, or Game.down()
.
game.left()
game.right()
game.up()
game.down()
Eventually, you'll either win or lose. To handle this, you want to catch some Exceptions
from Py2048_Engine.Exceptions
.
All thrown Exceptions
inherit from Py2048_Engine.Exceptions.GameException
, with Py2048_Engine.Exceptions.GameWonException
signifing a game win and Py2048_Engine.Exceptions.GameLostException
signifing a game loss.
GameException
and its children have a few important attributes, especially if you're prototyping.
Attribute | Desc |
---|---|
message |
a message describing why the GameException was thrown. |
numMoves |
the number of moves taken before the GameException was thrown. |
board |
the final board before the GameException was thrown. Type of List[List[int]] |
A sample catch might look like this:
...
from Py2048_Engine.Exceptions import GameException, GameWonException, GameLostException
while True:
try:
game.left()
game.right()
game.up()
game.down()
except GameException as e:
# At this point we have access to the final board,
# final number of moves, and an exit message.
if type(e) is GameWonException:
print("YOU WON!!!")
elif type(e) is GameLostException:
print("Better luck next time :(")
print(e.message)
print(f"Finished in {e.numMoves} moves.")
print(f"Final board:\n{e.finalBoard}")
Questions
See the full docs at DOCS.md. If you have any other questions, feel free to contact the author, Samarth Chitgopekar!
Indirect Contributors
Thanks to this post's marked answer on Stack Overflow for some help on Game.__repr__()
and Bhargavi Goel's 2017 paper on the mathematics behind 2048, which helped me create this engine!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Py2048-Engine-1.0.0.tar.gz
.
File metadata
- Download URL: Py2048-Engine-1.0.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dda6493a61bc751ea2f763dec7f8156010de01aafea66a25b463426f930326ab |
|
MD5 | 69e4789cedbc1f76ccf503093095d0ea |
|
BLAKE2b-256 | c52393e9bb88759dcb946a6bca637d3a9ad3ae7cb50ca248707fd7f2f7f0638c |
File details
Details for the file Py2048_Engine-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: Py2048_Engine-1.0.0-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d97b94ccf162491c441f83dcb1ff66e5a160e978222760f15ee28669ddfd068f |
|
MD5 | 33045c0fe3d60e60bcc7ded37353cd07 |
|
BLAKE2b-256 | dd7421feaa5efeca8925cadfa20d5f96740a572fedb7589fb1327ca46c32211f |