package com.gardeneaters.game_v0.rules;
import java.util.Vector;
import com.gardeneaters.game_v0.playingfield.Hexagon;
// head of path
class Head extends Hexagons{
private Vector<int[]> headPositions;
protected Head(Hexagon[][] _hexagons) {
super(_hexagons);
// TODO Auto-generated constructor stub
}
private void addAllHeadPosToVector()
{
headPositions = new Vector<int[]>();
int [] tempPos;
for(int i =0; i < NumOfHexY; i++)
{
for(int j =0; j<NumOfHexX; j++)
{
if(isPathHead(i,j))
{
tempPos = new int[2];
tempPos[0] = i;
tempPos[1] = j;
headPositions.add(tempPos);
}
}
}
}
private boolean isPathHead(int i, int j){
boolean isHead = false;
int pointingTo = hexagons[i][j].getPointingTo();
Hexagon tempHex = getHexagon(pointingTo,i,j);
if(tempHex==null && pointingTo!=0)// for edges of grid
return true;
//|| tempHex.getTeam()!= hexagons[i][j].getTeam()
if(pointingTo==0)// for no team hex pointingTo is always zero but not vice versa
{
if(hexagons[i][j].getTeam()!=0)// for single existing team hexagon pointing nowhere
isHead = true;
else
isHead = isPointingAtMe(i,j);// for hexagon with no team and is under attack
}
else if(pointingTo!=0)
{
if(tempHex.getTeam()!= hexagons[i][j].getTeam())// for opposite team and may be also for no-team hexagon
{
isHead = true;
}
else// runs if team is same // its for circular loop head
{
isHead = isCircularLoopHead(i,j);
}
}
return isHead;
}
private boolean isCircularLoopHead(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.
{
nextPos[0]=-1;
nextPos[1]=-1;
break;
}
else if(tempHex.getTeam()!= team)// when team changes
{
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;
int [] temp;
nextPos[0]=i;
nextPos[1]=j;
do{
hexagons[nextPos[0]][nextPos[1]].calculating=false;
if(isInLoop)// when another element of loop already exists in headPositions vector
{
for(int k=0; k<headPositions.size(); k++)
{
temp = headPositions.elementAt(k);
if(nextPos[0]==temp[0] && nextPos[1]==temp[1])
{
isInLoop=false;
//System.out.println("isINLoopHead said False");
break;
}
}
}
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()!= team)// when team changes
break;
nextPos = getHexagonPos(pointingTo,nextPos[0], nextPos[1]);
}while(hexagons[nextPos[0]][nextPos[1]].calculating);
return isInLoop;
}
protected void update() {
addAllHeadPosToVector();
}
protected Vector<int[]> getHeads() {
return headPositions;
}
}