public void Build(bool[,] creatures, FoodMatrix eatMatrix)
 {
     Build(creatures, 0, 0, eatMatrix);
     Build(creatures, 0, creatures.GetLength(0) - 1, eatMatrix);
     Build(creatures, creatures.GetLength(1) - 1, 0, eatMatrix);
     Build(creatures, creatures.GetLength(1) - 1, creatures.GetLength(0) - 1, eatMatrix);
 }
        private void Build(
            bool[,] creaturesMatrix,
            int pointX,
            int pointY,
            FoodMatrix eatMatrix)
        {
            int length = eatMatrix.Length;
            int height = eatMatrix.Height;

            var stack = new Stack<Point>();
            var visitedCells = new bool[length, height];

            stack.Push(new Point(pointX, pointY));

            while (stack.Count != 0)
            {
                var current = stack.Pop();
                visitedCells[current.X, current.Y] = true;

                if (CommonMethods.IsValid(current, length, height)
                    && !creaturesMatrix[current.X, current.Y])
                {
                    if (!eatMatrix.HasMaxFoodLevel(current))
                        eatMatrix.AddFood(current);

                    foreach (var point in CommonMethods.GetPoints(current))
                    {
                        if(CommonMethods.IsValid(point, length, height) && !visitedCells[point.X, point.Y])
                            stack.Push(point);
                    }
                }
            }
        }
Exemplo n.º 3
0
 public void Build(bool[,] creatures, FoodMatrix eatMatrix)
 {
     Build(creatures, 0, 0, eatMatrix);
     Build(creatures, 0, creatures.GetLength(0) - 1, eatMatrix);
     Build(creatures, creatures.GetLength(1) - 1, 0, eatMatrix);
     Build(creatures, creatures.GetLength(1) - 1, creatures.GetLength(0) - 1, eatMatrix);
 }
Exemplo n.º 4
0
 public void Eat(FoodMatrix eatMatrix)
 {
     if (eatMatrix.TakeFood(Position))
     {
         _energyPoints += CreatureConstants.OneBite;
     }
 }
        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)));
                }
            }
        }
Exemplo n.º 6
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);
 }
Exemplo n.º 7
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];
 }
        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");
        }
Exemplo n.º 9
0
        public static void RaiseFoodLevelToConstantWithAddFood(FoodMatrix eatMatrix, Point point, int constant)
        {
            var counter = 0;

            while (counter < constant)
            {
                eatMatrix.AddFood(point);
                counter += FoodMatrixConstants.AddedFoodLevel;
            }
        }
        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)));
        }
Exemplo n.º 11
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++;
            }
        }
        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)));
        }
Exemplo n.º 13
0
 public void Build(bool[,] creatures, FoodMatrix eatMatrix)
 {
     for (int i = 0; i < eatMatrix.Length; i++)
     {
         for (int j = 0; j < eatMatrix.Height; j++)
         {
             if (!creatures[i, j] &&
                 !eatMatrix.HasMaxFoodLevel(new Point(i, j)) &&
                 _random.Next(100) % _frequency == 0)
             {
                 eatMatrix.AddFood(new Point(i, j));
             }
         }
     }
 }
Exemplo n.º 14
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));
 }
Exemplo n.º 15
0
 public void Build(bool[,] creatures, FoodMatrix eatMatrix)
 {
     for (int i = 0; i < eatMatrix.Length; i++)
     {
         for (int j = 0; j < eatMatrix.Height; j++)
         {
             if (!creatures[i, j]
                 && !eatMatrix.HasMaxFoodLevel(new Point(i, j))
                 && _random.Next(100) % _frequency == 0)
             {
                 eatMatrix.AddFood(new Point(i, j));
             }
         }
     }
 }
        public void Build(bool[,] creatures, FoodMatrix eatMatrix)
        {
            _counterOfTurns++;

            if(_counterOfTurns % _frequency != 0)
                return;

            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    if (!creatures[i, j]
                        && !eatMatrix.HasMaxFoodLevel(new Point(i, j)))
                    {
                        eatMatrix.AddFood(new Point(i, j));
                    }
                }
            }
        }
        public void Build(bool[,] creatures, FoodMatrix eatMatrix)
        {
            _counterOfTurns++;

            if (_counterOfTurns % _frequency != 0)
            {
                return;
            }

            for (int i = 0; i < eatMatrix.Length; i++)
            {
                for (int j = 0; j < eatMatrix.Height; j++)
                {
                    if (!creatures[i, j] &&
                        !eatMatrix.HasMaxFoodLevel(new Point(i, j)))
                    {
                        eatMatrix.AddFood(new Point(i, j));
                    }
                }
            }
        }
Exemplo n.º 18
0
        private void Build(
            bool[,] creaturesMatrix,
            int pointX,
            int pointY,
            FoodMatrix eatMatrix)
        {
            int length = eatMatrix.Length;
            int height = eatMatrix.Height;

            var stack        = new Stack <Point>();
            var visitedCells = new bool[length, height];

            stack.Push(new Point(pointX, pointY));

            while (stack.Count != 0)
            {
                var current = stack.Pop();
                visitedCells[current.X, current.Y] = true;

                if (CommonMethods.IsValid(current, length, height) &&
                    !creaturesMatrix[current.X, current.Y])
                {
                    if (!eatMatrix.HasMaxFoodLevel(current))
                    {
                        eatMatrix.AddFood(current);
                    }

                    foreach (var point in CommonMethods.GetPoints(current))
                    {
                        if (CommonMethods.IsValid(point, length, height) && !visitedCells[point.X, point.Y])
                        {
                            stack.Push(point);
                        }
                    }
                }
            }
        }
Exemplo n.º 19
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));
        }
Exemplo n.º 20
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;
        }
Exemplo n.º 21
0
 protected abstract DirectionEnum GetDirection(FoodMatrix eatMatrix, Membrane[,] creatures, Point position, Random random);
Exemplo n.º 22
0
 private bool HasOneBite(FoodMatrix eatMatrix)
 {
     return eatMatrix.HasOneBite(Position);
 }