Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
refactor: renaming and other things
Browse files Browse the repository at this point in the history
  • Loading branch information
Step1532 committed Oct 1, 2022
1 parent 6cc5453 commit 900e3bb
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 12 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# MazeGenerator
# 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
17 changes: 14 additions & 3 deletions Source/Kysect.MazeGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
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();

for (int i = 0; i < maze.Size; i++)
{
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)
};
}
3 changes: 2 additions & 1 deletion Source/Kysect.MazeGenerator/Cells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public enum Cells
{
Wall,
Empty
Empty,
Exit,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Kysect.MazeGenerator;

public interface IMazeGenerator
public interface IMapGenerator
{
Cells[][] Generate(int size);
}
16 changes: 12 additions & 4 deletions Source/Kysect.MazeGenerator/Maze.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ namespace Kysect.MazeGenerator;

public class Maze
{
private readonly List<SpecialCell> _specialCells;

public Cells[][] Map { get; init; }

public IReadOnlyCollection<SpecialCell> SpecialCells => _specialCells;

public int Size { get; private set; }

public Maze(Cells[][] map)
{
Map = map;
Size = map.GetLength(0);
_specialCells = new List<SpecialCell>();
}

public Maze AddExit()
Expand All @@ -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));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

namespace Kysect.MazeGenerator.MazeGenerators.GrowingTree;

public class GrowingTreeGenerator : IMazeGenerator
public class GrowingTreeGenerator : IMapGenerator
{
private readonly Random _random;

Expand Down
5 changes: 5 additions & 0 deletions Source/Kysect.MazeGenerator/SpecialCell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Kysect.MazeGenerator.MazeGenerators.GrowingTree;

namespace Kysect.MazeGenerator;

public record SpecialCell(Cells CellType, Coordinate Position);

0 comments on commit 900e3bb

Please sign in to comment.