Beispiel #1
0
        public void TestWinningCondition()
        {
            // 1x1 map
            int           w            = 1;
            int           h            = 1;
            IMapGenerator mapGenerator = MapGeneratorFactory.CreateOpenMapGenerator();
            Map           map          = mapGenerator.GenerateMap(w, h, IMapGeneratorConstants.NO_SEED);

            // place player
            AbstractPlayer player = AIPlayerFactory.CreateEmptyAIPlayer("Test empty AI", map.Grid[0, 0]);

            // create game instance
            Game game = new Game()
            {
                GameMap = map
            };

            game.AddAIPlayer(player);

            // perform one game loop step
            game.GameLoopStep();

            // check winner
            Assert.IsTrue(game.IsWinner, "No winner after game loop step!");
            Assert.IsNotNull(game.Winner, "Winner is null!");
        }
        public void TestOpenMapGenerator()
        {
            IMapGenerator openMapGenerator = MapGeneratorFactory.CreateOpenMapGenerator();
            int           w = 10;
            int           h = 15;

            Direction[] allDirections = DirectionMethods.GetAllDirections();

            MapBlock[,] grid = openMapGenerator.GenerateGrid(w, h, 0);
            Assert.IsNotNull(grid, "Null grid returned!");
            Assert.AreEqual(w, grid.GetLength(0), "Wrong width of map grid!");
            Assert.AreEqual(h, grid.GetLength(1), "Wrong height of map grid!");
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    foreach (Direction dir in allDirections)
                    {
                        Assert.IsTrue(grid[i, j].EntranceInDirection(dir).IsOpen(), $"Entrance in direction {dir} of block [{i},{j}] should be open!");
                    }
                }
            }

            Map map = openMapGenerator.GenerateMap(w, h, 0);

            Assert.IsNotNull(map, "Null map returned!");
            Assert.AreEqual(w, map.Width, "Wrong map width!");
            Assert.AreEqual(h, map.Height, "Wrong map height!");
            MapBlock[,] grid2 = map.Grid;
            Assert.AreEqual(grid.GetLength(0), grid.GetLength(0), "Widths of grids don't match!");
            Assert.AreEqual(grid.GetLength(1), grid.GetLength(1), "Widths of grids don't match!");
            Assert.IsNotNull(map.WinningBlock, "Winning block is null!");
            Assert.AreEqual((w - 1) / 2, map.WinningBlock.X, "Wrong X coordinate of winning block.");
            Assert.AreEqual((h - 1) / 2, map.WinningBlock.Y, "Wrong Y coordinate of winning block.");

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    foreach (Direction dir in allDirections)
                    {
                        Assert.IsTrue(grid2[i, j].EntranceInDirection(dir).IsOpen(), $"Entrance in direction {dir} of block [{i},{j}] should be open!");
                    }
                }
            }
        }
        public void TestByteSerialize()
        {
            int w   = 4;
            int h   = 4;
            Map map = MapGeneratorFactory.CreateOpenMapGenerator().GenerateMap(w, h, IMapGeneratorConstants.NO_SEED);

            map.MapName = "";
            IMapSerializer <byte[], byte[]> byteSerializer = new BinaryMapSerializer();

            byte[] serializedMap = byteSerializer.Serialize(map);
            // 3 bytes for header, 20 bytes for map header, (w*h) / 2 bytes for map data, 4 bytes for creature count, 4 bytes for item count
            int expectedSize = 3 + 20 + (w * h) / 2 + 4 + 4;

            Assert.AreEqual(expectedSize, serializedMap.Length, "Wrong number of bytes returned!");

            // check header
            Assert.AreEqual(Convert.ToByte('D'), serializedMap[0], "Wrong first byte of the header!");
            Assert.AreEqual(Convert.ToByte('M'), serializedMap[1], "Wrong second byte of the header!");
            Assert.AreEqual(BinaryMapSerializer.VERSION, serializedMap[2], "Wrong third byte of the header!");

            // check map header
            int counter = 3;
            int nameLen = serializedMap[counter++] + 256 * serializedMap[counter++] + 256 * 256 * serializedMap[counter++] + 256 * 256 * 256 * serializedMap[counter++];
            int sw      = serializedMap[counter++] + 256 * serializedMap[counter++] + 256 * 256 * serializedMap[counter++] + 256 * 256 * 256 * serializedMap[counter++];
            int sh      = serializedMap[counter++] + 256 * serializedMap[counter++] + 256 * 256 * serializedMap[counter++] + 256 * 256 * 256 * serializedMap[counter++];
            int wx      = serializedMap[counter++] + 256 * serializedMap[counter++] + 256 * 256 * serializedMap[counter++] + 256 * 256 * 256 * serializedMap[counter++];
            int wy      = serializedMap[counter++] + 256 * serializedMap[counter++] + 256 * 256 * serializedMap[counter++] + 256 * 256 * 256 * serializedMap[counter++];

            Assert.AreEqual(0, nameLen, "Wrong name length!");
            Assert.AreEqual(w, sw, "Wrong map width!");
            Assert.AreEqual(h, sh, "Wrong map height!");
            Assert.AreEqual(wx, map.WinningBlock.X, "Wrong x coordinate of winning block!");
            Assert.AreEqual(wy, map.WinningBlock.Y, "Wrong y coordinate of winning block!");

            // check map data
            for (int i = counter; i < counter + 8; i++)
            {
                Assert.AreEqual(255, serializedMap[i], $"Wrong {i-18} byte of map data!");
            }

            // check creature and item counts
            Assert.AreEqual(0, serializedMap[33], "Wrong creature count!");
            Assert.AreEqual(0, serializedMap[34], "Wrong item count!");

            // try to deserialize
            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];

                    Assert.IsTrue(testedBlock.North.IsOpen(), $"North entrance of block [{i},{j}] is not open!");
                    Assert.IsTrue(testedBlock.East.IsOpen(), $"East entrance of block [{i},{j}] is not open!");
                    Assert.IsTrue(testedBlock.South.IsOpen(), $"South entrance of block [{i},{j}] is not open!");
                    Assert.IsTrue(testedBlock.West.IsOpen(), $"West entrance of block [{i},{j}] is not open!");
                }
            }
        }