-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStarAlgorithm.cs
More file actions
223 lines (192 loc) · 7.98 KB
/
AStarAlgorithm.cs
File metadata and controls
223 lines (192 loc) · 7.98 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
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
using System;
using System.Collections.Generic;
using SplashKitSDK;
namespace MyPathFinding
{
// This is an A* algorithm class that implements IWallManagement and IFindPath.
public class AStartPathFinding : IWallManagement, IFindPath<PathNode>
{
// Closest integer of squre root of 2 multipliies by 10 becasue we work with non decimal number.
private const int DIAGONAL_COST = 14;
// Just value of 1 but multiplies by 10 becasue we work with non decimal number (index).
private const int ADJACENT_COST = 10;
private Grid<PathNode> _pathNodeGrid;
private List<PathNode> _openList;
private List<PathNode> _closedList;
private List<PathNode> _visitedList;
// Returns _pathNodeGrid
public Grid<PathNode> GetGrid()
{
return _pathNodeGrid;
}
// Constructor that takes number of column, row, cell size, and starting point.
// It will create a Grid size of column * row that each cell contains pathNode object.
public AStartPathFinding(int column, int row, int cellSize, Point2D point)
{
_pathNodeGrid = new Grid<PathNode>(column, row, cellSize, point, (grid, column, row) => (PathNode) Factory.CreatePathNode(grid, column, row));
}
// Returns visited path.
public List<PathNode> GetVisitedPath(){
return _visitedList;
}
// Finds the shortest path from startx,y to endx,y in the grid.
public List<PathNode> FindPath(int startX, int startY, int endX, int endY)
{
PathNode startNode = _pathNodeGrid.GetValue(startX, startY);
PathNode endNode = _pathNodeGrid.GetValue(endX, endY);
if (startNode == null || endNode == null) return null;
_openList = new List<PathNode> { startNode };
_closedList = new List<PathNode>();
_visitedList = new List<PathNode>();
// Initialize f,g,h costs to each pathnode object in _pathNodeGrid
for (int x = 0; x < _pathNodeGrid.Column; x++)
{
for (int y = 0; y < _pathNodeGrid.Row; y++)
{
PathNode pathNode = _pathNodeGrid.GetValue(x, y);
pathNode.GCost = int.MaxValue;
pathNode.CalculateFCost();
pathNode.CameFromNode = null;
}
}
startNode.GCost = 0;
startNode.HCost = CalculateDistance(startNode, endNode);
startNode.CalculateFCost();
while (_openList.Count > 0)
{
PathNode currentLowestFCostNode = GetLowestFCostNode(_openList);
if (currentLowestFCostNode == endNode)
{
return CalculatePath(endNode);
}
_openList.Remove(currentLowestFCostNode);
_closedList.Add(currentLowestFCostNode);
_visitedList.Add(currentLowestFCostNode);
foreach (PathNode neighbour in GetNeighbourList(currentLowestFCostNode))
{
if (_closedList.Contains(neighbour))
{
continue;
}
if (!neighbour.IsWalkAble)
{
_closedList.Add(neighbour);
continue;
}
int tGcost = currentLowestFCostNode.GCost + CalculateDistance(currentLowestFCostNode, neighbour);
if (tGcost < neighbour.GCost)
{
neighbour.CameFromNode = currentLowestFCostNode;
neighbour.GCost = tGcost;
neighbour.HCost = CalculateDistance(neighbour, endNode);
neighbour.CalculateFCost();
if (!_openList.Contains(neighbour))
{
_openList.Add(neighbour);
}
}
}
}
// searched all areas of the grid, but no valid path found.
return null;
}
// Calsulates distance in integer from nodeA to nodeB
private int CalculateDistance(PathNode nodeA, PathNode nodeB)
{
int distanceX = Math.Abs(nodeA.X - nodeB.X);
int distanceY = Math.Abs(nodeA.Y - nodeB.Y);
int remaining = Math.Abs(distanceX - distanceY);
return DIAGONAL_COST * Math.Min(distanceX, distanceY) + ADJACENT_COST * remaining;
}
// Returns the node object for given axis(x) and ordinate(y).
private PathNode GetNode(int x, int y)
{
return _pathNodeGrid.GetValue(x, y);
}
// Returns the lowest FCost node from given list.
private PathNode GetLowestFCostNode(List<PathNode> pathNodes)
{
PathNode lowestFcostNode = pathNodes[0];
for (int i = 1; i < pathNodes.Count; i++)
{
if (pathNodes[i].FCost < lowestFcostNode.FCost)
{
lowestFcostNode = pathNodes[i];
}
}
return lowestFcostNode;
}
// Returns a list of all nodes that a given node came from.
private List<PathNode> CalculatePath(PathNode endNode)
{
List<PathNode> pathList = new List<PathNode>();
pathList.Add(endNode);
PathNode currenNode = endNode;
while (currenNode.CameFromNode != null)
{
pathList.Add(currenNode.CameFromNode);
currenNode = currenNode.CameFromNode;
}
pathList.Reverse();
return pathList;
}
// Returns a list of all nodes around a given node.
private List<PathNode> GetNeighbourList(PathNode currentNode)
{
List<PathNode> neighbourList = new List<PathNode>();
if (currentNode.X - 1 >= 0)
{
neighbourList.Add(GetNode(currentNode.X - 1, currentNode.Y));
if (currentNode.Y - 1 >= 0)
{
neighbourList.Add(GetNode(currentNode.X - 1, currentNode.Y - 1));
}
if (currentNode.Y + 1 < _pathNodeGrid.Row)
{
neighbourList.Add(GetNode(currentNode.X - 1, currentNode.Y + 1));
}
}
if (currentNode.X + 1 < _pathNodeGrid.Column)
{
neighbourList.Add(GetNode(currentNode.X + 1, currentNode.Y));
if (currentNode.Y - 1 >= 0)
{
neighbourList.Add(GetNode(currentNode.X + 1, currentNode.Y - 1));
}
if (currentNode.Y + 1 < _pathNodeGrid.Row)
{
neighbourList.Add(GetNode(currentNode.X + 1, currentNode.Y + 1));
}
}
if (currentNode.Y - 1 >= 0)
{
neighbourList.Add(GetNode(currentNode.X, currentNode.Y - 1));
}
if (currentNode.Y + 1 < _pathNodeGrid.Row)
{
neighbourList.Add(GetNode(currentNode.X, currentNode.Y + 1));
}
return neighbourList;
}
// Create a not walkable node (wall) with given axis(x) and ordinate(y).
public void MakeWall(int x, int y)
{
PathNode wallNode = _pathNodeGrid.GetValue(x, y);
if (wallNode != null)
{
wallNode.IsWalkAble = false;
_closedList.Add(wallNode);
}
}
// Removes a not walkable node (wall) with given axis(x) and ordinate(y).
public void RemoveWall(int x, int y)
{
PathNode wallNode = _pathNodeGrid.GetValue(x, y);
if (wallNode != null)
{
wallNode.IsWalkAble = true;
_closedList.Remove(wallNode);
}
}
}
}