Britbot
Location Britbot.Group.FindCenter ( bool  forcePirate) [inline]

Find the center of the group.

Parameters:
forcePirateif you ant the function to strictly return a location of a pirate or just the average location
Returns:
The center pirate

Definition at line 733 of file Group.cs.

        {
            if (this.Pirates.Count == 0)
                return new Location(0, 0);

            //convert all the pirates indexes to actual pirate list
            List<Pirate> myPirates = this.Pirates.ToList().ConvertAll(p => Bot.Game.GetMyPirate(p));

            //init the average location
            Location averageLocation = new Location(0, 0);

            //iterate over all the pirates and add their locations to the sum
            foreach (Pirate myPirate in myPirates)
            {
                if (myPirate == null)
                    continue;

                averageLocation.Row += myPirate.Loc.Row;
                averageLocation.Col += myPirate.Loc.Col;
            }

            //calc the average
            averageLocation.Col /= myPirates.Count;
            averageLocation.Row /= myPirates.Count;

            //if the caller strictly wants the central pirate of the group, 
            //calcute it by assuming that the center pirate is the closest to the average location
            if (forcePirate)
            {
                int minDistance = Bot.Game.GetCols() + Bot.Game.GetCols();
                Pirate pete = null;

                //iterate over all the pirate and find the one with the minimun distance to the average location
                foreach (Pirate pirate in myPirates)
                {
                    if (pirate.IsLost)
                        continue;

                    int currDistance = Bot.Game.Distance(averageLocation, pirate.Loc);
                    if (currDistance < minDistance)
                    {
                        minDistance = currDistance;
                        pete = pirate;
                    }
                }

                //set the returned location to the central pirate location
                if (pete != null)
                    averageLocation = pete.Loc;
            }

            //return the location
            return averageLocation;
        }