@litolax/get-optimal-turn
v1.0.1
Published
This library calculates the optimal move for a player in a TicTacToe game using the minimax algorithm. It ensures that the player makes the best possible move given the current state of the game.
Downloads
6
Readme
TicTacToe Optimal Turn Library
This library calculates the optimal move for a player in a TicTacToe game using the minimax algorithm. It ensures that the player makes the best possible move given the current state of the game.
Installation
To install the library, use gitlab:
git clone [email protected]:celadon-educational-projects/getoptimalturn.gitor
git clone https://gitlab.com/celadon-educational-projects/getoptimalturn.gitUsage
Here’s an example of how to use the library:
const gameField = [-1, -1, -1, 1, 1, -1, 0, 0, -1];
const player = 0; // Player "O"
const move = getOptimalTurn(gameField, player);
console.log(`The best move is at index: ${move}`);Parameters
gameField: An array of 9 integers representing the game state. Each cell can be:0: Represents "O".1: Represents "X".-1: Represents an empty cell.
player: The current player for whom the optimal move is being calculated. Can be:0: Player "O".1: Player "X".
Returns
The function returns the index (0-based) of the optimal move.
Error Handling
- Throws an error if the game field is not a valid array of length 9.
- Throws an error if the game is already over.
- Throws an error if the player parameter is invalid.
Development
To run tests:
npm test