-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.cs
94 lines (82 loc) · 3.05 KB
/
Grid.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace DFSRBMGA
{
/* depth-first search recursive backtracker maze generation algorithm */
public class Grid
{
public int CellSize { get; set; }
public int GridSize { get; set; }
public List<Cell> Cells { get; set; } = new List<Cell>();
public Cell ActiveCell { get; set; }
private Stack<Cell> MazedCells = new Stack<Cell>();
private Random Random;
private Cell NextCell;
public Grid(int cellSize, int gridSize) {
Random = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
CellSize = cellSize;
GridSize = gridSize;
var RowColumnCount = GridSize / CellSize;
for (var i = 0; i < RowColumnCount; i++) {
for (var j = 0; j < RowColumnCount; j++) {
Cells.Add(new Cell(i, j, cellSize));
}
}
ActiveCell = Cells[0];
}
public bool Update() {
if (!Cells.All(c => c.Mazed)) {
ActiveCell.Mazed = true;
try {
NextCell = GetRandomNeighbour(ActiveCell);
MazedCells.Push(ActiveCell);
RemoveBorders(ActiveCell, NextCell);
} catch {
NextCell = MazedCells.Pop();
}
ActiveCell = NextCell;
return true;
}
if (MazedCells.Count > 0) {
NextCell = MazedCells.Pop();
ActiveCell = NextCell;
return true;
}
return false;
}
private Cell GetRandomNeighbour(Cell cell) {
var k = cell.Location.X * cell.Location.Y * Random.Next(1, 11);
for (var r = 0; r < k; r++) {
Random.Next();
}
var Neighbours = Cells.FindAll(c => Helper.IsNeighbouringPoint(cell.Location, c.Location) && !c.Mazed);
if (Neighbours.Count == 0) {
throw new MissingFieldException();
}
return Neighbours[Random.Next(0, Neighbours.Count)];
}
private void RemoveBorders(Cell first, Cell other) {
var Dx = other.Location.X - first.Location.X;
var Dy = other.Location.Y - first.Location.Y;
var FirstBorder = Borders.None;
var OtherBorder = Borders.None;
if (Dx == 1) {
FirstBorder = Borders.Right;
OtherBorder = Borders.Left;
} else if (Dx == -1) {
FirstBorder = Borders.Left;
OtherBorder = Borders.Right;
}
if (Dy == 1) {
FirstBorder = Borders.Bottom;
OtherBorder = Borders.Top;
} else if (Dy == -1) {
FirstBorder = Borders.Top;
OtherBorder = Borders.Bottom;
}
first.Borders = first.Borders & ~FirstBorder;
other.Borders = other.Borders & ~OtherBorder;
}
}
}