Пример #1
0
        protected override DirectionEnum GetDirection(FoodMatrix eatMatrix,
                                                      Membrane[,] creatures, Point position, Random random)
        {
            var points = CommonMethods.GetPoints(position);
            var state  = new Dictionary <int, int>();

            foreach (var point in points)
            {
                var direction = DirectionEx.DirectionByPointsWithNumber(position, point);

                if (CommonMethods.IsValidAndFree(point, creatures))
                {
                    state.Add(direction, eatMatrix.HasOneBite(point) ? 4 : 3);
                }

                if (!CommonMethods.IsValid(point, eatMatrix.Length, eatMatrix.Height))
                {
                    state.Add(direction, 1);
                }
                else
                if (!CommonMethods.IsFree(point, creatures))
                {
                    state.Add(direction, 2);
                }
            }

            var result = _executor.Execute(CommandsForGetDirection, new MyExecutorToolset(random, state));

            return(DirectionEx.DirectionByNumber(int.Parse(result)));
        }
Пример #2
0
        public void TakeFoodIsFalseTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var point     = new Point(0, 0);

            Assert.IsFalse(eatMatrix.TakeFood(point));
        }
Пример #3
0
        public void BuildTest()
        {
            var frequency = 10;
            var eatMatrix = new FoodMatrix(2, 2, new FillingOfEntireFieldStrategy(frequency));
            var creatures = new bool[2, 2];

            for (int k = 0; k < 10; k++)
            {
                if (k != 9 && FoodMatrixConstants.AddedFoodLevel <= FoodMatrixConstants.MaxFoodLevel)
                {
                    eatMatrix.Build(creatures);
                    for (int i = 0; i < eatMatrix.Length; i++)
                    {
                        for (int j = 0; j < eatMatrix.Height; j++)
                        {
                            Assert.IsFalse(eatMatrix.HasMaxFoodLevel(new Point(i, j)));
                        }
                    }
                }

                if (k == 9)
                {
                    FrequentlyUsedMethods.RaiseFoodLevelToConstantWithBuild(eatMatrix, creatures, FoodMatrixConstants.MaxFoodLevel, frequency);
                    for (int i = 0; i < eatMatrix.Length; i++)
                    {
                        for (int j = 0; j < eatMatrix.Height; j++)
                        {
                            Assert.IsTrue(eatMatrix.HasMaxFoodLevel(new Point(i, j)));
                        }
                    }
                }
            }
        }
Пример #4
0
 public void Eat(FoodMatrix eatMatrix)
 {
     if (eatMatrix.TakeFood(Position))
     {
         _energyPoints += CreatureConstants.OneBite;
     }
 }
Пример #5
0
 public Matrix(int length, int height, Creator creator, IFoodDistributionStrategy strategy)
 {
     Length    = length;
     Height    = height;
     _creator  = creator;
     EatMatrix = new FoodMatrix(length, height, strategy);
     Creatures = new Membrane[length, height];
 }
Пример #6
0
        public void HasOneBiteIsTrueTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var point = new Point(0, 0);

            FrequentlyUsedMethods.RaiseFoodLevelToConstantWithAddFood(eatMatrix, point, CreatureConstants.OneBite);

            Assert.IsTrue(eatMatrix.HasOneBite(point));
        }
Пример #7
0
        public Tuple <ActionEnum, DirectionEnum> MyTurn(FoodMatrix eatMatrix, Membrane[,] creatures, Point position,
                                                        Random random, bool hasOneBite, int energyPoints)
        {
            var action    = GetAction(random, hasOneBite, energyPoints);
            var direction = (action == ActionEnum.Eat || action == ActionEnum.MakeChild)
                ? DirectionEnum.Stay : GetDirection(eatMatrix, creatures, position, random);

            return(Tuple.Create(action, direction));
        }
Пример #8
0
        public void AddFoodTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var point     = new Point(0, 0);

            FrequentlyUsedMethods.RaiseFoodLevelToConstantWithAddFood(eatMatrix, point, CreatureConstants.OneBite);

            Assert.IsTrue(eatMatrix.HasOneBite(point));
        }
Пример #9
0
        public void HasMaxFoodLevelIsTrueTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var point     = new Point(0, 0);

            FrequentlyUsedMethods.RaiseFoodLevelToConstantWithAddFood(eatMatrix, point, FoodMatrixConstants.MaxFoodLevel);

            Assert.IsTrue(eatMatrix.HasMaxFoodLevel(point));
        }
Пример #10
0
        public static void RaiseFoodLevelToConstantWithAddFood(FoodMatrix eatMatrix, Point point, int constant)
        {
            var counter = 0;

            while (counter < constant)
            {
                eatMatrix.AddFood(point);
                counter += FoodMatrixConstants.AddedFoodLevel;
            }
        }
Пример #11
0
        public void HasOneBiteIsFalseTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());

            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    Assert.IsFalse(eatMatrix.HasOneBite(new Point(i, j)));
                }
            }
        }
Пример #12
0
        public void HasOneBiteIsFalseTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());

            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    Assert.IsFalse(eatMatrix.HasOneBite(new Point(i, j)));
                }
            }
        }
Пример #13
0
        public void BuildWithOneFreeCellTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var creatures = new bool[2, 2];

            creatures[0, 0] = true;
            creatures[0, 1] = true;
            creatures[1, 0] = true;

            FrequentlyUsedMethods.RaiseFoodLevelToConstantWithBuild(eatMatrix, creatures, FoodMatrixConstants.MaxFoodLevel, 1);
            Assert.IsTrue(eatMatrix.HasMaxFoodLevel(new Point(1, 1)));
        }
Пример #14
0
        public Tuple <ActionEnum, DirectionEnum> Turn(FoodMatrix eatMatrix,
                                                      Membrane[,] creatures)
        {
            if (HasToDie())
            {
                return(Tuple.Create(ActionEnum.Die, DirectionEnum.Stay));
            }

            _energyPoints -= CreatureConstants.MinFoodToSurvive;

            var result = Creature.MyTurn(eatMatrix, creatures, Position, _random, HasOneBite(eatMatrix), _energyPoints);

            return(result.Item1 == ActionEnum.MakeChild ? Tuple.Create(ActionEnum.MakeChild, GetDirectionForChild(creatures)) : result);
        }
Пример #15
0
        public void BuildIsFullTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var creatures = new bool[2, 2];

            eatMatrix.Build(creatures);
            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    Assert.IsTrue(eatMatrix.HasMaxFoodLevel(new Point(i, j)), "Your MaxFoodLevel constant exceeds AddedFoodLevel multiplied by 4");
                }
            }
        }
Пример #16
0
        public void HasMaxFoodLevelIsFalseTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());

            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    if (FoodMatrixConstants.AddedFoodLevel < FoodMatrixConstants.MaxFoodLevel)
                        eatMatrix.AddFood(new Point(i, j));
                    Assert.IsFalse(eatMatrix.HasMaxFoodLevel(new Point(i, j)));
                }
            }
        }
Пример #17
0
        public static void RaiseFoodLevelToConstantWithBuild(FoodMatrix eatMatrix, bool[,] creatures, int constant, int frequency)
        {
            var counter = 0;
            var i       = 0;

            while (counter < constant)
            {
                eatMatrix.Build(creatures);
                if (i % frequency == 0)
                {
                    counter += FoodMatrixConstants.AddedFoodLevel;
                }
                i++;
            }
        }
Пример #18
0
        public void HasMaxFoodLevelIsFalseTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());

            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    if (FoodMatrixConstants.AddedFoodLevel < FoodMatrixConstants.MaxFoodLevel)
                    {
                        eatMatrix.AddFood(new Point(i, j));
                    }
                    Assert.IsFalse(eatMatrix.HasMaxFoodLevel(new Point(i, j)));
                }
            }
        }
Пример #19
0
        public void TakeFoodIsTrueTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var point     = new Point(0, 0);

            FrequentlyUsedMethods.RaiseFoodLevelToConstantWithAddFood(eatMatrix, point, CreatureConstants.OneBite);

            Assert.IsTrue(eatMatrix.HasOneBite(point));

            var counter = FoodMatrixConstants.AddedFoodLevel / CreatureConstants.OneBite;

            for (int i = 0; i < counter; i++)
            {
                Assert.IsTrue(eatMatrix.TakeFood(point));
            }

            Assert.IsFalse(eatMatrix.HasOneBite(point));
        }
Пример #20
0
        public void BuildIsEmptyTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var creatures = new bool[2, 2];

            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    creatures[i, j] = true;
                }
            }

            eatMatrix.Build(creatures);
            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    Assert.IsFalse(eatMatrix.HasOneBite(new Point(i, j)));
                }
            }
        }
Пример #21
0
        protected override DirectionEnum GetDirection(FoodMatrix eatMatrix, Membrane[,] creatures, Point position, Random random)
        {
            var points             = CommonMethods.GetPoints(position);
            var directions         = new List <DirectionEnum>();
            var directionsWithFood = new List <DirectionEnum>();

            foreach (var item in points)
            {
                if (!CommonMethods.IsValidAndFree(item, creatures))
                {
                    continue;
                }
                directions.Add(DirectionEx.DirectionByPoints(position, item));
                if (eatMatrix.HasOneBite(item))
                {
                    directionsWithFood.Add(DirectionEx.DirectionByPoints(position, item));
                }
            }
            if (directions.Count == 0)
            {
                return(DirectionEnum.Stay);
            }
            return(directionsWithFood.Count == 0 ? directions.ElementAt(random.Next(directions.Count)) : directionsWithFood.ElementAt(random.Next(directionsWithFood.Count)));
        }
Пример #22
0
        public void TakeFoodIsTrueTest()
        {
            var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
            var point = new Point(0, 0);

            FrequentlyUsedMethods.RaiseFoodLevelToConstantWithAddFood(eatMatrix, point, CreatureConstants.OneBite);

            Assert.IsTrue(eatMatrix.HasOneBite(point));

            var counter = FoodMatrixConstants.AddedFoodLevel / CreatureConstants.OneBite;
            for (int i = 0; i < counter; i++)
            {
                Assert.IsTrue(eatMatrix.TakeFood(point));
            }

            Assert.IsFalse(eatMatrix.HasOneBite(point));
        }
Пример #23
0
 public void TakeFoodIsFalseTest()
 {
     var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy());
     var point = new Point(0, 0);
     Assert.IsFalse(eatMatrix.TakeFood(point));
 }
Пример #24
0
 private bool HasOneBite(FoodMatrix eatMatrix)
 {
     return(eatMatrix.HasOneBite(Position));
 }
Пример #25
0
 protected abstract DirectionEnum GetDirection(FoodMatrix eatMatrix, Membrane[,] creatures, Point position, Random random);