This repository was archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.java
More file actions
152 lines (140 loc) · 3.82 KB
/
Algorithm.java
File metadata and controls
152 lines (140 loc) · 3.82 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
/*
The Gang
****************************
Marcin Sek 18254187
Jordan Marshall 18256716
Ademide Adenuga 18220258
Rioghan Lowry 18226531
****************************
*/
import java.util.ArrayList;
import java.lang.Math;
public class Algorithm
{
private ArrayList<Point> open, close;
private Point start, current, end;
/**
* Algorithm Constructor
*
* @param start the start point/node of the algorithm
* @param end the end point/goal state of the algorithm
*/
public Algorithm(Point start, Point end)
{
this.open = new ArrayList<Point>();
this.close = new ArrayList<Point>();
this.start = start;
this.current = start;
this.end = end;
}
/**
* Finds the shortest route from given the map
*
* @param matrix[][] the map on which the algorithm operates
* @return the path from start node to end node given the map
*/
public ArrayList<Point> find(int matrix[][])
{
current.setScore(current.getG() + manhattenDistance(current, end));
open.add(current);
while (!open.isEmpty())
{
current = open.get(0);
for (Point point : open)
{
current = point.getScore() > current.getScore() ? current : point;
}
if (current.equals(end))
{
break;
}
ArrayList<Point> newPoints = getPoints(current, matrix);
for (Point point : newPoints)
{
open.add(point);
}
close.add(current);
open.remove(current);
}
if (current.equals(end))
{
ArrayList<Point> res = new ArrayList<Point>();
current = current.getParent(); // push 1 gen back (current == end rn)
while (!current.equals(start))
{
res.add(current);
current = current.getParent();
}
return res;
}
return null;
}
/**
* Gets the points that are valid given the current point
*
* @param n the point we are currently looking at
* @param matrix[][] the map of the world in which the point is located
* @return a list of points that are valid given the map and point
*/
private ArrayList<Point> getPoints(Point n, int matrix[][])
{
ArrayList<Point> points = new ArrayList<Point>();
int row = n.getRow();
int col = n.getCol();
if (col + 1 < 8 && matrix[row][col+1] != 9 && !closed(row, col+1))
{
Point newPoint = new Point(row, col+1, n.getG()+1, n);
newPoint.setScore(newPoint.getG() + manhattenDistance(newPoint, end));
points.add(newPoint);
}
if (col - 1 >= 0 && matrix[row][col-1] != 9 && !closed(row, col-1))
{
Point newPoint = new Point(row, col-1, n.getG()+1, n);
newPoint.setScore(newPoint.getG() + manhattenDistance(newPoint, end));
points.add(newPoint);
}
if (row + 1 < 8 && matrix[row+1][col] != 9 && !closed(row+1, col))
{
Point newPoint = new Point(row+1, col, n.getG()+1, n);
newPoint.setScore(newPoint.getG() + manhattenDistance(newPoint, end));
points.add(newPoint);
}
if (row - 1 >= 0 && matrix[row-1][col] != 9 && !closed(row-1, col))
{
Point newPoint = new Point(row-1, col, n.getG()+1, n);
newPoint.setScore(newPoint.getG() + manhattenDistance(newPoint, end));
points.add(newPoint);
}
return points;
}
/**
* Gets the Manhatten Distance between two given points
*
* @param m point from which we are finding the distance
* @param n point to which we are finding the distance
* @return the Manhatten Distance between the points
*/
private int manhattenDistance(Point m, Point n)
{
int distance = (Math.abs(m.getRow()-n.getRow())) + (Math.abs(m.getCol()-n.getCol()));
return distance;
}
/**
* Checks if the point has been closed already
*
* @param row the row index of the point
* @param col the column index of the point
* @return boolean has it been closed already by the algorithm
*/
private boolean closed(int row, int col)
{
for (int i = 0; i < close.size(); i++)
{
if (close.get(i).getRow() == row && close.get(i).getCol() == col)
{
return true;
}
}
return false;
}
}