diff --git a/README.md b/README.md index e5d9fcf..f855420 100644 --- a/README.md +++ b/README.md @@ -1 +1,20 @@ -# MazeGenerator \ No newline at end of file +# MazeGenerator +![Nuget](https://img.shields.io/nuget/v/Kysect.MazeGenerator?style=flat-square) + + +MazeGenerator - учебная библиотека, которая позволяет генерировать карты для игры в лабиринт. + + +Карта лабиринта представляет из себя матрицу Сells[][] +```C# +public enum Cells +{ + Wall, + Empty, + Exit +} +``` + +Пример создания лабиринта + +https://github.com/kysect/MazeGenerator/blob/89657d5857d39e5961e1231bda079e7c4a863da4/Source/Kysect.MazeGenerator.Console/Program.cs diff --git a/Source/Kysect.MazeGenerator.Console/Program.cs b/Source/Kysect.MazeGenerator.Console/Program.cs index e41dbbe..3fa8a77 100644 --- a/Source/Kysect.MazeGenerator.Console/Program.cs +++ b/Source/Kysect.MazeGenerator.Console/Program.cs @@ -2,8 +2,8 @@ using Kysect.MazeGenerator.MazeGenerators.GrowingTree; -ushort size = 2; -IMazeGenerator generator = new GrowingTreeGenerator(); +int size = 5; +IMapGenerator generator = new GrowingTreeGenerator(); var maze = new Maze(generator.Generate(size)); maze.AddExit(); @@ -11,9 +11,20 @@ { for (int j = 0; j < maze.Size; j++) { - Console.Write(maze.Map[i][j] == Cells.Empty ? " " : "O"); + Console.Write(CellToString(maze.Map[i][j])); Console.Write(" "); } Console.WriteLine(); +} + +string CellToString(Cells cellType) +{ + return cellType switch + { + Cells.Wall => "0", + Cells.Empty => " ", + Cells.Exit => "#", + _ => throw new ArgumentOutOfRangeException(nameof(cellType), cellType, null) + }; } \ No newline at end of file diff --git a/Source/Kysect.MazeGenerator/Cells.cs b/Source/Kysect.MazeGenerator/Cells.cs index 6b76512..57af9bf 100644 --- a/Source/Kysect.MazeGenerator/Cells.cs +++ b/Source/Kysect.MazeGenerator/Cells.cs @@ -3,5 +3,6 @@ public enum Cells { Wall, - Empty + Empty, + Exit, } \ No newline at end of file diff --git a/Source/Kysect.MazeGenerator/IMazeGenerator.cs b/Source/Kysect.MazeGenerator/IMapGenerator.cs similarity index 69% rename from Source/Kysect.MazeGenerator/IMazeGenerator.cs rename to Source/Kysect.MazeGenerator/IMapGenerator.cs index 9cb061d..f137680 100644 --- a/Source/Kysect.MazeGenerator/IMazeGenerator.cs +++ b/Source/Kysect.MazeGenerator/IMapGenerator.cs @@ -1,6 +1,6 @@ namespace Kysect.MazeGenerator; -public interface IMazeGenerator +public interface IMapGenerator { Cells[][] Generate(int size); } \ No newline at end of file diff --git a/Source/Kysect.MazeGenerator/Maze.cs b/Source/Kysect.MazeGenerator/Maze.cs index e9a787b..9709ed6 100644 --- a/Source/Kysect.MazeGenerator/Maze.cs +++ b/Source/Kysect.MazeGenerator/Maze.cs @@ -5,14 +5,19 @@ namespace Kysect.MazeGenerator; public class Maze { + private readonly List _specialCells; + public Cells[][] Map { get; init; } + public IReadOnlyCollection SpecialCells => _specialCells; + public int Size { get; private set; } public Maze(Cells[][] map) { Map = map; Size = map.GetLength(0); + _specialCells = new List(); } public Maze AddExit() @@ -37,15 +42,18 @@ private void GenerateExit(Random random) Direction sideDirection = DirectionExtensions.GetRandomDirection(random); Coordinate d = sideDirection.TransformDirectionToDelta(); - int singleCoordinate = random.Next(1, Map.GetLength(0) - 2); - exitPosition = sideDirection.TransformDirectionToCoordinate(singleCoordinate, Map.GetLength(0)); + int singleCoordinate = random.Next(0, Size); + exitPosition = sideDirection.GetSideCoordinate(singleCoordinate, Size); var shift = new Coordinate { X = d.Y, Y = d.X, }; nearExitPosition = exitPosition - shift; - } while (Map[nearExitPosition.X][nearExitPosition.Y] == Cells.Wall); + } while (Map[nearExitPosition.X][nearExitPosition.Y] == Cells.Wall && + Map[exitPosition.X][exitPosition.Y] != Cells.Exit); + + Map[exitPosition.X][exitPosition.Y] = Cells.Exit; - Map[exitPosition.X][exitPosition.Y] = Cells.Empty; + _specialCells.Add(new SpecialCell(Cells.Exit, exitPosition)); } } \ No newline at end of file diff --git a/Source/Kysect.MazeGenerator/MazeGenerators/GrowingTree/Direction.cs b/Source/Kysect.MazeGenerator/MazeGenerators/GrowingTree/Direction.cs index a379519..7625fc3 100644 --- a/Source/Kysect.MazeGenerator/MazeGenerators/GrowingTree/Direction.cs +++ b/Source/Kysect.MazeGenerator/MazeGenerators/GrowingTree/Direction.cs @@ -56,7 +56,7 @@ public static Coordinate TransformDirectionToDelta(this Direction facingDirectio }; } - public static Coordinate TransformDirectionToCoordinate(this Direction facingDirection, int index, int size) + public static Coordinate GetSideCoordinate(this Direction facingDirection, int index, int size) { return facingDirection switch { diff --git a/Source/Kysect.MazeGenerator/MazeGenerators/GrowingTree/GrowingTreeGenerator.cs b/Source/Kysect.MazeGenerator/MazeGenerators/GrowingTree/GrowingTreeGenerator.cs index 636f968..d4182ec 100644 --- a/Source/Kysect.MazeGenerator/MazeGenerators/GrowingTree/GrowingTreeGenerator.cs +++ b/Source/Kysect.MazeGenerator/MazeGenerators/GrowingTree/GrowingTreeGenerator.cs @@ -1,7 +1,7 @@  namespace Kysect.MazeGenerator.MazeGenerators.GrowingTree; -public class GrowingTreeGenerator : IMazeGenerator +public class GrowingTreeGenerator : IMapGenerator { private readonly Random _random; diff --git a/Source/Kysect.MazeGenerator/SpecialCell.cs b/Source/Kysect.MazeGenerator/SpecialCell.cs new file mode 100644 index 0000000..01aaf06 --- /dev/null +++ b/Source/Kysect.MazeGenerator/SpecialCell.cs @@ -0,0 +1,5 @@ +using Kysect.MazeGenerator.MazeGenerators.GrowingTree; + +namespace Kysect.MazeGenerator; + +public record SpecialCell(Cells CellType, Coordinate Position); \ No newline at end of file