示例#1
0
        /// <summary>
        /// Shares food between ants.
        /// Also calculates the food closest to their own colonies if both ants have knowledge of different food.
        /// </summary>
        /// <param name="locA">Passes in the ant object</param>
        private void ShareFood(Ant locA)
        {
            int totDiff  = 0;
            int compDiff = 0;

            //If this ant doesn't know where food is and the other ant does then ask where to get food
            if (FoodLeft == false && locA.FoodLeft)
            {
                if (FoodSource != locA.FoodSource)
                {
                    LearnFood(locA);
                }
                else
                {
                    locA.FoodLeft = false;
                }
            } //If this ant knows where food is and the other ant does not then tell it where to get food
            else if (FoodLeft && locA.FoodLeft == false)
            {
                if (FoodSource != locA.FoodSource)
                {
                    TeachFood(locA);
                }
                else
                {
                    FoodLeft = false;
                }
            } //Both ants know where food is and they are different calculate closest to their nest
            else if (FoodLeft && locA.FoodLeft && FoodSource != locA.FoodSource)
            {
                totDiff  = CalculateStepDistance(FoodX, NestX, FoodY, NestY);
                compDiff = CalculateStepDistance(locA.FoodX, NestX, locA.FoodY, NestY);
                if (totDiff > compDiff)
                {
                    LearnFood(locA);
                }
                else
                {
                    TeachFood(locA);
                }
            }
        }