Java path finding simplified - java

Background: I am making a 2D moba game and i need pathfind for all the monsters in the game. I want to give a startPos and a endPos and have the monster travel there avoiding objects.
Question:
I have been trying to implement path finding into my game for awhile now and i just cant see to get it working. All i want is some method/class where i can give it a 2d array of values (ie. true = occupied & false = free), startPos, endPos and it gives me a list of moves to get to the end. All my implementations have failed thus far. Can anyone help by giving me code that is easy to implement?
Note:
So far i have tried implementing A and it either ignored walls or sent the character into a completely random direction.
*I did get it working but in a ugly and wrong way. I had the charater move forward until it hit and wall. Then it turned right and kept moving until it could turn left and continue towards the destination. This works but i dont think people want their teams monsters running around on walls
Edit:
Code below is now working! I found that for some reason the points were backwards so i had to invert the Point list. No all i need to do is interpolate between points to give smooth movement. However, I do ask is there any way i cant add more bias towards walls. For example making it so the point never goes within 1 unit of a wall?
package NavMesh;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import toolbox.Maths;
public class MovementPath {
private Node[][] mapOriginal;
private Node[][] mapPath;
public boolean solving = true;
public int startX, startY, finishX, finishY, cells;
private int checks = 0;
private int length = 0;
int realStartX, realStartY, realFinishX, realFinishY;
NavMesh mesh;
private Algorithm alg;
List<Point> path = new ArrayList<Point>();
public MovementPath(NavMesh mesh,int startX, int startY, int finishX, int finishY) {
this.mapOriginal = mesh.getMapCopy();
this.mesh = mesh;
this.startX = startX;
this.startY = startY;
this.finishX = finishX;
this.finishY = finishY;
this.cells = mapOriginal.length;
realStartX = startX;
realStartY = startY;
realFinishX = finishX;
realFinishY = finishY;
this.startX = (int) (Math.floor((float) startX / (float) mesh.cellWidth));
this.startY = (int) (Math.floor((float) startY / (float) mesh.cellHeight));
this.finishX = (int) (Math.floor((float) finishX / (float) mesh.cellWidth));
this.finishY = (int) (Math.floor((float) finishY / (float) mesh.cellHeight));
mapPath = new Node[mapOriginal.length][mapOriginal.length];
System.arraycopy(mapOriginal, 0, mapPath, 0, mapOriginal.length);
mapPath[this.startX][this.startY] = new Node(0,this.startX,this.startY);;
mapPath[this.finishX][this.finishY] = new Node(1,this.finishX,this.finishY);
addPointCentered(realFinishX,realFinishY);
alg = new Algorithm();
//alg.AStar();
alg.Dijkstra();
addPointCentered(realStartX,realStartY);
mesh.drawMap(Integer.toString(Maths.randomRange(0, 1000)), mapPath);
}
public Path getPath(){
//System.out.println("Returning path with " + getPathPoints().size() + " points");
return new Path(getPathPoints());
}
private void addPointCentered(int x, int y) {
path.add(new Point(x+(mesh.cellWidth/2),y+(mesh.cellHeight/2)));
}
public List<Point> getPathPoints(){
List<Point> rPath = new ArrayList<Point>();
for(int i = path.size()-1; i >= 0; i--) {
rPath.add(path.get(i));
}
return rPath;
}
class Algorithm { //ALGORITHM CLASS
//A STAR WORKS ESSENTIALLY THE SAME AS DIJKSTRA CREATING A PRIORITY QUE AND PROPAGATING OUTWARDS UNTIL IT FINDS THE END
//HOWEVER ASTAR BUILDS IN A HEURISTIC OF DISTANCE FROM ANY NODE TO THE FINISH
//THIS MEANS THAT NODES THAT ARE CLOSER TO THE FINISH WILL BE EXPLORED FIRST
//THIS HEURISTIC IS BUILT IN BY SORTING THE QUE ACCORDING TO HOPS PLUS DISTANCE UNTIL THE FINISH
public void AStar() {
ArrayList<Node> priority = new ArrayList<Node>();
priority.add(mapPath[startX][startY]);
while(solving) {
if(priority.size() <= 0) {
solving = false;
break;
}
int hops = priority.get(0).getHops()+1;
ArrayList<Node> explored = exploreNeighbors(priority.get(0),hops);
if(explored.size() > 0) {
priority.remove(0);
priority.addAll(explored);
} else {
priority.remove(0);
}
sortQue(priority); //SORT THE PRIORITY QUE
}
}
public void Dijkstra() {
ArrayList<Node> priority = new ArrayList<Node>(); //CREATE A PRIORITY QUE
priority.add(mapPath[startX][startY]); //ADD THE START TO THE QUE
while(solving) {
if(priority.size() <= 0) { //IF THE QUE IS 0 THEN NO PATH CAN BE FOUND
solving = false;
break;
}
int hops = priority.get(0).getHops()+1; //INCREMENT THE HOPS VARIABLE
ArrayList<Node> explored = exploreNeighbors(priority.get(0), hops); //CREATE AN ARRAYLIST OF NODES THAT WERE EXPLORED
if(explored.size() > 0) {
priority.remove(0); //REMOVE THE NODE FROM THE QUE
priority.addAll(explored); //ADD ALL THE NEW NODES TO THE QUE
} else { //IF NO NODES WERE EXPLORED THEN JUST REMOVE THE NODE FROM THE QUE
priority.remove(0);
}
}
}
public ArrayList<Node> sortQue(ArrayList<Node> sort) { //SORT PRIORITY QUE
int c = 0;
while(c < sort.size()) {
int sm = c;
for(int i = c+1; i < sort.size(); i++) {
if(sort.get(i).getEuclidDist(finishX,finishY)+sort.get(i).getHops() < sort.get(sm).getEuclidDist(finishX,finishY)+sort.get(sm).getHops())
sm = i;
}
if(c != sm) {
Node temp = sort.get(c);
sort.set(c, sort.get(sm));
sort.set(sm, temp);
}
c++;
}
return sort;
}
/*
public ArrayList<Node> exploreNeighbors(Node current, int hops) { //EXPLORE NEIGHBORS
ArrayList<Node> explored = new ArrayList<Node>(); //LIST OF NODES THAT HAVE BEEN EXPLORED
for(int a = -1; a <= 1; a++) {
for(int b = -1; b <= 1; b++) {
int xbound = current.getX()+a;
int ybound = current.getY()+b;
if((xbound > -1 && xbound < cells) && (ybound > -1 && ybound < cells)) { //MAKES SURE THE NODE IS NOT OUTSIDE THE GRID
Node neighbor = mapPath[xbound][ybound];
if((neighbor.getHops()==-1 || neighbor.getHops() > hops) && neighbor.getType()!=2) { //CHECKS IF THE NODE IS NOT A WALL AND THAT IT HAS NOT BEEN EXPLORED
explore(neighbor, current.getX(), current.getY(), hops); //EXPLORE THE NODE
explored.add(neighbor); //ADD THE NODE TO THE LIST
}
}
}
}
return explored;
}
*/
public ArrayList<Node> exploreNeighbors(Node current, int hops) { //EXPLORE NEIGHBORS
ArrayList<Node> explored = new ArrayList<Node>(); //LIST OF NODES THAT HAVE BEEN EXPLORED
//test(hops, current, explored,current.getX(),current.getY());
//test(hops, current, explored,current.getX()+1,current.getY());
//test(hops, current, explored,current.getX()-1,current.getY());
//test(hops, current, explored,current.getX(),current.getY()+1);
//test(hops, current, explored,current.getX(),current.getY()-1);
for(int a = -1; a <= 1; a++) {
for(int b = -1; b <= 1; b++) {
test(hops, current, explored,current.getX()+a,current.getY()+b);
}
}
return explored;
}
private void test(int hops, Node current, ArrayList<Node> explored, int xbound, int ybound) {
if((xbound > -1 && xbound < cells) && (ybound > -1 && ybound < cells)) { //MAKES SURE THE NODE IS NOT OUTSIDE THE GRID
Node neighbor = mapPath[xbound][ybound];
if((neighbor.getHops()==-1 || neighbor.getHops() > hops) && neighbor.getType()!=2) { //CHECKS IF THE NODE IS NOT A WALL AND THAT IT HAS NOT BEEN EXPLORED
explore(neighbor, current.getX(), current.getY(), hops); //EXPLORE THE NODE
explored.add(neighbor); //ADD THE NODE TO THE LIST
}
}
}
public void explore(Node current, int lastx, int lasty, int hops) { //EXPLORE A NODE
if(current.getType()!=0 && current.getType() != 1) //CHECK THAT THE NODE IS NOT THE START OR FINISH
current.setType(4); //SET IT TO EXPLORED
current.setLastNode(lastx, lasty); //KEEP TRACK OF THE NODE THAT THIS NODE IS EXPLORED FROM
current.setHops(hops); //SET THE HOPS FROM THE START
checks++;
if(current.getType() == 1) { //IF THE NODE IS THE FINISH THEN BACKTRACK TO GET THE PATH
backtrack(current.getLastX(), current.getLastY(),hops);
}
}
public void backtrack(int lx, int ly, int hops) { //BACKTRACK
length = hops;
while(hops > 1) { //BACKTRACK FROM THE END OF THE PATH TO THE START
Node current = mapPath[lx][ly];
current.setType(5);
addPointCentered(lx*mesh.cellWidth,ly*mesh.cellHeight);
//System.out.println("New Point: " + path.get(path.size()-1).toString());
lx = current.getLastX();
ly = current.getLastY();
hops--;
}
solving = false;
}
}
}

Try A*, I used that for a path finding problem. It is easy to implement for grid based movement and very fast. I implemented it using the pseudocode on the wikipedia page.

Related

User drawn line disappear but the functionality behind it does not

I am creating a puzzle game hashi, basically the purpose of the game is to solve a puzzle using connection (Bridges) and Nodes(Islands).
To solve the puzzle, the user has to connect the same amount of bridges to a corresponding nodes weight.
The user is allowed to join the islands using 2 bridges from one island, but after s/he tries to connect the 3 bridges from the same node the connection should disappear and the user should be allowed to create a new one from that node.
Problem:
The user can draw 2 bridges from the same node but once he tries to do a third one the connection disappear JUST VISUALLY meaning the nodes are still connected and then the user cannot draw a bridge again.
And in here the bridges are just deleted visually.
I tried to debug it, but unfortunately I cannot seem to figure out where the problem is.
Here is the code to my problem.
public class BoardCreation {
// This class member is used for random initialization purposes.
static private final Random random = new Random();
// The difficulty levels.
private static final int EASY = 0;
static public final int MEDIUM = 1;
static public final int HARD = 2;
static public final int EMPTY = 0;
private static int ConnectionFingerprint(BoardElement start, BoardElement end) {
int x = start.row * 100 + start.col;
int y = end.row * 100 + end.col;
// Swap to get always the same fingerprint independent whether we are called
// start-end or end-start
if (x > y ) {
int temp = x;
x = y;
y = temp;
}
Log.d("", String.format("%d %d" , x ,y));
return x ^ y;
}
public class State {
// The elements of the board are stored in this array.
// A value defined by "EMPTY" means that its not set yet.
public BoardElement [][] board_elements = null;
public int [][] cell_occupied = null;
// The width of the board. We only assume squared boards.
public int board_width=0;
public State(int width) {
board_width = width;
board_elements = new BoardElement[width][width];
cell_occupied = new int[width][width];
}
public State CloneWithoutConnections() {
State newstate = new State(board_width);
if (board_elements != null) {
newstate.board_elements = new BoardElement[board_elements.length][board_elements.length];
for (int i = 0; i < board_elements.length; ++i) {
for (int j = 0; j < board_elements.length; ++j) {
if (board_elements[i][j] == null)
continue;
newstate.board_elements[i][j] = board_elements[i][j].clone();
}
}
}
if (cell_occupied != null) {
assert board_elements != null;
newstate.cell_occupied = new int[board_elements.length][board_elements.length];
for (int i = 0; i < board_elements.length; ++i) {
System.arraycopy(cell_occupied[i], 0, newstate.cell_occupied[i], 0, board_elements.length);
}
}
return newstate;
}
public void AddToBridgeCache(BoardElement first, BoardElement second) {
if (first == null || second == null) { return; }
final int fingerprint = ConnectionFingerprint(first, second);
Log.d(getClass().getName(),
String.format("Fingerprint of this bridge %d", fingerprint));
// mark the end points as occupied.
cell_occupied[first.row][first.col] = fingerprint;
cell_occupied[second.row][second.col] = fingerprint;
int dcol = second.col - first.col;
int drow = second.row - first.row;
if (first.row == second.row) {
for (int i = (int) (first.col + Math.signum(dcol)); i != second.col; i += Math.signum(dcol)) {
cell_occupied[first.row][i] = fingerprint;
String.format("deleting bridge");
}
} else {
assert first.col == second.col;
for (int i = (int) (first.row + Math.signum(drow)); i != second.row; i+= Math.signum(drow)) {
cell_occupied[i][first.col] = fingerprint;
String.format("deleting bridge", fingerprint);
}
}
}
} // end of state
}

How to make a shuffle method in a linked list

I am trying to make a shuffle method in my LinkedList. Currently, my method of shuffling is to generate a random number, n, between 1 to 10, and take the n(th) number of card and move it to the front. Then it will loop in a random amount of time. However, my current code does not seem to work as the card it takes just get removed instead of bringing it to the front.
public void shuffle() {
Node current = head;
int randomX = (int) (Math.random() * 10 + 1);
for (int x = 0; x < randomX; x++) {
int randomY = (int) (Math.random() * 10 + 1);
for (int y = 0; y < randomY; y++) {
if (current.getNext() != null) {
current = current.getNext();
System.out.println("Yup");
System.out.println(current);
System.out.println(y);
}
else {
current = head;
System.out.println("nope");
current = current.getNext();
}
if (current.getPrevious() != null){
current.getPrevious().setNext(current.getNext());
head.setPrevious(current);
current.setPrevious(head);
}
head = current;
}
}
}
Make sure that when you find the node you are looking for that you set its previous node's next to its next AND you set the node's next previous to its previous
Node temp = head;
int randomX = (int) (Math.random() * 10 + 1);
//simply go until the randomX
while(randomX-- > 0 && temp.getNext() != null)
temp = temp.getNext();
//remove the Nth node from the list
temp.getPrevious().setNext(temp.getNext());
if(temp.getNext() != null)
temp.getNext().setPrevious(temp.getPrevious());
//set it to point to the head
temp.setNext(head);
temp.setPrevious(null);
//now set the Head to the Nth node we found
head = temp;
It look like your move the randomly chosen node to head is misplaced. It should be outside the loop that chooses the one to move.
A few comments would have made this obvious.
public void shuffle() {
Node current = head;
// How many times to shuffle.
int randomX = (int) (Math.random() * 10 + 1);
// Move random node to head random number of times.
for (int x = 0; x < randomX; x++) {
// Pick the one to move.
int randomY = (int) (Math.random() * 10 + 1);
// Go find it.
for (int y = 0; y < randomY; y++) {
if (current.getNext() != null) {
current = current.getNext();
System.out.println("Yup");
System.out.println(current);
System.out.println(y);
} else {
// Hit end of list - go back to start.
current = head;
System.out.println("nope");
current = current.getNext();
}
}
// Bring the chosen one to `head` - **** I moved this OUTSIDE the loop above.
if (current.getPrevious() != null) {
current.getPrevious().setNext(current.getNext());
head.setPrevious(current);
current.setPrevious(head);
}
head = current;
}
}

A* path finding algorithm. Movement Cost and Heuristic is Inaccurate

My problem is that the movement cost(G cost) of my node and heuristic is inaccurate it does not match with the picture.
Here is the image of what I'm following.There are three labels here and the movement cost is labelled at the bottom left and the heuristic is at bottom right. Label at top-left is the F = H + G
Here is my output. As you can see the movement cost is not the same as the desired output. The red circle is the goal node.
Also the same with my Heuristic cost.
public class AStarPathFinder implements PathFinder {
private List<Node> open = new ArrayList<Node>();
private List<Node> close = new ArrayList<Node>();
private Node[][] nodes;
private TileMap map;
private Heuristic heuristic;
public AStarPathFinder(TiledMapStage mapStage, Heuristic heuristic) {
this.heuristic = heuristic;
nodes = mapStage.getNodes();
map = mapStage.getMap();
}
#Override
public Path findPath(int startX, int startY, int goalX, int goalY) {
clearNodes();
Node goal = nodes[goalX][goalY];
Node current = nodes[startX][startY];
open.add(current);
while (!open.isEmpty()) {
current = getLowestFcost(open);
open.remove(current);
close.add(current);
if (current == goal) {
Path path = new Path();
while (current != null) {
path.add(current);
current = current.parent;
}
return path;
}
// neighbors of current
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
int dx = current.x + x;
int dy = current.y + y;
if (map.isValidLocation(dx, dy)) {
if (!map.isWalkable(nodes[dx][dy], x, y) || close.contains(nodes[dx][dy]))
continue;
float newScore = movementCost(current.g, isDiagonal(x, y));
if (!open.contains(nodes[dx][dy])) {
open.add(nodes[dx][dy]);
} else if (newScore >= nodes[dx][dy].g) continue;
nodes[dx][dy].g = newScore;
nodes[dx][dy].h = heuristic.estimate(nodes[dx][dy], goal);
nodes[dx][dy].f = nodes[dx][dy].g + nodes[dx][dy].h;
nodes[dx][dy].parent = current;
nodes[dx][dy].label.setText((int) nodes[dx][dy].g + "");
}
}
}
}
return null;
}
private Node getLowestFcost(List<Node> open) {
Node lowestNode = open.get(0);
for (int i = 0; i < open.size(); i++) {
if (open.get(i).f <= lowestNode.f && open.get(i).h < lowestNode.h) {
lowestNode = open.get(i);
}
}
return lowestNode;
}
private boolean isDiagonal(int x, int y) {
return (x == -1 && y == 1 ||
x == 1 && y == 1 ||
x == 1 && y == -1 ||
x == -1 && y == -1);
}
private float movementCost(float cost, boolean diagonal) {
return diagonal ? cost + 14 : cost + 10;
}
#Override
public void clearNodes() {
for (int i = 0; i < map.getTileWidth(); i++) {
for (int j = 0; j < map.getTileHeight(); j++) {
if (nodes[i][j].cell != null) {
nodes[i][j].label.setText("");
nodes[i][j].f = 0;
nodes[i][j].h = 0;
nodes[i][j].g = 0;
nodes[i][j].arrow.setDrawable("cursor");
nodes[i][j].arrow.setVisible(false);
nodes[i][j].parent = null;
}
}
}
close.clear();
open.clear();
}
}
Here is the pseudocode that I'm following. Also my heuristic is a diagonal distance
It looks like your problem is in the isWalkable method of your TileMap map variable.
The image you're following doesn't allow to pass diagonally alongside a wall, where your algorithm does.
You can see this because the score gets added with 14 as follows: 14 + 14 = 28. While you expected it to go as follows: 14 + 10 (going down first) + 10 (going right) = 34.
I hope I explained your problem clearly. I don't know your implementation of isWalkable, so I can't provide a full solution but I hope I have pointed you in the right direction.

Constructing a 3D KD Tree

So I am trying to construct a 3D KD Tree from a list of randomly generated points. I am trying to accomplish this task recursively as well. But in my recursion I am facing an error when I'm trying to partition my list of points. My code is as follows:
public class Scratch {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Point> points = new ArrayList<Point>();
Random rand = new Random(System.currentTimeMillis());
for (int i = 0; i < 100; i++) {
double x = rand.nextInt(100);
double y = rand.nextInt(100);
double z = rand.nextInt(100);
Point point = new Point(x, y, z);
points.add(point);
}
Node root = kdtree(points, 0);
System.out.println("Done");
}
static class Node {
Node leftChild;
Node rightChild;
Point location;
Node() {
}
Node(Node leftChild, Node rightChild, Point location) {
this.leftChild = leftChild;
this.rightChild = rightChild;
this.location = location;
}
}
public static Node kdtree(ArrayList<Point> points, int depth) {
int axis = depth % 3;
switch (axis) {
case 0:
Collections.sort(points, Point.X_COMPARATOR);
break;
case 1:
Collections.sort(points, Point.Y_COMPARATOR);
break;
case 2:
Collections.sort(points, Point.Z_COMPARATOR);
break;
default:
break;
}
int middle = points.size() / 2;
Point median = points.get(middle);
ArrayList<Point> greater = new ArrayList<Point>(points.subList(0, (middle - 1)));
ArrayList<Point> lesser = new ArrayList<Point>(points.subList((middle + 1), (points.size())));
Node node = new Node();
node.location = median;
if(greater.size() == 0 || lesser.size() == 0) {
node.leftChild = null;
node.rightChild = null;
} else {
node.leftChild = kdtree(lesser, depth + 1);
node.rightChild = kdtree(greater, depth + 1);
}
return node;
}
}
The class point is a simple object which contains an x, y and z coordinate. And three comparators used based on the depth of the tree.
The error I am getting is as follows:
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(0) > toIndex(-1)
at java.util.ArrayList.subListRangeCheck(ArrayList.java:1006)
at java.util.ArrayList.subList(ArrayList.java:996)
at scratch.Scratch.kdtree(Scratch.java:71)
at scratch.Scratch.kdtree(Scratch.java:80)
at scratch.Scratch.kdtree(Scratch.java:79)
at scratch.Scratch.kdtree(Scratch.java:79)
at scratch.Scratch.kdtree(Scratch.java:79)
at scratch.Scratch.kdtree(Scratch.java:79)
at scratch.Scratch.main(Scratch.java:32)
Im gonna go on a guess here and say that if you have only one point (or 0) on the points array, when you do middle=points.size/2 that equals 0 (integer division) and then when you try to make a sublist from 0 to -1 it throws that Exception.

Android Flow game filling algorithm

According to my question Java algorithm filling cells like an `Android - Flow` game
Suppose i have four points(two pairs) how i can check is the exist combination of pathes between points that filling all game board?
Like an right image, but with four points ( two pairs ).
I need to check can i fill all game board with two arcs(path).
Now i stopped after filling the structure :
private static void buildGrid(int gridResolution) {
for (int i = 1; i < 3; i++) {
for (int j = 0; j < gridResolution; j++) {
Node node = new Node();
if (startPoint1.x == i && startPoint1.y == j) {
node.point = new PointM(new Point(i, j), 1);
startNode1 = node;
} else if (startPoint2.x == i && startPoint2.y == j) {
node.point = new PointM(new Point(i, j), 1);
startNode2 = node;
} else if (endPoint1.x == i && endPoint1.y == j) {
node.point = new PointM(new Point(i, j), 2);
endNode1 = node;
} else if (endPoint2.x == i && endPoint2.y == j) {
node.point = new PointM(new Point(i, j), 2);
endNode2 = node;
} else {
node.point = new PointM(new Point(i, j), 0);
}
nodes[i][j] = node;
Node leftNode = getLeftNode(i, j);
Node topNode = getTopNode(i, j);
if (leftNode != null) {
node.left = leftNode;
leftNode.right = node;
}
if (topNode != null) {
node.top = topNode;
topNode.bottom = node.top;
}
}
}
}
private static Node getTopNode(int i, int j) {
return nodes[i - 1][j];
}
private static Node getLeftNode(int i, int j) {
if (j - 1 > 0)
return nodes[i][j - 1];
else return null;
}
private static class Node {
public PointM point;
public Node left;
public Node right;
public Node top;
public Node bottom;
public boolean isChecked;
}
And i doesn't know what i need to do after that. I stuck on this moment. As best and will circumvent this table. Perhaps it is what the algorithm?
Personally, for building-grid purpose I'd invert your approach. So, instead of checking if for each pair a proper path exists, we will create grid which satisfies this condition.
So, the algorithm would look like this:
Create 1st path using unmarked points. Then mark all points of path. Insert coins at starting and ending point of path.
Create 2nd path using unmarked points. Then mark all points of path. Insert coins at starting and ending point of path.
...
Stop when all point are marked.
Then you will have grid where for each pair, there is a proper path.
Here is an example of this algorithm:

Categories