public partial class Maze Form private Stream fileStream private Stack

 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
public partial class Maze : Form
{
private Stream fileStream;
private Stack<int> myStack;
private int[,] matrix;
private int N;
private int M;
private List<int> unvisited;
private Bitmap map;
private Graphics canvas;
public Maze()
{
InitializeComponent();
myStack = new Stack<int>();
unvisited = new List<int>();
}
private void browseFile_Click(object sender, EventArgs e)
{
fileStream = FileWork.BrowseFile();
comboBox.SelectedItem = null;
}
private void startBtn_Click(object sender, EventArgs e)
{
int nv = 0;
try
{
map = new Bitmap(canvasBox.Size.Width, canvasBox.Size.Height);
canvas = Graphics.FromImage(map);
FileWork.GetSize(fileStream, out N, out M);
matrix = FileWork.SaveMatrix(fileStream, N, M);
Draw draw = new Draw(matrix, canvas, canvasBox.Width, canvasBox.Height, N, M);
draw.DrawGrid();
draw.BreakWalls();
SetUnvisited();
MazeTraversal(nv);
canvasBox.Image = map;
canvas.Dispose();
fileStream.Dispose();
}
catch{}
}
private void MazeTraversal(int v)
{
myStack.Push(v);
unvisited.Remove(v);
if (v == M * N - 1)
{
foreach (int item in myStack)
{
Mark(item % N, item / N);
}
}
for (int w = 0; w < N * M; w++)
if (matrix[v, w] == 1)
if (unvisited.Contains(w))
MazeTraversal(w);
myStack.Pop();
}
private void SetUnvisited()
{
for (int i = 0; i < N * M; i++)
unvisited.Add(i);
}
private void Mark(int i, int j)
{
float xSpace = ((canvasBox.Width - 1) / N);
float ySpace = ((canvasBox.Height - 1) / M);
float x = xSpace * i;
float y = ySpace * j;
Pen myPen = new Pen(Color.Black, 1);
canvas.DrawLine(myPen, x, y , x + xSpace, y + ySpace);
canvas.DrawLine(myPen, x + xSpace, y, x, y + ySpace);
}
}