-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMaze.cs
498 lines (392 loc) · 13.4 KB
/
Maze.cs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Priority_Queue;
internal class Maze {
private const string WallWithLeftSideEmpty = " ";
private delegate void Go();
private const string WallWithLeftSideEmptyAndCursor = " ■ ";
private const string WallWithLeftSideFull = "| ";
private const string WallWithLeftSideFullAndCursor = "| ■ ";
private const string FloorWithHole = "+ ";
private const string FloorWithoutHole = "+---";
private const string WallEnd = "|\n";
private const string WallEndWithExit = " \n";
private const string FloorCorner = "+\n";
private static Cell[][] _maze;
private static (int, int) _cursor;
private static void Main(string[] args) {
Console.ForegroundColor = ConsoleColor.Green;
Console.CursorVisible = false;
Console.Title = "Maze";
Console.WriteLine("<----------------Maze----------------->");
Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)");
Console.WriteLine("Press any key to start.");
Console.ReadKey();
Start();
}
private static void Start() {
PrintTitle();
ChooseOption();
Console.ReadKey();
}
private static void ChooseOption() {
Console.WriteLine("Options");
Console.WriteLine("1 - Generate maze");
Console.WriteLine("2 - Generate maze with animations");
Console.WriteLine("3 - Generate and solve");
var isOptionCorrect = false;
do {
var key = Console.ReadKey().Key;
switch (key) {
case ConsoleKey.D1:
GenerateMaze(false);
PrintMaze(false);
isOptionCorrect = true;
break;
case ConsoleKey.D2:
GenerateMaze(true);
PrintMaze(false);
isOptionCorrect = true;
break;
case ConsoleKey.D3:
Solve();
isOptionCorrect = true;
break;
default:
Console.Write("\b");
continue;
}
} while (!isOptionCorrect);
}
private static void Solve() {
GenerateMaze(false);
// Starting point
var startingPoint = (0, 0);
var goal = (_maze.Length - 1, _maze[0].Length - 1);
// A* Algorithm
var frontierQueue = new SimplePriorityQueue<(int, int)>();
var cameFrom = new Dictionary<(int, int), (int, int)?>();
var costSoFar = new Dictionary<(int, int), int>();
frontierQueue.Enqueue(startingPoint, 0);
cameFrom.Add(startingPoint, null);
costSoFar.Add(startingPoint, 0);
while (frontierQueue.Count > 0) {
var current = frontierQueue.Dequeue();
//Early exit
if (current == goal) break;
foreach (var next in GetNeighbors(current)) {
var newCost = costSoFar[current] + CalculateCost(next, current);
if (!costSoFar.ContainsKey(next) && !cameFrom.ContainsKey(next)) {
costSoFar.Add(next, newCost);
var priority = newCost + CalculateCost(goal, next);
frontierQueue.Enqueue(next, priority);
cameFrom.Add(next, current);
}
}
}
// Get path
var currentBackTrack = cameFrom[goal].Value;
var path = new Queue<(int, int)>();
path.Enqueue(goal);
do {
path.Enqueue(currentBackTrack);
currentBackTrack = cameFrom[currentBackTrack].Value;
} while (currentBackTrack != startingPoint);
path.Enqueue(startingPoint);
PrintMazeWithSolution(path);
}
private static IEnumerable<(int, int)> GetNeighbors((int, int) position) {
var neighbors = new List<(int, int)>();
if (IsCellAbove(position) && _maze[position.Item1][position.Item2].Top) {
neighbors.Add((position.Item1 - 1, position.Item2));
}
if (IsCellBelow(position) && _maze[position.Item1][position.Item2].Bottom) {
neighbors.Add((position.Item1 + 1, position.Item2));
}
if (IsCellOnLeft(position) && _maze[position.Item1][position.Item2].Left) {
neighbors.Add((position.Item1, position.Item2 - 1));
}
if (IsCellOnRight(position) && _maze[position.Item1][position.Item2].Right) {
neighbors.Add((position.Item1, position.Item2 + 1));
}
return neighbors;
}
private static int CalculateCost((int, int) goal, (int, int) point) {
return
// Rows
Math.Abs(point.Item1 - goal.Item1)
// Columns
+ Math.Abs(point.Item2 - goal.Item2);
}
private static void GenerateMaze(bool showAnimation) {
var width = GetWidthFromPlayer();
var height = GetHeightFromPlayer();
Console.WriteLine("Generating maze " + width + "x" + height);
GenerateEmptyMaze(width, height);
DepthFirstSearchAlgorithm(showAnimation);
}
private static void DepthFirstSearchAlgorithm(bool showAnimation) {
var random = new Random();
// Entrance at (0, 0)
_maze[0][0].Left = true;
// Pick random place (why not?)
_cursor = (random.Next(0, _maze.Length), random.Next(0, _maze[0].Length));
_maze[_cursor.Item1][_cursor.Item2].Visited = true;
// Add first item to stack
var logStack = new Stack<(int, int)>();
logStack.Push(_cursor);
// Delegate to store method. I just wanted to try it...
Go go = null;
// Depth-first search algorithm
do {
if (IsDeadEnd(_cursor)) {
if (logStack.Count <= 1) break;
logStack.Pop();
// Go back (backtrack)
_cursor = logStack.Peek();
if (showAnimation) {
Thread.Sleep(100);
PrintMaze(true);
}
continue;
}
if (IsBottomCellNotVisited(_cursor))
go += GoBottom;
if (IsTopCellNotVisited(_cursor))
go += GoTop;
if (IsLeftCellNotVisited(_cursor))
go += GoLeft;
if (IsRightCellNotVisited(_cursor))
go += GoRight;
// Get random direction
go = (Go) go.GetInvocationList()[random.Next(0, go.GetInvocationList().Length)];
//Goto that direction
go();
// Clear delegate
go = null;
// Add new entry to log
logStack.Push(_cursor);
if (showAnimation) {
Thread.Sleep(300);
PrintMaze(true);
}
} while (logStack.Count > 0);
// Exit at bottom right corner
_maze[_maze.Length - 1][_maze[0].Length - 1].Right = true;
}
private static void GoBottom() {
_maze[_cursor.Item1][_cursor.Item2].Bottom = true;
_cursor.Item1 += 1;
_cursor.Item2 += 0;
_maze[_cursor.Item1][_cursor.Item2].Visited = true;
_maze[_cursor.Item1][_cursor.Item2].Top = true;
}
private static void GoTop() {
_maze[_cursor.Item1][_cursor.Item2].Top = true;
_cursor.Item1 += -1;
_cursor.Item2 += 0;
_maze[_cursor.Item1][_cursor.Item2].Visited = true;
_maze[_cursor.Item1][_cursor.Item2].Bottom = true;
}
private static void GoLeft() {
_maze[_cursor.Item1][_cursor.Item2].Left = true;
_cursor.Item1 += 0;
_cursor.Item2 += -1;
_maze[_cursor.Item1][_cursor.Item2].Visited = true;
_maze[_cursor.Item1][_cursor.Item2].Right = true;
}
private static void GoRight() {
_maze[_cursor.Item1][_cursor.Item2].Right = true;
_cursor.Item1 += 0;
_cursor.Item2 += 1;
_maze[_cursor.Item1][_cursor.Item2].Visited = true;
_maze[_cursor.Item1][_cursor.Item2].Left = true;
}
private static void PrintMaze(bool printCursor) {
Console.Clear();
PrintTitle();
//+---+
//| | 1x1 block
//+---+
var board = " ";
// Add first line
board = AddOneLine(board);
// Crate maze in one string
for (var row = 0; row < _maze.Length; row++)
for (var k = 1; k <= 2; k++) {
board += " ";
for (var column = 0; column < _maze[row].Length; column++)
board = AddMazeFragment(new[] {_cursor}, board, row, k, column);
board = AddEndLines(board, k, row);
}
// Print maze
Console.WriteLine(board);
}
private static void PrintMazeWithSolution(IEnumerable<(int, int)> solution) {
Console.Clear();
PrintTitle();
//+---+
//| | 1x1 block
//+---+
var board = " ";
// Add first line
board = AddOneLine(board);
// Crate maze in one string
var cursors = solution.ToList();
for (var row = 0; row < _maze.Length; row++)
for (var k = 1; k <= 2; k++) {
board += " ";
for (var column = 0; column < _maze[row].Length; column++)
board = AddMazeFragment(cursors, board, row, k, column);
board = AddEndLines(board, k, row);
}
// Print maze
Console.WriteLine(board);
}
private static string AddMazeFragment(IEnumerable<(int, int)> cursors, string board, int row, int k, int column) {
if (ShouldPrintVertical(k))
board = AddWall(cursors, board, row, column);
else
board = AddFloor(board, row, column);
return board;
}
private static string AddWall(IEnumerable<(int, int)> cursors, string board, int row, int column) {
if (ShouldPrintWall(row, column))
board = AddWallWithFullSideAndCursor(cursors, board, row, column);
else
board = AddWallWithEmptySideAndCursor(cursors, board, row, column);
return board;
}
private static string AddFloor(string board, int row, int column) {
if (ShouldPrintFloor(row, column))
board += FloorWithoutHole;
else
board += FloorWithHole;
return board;
}
private static string AddWallWithEmptySideAndCursor(IEnumerable<(int, int)> cursors, string board, int row, int column) {
var positions = cursors.ToList();
if (positions.Any() && CursorIsAtPositions(positions, row, column))
board += WallWithLeftSideEmptyAndCursor;
else
board += WallWithLeftSideEmpty;
return board;
}
private static string AddWallWithFullSideAndCursor(IEnumerable<(int, int)> cursors, string board, int row, int column) {
var positions = cursors.ToList();
if (positions.Any() && CursorIsAtPositions(positions, row, column))
board += WallWithLeftSideFullAndCursor;
else
board += WallWithLeftSideFull;
return board;
}
private static bool IsDeadEnd((int, int) cursor) {
// Cell that has no unvisited neighbors being considered a dead-end ~ Wikipedia
// cursor.Item1 - Row (Y)
// cursor.Item2 - Column (X)
// Top
if (IsTopCellNotVisited(cursor)) return false;
// Bottom
if (IsBottomCellNotVisited(cursor)) return false;
// Left
if (IsLeftCellNotVisited(cursor)) return false;
// Right
if (IsRightCellNotVisited(cursor)) return false;
return true;
}
private static bool IsRightCellNotVisited((int, int) cursor) {
return IsCellOnRight(cursor) && !_maze[cursor.Item1][cursor.Item2 + 1].Visited;
}
private static bool IsLeftCellNotVisited((int, int) cursor) {
return IsCellOnLeft(cursor) && !_maze[cursor.Item1][cursor.Item2 - 1].Visited;
}
private static bool IsBottomCellNotVisited((int, int) cursor) {
return IsCellBelow(cursor) && !_maze[cursor.Item1 + 1][cursor.Item2].Visited;
}
private static bool IsTopCellNotVisited((int, int) cursor) {
return IsCellAbove(cursor) && !_maze[cursor.Item1 - 1][cursor.Item2].Visited;
}
private static bool IsCellOnRight((int, int) cursor) {
return cursor.Item2 + 1 < _maze[0].Length;
}
private static bool IsCellOnLeft((int, int) cursor) {
return cursor.Item2 - 1 >= 0;
}
private static bool IsCellBelow((int, int) cursor) {
return cursor.Item1 + 1 < _maze.Length;
}
private static bool IsCellAbove((int, int) cursor) {
return cursor.Item1 - 1 >= 0;
}
private static bool CursorIsAtPositions(IEnumerable<(int, int)> positions, int row, int column) {
return positions.Contains((row, column));
}
private static bool ShouldPrintFloor(int row, int column) {
return _maze[row][column].Bottom == false;
}
private static bool ShouldPrintWall(int row, int column) {
return _maze[row][column].Left == false;
}
private static string AddEndLines(string board, int k, int row) {
if (ShouldPrintVertical(k)) {
if (_maze[row][_maze[0].Length - 1].Right)
board += WallEndWithExit;
else
board += WallEnd;
} else {
board += FloorCorner;
}
return board;
}
private static bool ShouldPrintVertical(int k) {
return k % 2 == 1;
}
private static string AddOneLine(string board) {
for (var j = 0; j < _maze[0].Length; j++) board += "+---";
board += "+\n";
return board;
}
private static void GenerateEmptyMaze(int width, int height) {
_maze = new Cell[height][];
for (var row = 0; row < height; row++) {
_maze[row] = new Cell[width];
for (var column = 0; column < _maze[row].Length; column++) _maze[row][column] = new Cell();
}
}
private static int GetWidthFromPlayer() {
int width;
Console.Write("\bEnter width: ");
while (!int.TryParse(Console.ReadLine(), out width) || width < 1 || width > 100) {
Console.WriteLine("Wrong value");
Console.Write("\bEnter width: ");
}
return width;
}
private static int GetHeightFromPlayer() {
int height;
Console.Write("\bEnter height: ");
while (!int.TryParse(Console.ReadLine(), out height) || height < 1 || height > 100) {
Console.WriteLine("Wrong value");
Console.Write("\bEnter height: ");
}
return height;
}
private static void PrintTitle() {
Console.Clear();
Console.WriteLine(" +----------+");
Console.WriteLine(" |/ \\|");
Console.WriteLine(" | Maze |");
Console.WriteLine(" |\\ /|");
Console.WriteLine(" +----------+");
Console.Write("\n");
}
}
internal class Cell {
public bool Top;
public bool Bottom;
public bool Left;
public bool Right;
public bool Visited;
}