public void TestDfsAi() { IMapGenerator simpleMapGenerator = new OpenMapGenerator(); int w = 5; int h = 1; Map map = simpleMapGenerator.GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); AbstractPlayer simpleAIPlayer = new SimpleAIPlayer("Test simple AI player", map.Grid[0, 0]) { IgnoreSpeed = true }; map.AddCreature(simpleAIPlayer); // player should get to the block [4,0] in 4 turns for (int i = 0; i < w - 1; i++) { simpleAIPlayer.Think(); // check taht correct action was produced AbstractAction nextAction = simpleAIPlayer.NextAction; Assert.IsNotNull(nextAction, $"Next action is nul in {i} iteration!"); Assert.AreEqual(typeof(Move), nextAction.GetType(), $"Wrong type of action in {i} iteration!"); Assert.AreEqual(Direction.EAST, ((Move)nextAction).Direction, $"Wrong direction in {i} iteration!"); // execute the action and check the position is correct nextAction.Execute(); Assert.AreEqual(0, simpleAIPlayer.Position.Y, $"Wrong Y coordinate in {i} direction!"); Assert.AreEqual(i + 1, simpleAIPlayer.Position.X, $"Wrong X coordinate in {i} direction!"); } }
public void TestSimpleMonsterPath() { int w = 5; int h = 1; OpenMapGenerator mapGenerator = new OpenMapGenerator(); Map map = mapGenerator.GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); // create monster and let it plan its next action 4 times // monster should generate 4 'move to EAST' actions Monster monster = new Monster("Test monster", map.Grid[0, 0], 100, 0, 0, w - 1) { IgnoreSpeed = true }; map.AddCreature(monster); Stack <Direction> dirStack = new Stack <Direction>(); for (int i = 0; i < w - 1; i++) { monster.Think(); AbstractAction action = monster.NextAction; Assert.IsNotNull(action, $"Action in {i} iteration is null!"); Assert.AreEqual(typeof(Move), action.GetType(), $"Aciton in {i} iteration is not move!"); Move moveAction = (Move)action; Assert.IsFalse(moveAction.Direction.IsNoDirection(), "Direction in move action is NO_DIRECTION."); dirStack.Push(moveAction.Direction); moveAction.Execute(); } // think again - monster should stay still now and next action should be null monster.Think(); Assert.IsNull(monster.NextAction, "Next action should be null if the stack is empty!"); // think again 4 times, monster should now return to its initial position for (int i = 0; i < w - 1; i++) { monster.Think(); AbstractAction action = monster.NextAction; Assert.IsNotNull(action, $"Action in {i} iteration is null!"); Assert.AreEqual(typeof(Move), action.GetType(), $"Aciton in {i} iteration is not move!"); Move moveAction = (Move)action; Assert.AreEqual(dirStack.Pop().OppositeDirection(), moveAction.Direction, $"Action in {i} iteration has wrong direction {moveAction.Direction} when moving backwards"); moveAction.Execute(); } // check current position of monster, it should be [0,0] Assert.AreEqual(0, monster.Position.X, "X coordinate of monster's final position is not correct!"); Assert.AreEqual(0, monster.Position.Y, "Y coordinate of monster's final position is not correct!"); }
public void TestDfsMaze() { int w = 50; int h = 50; int maxIter = 2 * (50 * 50) + 1; IMapGenerator simpleMapGenerator = new SimpleMapGenerator(); Map map = simpleMapGenerator.GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); AbstractPlayer simpleAIPlayer = new SimpleAIPlayer("Simple maze solver", map.Grid[0, 0]) { IgnoreSpeed = true }; map.AddCreature(simpleAIPlayer); MapBlock finish = map.Grid[w - 1, h - 1]; bool finishReached = false; int iter = 0; int nullActions = 0; while (!finishReached && iter < maxIter) { simpleAIPlayer.Think(); AbstractAction action = simpleAIPlayer.NextAction; if (action == null) { nullActions++; } else { Assert.AreEqual(typeof(Move), action.GetType(), $"Wrong type of action in {iter} iteration!"); ((Move)action).Execute(); finishReached = finish.Equals(simpleAIPlayer.Position); } iter++; } Assert.IsTrue(finishReached, $"Finish not reached in {maxIter} iterations! Null actions: {nullActions}."); }