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!"); } }
/// <summary> /// Read creature from byte stream. /// </summary> /// <param name="byteStream">Byte stream to read creature from.</param> /// <param name="map">Map to write data to.</param> /// <returns>Creature.</returns> private AbstractCreature ReadCreature(Stream byteStream, Map map) { AbstractCreature creature; int uid = ReadInt(byteStream); string name = ReadName(byteStream); int x = ReadInt(byteStream); int y = ReadInt(byteStream); int hp = ReadInt(byteStream); int dmg = ReadInt(byteStream); int def = ReadInt(byteStream); byte type = ReadByte(byteStream); switch (type) { case 0: creature = new Monster(name, map.Grid[x, y], hp, dmg, def) { UniqueId = uid, BaseHitPoints = hp, BaseAttack = dmg, BaseDeffense = def }; break; case 1: creature = new HumanPlayer(name, map.Grid[x, y]) { UniqueId = uid, BaseHitPoints = hp, BaseAttack = dmg, BaseDeffense = def }; break; case 2: creature = new EmptyAIPlayer(name, map.Grid[x, y]) { UniqueId = uid, BaseHitPoints = hp, BaseAttack = dmg, BaseDeffense = def }; break; case 3: creature = new SimpleAIPlayer(name, map.Grid[x, y]) { UniqueId = uid, BaseHitPoints = hp, BaseAttack = dmg, BaseDeffense = def }; break; default: throw new Exception($"Neznámý typ bytosti: {type}!"); } map.AddCreature(creature); return(creature); }
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}."); }
public void TestOccupiedBlock() { IMapGenerator openMapGenerator = new OpenMapGenerator(); int w = 2; int h = 1; Map map = openMapGenerator.GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); AbstractPlayer simpleAIPlayer = new SimpleAIPlayer("Test simple AI player", map.Grid[0, 0]) { IgnoreSpeed = true }; map.AddCreature(simpleAIPlayer); Monster monster = new Monster("Test monster", map.Grid[1, 0], 10, 10, 10); map.AddCreature(monster); simpleAIPlayer.Think(); // check that next action is null Assert.IsNull(simpleAIPlayer.NextAction, "Next action should be null!"); }
public void TestSerializeMaze() { int w = 4; int h = 4; Map map = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); map.MapName = "Test map"; // add creatures to map Monster origMonster = new Monster("Test monster", map.Grid[0, 0], 4, 3654123, 87621235); map.AddCreature(origMonster); SimpleAIPlayer aiPlayer = new SimpleAIPlayer("Test player", map.Grid[3, 2]); map.AddCreature(aiPlayer); HumanPlayer hPlayer = new HumanPlayer("Příliš žluťoučký kůň úpěl ďábelské ódy", map.Grid[1, 3]) { BaseHitPoints = 98432156, BaseAttack = 112348, BaseDeffense = 41226987 }; map.AddCreature(hPlayer); // add items to map AbstractWeapon weapon = ItemFactory.CreateAxe(map.Grid[1, 3]); map.AddItem(weapon); AbstractArmor armor = ItemFactory.CreateLeatherArmor(map.Grid[1, 1]); map.AddItem(armor); AbstractInventoryItem item = new BasicItem("Příliš žluťoučký kůň úpěl ďábelské ódy.!?_/()':123456789<>&@{}[]", map.Grid[2, 2], 514) { UniqueId = 6284 }; map.AddItem(item); // serialize - deserialize IMapSerializer <byte[], byte[]> byteSerializer = new BinaryMapSerializer(); byte[] serializedMap = byteSerializer.Serialize(map); Map deserializedMap = byteSerializer.Deserialize(serializedMap); // check map Assert.AreEqual(map.MapName, deserializedMap.MapName, "Wrong map name!"); Assert.AreEqual(w, deserializedMap.Width, "Wrong width after deserialization!"); Assert.AreEqual(h, deserializedMap.Width, "Wrong height after deserialization!"); Assert.AreEqual(map.WinningBlock.X, deserializedMap.WinningBlock.X, "Wrong x coordinate of winning block!"); Assert.AreEqual(map.WinningBlock.Y, deserializedMap.WinningBlock.Y, "Wrong Y coordinate of winning block!"); // check map blocks for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { MapBlock origBlock = map.Grid[i, j]; MapBlock testedBlock = deserializedMap.Grid[i, j]; foreach (Direction dir in DirectionMethods.GetAllDirections()) { Assert.AreEqual(origBlock.EntranceInDirection(dir).IsOpen(), testedBlock.EntranceInDirection(dir).IsOpen(), $"Wrong entrance in direction {dir} in block [{i},{j}]."); } } } // check creatures Monster m = (Monster)deserializedMap.Grid[0, 0].Creature; CheckCreature(origMonster, m); SimpleAIPlayer p = (SimpleAIPlayer)deserializedMap.Grid[3, 2].Creature; CheckCreature(aiPlayer, p); HumanPlayer hp = (HumanPlayer)deserializedMap.Grid[1, 3].Creature; CheckCreature(hPlayer, hp); // check items AbstractWeapon weap = (AbstractWeapon)map.Grid[1, 3].Item; CheckItem(weap, weapon); AbstractArmor arm = (AbstractArmor)map.Grid[1, 1].Item; CheckItem(arm, armor); AbstractInventoryItem itm = (AbstractInventoryItem)map.Grid[2, 2].Item; CheckItem(item, itm); }