www.damionmullins.com Damion Mullins, MS, MBA

Tic Tac Toe Machine Learning with JavaScript

Version 1.0, developed in 2021



I developed this Tic Tac Toe application in JavaScript in an attempt to simplify and demonstrate machine learning with an actual and readable working example. This application applies machine learning algorithms and basic statics in order to learn and adapt without explicit instructions.

Aside from moving randomly while there is no data to analyze, like during the first game, there is no hard-coded source code that tells the app where to move. Instead, the app analyzes the game board and each game played to determine its moves. The more you play well, the more the app should get better. First, it'll start to tie games, and then eventually it should start to win.

You are player X and the app is Player O. Since the app needs you to teach it, you will go first. The app saves every game using indexedDB. Pressing the reset button will clear out the app's memory. You can also choose either a 3x3 or 5x5 game board.

3 x 3 5 x 5 Reset
Games Played: Player X Wins: Player O Wins: Ties:
     
     
     
In order for the application (player O) to play a game, it performs the following workflow:
  1. First, player O will analyze the game board to determine the type of game and winning moves
  2. If player O goes first, it will choose a first move based on the frequency of first moves of previous winning games
  3. During game play, player O checks for a winning move based on current moves compared to previous games where a player won using the same moves
  4. If there are no winning moves, player O checks for a blocking move based on current moves compared to previous games where the same moves were played
  5. If there are no blocking moves, player O tries to ascertain the best move based on current moves compared to previous games.
Player O uses frequency in order to determine what move to use when comparing data. In short, if a move occurs more often than another move, then it'll be chosen. The only hard-coded instructions for player O is to choose a random move if no data is available to determine a move. The below code is the entire file for this Tic Tac Toe application. Likewise, you can see the same JavaScript file by viewing sources (js/ticTacToe.js) in your browser
        
         // comes from JS
        
    
More Projects!