Skip to content
Simple artificial intelligence based on Minimax algorithm that calculates the best move in tic-tac-toe.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
distribution
source
tests
.babelrc
.eslintrc.json
.gitignore
LICENSE
README.md
gulpfile.js
package.json

README.md

Tic Tac Toe AI

Simple library based on Minimax algorithm that provides useful API in developing tic-tac-toe games. Go ahead and see it in action: Tic Tac Toe.

Features available:

  • Best move based on current board state
  • Board state after AI move
  • Checking whether game is over
  • Information about current game state

Usage Examples:

This is standard data format, used to exchange information about current game state. Board property is one-dimensional array which consists of field characters: [ 'x', 'o', 'e']. 'e' stands for empty field. 0th index of board array coresponds to left-top field, 1st index corresponds to center-top field and so on...

const data = {
  	aiCharacter: 'o',
	playerCharacter: 'x',
	startingCharacter: 'x',
	board: ['e','e','o',
		'x','e','e',
		'e','x','e']
};

The most basic usage of this library is getting the best move, based on current board state. This can be achieved be simply calling getAIMove(data). This function returns index of board array which reporesents AI's move.

AI.getAIMove(data); //returns 0;

In certain situations, more convenient thing to do, would be calling getBoardAfterAIMove(data) . This function returns array representing board state after AI movement;

AI.getBoardAfterAIMove(data);
//returns [ 'o', 'e', 'o',
//          'x', 'e', 'e',
//          'e', 'x', 'e' ]

Whenever it is needed to check whether game is finished, isTerminated(board) can be called. This function returns true or false, depending on passed board state.

AI.isTerminated(data.board); //returns false

const newBoard = ['x','e','o',
                'e','o','x',
                'o','x','o' ];

AI.isTerminated(newBoard); //returns true

Another way to check whether game is finished, is calling getStateOfGame(board). This time, more specific result is provided. This function can return one, out of four values: 'x-won', 'o-won', 'draw' and 'not-end'.

const  currentGameState = AI.getStateOfGame(data.board);
if(currentGameState === `${data.aiCharacter}-won`){
    //show 'YOU LOST!' on screen
}

For now, those are all of the features that can help you in developing your tic tac toe game.

Instalation

If you want to use this library in browser environment, simply download minified script from here by clicking on link with right mouse button and choosing Save link as..., put it in your project's scripts folder and link it in your HTML file above your scripts. Now you can make use of every function mentioned above by calling them on global object named AI, as shown in the examples.

License

The project is licensed under MIT license.

You can’t perform that action at this time.