@ -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
}