Skip to content

daon/tic-tac-toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tic Tac Toe

Build Status

User Stories

  1. I can play a game of Tic Tac Toe with the computer.
  2. My game will reset as soon as it's over so I can play again.
  3. I can choose whether I want to play as X or O.

Game Factory

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):

  1. _ (0) - Empty
  2. X (1) - Cross
  3. 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);

Game Object

The game object has the following methods:

  • getBoard() - Returns the board state for the game
  • getAvailableMoves() - Returns empty (_) board positions for the game
  • getActiveTurn() - Returns current player turn, X or O
  • isWinner(player) - Returns true if the argument player (X or O) is the winner else it returns false
  • getNewState(move) - Returns new game state after player move.

Example: getBoard()

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]

Example: getAvailableMoves()

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]

Example: getActiveTurn()

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

Example: isWinner(player)

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

Example: getNewState(move)

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

Score

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.

Example: There no winner

import { createGame } from './gameFactory';
import { score } from './score';

const game = createGame();
const gameScore = score(game);

console.log(gameScore);
// -> 0

Example: X is the winner

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

Example: O is the winner

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