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!"); }
/// <summary> /// Generate new map from values stored in this view model. /// Note that this will also remove all placed items. /// </summary> public void GenerateMap() { GameMap = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(MapWidth, MapHeight, MapSeed); PlacedItems.Clear(); GameMap.MapName = MapName; HumanPlayerPlaced = false; OnPropertyChanged("PlacedItems"); }
public GameViewModel() { // game initialization will be somehow placed here GameMap = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(20, 20, MAP_SEED); AbstractPlayer player = new HumanPlayer("Test player", GameMap.Grid[2, 2]); Player.Inventory.AddRange(new BasicItem[] { new BasicItem("Test item", new MapBlock(), 10), new BasicItem("Test item 2", new MapBlock(), 10), new BasicItem("Some cool item", new MapBlock(), 10) }); GameInstance = new Game(); GameInstance.GameMap = GameMap; GameInstance.AddHumanPlayer(player); }
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!"); } } } }
static IMap _loadMap(string fileName, DiagonalMovement?diagonal) { if (!File.Exists(fileName)) { Console.WriteLine("map not found"); return(null); } var map = MapGeneratorFactory .GetFileMapGeneratorImplementation(fileName) .DefineMap(); if (diagonal.HasValue) //whants to force the diagonal movement { map.Diagonal = diagonal.Value; } return(map); }
public void TestSimpleMapGeneratorSeed() { IMapGenerator simpleMapGenerator = MapGeneratorFactory.CreateSimpleMapGenerator(); int w = 5; int h = 10; int seed = 87452; Direction[] allDirections = DirectionMethods.GetAllDirections(); MapBlock[,] grid = simpleMapGenerator.GenerateGrid(w, h, seed); MapBlock[,] grid2 = simpleMapGenerator.GenerateGrid(w, h, seed); Assert.IsNotNull(grid, "Grid 1 is null!"); Assert.IsNotNull(grid2, "Grid 2 is null!"); Assert.AreEqual(w, grid.GetLength(0), "Wrong width of map grid!"); Assert.AreEqual(h, grid.GetLength(1), "Wrong height of map grid!"); Assert.AreEqual(w, grid2.GetLength(0), "Wrong width of map grid 2!"); Assert.AreEqual(h, grid2.GetLength(1), "Wrong height of map grid 2!"); // all map block should be same for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int entrances = 0; foreach (Direction dir in allDirections) { Assert.AreEqual(grid[i, j].EntranceInDirection(dir).Exists(), grid2[i, j].EntranceInDirection(dir).Exists(), $"Map block at position [{i},{j}] has different entrance in direction {dir}."); if (grid[i, j].EntranceInDirection(dir).Exists()) { entrances++; } } Assert.IsTrue(entrances > 0, $"Block at [{i},{j}] has no entrance!"); } } }
public static int RunMapGeneration(MapGenerationOption options) { IMapGenerator generator; generator = options.PatternLength > 0 ? MapGeneratorFactory.GetStandardMapGeneratorImplementation(options.PatternLength) : MapGeneratorFactory.GetRandomMapGeneratorImplementation(); if (options.Qtd > 0) { var filename = Path.GetFileName(options.Filename); var ext = Path.GetExtension(options.Filename); if (string.IsNullOrEmpty(ext)) { ext = "txt"; } for (int i = 0; i < options.Qtd; i++) { options.Filename = i.ToString().PadLeft(options.Qtd.ToString().Length, '0') + filename + "." + ext; GenerateMap(generator, options); DrawTextProgressBar(i, options.Qtd); } DrawTextProgressBar(options.Qtd, options.Qtd); } else { GenerateMap(generator, options); } return(0); }
public void TestSimpleMapGenerator() { IMapGenerator simpleMapGenerator = MapGeneratorFactory.CreateSimpleMapGenerator(); int w = 5; int h = 10; Direction[] allDirections = DirectionMethods.GetAllDirections(); Map map = simpleMapGenerator.GenerateMap(w, h, IMapGeneratorConstants.NO_SEED); MapBlock[,] grid = map.Grid; 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!"); 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."); // test that no map block has all entrances closed for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int entrances = 0; foreach (Direction dir in allDirections) { if (grid[i, j].EntranceInDirection(dir).Exists()) { entrances++; } } Assert.IsTrue(entrances > 0, $"Block at [{i},{j}] has no entrance!"); } } }
/// <summary> /// Generates new game with given parameters. If aiCount+1 (1 for human player) is greater than width*heght, exception is thrown. /// </summary> /// <param name="width">Width of the map.</param> /// <param name="height">Height of the map.</param> /// <param name="mapSeed">Map seed.</param> /// <param name="aiCount">Number of AIs (simple AI is used).</param> /// <param name="monsterDensity">Density of monsters 0..1</param> /// <param name="itemDensity">Density of items 0..1</param> /// <param name="humanPlayerName">Name of the human player.</param> /// <returns>Generated game instance.</returns> public static Game GenerateGame(int width, int height, int mapSeed, int aiCount, double monsterDensity, double itemDensity, string humanPlayerName) { // check if (aiCount + 1 > width * height) { throw new Exception($"Počet protihráčů {aiCount} je na plochu {width}x{height} moc velký!"); } Map.Map gameMap = MapGeneratorFactory.CreateSimpleMapGenerator().GenerateMap(width, height, mapSeed); Game game = new Game() { GameMap = gameMap }; Random r = new Random(); // sets of occupied position for creatures and items, '{x}:{y}' HashSet <String> creatureOccupiedPositions = new HashSet <string>(); HashSet <string> itemsOccupiedPositions = new HashSet <string>(); // place human player int x = r.Next(width); int y = r.Next(height); AbstractPlayer player = new HumanPlayer(humanPlayerName, gameMap.Grid[x, y]); game.AddHumanPlayer(player); creatureOccupiedPositions.Add($"{x}:{y}"); // place AI players int pCount = 1; AddObjectsToGame(width, height, aiCount, 20, (ox, oy) => { if (!creatureOccupiedPositions.Contains($"{ox}:{oy}")) { game.AddAIPlayer(AIPlayerFactory.CreateSimpleAIPLayer($"Simple AI Player {pCount}", gameMap.Grid[ox, oy])); creatureOccupiedPositions.Add(($"{ox}:{oy}")); pCount++; return(true); } else { return(false); } }); // monster count from density: // density is expected to be in range 0..1 and will be mapped to the count of remaining free map blocks // this number is then lowered to 2/3 and this result is used as a base // then monsterCount = base + random.next(base/3) double monsterCountBase = 2 * (monsterDensity * (width * height - aiCount)) / 3; int monsterCount = (int)(monsterCountBase + r.NextDouble() * (monsterCountBase / 3)); // place monsters AddObjectsToGame(width, height, monsterCount, 20, (ox, oy) => { if (!creatureOccupiedPositions.Contains($"{ox}:{oy}")) { game.AddMonster(MonsterFactory.CreateRandomMonster(gameMap.Grid[ox, oy])); creatureOccupiedPositions.Add(($"{ox}:{oy}")); return(true); } else { return(false); } }); // item count is calculated the same way as monster count is double itemCountBase = 2 * (itemDensity * (width * height - aiCount)) / 3; int itemCount = (int)(itemCountBase + r.NextDouble() * (itemCountBase / 3)); // place items AddObjectsToGame(width, height, itemCount, 20, (ox, oy) => { if (!itemsOccupiedPositions.Contains($"{ox}:{oy}")) { game.AddItem(ItemFactory.CreateRandomItem(gameMap.Grid[ox, oy])); itemsOccupiedPositions.Add(($"{ox}:{oy}")); return(true); } else { return(false); } }); return(game); }
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!"); } } }
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); }