/// <summary> /// Checks the results of the attack and switches to /// Ending the Game if the result was game over. /// </summary> /// <param name="result">the result of the last /// attack</param> /// <remarks>Gets the AI to attack if the result switched /// to the AI player.</remarks> private static void CheckAttackResult(AttackResult result) { var switchExpr = result.Value; switch (switchExpr) { case ResultOfAttack.Miss: { if (_theGame.Player == ComputerPlayer) { AIAttack(); } break; } case ResultOfAttack.GameOver: { SwitchState(GameState.EndingGame); break; } } }
/// <summary> /// Shoot will swap between players and check if a player has been killed. /// It also allows the current player to hit on the enemygrid. /// </summary> /// <param name="row">the row fired upon</param> /// <param name="col">the column fired upon</param> /// <returns>The result of the attack</returns> public AttackResult Shoot(int row, int col) { AttackResult newAttack; int otherPlayer = (_playerIndex + 1) % 2; newAttack = Player.Shoot(row, col); // Will exit the game when all players ships are destroyed if (_players[otherPlayer].IsDestroyed) { newAttack = new AttackResult(ResultOfAttack.GameOver, newAttack.Ship, newAttack.Text, row, col); } AttackCompleted?.Invoke(this, newAttack); // change player if the last hit was a miss if (newAttack.Value == ResultOfAttack.Miss) { _playerIndex = otherPlayer; } return(newAttack); }
/// <summary> /// ProcessShot is able to process each shot that is made and call the right methods belonging /// to that shot. For example, if its a miss = do nothing, if it's a hit = process that hit location /// </summary> /// <param name="row">the row that was shot at</param> /// <param name="col">the column that was shot at</param> /// <param name="result">the result from that hit</param> protected override void ProcessShot(int row, int col, AttackResult result) { var switchExpr = result.Value; switch (switchExpr) { case ResultOfAttack.Miss: { _CurrentTarget = null; break; } case ResultOfAttack.Hit: { ProcessHit(row, col); break; } case ResultOfAttack.Destroyed: { ProcessDestroy(row, col, result.Ship); break; } case ResultOfAttack.ShotAlready: { throw new ApplicationException("Error in AI"); //break; } } if (_Targets.Count == 0) { _CurrentState = AIStates.Searching; } }
/// <summary> /// Listens for attacks to be completed. /// </summary> /// <param name="sender">the game</param> /// <param name="result">the result of the attack</param> /// <remarks> /// Displays a message, plays sound and redraws the screen /// </remarks> private static void AttackCompleted(object sender, AttackResult result) { bool isHuman; isHuman = (_theGame.Player == HumanPlayer); if (isHuman) { UtilityFunctions.Message = "You " + result.ToString(); } else { UtilityFunctions.Message = "The AI " + result.ToString(); } var switchExpr = result.Value; switch (switchExpr) { case ResultOfAttack.Destroyed: { PlayHitSequence(result.Row, result.Column, isHuman); if (Variables._isMute == false) //mute extension { Audio.PlaySoundEffect(GameResources.GameSound("Sink")); } break; } case ResultOfAttack.GameOver: { PlayHitSequence(result.Row, result.Column, isHuman); if (Variables._isMute == false) //mute extension { Audio.PlaySoundEffect(GameResources.GameSound("Sink")); } while (Audio.SoundEffectPlaying(GameResources.GameSound("Sink"))) { SwinGame.Delay(10); SwinGame.RefreshScreen(); } if (HumanPlayer.IsDestroyed) { if (Variables._isMute == false) //mute extension { Audio.PlaySoundEffect(GameResources.GameSound("Lose")); } } else { if (Variables._isMute == false) //mute extension { Audio.PlaySoundEffect(GameResources.GameSound("Winner")); } } break; } case ResultOfAttack.Hit: { PlayHitSequence(result.Row, result.Column, isHuman); break; } case ResultOfAttack.Miss: { PlayMissSequence(result.Row, result.Column, isHuman); break; } case ResultOfAttack.ShotAlready: { if (Variables._isMute == false) //mute extension { Audio.PlaySoundEffect(GameResources.GameSound("Error")); } break; } } }
/// <summary> /// The last shot had the following result. Child classes can use this /// to prepare for the next shot. /// </summary> /// <param name="result">The result of the shot</param> /// <param name="row">the row shot</param> /// <param name="col">the column shot</param> protected abstract void ProcessShot(int row, int col, AttackResult result);