Fixed pathfinding v1
This commit is contained in:
@@ -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);
|
vertices.add(new Vertex(0, src));
|
||||||
int tmp, tmp_i = 0;
|
|
||||||
int alt = 0;
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
final Point[] offsets = {
|
||||||
|
new Point(0,1),
|
||||||
|
new Point(0,-1),
|
||||||
|
new Point(1,0),
|
||||||
|
new Point(-1,0)
|
||||||
|
};
|
||||||
|
|
||||||
for(int i = 0; i < 4; i++){ // for each neighbour of u ...
|
for(int i = 0; i < 4; i++){ // for each neighbour of u ...
|
||||||
if(i == 0){
|
final Point p = u.getPos();
|
||||||
tmp_i = u.getPrev().x * u.getPrev().y - 7; // neighbour above
|
final Point offs = offsets[i];
|
||||||
}
|
int x = p.x + offs.x;
|
||||||
|
int y = p.y + offs.y;
|
||||||
|
if(x<0 || y<0 || x>=size || y>= size) continue;
|
||||||
|
|
||||||
if(i == 1){
|
final Vertex v = findVertex(x,y, vertices);
|
||||||
tmp_i = u.getPrev().x * u.getPrev().y + 7; // neighbour below
|
//distanz_update(u,v)
|
||||||
}
|
if(v!=null){
|
||||||
|
int alternative = u.getDist()+1;
|
||||||
if(i == 2){
|
if( alternative< v.getDist()) {
|
||||||
tmp_i = u.getPrev().x * u.getPrev().y - 1; // left neighbour
|
v.setDist(alternative);
|
||||||
}
|
v.setPrev(u);
|
||||||
|
|
||||||
if(i == 3){
|
|
||||||
tmp_i = u.getPrev().x * u.getPrev().y + 1; // right neighbour
|
|
||||||
}
|
|
||||||
|
|
||||||
if(tmp_i >= 0 && tmp_i <= 49){
|
|
||||||
alt = u.getDist() + distanceBetween(u,v);
|
|
||||||
if(alt < v.getDist()){ // A shorter path has been found
|
|
||||||
v.setDist(alt);
|
|
||||||
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 = 1; i < vertices.size(); i++) {
|
||||||
|
Vertex n = vertices.get(i);
|
||||||
|
if(n.getDist() < tmp.getDist()) {
|
||||||
|
tmp = n;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vertex findVertex(int x, int y, final List<Vertex> vertices) {
|
||||||
|
return findVertex(new Point(x,y), vertices);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vertex findVertex(final Point pos, final List<Vertex> vertices) {
|
||||||
for (int i = 0; i < vertices.size(); i++) {
|
for (int i = 0; i < vertices.size(); i++) {
|
||||||
tmp = distanceBetween(v, vertices.get(i));
|
Vertex n = vertices.get(i);
|
||||||
|
if(n.getPos().equals(pos)) {
|
||||||
if(tmp < dist){
|
return n;
|
||||||
dist = tmp;
|
|
||||||
out = i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private List<Point> reconstructShortestPath(final List<Vertex> vertices, final Point dst) {
|
||||||
* Calculates the distance between two vertices.
|
ArrayList<Point> path = new ArrayList<Point>();
|
||||||
*/
|
path.add(dst);
|
||||||
private int distanceBetween(Vertex v1, Vertex v2){
|
Vertex u = findVertex(dst, vertices);
|
||||||
int dx = 0;
|
if(u==null) {
|
||||||
int dy = 0;
|
return null;
|
||||||
|
}
|
||||||
dx = v2.getPrev().x - v2.getPrev().x;
|
while(u.getPrev()!=null) {
|
||||||
dy = v2.getPrev().x - v2.getPrev().x;
|
u= u.getPrev();
|
||||||
|
path.add(0, u.getPos());
|
||||||
return (int) Math.sqrt(dx*dx + dy*dy);
|
}
|
||||||
|
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
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user