Buggle
class represent
whimsical creatures that inhabit an instance of
the BuggleWorld
class. Conceptually, every
buggle has four abstract state variables:
- position: The location of the buggle in the BuggleWorld grid.
- heading: The direction that the buggle is facing.
- color: The color of the buggle.
- brushDown?: A boolean (true/false) indicating whether the buggle's brush is down.
Constructor Methods
Buggle ()
Returns a new buggle at location (1,1) whose heading is
Direction.EAST
, whose color is Color.red
,
and whose brush is down.
Instance Methods
public void forward ()
Moves this buggle forward one step (in the direction of its current heading). Complains if the buggle is facing a wall.
public void forward (int n)
Moves this buggle forward n
steps.
If the buggle encounters a wall along the way, it will stop and complain.
If n
is 0, has no effect.
If n
is less than 0, goes backward n
steps.
public void backward ()
Moves this buggle backward one step (in the direction opposite to its current heading). Complains if the buggle is facing a wall.
public void backward (int n)
Moves this buggle backward n steps.
If the buggle encounters a wall along the way, it will stop and complain.
If n
is 0, has no effect.
If n
is less than 0, goes forward n
steps.
public void left ()
Turns this buggle left by 90 degrees.
public void right ()
Turns this buggle right by 90 degrees.
public void brushDown ()
Lowers this buggle's brush. When the brush is lowered, the buggle leaves a trail when it moves.
public void brushUp ()
Raises this buggle's brush. When the brush is raised, the buggle leaves no trail when it moves.
public Location getPosition ()
Returns a Location
instance that specifies the current position of this buggle in
the grid.
public void setPosition (Location loc)
Changes the position of this buggle to be the grid location whose x-
and y-coordinates are given by the Location
instance loc
.
Complains if loc
is not in the grid.
public Direction getHeading ()
Returns a Direction
instance that specifies the heading of this buggle.
public void setHeading (Direction d)
Changes the heading of this buggle to be the Direction
instance d
.
public Color getColor ()
Returns a Color
instance that specifies the color of this buggle.
public void setColor (Color c)
Changes the color of this buggle to be the Color
instance c
.
public String toString ()
Returns a string representation of this buggle.
public boolean isFacingWall ()
Returns true
if there is a wall in front of the buggle, and false
otherwise.
public boolean isOverBagel ()
Returns true
if the buggle is in a cell that contains a bagel, and false
otherwise.
public void pickUpBagel ()
Picks up the bagel that is in the buggle's cell. Complains if there is no bagel.
public void dropBagel ()
Drops one bagel in the buggle's cell. Complains if a bagel is already present in that cell.
public Color getCellColor ()
Returns the color of the cell under this buggle.
public void setCellColor (Color c)
Changes the cell under this buggle to have the color c
.
public void paintCell (Color c)
Paints the cell under this buggle with Color
instance c
.
This is a synonym for setCellColor(c)
.
public void dropInt (int n)
Drops the integer n
into the cell below this buggle.
If there is already an integer in the cell, it is replaced by n
.
public void dropString (String s)
Drops the string s
into the cell below this buggle.
If there is already a string in the cell, it is replaced
by s
.