tic-tac-toe-optimal-turn
Version:
Tic-tac-toe optimal turn package based on alpha-beta algorithm.
73 lines (56 loc) • 2.06 kB
Markdown
# 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-turn
```
## Usage
```
const optimalMove = getOptimalTurn({ playerSymbol, gameField }) //returns a Number
```
### Parameters
`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 field
```
`playerSymbol` - symbol of the player for whom the next move is calculated:
```
const playerSymbol = 'X' //String
```
`boardSize` - optional, 3 by default. For game fields more than 3x3.
```
const boardSize = 3 //Number
```
### Example 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 6
```
### Example 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 0
```
### TypeScript
```
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
```