- I can play a game of Tic Tac Toe with the computer.
- My game will reset as soon as it's over so I can play again.
- I can choose whether I want to play as X or O.
To create a game, call the createGame function without any arguments:
import { createGame } from './gameFactory';
const game = createGame();
The game factory also take an argument of a board state. The board state is represented by an array of nine values of different cell states. A cell can have three different states (state value in parentheses):
- _
(0)
- Empty - X
(1)
- Cross - O
(2)
- Nought
Here is an example of how to set the board state of a game:
import { createGame, _, X, O } from './gameFactory';
const board = [
_, X, _,
O, O, _,
_, _, X
];
const game = createGame(board);
The game object has the following methods:
getBoard()
- Returns the board state for the gamegetAvailableMoves()
- Returns empty (_) board positions for the gamegetActiveTurn()
- Returns current player turn, X or OisWinner(player)
- Returns true if the argumentplayer
(X or O) is the winner else it returns falsegetNewState(move)
- Returns new game state after player move.
import { createGame, _, X, O } from './gameFactory';
const board = [
_, X, _,
O, O, _,
_, _, X
];
const game = createGame(board);
const board = game.getBoard();
console.log(board);
// -> [0, 1, 0, 2, 2, 0, 0, 0, 1]
import { createGame, _, X, O } from './gameFactory';
const board = [
_, X, _,
O, O, _,
_, _, X
];
const game = createGame(board);
const availableMoves = game.getAvailableMoves();
console.log(availableMoves);
// -> [0, 2, 5, 6, 7]
import { createGame, _, X, O } from './gameFactory';
const board = [
_, X, _,
O, O, X,
_, _, X
];
const game = createGame(board);
const activeTurn = game.getActiveTurn();
console.log(activeTurn);
// -> 2
import { createGame, _, X, O } from './gameFactory';
const board = [
_, _, X,
O, O, X,
_, _, X
];
const game = createGame(board);
const isXWinner = game.isWinner(X);
console.log(isXWinner);
// -> true
import { createGame, _, X, O } from './gameFactory';
const board = [
_, X, X,
O, O, _,
_, _, X
];
const game = createGame(board);
const newGameState = game.getNewState(5);
console.log(game.getBoard());
console.log(newGameState.getBoard());
console.log(game.getAvailableMoves());
console.log(newGameState.getAvailableMoves());
console.log(game.getActiveTurn());
console.log(newGameState.getActiveTurn());
console.log(game.isWinner(X));
console.log(newGameState.isWinner(X));
console.log(game.isWinner(O));
console.log(newGameState.isWinner(O));
// -> [0, 1, 1, 2, 2, 0, 0, 0, 1]
// -> [0, 1, 1, 2, 2, 2, 0, 0, 1]
// -> [0, 5, 6, 7]
// -> [0, 6, 7]
// -> 2
// -> 0
// -> false
// -> false
// -> false
// -> true
To score a game, call the score function on a game object:
import { createGame } from './gameFactory';
import { score } from './score';
const game = createGame();
const gameScore = score(game);
The score return 0, 10 or -10 depending on who is the winner of the game.
import { createGame } from './gameFactory';
import { score } from './score';
const game = createGame();
const gameScore = score(game);
console.log(gameScore);
// -> 0
import { createGame } from './gameFactory';
import { score } from './score';
const board = [
_, X, O,
O, X, _
_, X, _
];
const game = createGame(board);
const gameScore = score(game);
console.log(gameScore);
// -> 10
import { createGame } from './gameFactory';
import { score } from './score';
const board = [
_, X, O,
X, O, _
O, X, _
];
const game = createGame(board);
const gameScore = score(game);
console.log(gameScore);
// -> -10