Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SubBoard

SubBoard for TicTacToe games This class implements the traditional game of TicTacToe

Hierarchy

Index

Constructors

constructor

Properties

board

board: Array<Array<Cell>>

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

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.

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

    • move: Coord

      move coordinates

    • Default value index: number = -1

      which turn this was (to enable replaying UTTT games)

    Returns SubBoard

addOpponentMove

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

    Parameters

    • move: Coord
    • Default value index: number = -1

      which turn this was (to enable replaying UTTT games)

    Returns SubBoard

Private checkColumn

  • checkColumn(col: number): void
  • Check if a given column has been won

    Parameters

    • col: number

      Column index

    Returns void

Private checkLtRDiagonal

  • checkLtRDiagonal(): void
  • Check if the left to right diagonal has been won

    Returns void

Private checkRow

  • checkRow(row: number): void
  • Check if a given row has been won

    Parameters

    • row: number

      Row index

    Returns void

Private checkRtLDiagonal

  • checkRtLDiagonal(): void
  • Check if the right to left diagonal has been won

    Returns void

copy

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

getValidMoves

  • getValidMoves(): Array<Coord>

isFinished

  • isFinished(): boolean
  • Returns true if the game is over, undefined if it hasn't finished yet

    Returns boolean

isFull

  • isFull(): boolean

isValidMove

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

    Parameters

    • move: Coord

      Move coordinates as an array [x, y]

    Returns boolean

    true if the move is valid

Private isValidPlayer

move

  • Execute a move. This is an immutable method, that returns a new SubBoard. It may be easier and more clear to use the addOpponentMove and addMyMove methods instead.

    Parameters

    • player: PlayerOrTie

      Player identifier (0 || 1)

    • move: Coord

      Move coordinates as an array [x, y]

    • Default value index: number = -1

      which turn this was (to enable replaying UTTT games)

    Returns SubBoard

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

prettyPrint

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

    Parameters

    • Default value printTies: boolean = false

    Returns string

Generated using TypeDoc