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))); }
public void TakeFoodIsFalseTest() { var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy()); var point = new Point(0, 0); Assert.IsFalse(eatMatrix.TakeFood(point)); }
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))); } } } } }
public void Eat(FoodMatrix eatMatrix) { if (eatMatrix.TakeFood(Position)) { _energyPoints += CreatureConstants.OneBite; } }
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 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)); }
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)); }
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)); }
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)); }
public static void RaiseFoodLevelToConstantWithAddFood(FoodMatrix eatMatrix, Point point, int constant) { var counter = 0; while (counter < constant) { eatMatrix.AddFood(point); counter += FoodMatrixConstants.AddedFoodLevel; } }
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))); } } }
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))); } } }
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))); }
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); }
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"); } } }
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))); } } }
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 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))); } } }
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)); }
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))); } } }
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))); }
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)); }
public void TakeFoodIsFalseTest() { var eatMatrix = new FoodMatrix(2, 2, new FillingFromCornersByWavesStrategy()); var point = new Point(0, 0); Assert.IsFalse(eatMatrix.TakeFood(point)); }
private bool HasOneBite(FoodMatrix eatMatrix) { return(eatMatrix.HasOneBite(Position)); }
protected abstract DirectionEnum GetDirection(FoodMatrix eatMatrix, Membrane[,] creatures, Point position, Random random);