/// <summary> /// ProcessShot will be called uppon when a ship is found. /// It will create a stack with targets it will try to hit. These targets /// will be randomly assigned. /// </summary> /// <param name="row">the row it needs to process</param> /// <param name="rowOld">the row it contained before hit</param> /// <param name="col">the column it needs to process</param> /// <param name="colOld">the column it contained before hit</param> /// <param name="result">the result of the last shot (should be hit)</param> protected override void ProcessShot(int row, int col, AttackResult result) { if (result.Value == ResultOfAttack.Hit) { _CurrentState = AIStates.Searching; int rowOld = row; int colOld = col; SearchCoords(ref row, ref col); AddTarget(row, colOld); AddTarget(rowOld, col); } else if (result.Value == ResultOfAttack.ShotAlready) { throw new ApplicationException("Error in AI"); } }
// Summary: Shoot at a given row/column // Parameter: row - The row to attack // Parameter: col - The column to attack // Returns: The result of the attack internal AttackResult Shoot(int row, int col) { AttackResult result = default(AttackResult); result = EnemyGrid.HitTile(row, col); switch (result.Value) { case ResultOfAttack.Destroyed: case ResultOfAttack.Hit: _hits += 1; _shots += 1; break; case ResultOfAttack.Miss: _misses += 1; _shots += 1; break; } return(result); }
/* * Summary: The last shot had the following result. Child classes can use this * to prepare for the next shot. * Parameter: result - The result of the shot * Parameter: row - the row shot * Parameter: col - the column shot */ protected abstract void ProcessShot(int row, int col, AttackResult result);
protected override void ProcessShot(int row, int col, AttackResult result) { }
/// <summary> /// ProcessShot will be called uppon when a ship is found. /// It will create a stack with targets it will try to hit. These targets /// will be around the tile that has been hit. /// </summary> /// <param name="row">the row it needs to process</param> /// <param name="col">the column it needs to process</param> /// <param name="result">the result og the last shot (should be hit)</param> protected override void ProcessShot(int row, int col, AttackResult result) { if (result.Value == ResultOfAttack.Hit) { _CurrentState = AIStates.TargetingShip; AddTarget(row - 1, col); AddTarget(row, col - 1); AddTarget(row + 1, col); AddTarget(row, col + 1); } else if (result.Value == ResultOfAttack.ShotAlready) { throw new ApplicationException("Error in AI"); } }
/// <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 = false; isHuman = object.ReferenceEquals(_theGame.Player, HumanPlayer); Console.WriteLine(result.ToString()); if (isHuman) { if (result.ToString().Contains("destroyed")) { Message = "You destroyed the enemies" + result.ToString().Replace("destroyed", ""); } else { Message = "You " + result.ToString(); } } else { if (result.ToString().Contains("destroyed")) { Message = "The enemy destroyed your" + result.ToString().Replace("destroyed", ""); } else { Message = "The AI " + result.ToString(); } } switch (result.Value) { case ResultOfAttack.Destroyed: PlayHitSequence(result.Row, result.Column, isHuman); Audio.PlaySoundEffect(GameSound("Sink")); break; case ResultOfAttack.GameOver: PlayHitSequence(result.Row, result.Column, isHuman); Audio.PlaySoundEffect(GameSound("Sink")); while (Audio.SoundEffectPlaying(GameSound("Sink"))) { SwinGame.Delay(10); SwinGame.RefreshScreen(); } if (HumanPlayer.IsDestroyed) { Audio.PlaySoundEffect(GameSound("Lose")); } else { Audio.PlaySoundEffect(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: Audio.PlaySoundEffect(GameSound("Error")); break; } }
/// <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) { switch (result.Value) { case ResultOfAttack.Miss: if (object.ReferenceEquals(_theGame.Player, ComputerPlayer)) AIAttack(); break; case ResultOfAttack.GameOver: SwitchState(GameState.EndingGame); break; } }
/// <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 = false; isHuman = object.ReferenceEquals(_theGame.Player, HumanPlayer); Console.WriteLine (result.ToString ()); if (isHuman) { if (result.ToString ().Contains ("destroyed") ) Message = "You destroyed the enemies" + result.ToString().Replace ("destroyed", ""); else Message = "You " + result.ToString(); } else { if (result.ToString ().Contains ("destroyed") ) Message = "The enemy destroyed your" + result.ToString().Replace ("destroyed", ""); else Message = "The AI " + result.ToString (); } switch (result.Value) { case ResultOfAttack.Destroyed: PlayHitSequence(result.Row, result.Column, isHuman); Audio.PlaySoundEffect(GameSound("Sink")); break; case ResultOfAttack.GameOver: PlayHitSequence(result.Row, result.Column, isHuman); Audio.PlaySoundEffect(GameSound("Sink")); while (Audio.SoundEffectPlaying(GameSound("Sink"))) { SwinGame.Delay(10); SwinGame.RefreshScreen(); } if (HumanPlayer.IsDestroyed) { Audio.PlaySoundEffect(GameSound("Lose")); } else { Audio.PlaySoundEffect(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: Audio.PlaySoundEffect(GameSound("Error")); 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 = default(AttackResult); 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); } if (AttackCompleted != null) { AttackCompleted(this, newAttack); } //change player if the last hit was a miss if (newAttack.Value == ResultOfAttack.Miss) { _playerIndex = otherPlayer; } return newAttack; }
/// <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);
/// <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 = false; isHuman = object.ReferenceEquals(_theGame.Player, HumanPlayer); if (isHuman) { UtilityFunctions.Message = "You " + result.ToString(); } else { UtilityFunctions.Message = "The AI " + result.ToString(); } switch (result.Value) { case ResultOfAttack.Destroyed: PlayHitSequence(result.Row, result.Column, isHuman); Audio.PlaySoundEffect(GameResources.GameSound("Sink")); break; case ResultOfAttack.GameOver: PlayHitSequence(result.Row, result.Column, isHuman); Audio.PlaySoundEffect(GameResources.GameSound("Sink")); while (Audio.SoundEffectPlaying(GameResources.GameSound("Sink"))) { SwinGame.Delay(10); SwinGame.RefreshScreen(); } if (HumanPlayer.IsDestroyed) { Audio.PlaySoundEffect(GameResources.GameSound("Lose")); } else { 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: Audio.PlaySoundEffect(GameResources.GameSound("Error")); break; } }
/// <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) { switch (result.Value) { 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"); } if (_Targets.Count == 0) _CurrentState = AIStates.Searching; }