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}!");
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Demolishes walls between two blocks. Determines fromDirection - direction in which the wall in fromBlock will be demolished.
        /// </summary>
        /// <param name="fromBlock">Wall which lies in fromDirection in this block will be demolished.</param>
        /// <param name="toBlock">Wall which lies in oposite to fromDirection in this block will be demolished.</param>
        private void DemolishWalls(MapBlock fromBlock, MapBlock toBlock)
        {
            // determine fromDirection
            Direction fromDirection = Direction.NORTH;

            if (fromBlock.X < toBlock.X)
            {
                fromDirection = Direction.EAST;
            }
            else if (fromBlock.X > toBlock.X)
            {
                fromDirection = Direction.WEST;
            }
            else if (fromBlock.Y < toBlock.Y)
            {
                fromDirection = Direction.SOUTH;
            }
            else
            {
                fromDirection = Direction.NORTH;
            }

            fromBlock.EntranceInDirection(fromDirection).DemolishWall();
            toBlock.EntranceInDirection(fromDirection.OppositeDirection()).DemolishWall();
        }
        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);
        }