From 4075f282a2189b4431682eac85f796eb73514de6 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Wed, 9 Sep 2020 11:32:44 -0500 Subject: [PATCH] Game state service bugfixes (#3) - Add appropriate "else if" clauses to advance game state - Add ability to listen for game state changes - Don't have winner if no ships are present - Reset "current_turn_had_missile_attempt" to false every state change --- src/services/GameState.service.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/services/GameState.service.js b/src/services/GameState.service.js index 2720368..d0126cc 100644 --- a/src/services/GameState.service.js +++ b/src/services/GameState.service.js @@ -265,7 +265,7 @@ export class GameStateService { throw new InvalidAdvanceStateError("Invalid Number of Boats"); } } - if (this.current_state === GameState.PlayerSetup) { + else if (this.current_state === GameState.PlayerSetup) { if (this.current_player === Player.One) { // because the place_ship handles all the validation // all you need to do is make sure they have placed all the appropriate ships @@ -277,7 +277,7 @@ export class GameStateService { throw new InvalidAdvanceStateError("Player One has a problem with the number of boats selected"); } } - if (this.current_player === Player.Two) { + else if (this.current_player === Player.Two) { if ( this.get_ship_entities(this.current_player).length === this.n_boats ) { this.current_state = GameState.PlayerTurn; this.current_player = Player.One; @@ -288,7 +288,7 @@ export class GameStateService { } } } - if (this.current_state === GameState.PlayerTurn && this.current_player === Player.One) { + else if (this.current_state === GameState.PlayerTurn && this.current_player === Player.One) { if (this.current_turn_had_missile_attempt === true) { this.current_player = Player.Two; this.current_opponent = Player.One; @@ -297,7 +297,7 @@ export class GameStateService { throw new InvalidAdvanceStateError("the player has not fired a missle"); } } - if (this.current_state === GameState.PlayerTurn && this.current_player === Player.Two) { + else if (this.current_state === GameState.PlayerTurn && this.current_player === Player.Two) { if (this.current_turn_had_missile_attempt === true) { this.current_player = Player.One; this.current_opponent = Player.Two; @@ -313,9 +313,18 @@ export class GameStateService { this.current_player = winner; } + this.current_turn_had_missile_attempt = false this.game_state_change_listeners.forEach(fn => fn(this.current_state)) } + /** + * Register a handler to be called when the game state changes. + * @param {function} handler + */ + on_state_change(handler) { + this.game_state_change_listeners.push(handler) + } + /** * 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. @@ -543,12 +552,14 @@ export class GameStateService { // Make sure to sink any fully-damaged ships this._sink_damaged_ships(player_1) - const player_1_loses = this.get_ship_cells(player_1).every(cell => cell.render === GridCellState.Sunk) + const player_1_ship_cells = this.get_ship_cells(player_1) + const player_1_loses = (player_1_ship_cells.length > 0) && player_1_ship_cells.every(cell => cell.render === GridCellState.Sunk) if ( player_1_loses ) return player_2 // Make sure to sink any fully-damaged ships this._sink_damaged_ships(player_2) - const player_2_loses = this.get_ship_cells(player_2).every(cell => cell.render === GridCellState.Sunk) + const player_2_ship_cells = this.get_ship_cells(player_2) + const player_2_loses = (player_2_ship_cells.length > 0) && player_2_ship_cells.every(cell => cell.render === GridCellState.Sunk) if ( player_2_loses ) return player_2 }