Bug inherits from Actor.
Bug is an Actor Bug inherits methods from Actor
but it can create its own methods.
Bug differs from actor in that a bug actually moves
from cell to cell.
A bug moves to the cell immediately in front if it can.
If a move is not possible, the bug turns in 45 degree
increments until it finds a spot to which it can move.
/**
* Constructs a red bug.
*/
public Bug()
{
setColor(Color.RED);
}
/**
* Constructs a bug of a given color.
* @param bugColor the color for this bug
*/
public Bug(Color bugColor)
{
setColor(bugColor);
}
When a Bug is created with the
default constructor it is Red,
faces North.
Direction and location are
inherited from Actor.
The overloaded constructor lets
you select the color for the Bug,
but it also faces North.
• Bug class:
- constructor method
- act()
- canMove()
- move()
- turn()
The bug act method calls move if canMove returns true.
canMove returns a boolean true/false if the location in front of the bug is empty or
is a valid location.
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 (neighbor == null) || (neighbor instanceof Flower);
//ok to move to empty location or onto flower
//not ok tomove onto any other actor.
}
The bug act method calls move if canMove returns true.
move calls moveTo to move the bug to the location in front of this bug.
move leaves a flower in the
old location.
public void move()
{
Grid <Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
Flower flower = new Flower(getColor());
flower.putSelfInGrid(gr,loc);
}
The bug act method calls turn if canMove returns false.
turn changes the direction of the bug by 45 degrees to the right. If the bug cannot
move it will turn.
public void turn()
{
setDirection(getDirection() + Location.HALF_RIGHT);
}
Act method will always be overriden in the extended classes.
The bug act method will check to see if the bug can move and if so it will move
(according to the move method).
Else it will turn (according to the turn method)
BugRunner Class
ActorWorld world = new ActorWorld(); // must create world
Bug dude = new Bug(Color.GREEN); // created a bug
dude.setDirection(Location.EAST); // set the direction
Location loc = new Location(5,5); // created a location
world.add(loc , new Rock()); // added a rock to the location loc
loc = new Location(2,5);
// changed the location
world.add(loc, new Flower()); // added Flower at the new loc
loc = new Location(2,7); // changed loc
world.add(loc, dude); // added bug at the loc
world.show(); // show the world and actors
•
Create a new Class that extends Bug
•
Override the act method
•
Override other methods as needed
•
Each call to the move() method should be
guarded by a call to the canMove() method
•
Add additional instance fields if needed
•
Add new methods to the subclass if needed
•
Constructors for the subclass call super() or
super(someColor)
• Bug class:
- constructor method
- act()
- canMove()
- move()
- turn()
• Grid class:
- isValid()
- get()
- getNumRows()
- getNumCols()
• Location class:
- getRow()
- getCol()
- getAdjacentLocation()
• Actor class:
- getGrid()
- putSelfInGrid()
- removeSelfFromGrid()
- moveTo()
• BoxBug class:
- overriding act()
- using instance variables to
control movement of a bug
© Copyright 2026 Paperzz