Class: GameStateService

GameStateService()

Singleton service for managing the state of the game.

Constructor

new GameStateService()

Construct a new game service. Initialize any internal states.
Source:

Classes

GameStateService

Members

current_turn_had_missile_attempt :boolean

True if, during the current turn, the user has tried to fire a missile.
Type:
  • boolean
Source:

game_state_change_listeners :Array.<function()>

Array of functions that are called when the game state changes.
Type:
  • Array.<function()>
Source:

post_player_change_state :undefined|string

If the current state is the PromptPlayerChange, then this is the state that we should move to next.
Type:
  • undefined | string
Source:

Methods

advance_game_state()

responsible for advancing the game state will be consisting of
Source:

attempt_missile_fire(coords) → {boolean}

Attempt to fire a missile at the current opponent at the given coordinates. The coordinates should be an array of [row_index, column_index] where the missile should fire. Returns true if the missile hit an undamaged cell of a ship.
Parameters:
Name Type Description
coords Array.<number>
Source:
Returns:
Type
boolean
Example
If I want to fire a missile at row 5 column 7, then:
game_service.attempt_missile_fire([5, 7])

get_covered_cells(coords_one, coords_two) → {Array.<Array.<number>>}

Get an array of cell coordinates that are covered by a ship that spans the given coordinates.
Parameters:
Name Type Description
coords_one Array.<number>
coords_two Array.<number>
Source:
Returns:
Type
Array.<Array.<number>>
Example
If a ship goes from row 1 column 1 to row 4 column 1, then I can get
the coordinates of all cells covered by that ship using:
game_service.get_covered_cells([1,1], [4,1])
Which would return [[1,1], [2,1], [3,1], [4,1]].

get_current_opponent() → {string}

Get the player who is NOT the focus of the current game state.
Source:
Returns:
Type
string

get_current_opponent_state() → {Array.<Array.<object>>}

Get the state of the current opponent's board, as it should appear to the current player. Note that the current player cannot see "ship" spaces, only available, damaged, missed, or sunk.
Source:
Returns:
Type
Array.<Array.<object>>

get_current_player() → {string}

Get the player who is the focus of the current game state.
Source:
Returns:
Type
string

get_current_player_state() → {Array.<Array.<object>>}

Get the state of the current player's board, as it should appear to them.
Source:
Returns:
Type
Array.<Array.<object>>

get_dimensions() → {Array.<number>}

Get the dimensions of the board as [rows, cols].
Source:
Returns:
Type
Array.<number>
Example
const [n_rows, n_cols] = game_service.get_dimensions()

get_game_state() → {string}

Get the current game state.
Source:
Returns:
Type
string

get_other_player(player) → {string}

Returns the other player.
Parameters:
Name Type Description
player string
Source:
Returns:
Type
string

get_player_display(player) → {string}

Given a Player type, return the display value of that player.
Parameters:
Name Type Description
player string
Source:
Returns:
Type
string

get_player_victory_state() → {Array.<Array.<Array.<object>>>}

Get the states that should be shown on the victory screen. First element is the winner's state, second element is the loser's state.
Source:
Returns:
Type
Array.<Array.<Array.<object>>>

get_possible_boats() → {Array.<string>}

Given the number of boats set by the player (n_boats), return an array of possible ShipTypes.
Source:
Returns:
Type
Array.<string>

get_ship_cells(player) → {Array.<number>}

Get the coordinates of all cells that have ships in them, for the given player.
Parameters:
Name Type Description
player string
Source:
Returns:
Type
Array.<number>

get_ship_entities(player) → {Array.<object>}

Get an array of ship entities for the given player.
Parameters:
Name Type Description
player string
Source:
Returns:
Type
Array.<object>

get_ship_length(ship_type) → {number}

Get the number of cells the given ship type should occupy.
Parameters:
Name Type Description
ship_type string
Source:
Returns:
Type
number

get_winner() → {string|undefined}

If there is a winner, this will return the Player that won. If no winner has been decided yet, will return undefined.
Source:
Returns:
Type
string | undefined

on_state_change(handler)

Register a handler to be called when the game state changes.
Parameters:
Name Type Description
handler function
Source:

place_ship(ship_type, coords_one, coords_two)

Attempt to place a ship of the given type at the given coordinates. Throws an InvalidShipPlacementError if the coordinates are invalid. Coordinates should be [row_index, column_index] of either end of the ship.
Parameters:
Name Type Description
ship_type string
coords_one Array.<number>
coords_two Array.<number>
Source:
Example
If I am placing a 1x3 ship and I want it to be in row 3 column 2 horizontal
to row 3 column 4, then I would call:
game_service.place_ship(ShipType.x3, [3,2], [3,4])

validate_coordinates(ship_type, coords_one, coords_two)

Validate the given coordinates for the given ship type. Throws an InvalidShipPlacementError if the coordinates are invalid. Coordinates should be [row_index, column_index] of either end of the ship.
Parameters:
Name Type Description
ship_type string
coords_one Array.<number>
coords_two Array.<number>
Source:
Example
If I am placing a 1x3 ship and I want it to be in row 3 column 2 horizontal
to row 3 column 4, then I would call:
game_service.validate_coordinates(ShipType.x3, [3,2], [3,4])