Report this | Email | Print
package projects.jumpingBug;
import info.gridworld.grid.*;
import info.gridworld.actor.*;
public class JumpingBug extends Bug
{
public JumpingBug(){}

public void move()
{
Grid<Actor> gr = getGrid();
if(gr==null){return;}
Location loc = getLocation();
Location next1 = loc.getAdjacentLocation(getDirection()).getAdjacentLocation(getDirection());
if(gr.isValid(next1))
{
Actor neighbor1 = gr.get(next1);
if(neighbor1 instanceof Rock)
{
Location next2 = next1.getAdjacentLocation(getDirection()).getAdjacentLocation(getDirection());
Actor neighbor2 = gr.get(next2);
if(gr.isValid(next2)&&!(neighbor2 instanceof Rock)){moveTo(next2);}
else{removeSelfFromGrid();}
}
else
{
moveTo(next1);
}
}
else{removeSelfFromGrid();}
}

public boolean canMove()
{
Grid<Actor> gr = getGrid();
if(gr==null){return false;}
Location loc = getLocation();
Location next1 = loc.getAdjacentLocation(getDirection());
Location next2 = next1.getAdjacentLocation(getDirection());
Location next3 = next2.getAdjacentLocation(getDirection());
Location next4 = next3.getAdjacentLocation(getDirection());
if(!gr.isValid(next2)){return false;}
Actor neighbor1 = gr.get(next1), neighbor2=null, neighbor3=null, neighbor4=null;
if(gr.isValid(next2)){neighbor2 = gr.get(next2);}
if(gr.isValid(next3)){neighbor3 = gr.get(next3);}
if(gr.isValid(next4)){neighbor4 = gr.get(next4);}
if(neighbor2 instanceof Rock)
{
if(!gr.isValid(next4)){return false;}
return (neighbor1==null||neighbor1 instanceof Flower||neighbor1 instanceof Rock) // can move over next space
&&(neighbor3==null||neighbor3 instanceof Flower||neighbor3 instanceof Rock) // can move over space after landing rock
&&(neighbor4==null||neighbor4 instanceof Flower); // can land in 4th space
}
else
{
return (neighbor1==null||neighbor1 instanceof Flower||neighbor1 instanceof Rock)
&&(neighbor2==null||neighbor2 instanceof Flower||neighbor2 instanceof Rock);
}
}
}
Views - Today : 91 Total : 91