Ejemplo n.º 1
0
        public IEnumerable <Shot> GetShots(IEnumerable <EnemyFleetInfo> enemyFleetInfo, int numberOfShots)
        {
            if (this.SearchShots == null)
            {
                CreateSearchShots(enemyFleetInfo);
            }
            if (LastHittMe != null)
            {
                // if the person who last shot at me has no ships then go back to the weakest
                var shooter = enemyFleetInfo.Single(s => s.Name == LastHittMe);
                if (shooter.NumberOfAfloatShipts == 0)
                {
                    LastHittMe = null;
                }
            }

            var shotsToFire = new List <Shot>();

            string target = LastHittMe;

            if (target == null)
            {
                target = enemyFleetInfo
                         .Where(f => f.NumberOfAfloatShipts > 0)
                         .OrderByDescending(f => f.NumberOfAfloatShipts)
                         .FirstOrDefault()
                         ?.Name;
            }

            // see if we've got any finds first
            foreach (var find in Finds)
            {
                while (shotsToFire.Count < numberOfShots && find.UpcomingShots.Any())
                {
                    var nextShot = find.UpcomingShots.Pop();
                    if (!WouldShotBeAWaste(nextShot))
                    {
                        shotsToFire.Add(nextShot);
                    }
                }
            }

            // then fill up with searches
            while (shotsToFire.Count < numberOfShots)
            {
                if (SearchShots.ContainsKey(target) &&
                    SearchShots[target].Any())
                {
                    var nextSearchingShot = SearchShots[target].Pop();
                    if (!WouldShotBeAWaste(nextSearchingShot))
                    {
                        shotsToFire.Add(nextSearchingShot);
                    }
                }
            }

            ShotHistory.AddRange(shotsToFire);
            return(shotsToFire);
        }