Wednesday, March 14, 2012
Challenge 045: Whaddup Gridworld???
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(90);
}
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> gg = getGrid();
if(gg == null)
return false;
Location last = getLocation();
Location next = last.getAdjacentLocation(getDirection());
Actor neighbor = gg.get(next);
return true;
}
}
Subscribe to:
Post Comments (Atom)
Love the title!
ReplyDeleteI did come across a runtime error. it occurred as I placed the RollingStone in the center of a grid, and then ran the simulation.
The RollingStone object moved to the "East" and as it contacted the edge of the bounded grid, the runtime error occurred.
Test and revise when able.
It should now work. The problem was I had gr and gg instead of only gr for getGrid
ReplyDeleteimport 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(90);
}
public void act ()
{
if (canMove())
move();
else
{
turn();
}
}
public void turn()
{
setDirection(getDirection() + Location.HALF_CIRCLE);
}
public void move ()
{
Grid 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 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;
}
}
I get a compiler error in BlueJ with this code.
ReplyDeleteTest and revise when able.
This is finally the correct code. Phoenix created an actor for me and we tested the actual code
ReplyDeleteimport 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(90);
}
public void act ()
{
if (canMove())
move();
else
{
turn();
}
}
public void turn()
{
setDirection(getDirection() + Location.HALF_CIRCLE);
}
public void move ()
{
Grid 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 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;
}
}
I'll be swinging by tomorrow, remind me to chat with you about a minor detail on this.
ReplyDeleteI cant copy and paste correctly so this is the correct code
ReplyDeleteimport 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(90);
}
public void act ()
{
if (canMove())
move();
else
{
turn();
}
}
public void turn()
{
setDirection(getDirection() + Location.HALF_CIRCLE);
}
public void move ()
{
Grid 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 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;
}
}
Hi Ry,
ReplyDeleteThe fix, even for this code posted, is here:
Actor neighbor = (Actor)gr.get(next);
When an instance of a Grid object (gr in this case) passes a Location (next) to the get method, it returns an object of type Object (the generic type of all objects in Java).
This gives the compiler cause to hava a fit.
It doesn't allow and Object type to be save to an Actor type, so we have to use casting.
I saw this work in class, and we had a discussion involving this, so I do't have a problem awarding the XPs for this challenge.
750 XPs awarded.