tic-tac-toe-optimal-turn
v0.0.8
Published
Tic-tac-toe optimal turn package based on alpha-beta algorithm.
Readme
Tic-tac-toe optimal turn
Description
Tic-tac-toe optimal turn package based on alpha-beta algorithm. That package determines the AI's move and returns next optimal step based on the given board. Only tested on a 3x3, 4x4 board.
Installation
npm install tic-tac-toe-optimal-turnUsage
const optimalMove = getOptimalTurn({ playerSymbol, gameField }) //returns a NumberParameters
gameField presented as an array of 'X' and 'O', empty cell as null:
const gameField = [ null, 'O', 'X',
'X', 'X', 'O',
null, 'O', 'O' ] //3x3 game fieldplayerSymbol - symbol of the player for whom the next move is calculated:
const playerSymbol = 'X' //StringboardSize - optional, 3 by default. For game fields more than 3x3.
const boardSize = 3 //NumberExample 3x3 game field
import { getOptimalTurn } from 'tic-tac-toe-optimal-turn'
const playerSymbol = 'X'
const gameField = [ null, 'O', 'X',
'X', 'X', 'O',
null, 'O', 'O' ] //3x3 game field
const optimalMove = getOptimalTurn({ playerSymbol, gameField }) //returns 6Example 4x4 game field
import { getOptimalTurn } from 'tic-tac-toe-optimal-turn'
const boardSize = 4
const playerSymbol = 'X'
const gameField = [ null, null, null, null,
null, 'O', null, 'X',
null, null, 'O', null,
null, 'X', 'X', 'O' ] //4x4 game field
const optimalMove = getOptimalTurn({ boardSize, playerSymbol, gameField }) //returns 0TypeScript
import { FieldCellType, PlayerSymbolType, getOptimalTurn } from 'tic-tac-toe-optimal-turn'
const playerSymbol: PlayerSymbolType = 'X'
const gameField: FieldCellType = [ null, 'O', 'X',
'X', 'X', 'O',
null, 'O', 'O' ] //3x3 game field
const optimalMove = getOptimalTurn({ playerSymbol, gameField }) //returns 6