this package provides a simple API to play at the Tic Tac Toe
-------------
| X | O | O |
-------------
| O | X | X |
-------------
| O | O | X |
-------------
yarn add amaurycoudr-tictactoe
#or
npm i amaurycoudr-tictactoe
The Board is represented by an array of array of Box
type Box = 'X' | 'O' | undefined;
type RowState = [Box, Box, Box];
type BoardState = [RowState, RowState, RowState];
The package expose 5 functions:
- getEmptyInBoard
- makePlay
- getWinner
- isDraw
- printBoard
This function take in arguement a board and return the list of the emptyBoxes in this board
const emptyBoxes = getEmptyInBoard([
[undefined, 'X', '0'],
[undefined, 'X', '0'],
[undefined, 'X', '0'],
]);
const result = [
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 0, y: 1 },
];
console.log(emptyBox === result); // true
This function take in argument a board and a play and return the board afer the play
const play = { coordinate: { x: 0, y: 0 }, player: 'X' };
const board = [
[undefined, undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined],
];
const newBoard = makePlayer(board, play);
const result = [
['X', undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined],
];
console.log(newBoar === result); // true
This function wil return of the party if there is no return it return undefined
const board = [
['X', undefined, undefined],
[undefined, 'X', undefined],
[undefined, undefined, 'X'],
];
const winner = getWinner(board);
const result = 'X';
console.log(winner === result); // true
This function wil return true if the party is a draw
const board = [
['X', 'X', 'O'],
['O', 'O', 'X'],
['X', 'O', 'X'],
];
const draw = isDraw(board);
const result = false;
console.log(draw === result); // true