-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStarProgram.cs
431 lines (395 loc) · 17.9 KB
/
AStarProgram.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
using System;
using System.Collections;
using System.IO;
//William Jones
//Simple A* program
//Asks the user to input a maze and then uses the A* pathfinding algorithm to solve it
namespace Astar
{
class AStarProgram
{
static readonly int IMPASSABLE_VALUE = 0;
static readonly int MAX_DRAW_WIDTH = 20;
static bool shouldPrintGrid = true;
static int gridWidth = 0;
static int gridHeight = 0;
static int startX = 0;
static int startY = 0;
static int endX = 0;
static int endY = 0;
static Node[,] gridArray;
static ArrayList checkNodes = new ArrayList();
static void Main(string[] args)
{
//Get the file name and path and try to open the file
Console.Write("Enter the file: ");
string fileLocation = Console.ReadLine();
try { openFile(fileLocation);}
catch (Exception e) {Console.WriteLine("Bad file: " + e.ToString()); Environment.Exit(-1); }
//The file has been loaded now ask for starting and ending locations
try { getLocations(); }
catch (Exception e) { Console.WriteLine("Bad input: " + e.ToString()); Environment.Exit(-1);}
//Try to find the path
try { findPath(); }
catch (Exception e) { Console.WriteLine("Bad map: " + e.ToString()); Environment.Exit(-1); }
}
private static void getLocations()
{
//Collect the inputs, parse them, and make sure they're good values
//Start X
Console.Write("Input the x coordinate of the start (with 0 being left): ");
if (int.TryParse(Console.ReadLine(), out startX))
{
if (startX >= gridWidth) { throw new Exception("Start X is too large"); }
if (startX < 0) { throw new Exception("Start X is too small"); }
}
//Start Y
Console.Write("Input the y coordinate of the start (with 0 being top): ");
if (int.TryParse(Console.ReadLine(), out startY))
{
if (startY >= gridHeight) { throw new Exception("Start y is too large"); }
if (startY < 0) { throw new Exception("Start y is too small"); }
}
//End X
Console.Write("Input the x coordinate of the end (with 0 being left): ");
if (int.TryParse(Console.ReadLine(), out endX))
{
if (endX >= gridWidth) { throw new Exception("End x is too large"); }
if (endX < 0) { throw new Exception("End x is too small"); }
}
//End Y
Console.Write("Input the y coordinate of the end (with 0 being top): ");
if (int.TryParse(Console.ReadLine(), out endY))
{
if (endY >= gridHeight) { throw new Exception("End y is too large"); }
if (endY < 0) { throw new Exception("End y is too small"); }
}
}
//Opens the file with the given string
private static void openFile(string fileName)
{
using (StreamReader fileStream= new StreamReader(fileName))
{
//Get file header
string headerLine = fileStream.ReadLine();
if (headerLine != null)
{
//Get the header line as an array of chars
string[] headerArray = headerLine.Split(" ");
//Parse header
if (headerArray.Length != 2)
{
throw new Exception("File parsing error, wrong number of arguments in header");
}
else
{
try
{
//Set the grid width and height
gridHeight = int.Parse(headerArray[0]);
gridWidth = int.Parse(headerArray[1]);
if (gridWidth > MAX_DRAW_WIDTH)
{
shouldPrintGrid = false;
}
} catch
{
throw new Exception("File parsing error, bad header value");
}
}
}
else
{
throw new Exception("File parsing error, no header");
}
//Create the grid array
gridArray = new Node[gridWidth, gridHeight];
//Now loop through the rest of the file to fill in the grid
int putY = 0; //Where to put the node
string currentLine = "";
while ((currentLine = fileStream.ReadLine()) != null)
{
//Make sure there aren't too many rows
if (putY < gridHeight)
{
//Loop through the entire line
int putX = 0; //Where to put the node
foreach (char currentChar in currentLine.ToCharArray())
{
//Make sure the X value isn't too large
if (putX < gridWidth)
{
int currentInt = 0;
if (int.TryParse(currentChar.ToString(), out currentInt))
{
//If the int can be parsed put it in the grid
Node createdNode = new Node(currentInt, putX, putY);
gridArray[putX, putY] = createdNode;
createdNode.setManhattanDistance(endX, endY);
//And print it out if it should be printed
if (shouldPrintGrid)
{
Console.Write(currentInt);
Console.Write(" ");
}
}
else
{
throw new Exception("Grid parsing error: Integer parsing error");
}
//Increment the x position
putX += 1;
}
else
{
throw new Exception("Grid parsing error: too many columns in row number " + putY);
}
}
//If there aren't enough columns then throw an error
if (putX < gridWidth)
{
throw new Exception("Grid parsing error: not enough columns in row number " + putY);
}
//Go to the next line in the grid array
putY += 1;
//Print out a new line if the grid isn't too wide
if (shouldPrintGrid) { Console.WriteLine(); }
}
else
{
throw new Exception("Grid parsing error: too many rows");
}
}
//Check to make sure there are enough lines
if (putY < gridHeight)
{
throw new Exception("Grid parsing error: not enough rows");
}
}
}
//The base method for finding a path using A*
private static void findPath()
{
bool foundPath = false; //Whether or not a path has been found
Node startingNode = getNodeAtPosition(startX, startY); //The starting node
startingNode.setPath(true); //Tell the starting node it's on the final path
Node currentNode = startingNode; //It should start at the beginning
Node endingNode = getNodeAtPosition(endX, endY); //It should end at the end
while (!foundPath)
{
//Circle the current node
//and set the estimated distances for all nodes surrounding the current node
currentNode.setCircled(true);
for (int ii = 0; ii < 8; ii += 1)
{
checkNeighborNode(currentNode, ii);
}
Node nextNode;
//Get the next node if there is one in the check list
if (checkNodes.Count > 0) { nextNode = (Node)checkNodes[0]; }
else { nextNode = null; }
//If the nextNode isn't null
if (nextNode != null)
{
//Remove it from the check list because we're using it now
checkNodes.RemoveAt(0);
//Circle it
nextNode.setCircled(true);
//Now set it as the current node
currentNode = nextNode;
//Now see if we've found the end
if (currentNode == endingNode)
{
Console.WriteLine("Found the end node");
Console.WriteLine("Total distance is: " + currentNode.getDistanceFromStart());
Console.WriteLine("The backtracking path is: ");
foundPath = true;
//We know for sure that we've found the end because the end node was in the top of the check list
}
}
else //We're all out of nodes to check and there isn't an end anywhere
{
throw new Exception("End not reachable");
}
}
//Now that the path is found, backtrack and build the path
Node backTrackCurrent = currentNode;
Console.WriteLine(backTrackCurrent.getSimpleString());
while (backTrackCurrent != startingNode)
{
backTrackCurrent.setPath(true);
backTrackCurrent = backTrackCurrent.getParent();
Console.WriteLine(backTrackCurrent.getSimpleString());
}
//Print the path if it can be printed
if (shouldPrintGrid)
{
printPath();
}
}
//Gets the node at the given position and makes sure it exists
private static Node getNodeAtPosition(int inX, int inY)
{
Node returnNode = null;
if (inX < gridArray.GetLength(0) && inY < gridArray.GetLength(1))
{
returnNode = gridArray[inX, inY];
}
//Throw an exception
if (returnNode == null)
{
throw new Exception("Invalid position of (" + inX + "," + inY + ")");
}
return returnNode;
}
//Prints out the entire path
private static void printPath()
{
//Loop through every node in the grid
for (int yy = 0; yy < gridHeight; yy += 1)
{
//Add a new line
Console.WriteLine();
for (int xx = 0; xx < gridWidth; xx += 1)
{
//Get the current node
Node currentNode = getNodeAtPosition(xx, yy);
string printString = "";
//If it's on the path print an X if not print a -
if (currentNode.getPath())
{
printString = "X";
}
else
{
printString = "-";
}
Console.Write(printString);
}
}
}
/// <summary>
/// Checks the neiboring nodes
/// </summary>
/// <param name="inDirection">0-7, counter-clockwise starting at 0 degrees. (degrees = direction*45)</param>
private static void checkNeighborNode(Node inNode, int inDirection)
{
int nodeX = inNode.getX();
int nodeY = inNode.getY();
int checkX = nodeX;
int checkY = nodeY;
//Simple equation to set the distance multiplication
float distanceMultiplication = (inDirection % 2==0) ? 1.0f : 1.414f;
//Get the neighboring node's position
switch(inDirection)
{
case 0: checkX += 1; break;
case 1: checkX += 1; checkY -= 1; break;
case 2: checkY -= 1; break;
case 3: checkX -= 1; checkY -= 1; break;
case 4: checkX -= 1; break;
case 5: checkX -= 1; checkY += 1; break;
case 6: checkY += 1; break;
case 7: checkX += 1; checkY += 1; break;
}
//Only continue if the check position is still on the grid
if (checkX < gridWidth && checkX >= 0 && checkY < gridHeight && checkY >= 0)
{
//Now that we know it's still on the grid, get the node at that position
Node checkNode = getNodeAtPosition(checkX, checkY);
//Now only continue if the checkNode hasn't been circled and it's passable
if (!checkNode.getCircled() && checkNode.getValue() != IMPASSABLE_VALUE)
{
//Now calculate the various values for the check node
float currentNodeDistanceFromStart = inNode.getDistanceFromStart();
float toNodeDistance = checkNode.getValue() * distanceMultiplication;
float estimatedDistance = currentNodeDistanceFromStart + toNodeDistance + checkNode.getManhattanDistance();
//Update the estimated distance if it'll be smaller
//or if it hasn't been set yet
if (estimatedDistance < checkNode.getEstimatedDistance() || checkNode.getEstimatedDistance() < 0)
{
checkNode.setParent(inNode);
checkNode.setDistanceFromStart(currentNodeDistanceFromStart + toNodeDistance);
checkNode.setEstimatedDistance(estimatedDistance);
addToCheckList(checkNode);
}
}
}
}
//Adds a node to the check list and orders it according to its estimated distance
private static void addToCheckList(Node inNode)
{
//If it's already in the checkList then delete it
if (checkNodes.Contains(inNode))
{
checkNodes.Remove(inNode);
}
//Now try to insert it as early as possible to the 0th index will always be the lowest
bool inserted = false;
for (int ii = 0; ii< checkNodes.Count; ii+=1)
{
if (inNode.getEstimatedDistance() < ((Node) checkNodes[ii]).getEstimatedDistance())
{
checkNodes.Insert(ii, inNode);
inserted = true;
break;
}
}
//If it wasn't inserted then that means it's the largest so add it to the end
if (inserted == false)
{
checkNodes.Add(inNode);
}
}
}
//A simple node class to keep things easier to manage
class Node
{
//The node's properties
private float myValue = 0.0f; //the value
private int myX = 0; //the X position on the grid
private int myY = 0; //the Y position on the grid
private Node myParent = null; //the node that this node should point to on the path
private bool myCircled = false; //Whether or not the node is "circled"
private float myDistanceFromStart = 0; //The node's distance from the start node
private int myManhattanDistance = -1; //The node's manhattan distance to the end
private float myEstimatedDistance = -1; //The node's current estimated distance
private bool isOnPath = false; //Whether or not the node is on the final path
//Constructor to get the value and the x and y positions
public Node (int value, int x, int y)
{
myValue = value;
myX = x;
myY = y;
}
//Simple math for the manhattan distance
public void setManhattanDistance(int endPosX, int endPosY)
{
myManhattanDistance = Math.Abs(endPosX - myX) + Math.Abs(endPosY - myY);
}
//Getters and setters, lots of them
public int getX() { return myX; }
public int getY() { return myY; }
public void setParent(Node inParent) { myParent = inParent; }
public Node getParent() { return myParent;}
public void setEstimatedDistance(float inEstimated) { myEstimatedDistance = inEstimated; }
public float getEstimatedDistance() { return myEstimatedDistance; }
public void setDistanceFromStart(float inDistance) { myDistanceFromStart = inDistance; }
public float getDistanceFromStart() { return myDistanceFromStart; }
public int getManhattanDistance() { return myManhattanDistance; }
public float getValue() { return myValue; }
public string getSimpleString() { return "[" + myX + "," + myY + "]"; }
public bool getCircled() { return myCircled; }
public void setCircled(bool inValue) { myCircled = inValue; }
public bool getPath() { return isOnPath; }
public void setPath(bool inValue) { isOnPath = inValue; }
//A toString method for debugging
override
public string ToString()
{
return "["+ myX +","+ myY +"] val:" + myValue.ToString()
+ " est:" + myEstimatedDistance
+ " man:" + myManhattanDistance;
}
}
}