public static void then_if_lumberjack_should_maul() { // Arrange var numberGeneratorService = new Mock <INumberGeneratorService>(); var specificSet = new[] { 2 }; var calls = 0; numberGeneratorService.Setup(service => service.GetNextRandomOfBound(It.IsAny <int>())).Returns(() => specificSet[calls]).Callback(() => calls++); var bear = new Bear(0, 0, numberGeneratorService.Object); var forest = new Mock <IForest>(); forest.Setup(forest1 => forest1.Bound).Returns(3); forest.Setup(forest1 => forest1.IsOpenSpot(1, 1)).Returns(false); forest.Setup(forest1 => forest1.GridContents(new Point(0, 0))).Returns((List <IForestObject>)null); var lumberJack = new LumberJack(0, 0); forest.Setup(forest1 => forest1.GridContents(new Point(1, 1))) .Returns(new List <IForestObject>() { lumberJack }); // Act bear.Tick(forest.Object); // Assert forest.Verify(forest1 => forest1.Remove(lumberJack)); }
public static void then_should_stop_moving() { // Arrange var numberGeneratorService = new Mock <INumberGeneratorService>(); var specificSet = new[] { 2, 7, 7 }; var calls = 0; numberGeneratorService.Setup(service => service.GetNextRandomOfBound(It.IsAny <int>())).Returns(() => specificSet[calls]).Callback(() => calls++); var lumberjack = new LumberJack(0, 0, numberGeneratorService.Object); var forest = new Mock <IForest>(); forest.Setup(forest1 => forest1.Bound).Returns(3); forest.Setup(forest1 => forest1.IsOpenSpot(1, 1)).Returns(false); forest.Setup(forest1 => forest1.GridContents(new Point(0, 0))).Returns((List <IForestObject>)null); forest.Setup(forest1 => forest1.GridContents(new Point(1, 1))).Returns( new List <IForestObject>() { new Tree(1, 1, new Mock <ISaplingGeneratorService>().Object) { Age = TreeAge.Tree } }); // Act lumberjack.Tick(forest.Object); // Assert Assert.That(lumberjack.Location.X, Is.EqualTo(1)); Assert.That(lumberjack.Location.Y, Is.EqualTo(1)); }
public static void then_if_tree_should_cut_down_the_tree() { // Arrange var numberGeneratorService = new Mock <INumberGeneratorService>(); var specificSet = new[] { 2 }; var calls = 0; numberGeneratorService.Setup(service => service.GetNextRandomOfBound(It.IsAny <int>())).Returns(() => specificSet[calls]).Callback(() => calls++); var lumberjack = new LumberJack(0, 0, numberGeneratorService.Object); var forest = new Mock <IForest>(); forest.Setup(forest1 => forest1.Bound).Returns(3); forest.Setup(forest1 => forest1.IsOpenSpot(1, 1)).Returns(false); forest.Setup(forest1 => forest1.GridContents(new Point(0, 0))).Returns((List <IForestObject>)null); var tree = new Tree(1, 1, new Mock <ISaplingGeneratorService>().Object) { Age = TreeAge.Tree }; forest.Setup(forest1 => forest1.GridContents(new Point(1, 1))).Returns(new List <IForestObject>() { tree }); // Act lumberjack.Tick(forest.Object); // Assert forest.Verify(forest1 => forest1.Remove(tree)); }
public override void ProcessRule(Forest forest, long currentTick) { _ruleAge++; if (_ruleAge % Forest.YEAR_LENGTH == 0) { // Get all lumberjacks var lumberJacks = from e in forest.Entities where typeof(LumberJack).IsAssignableFrom(e.GetType()) select e as LumberJack; // Get the total number of logs collected from all lumberjacks int logSum = 0; lumberJacks.ToList().ForEach((e) => logSum += e.Logs); // If more logs have been collected than we have lumberjacks, we // might need to hire some people if (logSum >= lumberJacks.Count()) { int newHires = (logSum - lumberJacks.Count()) / 10; for (int i = 0; i < newHires; i++) { ForestEntity randomEmpty = forest.GetRandomEmptyPosition(); LumberJack l = new LumberJack(); l.Location = randomEmpty.Location; forest.AddEntity(l); } } else { // They're not working hard enough! Fire someone! // (But make sure we have at least one person working for us...) if (lumberJacks.Count() > 1) { forest.RemoveEntity(lumberJacks.First()); } } if (lumberJacks.Count() == 0) { // No more lumberjacks. We have to make sure there's someone working. LumberJack nl = new LumberJack(); nl.Location = forest.GetRandomEmptyPosition().Location; forest.AddEntity(nl); } // Start of a new year... reset log counts for each lumberjack. lumberJacks.ToList().ForEach((e) => e.Logs = 0); } }
public static void then_should_move_three_times_in_a_random_direction_if_all_spots_empty() { // Arrange var numberGeneratorService = new Mock <INumberGeneratorService>(); var specificSet = new[] { 2, 7, 7 }; var calls = 0; numberGeneratorService.Setup(service => service.GetNextRandomOfBound(It.IsAny <int>())).Returns(() => specificSet[calls]).Callback(() => calls++); var forest = new Mock <IForest>(); forest.Setup(forest1 => forest1.Bound).Returns(3); var lumberjack = new LumberJack(0, 0, numberGeneratorService.Object); // Act lumberjack.Tick(forest.Object); // Assert Assert.That(lumberjack.Location.X, Is.EqualTo(3)); Assert.That(lumberjack.Location.Y, Is.EqualTo(3)); }