Neural Tetris is a pet project I created in order to familiarize myself with deep learning. The project includes:
- A fully playable version of Tetris via the web browser
- A python web server backend for storing user games
- A rethinkdb instance with example data
- A script for training a neural network to play like a specified user
The game logic is programmed using JavaScript and utilizes an HTML5 canvas for rendering the game. While a user is playing the game, data will be sent in real-time to a web server using websockets where it will be pushed to a database for training a neural network.
- ← - Move tetrimino left
- → - Move tetrimino right
- ↓ - Soft drop
- X - Rotate tetrimino clockwise
- Z - Rotate tetrimino counterclockwise
- Single line clear - 100 × level
- Double line clear - 300 × level
- Triple line clear - 500 × level
- Tetris line clear - 800 × level
- Combo clear - 50 × count × level
- Soft drop - 1 point per unit dropped
Regarding the game itself, I referred to the Tetris Wiki in order to specify game behaviors, scoring, and color themes. I have chosen not to implement a hard drop or scoring for T-spins.
TODO: Description of functionality
TODO: NNet architecture
Install the following prerequisite libraries:
- Rethinkdb
- Python 3.4+
- The following python libraries:
- Numpy
- Pandas
- websockets
- rethinkdb python driver
- Keras
- One of the following backends:
- The following python libraries:
Navigate to the server
directory and start the rethinkdb engine:
rethinkdb
Once the database server is running on your local machine, you will need to restore the test data by executing the following command in the same directory as above:
rethinkdb restore moves.tar.gz
The engine should pick up on the existing data and you can test that this is working by navigating a browser instance to: http://localhost:8080/
. You should see a web interface and under the "Tables" heading, it should read "1/1 tables ready."
You can verify that the import worked correctly by navigating to the "Data Explorer" tab on the top menu bar of the web interface and typing the following command:
r.db('Tetris').table('moves').group('userId','gameId').pluck('userId','gameId').count()
And pressing the 'Run' button. The resulting "Table view" should look like the following:
While still in the server
directory initiate the web server by running the following command:
python TetrisServer.py
This script will listen for incoming websockets and respond appropriately. There are currently four modes:
- Recording player's actions of a new game
- Replaying player's action of a previously stored game
- Debug replaying of the board state of a previously stored game
- Playing a new game with a saved AI instance
The browser's first message to the webserver will dictate which path is executed by that instance of the web server. The good news is with the newer asyncio library of Python, we only need to execute this script once and it will allow for multiple incoming connections.
At this point, you should be able to load one of the html files in the root directory of the project, their names should correspond to the actions above for the web server.
I have not gotten this far yet, but if you are interested shoot me an email or leave a comment somewhere on the project and we can figure something out.
Dan Maljovec