Fixed pathfinding v1

This commit is contained in:
t-moe
2016-06-17 00:55:49 +02:00
parent 5ac72ae909
commit d221c222c4
2 changed files with 71 additions and 71 deletions

View File

@@ -58,10 +58,8 @@ public class Game {
} }
public boolean canMove(Point src, Point dst){ public boolean canMove(Point src, Point dst){
//TODO: uncomment next line and implement getPath return getPath(src, dst)!=null;
//return getPath(src, dst)!=null;
return true;
} }
public boolean doMove(Point src, Point dst){ public boolean doMove(Point src, Point dst){
@@ -85,66 +83,56 @@ public class Game {
/* /*
* Pathfinding of shortest path between src and dst. * Pathfinding of shortest path between src and dst.
*/ */
public ArrayList<Vertex> getPath(Point src, Point dst){ public List<Point> getPath(final Point src, final Point dst){
ArrayList<Vertex> vertices = new ArrayList<Vertex>(); // List of vertices ArrayList<Vertex> vertices = new ArrayList<Vertex>(); // List of vertices
ArrayList<Vertex> path = new ArrayList<Vertex>(); // Output
Vertex u = new Vertex(0, src);
Vertex v = new Vertex(0, dst);
int tmp, tmp_i = 0;
int alt = 0;
vertices.add(new Vertex(0, src));
// Get a verticies list from the field data // Get a verticies list from the field data
for(int i = 0; i < size; i++){ for(int i= 0; i < size; i++){
for(int j = 0; i < size; i++){ for(int j = 0; j < size; j++){
if(field[i][j] == 0){ if(field[i][j] == 0 && (src.x!=i || src.y!=j)){ //field empty and not src
vertices.add(new Vertex(Integer.MAX_VALUE, new Point(i, j))); vertices.add(new Vertex(Integer.MAX_VALUE, new Point(i, j)));
} }
} }
} }
ArrayList<Vertex> allVerticies = new ArrayList<Vertex>(vertices); // List of vertices
for(int i = 0; i < size*size; i++){
vertices.add(new Vertex(Integer.MAX_VALUE, new Point(i%7,i%7))); // Initialize all vertices
}
vertices.get(src.x * src.y).setDist(0); // Set distance from source to source
while(!vertices.isEmpty()){ // As long as there are vertices while(!vertices.isEmpty()){ // As long as there are vertices
tmp = minDistance(u, vertices); // Get the index of v with the least distance to u final Vertex u = findNearestVertex(vertices);
u = vertices.get(tmp); vertices.remove(u); // Remove u from the set of vertices
vertices.remove(tmp); // Remove u from the set of vertices
for(int i = 0; i < 4; i++){ // for each neighbour of u ... final Point[] offsets = {
if(i == 0){ new Point(0,1),
tmp_i = u.getPrev().x * u.getPrev().y - 7; // neighbour above new Point(0,-1),
} new Point(1,0),
new Point(-1,0)
if(i == 1){ };
tmp_i = u.getPrev().x * u.getPrev().y + 7; // neighbour below
} for(int i = 0; i < 4; i++){ // for each neighbour of u ...
final Point p = u.getPos();
if(i == 2){ final Point offs = offsets[i];
tmp_i = u.getPrev().x * u.getPrev().y - 1; // left neighbour int x = p.x + offs.x;
} int y = p.y + offs.y;
if(x<0 || y<0 || x>=size || y>= size) continue;
if(i == 3){
tmp_i = u.getPrev().x * u.getPrev().y + 1; // right neighbour final Vertex v = findVertex(x,y, vertices);
} //distanz_update(u,v)
if(v!=null){
if(tmp_i >= 0 && tmp_i <= 49){ int alternative = u.getDist()+1;
alt = u.getDist() + distanceBetween(u,v); if( alternative< v.getDist()) {
if(alt < v.getDist()){ // A shorter path has been found v.setDist(alternative);
v.setDist(alt); v.setPrev(u);
v.setPrev(u.getPrev());
path.add(v);
} }
} }
} }
} }
return path; return reconstructShortestPath(allVerticies, dst);
} }
public boolean doUndo(){ public boolean doUndo(){
@@ -195,38 +183,50 @@ public class Game {
} }
/** /**
* Calculates the minimal distance between a vertex and a set of vertices. * Finds the nearest vertex to start
*/ */
private int minDistance(Vertex v, ArrayList<Vertex> vertices){ private Vertex findNearestVertex(final List<Vertex> vertices){
int dist = Integer.MAX_VALUE; Vertex tmp = vertices.get(0);
int tmp = 0;
int out = 0;
for (int i = 0; i < vertices.size(); i++) { for (int i = 1; i < vertices.size(); i++) {
tmp = distanceBetween(v, vertices.get(i)); Vertex n = vertices.get(i);
if(n.getDist() < tmp.getDist()) {
if(tmp < dist){ tmp = n;
dist = tmp;
out = i;
} }
} }
return out; return tmp;
} }
/** private Vertex findVertex(int x, int y, final List<Vertex> vertices) {
* Calculates the distance between two vertices. return findVertex(new Point(x,y), vertices);
*/
private int distanceBetween(Vertex v1, Vertex v2){ }
int dx = 0;
int dy = 0; private Vertex findVertex(final Point pos, final List<Vertex> vertices) {
for (int i = 0; i < vertices.size(); i++) {
dx = v2.getPrev().x - v2.getPrev().x; Vertex n = vertices.get(i);
dy = v2.getPrev().x - v2.getPrev().x; if(n.getPos().equals(pos)) {
return n;
return (int) Math.sqrt(dx*dx + dy*dy); }
}
return null;
} }
private List<Point> reconstructShortestPath(final List<Vertex> vertices, final Point dst) {
ArrayList<Point> path = new ArrayList<Point>();
path.add(dst);
Vertex u = findVertex(dst, vertices);
if(u==null) {
return null;
}
while(u.getPrev()!=null) {
u= u.getPrev();
path.add(0, u.getPos());
}
return path;
}
/** /**
* Calculates the next game step. This method will either call populateField, or it will cleanup blocks * Calculates the next game step. This method will either call populateField, or it will cleanup blocks
*/ */

View File

@@ -19,7 +19,7 @@ public class Vertex {
public Vertex(int dist, Point pos) { public Vertex(int dist, Point pos) {
this.dist = dist; this.dist = dist;
this.pos = pos; this.pos = pos;
this.prev = new Vertex(0, new Point(0,0)); this.prev = null;
} }
/** /**