Options
All
  • Public
  • Public/Protected
  • All
Menu

Class UTTT

Ultimate Tic Tac Toe Class

This holds a full game, with all the associated state, and exposes several methods to interact with it.

All methods that modify the state have been implemented as a functional/immutable API, which means that they return a modified game, but don't change the original one. This has been done to make tree searching easier, since your root nodes at each step won't be accidentally modified.

Hierarchy

Index

Constructors

constructor

  • new UTTT(size?: number): UTTT
  • Parameters

    • Default value size: number = 3

    Returns UTTT

Properties

board

board: Array<Array<SubBoard>>

Holds the state of the game board as a two dimensional array each element of the inner array is a SubBoard

Protected maxMoves

maxMoves: number

Holds the maximum number of moves before the board is full this is here to avoid recalculating it every time its needed

Protected moves

moves: number

Counter of moves that have been played so far

nextBoard

nextBoard: Coord

Holds the coordinates of the board that should be played next If the last move sends you to a finished board, then this will be null and you may choose any.

Protected size

size: number

Indicates the size of Ultimate TTT we're dealing with typically this will be 3 for a 3x3 board.

stateBoard

stateBoard: SubBoard

The state board is a typical 3x3 TTT board that holds the "state" of the big game so if a cell of the big game has been won, it will be 0 or 1 on this state board. This is very useful to easily see "the big picture" in the game.

Accessors

winner

  • get winner(): 0 | 1 | -1
  • set winner(value: PlayerOrTie): void
  • Game winner, will be null if no one has won yet, -1 in a tie, 0 if player 1 won or 1 if player 2 won.

    Returns 0 | 1 | -1

  • Game winner, will be null if no one has won yet, -1 in a tie, 0 if player 1 won or 1 if player 2 won.

    Parameters

    Returns void

Methods

addMyMove

  • Adds your move to the board, throws exception if move is invalid or board is already finished.

    Parameters

    • boardRowCol: Coord

      Board coordinates [row, col]

    • move: Coord

      Move coordinates [row, col]

    Returns UTTT

    Updated copy of the current game with the move added and the state updated

addOpponentMove

  • Adds an opponent move to the board, throws exception if move is invalid or board is already finished.

    Parameters

    • boardRowCol: Coord

      Board coordinates [row, col]

    • move: Coord

      Move coordinates [row, col]

    Returns UTTT

    Updated copy of the current game with the move added and the state updated

copy

  • Return a new UTTT board as a copy of this one

    Returns UTTT

    Copy of the current game

getMoves

  • getMoves(): number

getResult

  • getResult(): number
  • Returns the winner for the game, throws an exception if the game hasn't finished yet.

    Returns number

    -1 for a tie, 0 you won, 1 opponent won

getValidBoards

  • getValidBoards(): Array<Coord>
  • Get a list of all the valid sub-boards in the main board

    Returns Array<Coord>

isFinished

  • isFinished(): boolean

isValidBoardRowCol

  • isValidBoardRowCol(board: Coord): boolean
  • Validates a board selection before playing it

    Parameters

    • board: Coord

      Board coordinates as an array [row, col]

    Returns boolean

    true if the board is playable

isValidMove

  • isValidMove(boardRowCol: Coord, move: Coord): boolean
  • Validates a given board & move combination (check for right format, data ranges, and that the move hasn't already been played)

    Parameters

    • boardRowCol: Coord

      Board coordinates [row, col]

    • move: Coord

      Move coordinates [row, col]

    Returns boolean

    true if the move is valid

move

  • Execute a move

    Parameters

    • player: number

      Player identifier (1 || 2)

    • board: Coord

      Board coordinates as an array [x, y]

    • move: Coord

      Move coordinates as an array [x, y]

    Returns UTTT

    Updated copy of the current game with the move added and the state updated

prettyPrint

  • prettyPrint(): string
  • Returns a string with the board formatted for display including new lines.

    Returns string

    Printable version of the game board

Generated using TypeDoc