}
protected boolean isInLoop(int i, int j) {
boolean isInLoop = false;
Hexagon tempHex;
int pointingTo;
int[] nextPos = new int[2];
int team = hexagons[i][j].getTeam();
nextPos[0]=i;
nextPos[1]=j;
do{
hexagons[nextPos[0]][nextPos[1]].calculating=true;
pointingTo = hexagons[nextPos[0]][nextPos[1]].getPointingTo();
tempHex = getHexagon(pointingTo,nextPos[0], nextPos[1]);
if(tempHex==null)// accommodates pointingTo==0 condition and also edge of grid.
{
// System.out.println("yoo"+i+" "+j);
nextPos[0]=-1;
nextPos[1]=-1;
break;
}
else if(tempHex.getTeam()!= team)// when team changes
{
// System.out.println("yo"+i+" "+j+" team "+hexagons[i][j].getTeam()+" "+team);
// System.out.println("nextPos "+nextPos[0]+" "+nextPos[1]);
// nextPos = getHexagonPos(pointingTo,nextPos[0], nextPos[1]);
// System.out.println("nextPos2 "+nextPos[0]+" "+nextPos[1]+" team "+tempHex.getTeam()+" "+hexagons[nextPos[0]][nextPos[1]].getTeam());
nextPos[0]=-1;
nextPos[1]=-1;
break;
}
nextPos = getHexagonPos(pointingTo,nextPos[0], nextPos[1]);
}while(!hexagons[nextPos[0]][nextPos[1]].calculating);
if(nextPos[0]==i && nextPos[1]==j)
isInLoop = true;
nextPos[0]=i;
nextPos[1]=j;
do{
hexagons[nextPos[0]][nextPos[1]].calculating=false;
pointingTo = hexagons[nextPos[0]][nextPos[1]].getPointingTo();
tempHex = getHexagon(pointingTo, nextPos[0], nextPos[1]);
if(tempHex==null)// accommodates pointingTo==0 condition and also edge of grid.
break;
else if(tempHex.getTeam()!= hexagons[i][j].getTeam())// when team changes
break;
nextPos = getHexagonPos(pointingTo,nextPos[0], nextPos[1]);
}while(hexagons[nextPos[0]][nextPos[1]].calculating);
return isInLoop;