/neuralTetris

An HTML5/javascript implementation of Tetris by rendering to a <canvas> element. This version records users' moves and changes to board state and sends them to an included python web server script that will record the data into a rethinkdb database. This data can then be used to train a neural network using keras which can feed a client with data allowing the neural network to play like a given user.

Primary LanguageJavaScriptMIT LicenseMIT

Neural Tetris

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

Implementation Details

The Game

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.

Gameplay details

Controls

  • - Move tetrimino left
  • - Move tetrimino right
  • - Soft drop
  • X - Rotate tetrimino clockwise
  • Z - Rotate tetrimino counterclockwise

Scoring

  • 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

Advanced Players

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.

The Server

TODO: Description of functionality

The Brain

TODO: NNet architecture

Setting up your own instance

Installing Prerequisites

Install the following prerequisite libraries:

Spinning up the Database

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:

A listing of the example games in the test database

Starting the Web server

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.

Testing the game

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.

Contributing

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.

Author

Dan Maljovec