public void TestJsonSerialize() { Direction[] allDirections = DirectionMethods.GetAllDirections(); int w = 10; int h = 15; Map map = new SimpleMapGenerator().GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); IMapSerializer <string, string> jsonSerializer = new JsonMapSerializer(); string jsonString = jsonSerializer.Serialize(map); Assert.IsFalse(jsonString.Length == 0, "Empty string returned!"); Map deserialized = jsonSerializer.Deserialize(jsonString); // compare pre-serialization vs post-serialization Assert.AreEqual(map.Width, deserialized.Width, "Wrong width!"); Assert.AreEqual(map.Height, deserialized.Height, "Wrong width!"); for (int i = 0; i < map.Width; i++) { for (int j = 0; j < map.Height; j++) { MapBlock orig = map.Grid[i, j]; MapBlock deser = deserialized.Grid[i, j]; Assert.AreEqual(orig, deser, $"Blocks at [{i},{j}] have wrong coordinates!"); foreach (Direction d in allDirections) { Assert.AreEqual(orig.EntranceInDirection(d), deser.EntranceInDirection(d), $"Blocks at [{i},{j}] have different entrance in direction {d}!"); } } } }
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}."); }