@dvshanup0/tictactoedi-botgame
v1.0.1
Published
Minimax AI for Tic-Tac-Toe-Di. Fully ESM. Bot = 0, Human = 1. ### Field format: [-1, 0, 1] * -1 → empty * 0 → bot (O) * 1 → player (X)
Downloads
8
Readme
@dvshanup0/tictactoedi-botgame
Minimax AI for Tic-Tac-Toe-Di. Fully ESM. Bot = 0, Human = 1.
Field format: [-1, 0, 1]
- -1 → empty
- 0 → bot (O)
- 1 → player (X)
Install
pnpm add @dvshanup0/tictactoedi-botgame
# or
npm install @dvshanup0/tictactoedi-botgameUsage
import { getOptimalTurn, minimax, checkWinner } from "@dvshanup0/tictactoedi-botgame";
const field = [
1, -1, 0,
-1, 1, -1,
0, -1, -1
];
// bot = 0
const bestMove = getOptimalTurn(field, 0);
console.log("Bot move:", bestMove);API
getOptimalTurn(field, player)
Returns the best move (index 0–8).
minimax(field, player)
Returns the numerical score of the position (usually not needed directly).
applyMove(field, index, player)
Returns the new field after the move.
getAvailableMoves(field)
Returns an array of available cells.
checkWinner(field)
Returns:
- 0 → bot win
- 1 → human win
- -2 → draw
- null → no one has won yet
✔ Example: simple bot move
import { getOptimalTurn, applyMove } from "dvshanup0/tictactoedi-botgame";
const field = Array(9).fill(-1);
const move = getOptimalTurn(field, 0);
const newField = applyMove(field, move, 0);