-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
127 lines (106 loc) · 3.42 KB
/
Copy pathProgram.cs
File metadata and controls
127 lines (106 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DimonSmart.MazeGenerator;
Console.OutputEncoding = Encoding.UTF8;
RunBasicGeneration();
RunCustomPlotter();
RunWavePathfinding();
static void RunBasicGeneration()
{
Console.WriteLine("=== Example 1: minimal generation (defaults) ===");
const int size = 9;
var maze = new Maze<TutorialCell>(size, size);
new MazeBuilder<TutorialCell>(maze).Build();
RenderMaze(maze);
Console.WriteLine();
}
static void RunCustomPlotter()
{
Console.WriteLine("=== Example 2: custom options + custom plotter ===");
const int size = 9;
var maze = new Maze<TutorialCell>(size, size);
var options = new MazeBuildOptions(WallShortness: 0.35, Emptiness: 0.2);
var plotter = new SnapshotPlotter(size, size);
new MazeBuilder<TutorialCell>(maze, options).Build(plotter);
Console.WriteLine(plotter.Render());
}
static void RunWavePathfinding()
{
Console.WriteLine("=== Example 3: wave propagation + path reconstruction ===");
const int size = 9;
var maze = new Maze<TutorialCell>(size, size);
var options = new MazeBuildOptions(WallShortness: 0.15, Emptiness: 0.05);
new MazeBuilder<TutorialCell>(maze, options).Build();
var start = new Point(1, 1);
var finish = new Point(size - 2, size - 2);
var wave = new MazeWaveGenerator<TutorialCell>(maze).GenerateWave(start.X, start.Y, finish.X, finish.Y);
var path = new MazePathBuilder(wave).BuildPath();
var pathPoints = path.Cells.Select(cell => cell.Point).ToHashSet();
RenderMaze(maze, pathPoints, start, finish);
Console.WriteLine(path.End is null
? "No path could be found."
: $"Shortest path length: {path.Length} cells");
}
static void RenderMaze(Maze<TutorialCell> maze, HashSet<Point>? path = null, Point? start = null, Point? finish = null)
{
for (var y = 0; y < maze.Height; y++)
{
var buffer = new char[maze.Width];
for (var x = 0; x < maze.Width; x++)
{
buffer[x] = maze.IsWall(x, y) ? '#' : ' ';
if (path != null && path.Contains(new Point(x, y)))
{
buffer[x] = '*';
}
if (start is { } s && s.X == x && s.Y == y)
{
buffer[x] = 'S';
}
if (finish is { } f && f.X == x && f.Y == y)
{
buffer[x] = 'E';
}
}
Console.WriteLine(new string(buffer));
}
}
public sealed class TutorialCell : ICell
{
private bool _isWall;
public bool IsWall() => _isWall;
public void MakeWall() => _isWall = true;
}
public sealed class SnapshotPlotter : IMazePlotter
{
private readonly char[,] _canvas;
public SnapshotPlotter(int width, int height)
{
_canvas = new char[height, width];
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
_canvas[y, x] = ' ';
}
public void PlotWall(int x, int y)
{
_canvas[y, x] = '#';
}
public void PlotPassage(int x, int y)
{
_canvas[y, x] = ' ';
}
public string Render()
{
var builder = new StringBuilder();
for (var y = 0; y < _canvas.GetLength(0); y++)
{
for (var x = 0; x < _canvas.GetLength(1); x++)
{
builder.Append(_canvas[y, x]);
}
builder.AppendLine();
}
return builder.ToString();
}
}