/// <summary> /// Adds a single candidate shot /// </summary> /// <param name="shot"></param> public void AddCandidateShot(IShotResult shot) { if (!shot.IsWithinRange) { CheckOutOfRangeShot(shot); } else { CheckInRangeShot(shot); } }
private void CheckInRangeShot(IShotResult shot) { if (!shot.IsWithinRange) { CheckOutOfRangeShot(shot); } RemoveOutOfRangeShots(); this.shots.Add(shot); this.shots = this.shots.OrderBy(x => x.DistanceToTarget).Take(maximumShots).ToList(); }
/// <summary> /// Check battleships to be damaged or destroyed /// </summary> /// <param name="gameBattleShips"></param> /// <param name="shotResult"></param> /// <returns>true - if destroyed, false - if damaged, null - if missed</returns> private bool?CheckBattleShipDestroyed(IEnumerable <GameBattleShip> gameBattleShips, IShotResult shotResult) { var shot = shotResult.Shot; var shotPoint = shot.ShotPoint; foreach (var gameBattleShip in gameBattleShips) { var battleShip = gameBattleShip.BattleShip; var battleShipPoints = battleShip.CreateBattleshipSetOfPoints(); if (battleShipPoints.Contains(shotPoint)) { gameBattleShip.DamagedPointsCnt = gameBattleShip.DamagedPointsCnt + 1; if (gameBattleShip.DamagedPointsCnt == battleShip.Length) { gameBattleShip.State = BattleShipState.Destroyed; shotResult.GameBattleShip = gameBattleShip; return(true); } if (gameBattleShip.DamagedPointsCnt < battleShip.Length) { gameBattleShip.State = BattleShipState.Damaged; shotResult.GameBattleShip = gameBattleShip; return(false); } } } return(null); }
private void CheckOutOfRangeShot(IShotResult shot) { if (!shot.IsWithinRange) { if (shots.Count > 0) { foreach (var shotResult in Shots) { if (!shotResult.IsWithinRange) { if (shot.DistanceToTarget < shotResult.DistanceToTarget) { this.shots = new List<IShotResult>() { shot }; return; } else if (shot.DistanceToTarget == shotResult.DistanceToTarget) { this.Shots.Add(shot); return; } } } } else { this.shots.Add(shot); } } }