To complete Challenge 45, the following code must be placed into BlueJ™:
import info.gridworld.actor.Actor;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
public class RollingStone extends Rock
{
public RollingStone()
{
super();
setDirection(270);
}
public void act()
{
if (canMove())
move();
else
turn();
}
public void turn()
{
setDirection(getDirection() + Location.HALF_CIRCLE);
}
public void move()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
Actor neighbor = gr.get(next);
if (neighbor != null)
neighbor.removeSelfFromGrid();
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
public boolean canMove()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return false;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if (!gr.isValid(next))
return false;
Actor neighbor = gr.get(next);
return true;
}
}
−Phoenix
Good work Phoenix, well done. 750 XPs awarded for this completion.
ReplyDelete