This is a chunk of code I'm using for Google's antai challenge whenever I run it it seems to go into some endless loop then I get the stack trace at the bottom. I'm burnt out looking at this code I can't for the life of me figure this out.
public class Node
{
private final int xCoord, yCoord;
private int F, G, H;
private Tile location;
Node previousNode;
private Tile [] neighbors;
/*
G
the exact cost to reach this node from the starting node.
H
the estimated(heuristic) cost to reach the destination from here.
F = G + H
As the algorithm runs the F value of a node tells us how expensive we think it will be to reach our goal by way of that node.
*/
public Node(Tile loc)
{
location = loc;
xCoord = location.getCol();
yCoord = location.getRow();
F=G=H=0;
setNeighbors();
}
private void setNeighbors()
{
if(neighbors == null)
{
neighbors = new Tile[4];
}
neighbors[0] = new Tile(xCoord+1,yCoord);
neighbors[1] = new Tile(xCoord-1,yCoord);
neighbors[2] = new Tile(xCoord,yCoord+1);
neighbors[3] = new Tile(xCoord,yCoord-1);//error occurs here!!!!!!
}
}
/**
* Represents a tile of the game map.
*/
public class Tile implements Comparable<Tile> {
private final int row;
private final int col;
/**
* Creates new {@link Tile} object.
*
* @param row row index
* @param col column index
*/
public Tile(int row, int col) {
this.row = row;
this.col = col;
}
/**
* Returns row index.
*
* @return row index
*/
public int getRow() {
return row;
}
/**
* Returns column index.
*
* @return column index
*/
public int getCol() {
return col;
}
/**
* {@inheritDoc}
*/
@Override
public int compareTo(Tile o) {
return hashCode() - o.hashCode();
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return row * Ants.MAX_MAP_SIZE + col;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
boolean result = false;
if (o instanceof Tile) {
Tile tile = (Tile)o;
result = row == tile.row && col == tile.col;
}
return result;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return row + " " + col;
}
}
the actual error I'm receiving is:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at Node.setNeighbors(Node.java:37)
at Node.<init>(Node.java:25)
at AstarSearch.assessRoute(AstarSearch.java:73)
at MyBot.gatherFood(MyBot.java:153)
at MyBot.doTurn(MyBot.java:124)
at AbstractSystemInputParser.processLine(AbstractSystemInputParser.java:54)
at AbstractSystemInputReader.readSystemInput(AbstractSystemInputReader.java:18)
at MyBot.main(MyBot.java:25)
any help is appreciated