-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMazeSolver.cs
134 lines (110 loc) · 4.77 KB
/
MazeSolver.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
using System.Collections.Generic;
using System.Drawing;
namespace Arkose_maze_Solver
{
public class MazeSolver
{
#region Variables
private Color StartColor;
private Color FinishColor;
private Color WallColor;
private Color PathColor;
private Bitmap mazeimg;
private Node[,] Nodes;
private Queue<Node> StartNodes;
#endregion // All Variables we need in this class
public MazeSolver(Bitmap mazeImage, Color start, Color finish, Color wall, Color path)
{
mazeimg = mazeImage;
StartColor = start;
FinishColor = finish;
WallColor = wall;
PathColor = path;
Nodes = new Node[mazeimg.Width, mazeimg.Height];
StartNodes = new Queue<Node>();
}
public CropDetail SolveMaze()
{
for (int i = 0; i < mazeimg.Width; i++)// Find Start Color Pixels and add all other pixels to Nodes Queue
{
for (int j = 0; j < mazeimg.Height; j++)
{
Color pixel = mazeimg.GetPixel(i, j);
Nodes[i, j] = new Node(null, pixel, i, j, false);
if (pixel.ToArgb().Equals(StartColor.ToArgb()))
{
StartNodes.Enqueue(new Node(null, pixel, i, j, true));
}
}
}
if (StartNodes.Count == 0)
{
return new CropDetail(mazeimg, false);
}
Node finishNode = BFSSolver(); // Maze solution using Breadth-first search algorithm
if (finishNode == null)
{
return new CropDetail(mazeimg, false);
}
using (Graphics graphics = Graphics.FromImage(mazeimg))
{
while (finishNode != null) // Create Path Color
{
using (Pen pen = new Pen(PathColor, 1))
{
graphics.DrawRectangle(pen, finishNode.GetX(), finishNode.GetY(), 1, 1);
}
finishNode = finishNode.GetParent();
}
}
return new CropDetail(mazeimg, true);
}
public Node BFSSolver()
{
Queue<Node> nodeQueue = new Queue<Node>();
while (StartNodes.Count != 0)
{
nodeQueue.Enqueue(StartNodes.Dequeue());
Node current;
while (nodeQueue.Count != 0)
{
current = nodeQueue.Dequeue();
Color nowColor = mazeimg.GetPixel(current.GetX(), current.GetY());
if (nowColor.ToArgb().Equals(WallColor.ToArgb())) // Skip Walls
{
continue;
}
if (nowColor.ToArgb().Equals(FinishColor.ToArgb())) // if Find FinishColor then we solved maze and return Node for create Path color
{
return current;
}
if (current.GetY() - 1 >= 0 && !Nodes[current.GetX(), current.GetY() - 1].isVisit())
{
Nodes[current.GetX(), current.GetY() - 1].isSeen(true);
Nodes[current.GetX(), current.GetY() - 1].SetParent(current);
nodeQueue.Enqueue(Nodes[current.GetX(), current.GetY() - 1]);
}
if (current.GetY() + 1 < mazeimg.Height && !Nodes[current.GetX(), current.GetY() + 1].isVisit())
{
Nodes[current.GetX(), current.GetY() + 1].isSeen(true);
Nodes[current.GetX(), current.GetY() + 1].SetParent(current);
nodeQueue.Enqueue(Nodes[current.GetX(), current.GetY() + 1]);
}
if (current.GetX() - 1 >= 0 && !Nodes[current.GetX() - 1, current.GetY()].isVisit())
{
Nodes[current.GetX() - 1, current.GetY()].isSeen(true);
Nodes[current.GetX() - 1, current.GetY()].SetParent(current);
nodeQueue.Enqueue(Nodes[current.GetX() - 1, current.GetY()]);
}
if (current.GetX() + 1 < mazeimg.Width && !Nodes[current.GetX() + 1, current.GetY()].isVisit())
{
Nodes[current.GetX() + 1, current.GetY()].isSeen(true);
Nodes[current.GetX() + 1, current.GetY()].SetParent(current);
nodeQueue.Enqueue(Nodes[current.GetX() + 1, current.GetY()]);
}
}
}
return null;
}
}
}