Tic Tac Toe


'Ravi Shekhar', 'jayawan wijekoon'


The motivation for this project came from last year when we tried to build a spiking neural network that can play hearts (card game). Then we didnot have a framework like Nengo to work on so we had to build everything from scratch. This year since we had the Nengo framework, i wanted to try the project again. I talked to Chris about building a game and especially a Hearts game, he encouraged me saying it can be done but it will be difficult to build. He suggested we should start with something smaller maybe like comparing cards or something. We settled on Tic Tac Toe. It is a small enough game with few basic rules. This seemed like a problem that can be tackled during the workshop.

Implementation 1

'Ravi Shekhar'

A basic implementation of the game was done in JAVA for 2 human players. It had a command line output. The files can be found added as the attachments. Further a GUI interface with the Tic Tac Toe grid board was added and the second human player was replaced with a computer program making the moves. The idea was to replace this computer player with the Nengo engine eventually.

The basic strategy while building the Nengo engine was three fold. In order to make it easy to understand, let us assume the cue for the computer is 'O' and the human is 'X'.
1. Win: The first thing to look for is a win. If you have 2 'O' already in a row/column/diagonal and there is a blank square in the same row/column/diagonal then place the cue and win the board. So the idea is to scan for winning combinations and make the winning move.
2. Block: The next strategy is to look to block the winning combinations of the opponent. If you have 2 'X' already in a row/column/diagonal and there is a blank square in the same row/column/diagonal then place the cue and block it. The idea is to scan for losing combinations and make the blocking move.
3. Intelligent Move: If it is neither a winning move or a blocking move, the we make an "intelligent" move. This was first experimented with being finding the first empty square and placing a cue. The the squares were prioritized depending upon the locations. The center squares got the highest priority, then the corners and then the other 4 squares. So if the center square is not occupied we go ahead and place the cue there, else we move on to the corners and then to the rest. Even though tic tac toe is simple game, it is really complex to construct winning situations for the program. We as humans can play some complex moves which will construct into winning positions over time but planning ahead was not considered for the implementation.

Also an important thing to note is that the system does not learn over time to make better moves. It has a set of rules it follows and it will come up with same moves if presented with the same situation again (even if it lost earlier in the given situation). The system needs to be told about the rules. Just like we as humans need to know the basic rules for playing a game, however we can learn new strategies over time.


The Tic Tac Toe engine running on Nengo was demonstrated at the final presentation of the workshop. A video of the demo is added to the attachments. The image below shows the nengo network, the basal ganglia output (which decides which rule wins), the motor output which is the play suggested by nengo engine depending upon the current board situation. The image also shows the Tic Tac Toe grid board on the right. The "Nengo" button is used to get the output from the nengo engine. The other player can just click on the particular grid square he wants to place his cue on.

Demo Image with Nengo engine running

The files for nengo engine with JAVA front end are added as TicTacToeNengoProject?.zip. The folder contains the TicTacToe?.py file which contains the rules and modules for implementation. The Source folder contains the JAVA class files.

In order to run the project, create a java project and add the class files. Also include the libraries from nengo to the project. Put the TicTacToe?.py file in the java classpath. When you the run the main function from java it should create the nengo network and open up 2 windows. One of them is the tic tac toe grid board and the other is the nengo simulation window. contains simple basic rules to make an "intelligent move". The file contains the full rules. The full rules were not used because it takes a lot of time for the rules to converge and real time demo could not be made.


Terry Stewart, Chris Eliasmith

Implementation 2

'jayawan wijekoon'

The implementation 1 uses 7000 to 10000 neurons. The motive of this implementation is to reduce the number of neurons to less than 600 neurons. So that this Tic Tac Toe network could be implemented in a small-scale neuromorpic spiking hardware. By considering the logical operations needed to perform 'Win' and 'Block' modes a simplified common processing element (PE) has been formulated in Nengo as follows.

Common Processing Element (PE)

1. PE acquires three consecutive elements to test for the Winning (Blocking) move with 'O' ('X'). This can be a row, column or diagonal.
2. Multiply these input elements with their corresponding location weights (weight values: 6 for the 1st element, 5 for the 2nd element, 4 for the 3rd element).
3. Sum all the weighted inputs.
4. Subtract 8 from the sum of the weighted inputs gives the location of the winning (blocking) location.

E.g.: Let's assume a row having 'O', space and 'O';
when testing for the Winning move,

1=> this will give an input matrix [1, 0, 1] ('O' has input hence 1, and space has no input hence 0)
2=> input multiplied by weight matrix W=[6, 5, 4] give us [6, 0, 4]
3=> sum of all the weighted inputs gives value 10.
4=> subtract 8 from the sum of the weighted inputs gives 2. I.e. the location of the second element is the Winning move location for the computer.

The Nengo program goes through different modes of operation to perform Winning, Blocking and Intelligent moves. A Switcher (as in the ppt slides) implements a winner take all to change the modes of operation.

Mode 1: This is the initial mode. It tests for the Winning move. The test is performed with rows/columns and diagonals with the 'O' inputs. PE performs these iterations (3 rows, 3 columns and 2 diagonals) until the Winning move is found.
Mode 2: If there is not any Winning move switcher switch to Mode 2 and the Blocking move test is performed with rows/columns and diagonals with 'X' inputs. PE performs these iterations until Blocking move is found.
Mode 3: If there is not any Blocking move switcher switch to Mode 3, Mode 3 performs 'Intelligent Move' (e.g.: center of the square get the highest priority) or 'Do Any' move.


Statement of results/simulation picture, etc.