Description
This is my 2048 game simulation in Python. To play the original game, click here. This code can also be used to strenghten some basic concepts of Python for beginners. Do feel free to let me know your feedback through the comments section. Thanks!
Compatibility
This code has been developed and tested well in Windows in the Jupyter Notebook environment (specifically, the Anaconda distribution).
- If you would like to run the code in a Jupyter Notebook, CoLab, or similiar, download the py2048.ipynb version and run it on your environment of choice.
- If you would like to run it directly in your terminal, download the py2048.py version and run it from terminal. Note that this has clear_screen() functionality disabled since it does not work in terminal.
- If you would like to run it on an android device's mobile browser or without any prior installation of python environments on a laptop or PC, the code will also work well in Google's CoLab environment. Click here to view and run my code in the CoLab environment. You may provide your comments there as well.
First, enter the size of the board. For example, if you want to play on a 3x3 board, enter 3.
Next, enter the winning value, which is the value at which you would win if you add up to it. Remember that it should be a power of 2.
Note: The default board size is 5 and win value is 2048. This is set incase of an invalid entry of either of the two respectively.
Now, you may enter your move. To make a move, press either of the WASD keys, or press 'x' to stop the game, 'r' to clear the board or 'g' to restart the entire game. You may do the aforementioned in CAPS LOCK as well.
The game will stop by itself if the win value is identified in the board or if further valid moves are not possible. You will recieve your game stats at the end too !
Special Notes:
- The program will randomly spawn a 2 or a 4 after every valid move if the board size is 5 or above. For values, lesser than 5, it would spawn only one 2.
- Hidden developer option ! Press capital 'Z' at any point in the game to fill the empty spots of the board with just one entry. The spots would be filled randomly with a 2 or a 4 in accordance with the previous point. This is to test the game overall, but more specifically, the
move(<parameters>)
function. Note that this will increment themoves_count
andattempts
variable by just one irrespective of number of spaces automatically filled by the function.
Demonstration
Here's a simple demonstration of the game in .gif format.