![]() |
CS111 Tuggle Contract |
Tuggle
class represent
whimsical creatures that inhabit an instance of
the TuggleWorld
class.
Tuggles are like buggles except that they live in a
text-based world rather than a graphical one.
Conceptually, every tuggle has five abstract state variables:
Constructor Methods
Tuggle (TuggleWorld tw)
Returns a new tuggle at position (1,1) whose heading is
Direction.EAST
, whose color is Color.red
,
whose brush is down, and whose world is tw
.
Instance Methods
public void forward ()
Moves this tuggle forward one step (in the direction of its current heading). Complains if the tuggle is facing a perimeter wall.
public void forward (int n)
If n
>= 0, moves this tuggle forward n
steps;
otherwise moves this tuggle backward n
steps.
If the tuggle encounters a perimeter wall along the way,
it will stop and complain.
public void backward ()
Moves this tuggle backward one step (in the direction opposite to its
current heading). Complains if the tuggle encounters a perimeter
wall.
public void backward (int n)
If n
>= 0, moves this tuggle backward n
steps;
otherwise moves this tuggle forward n
steps.
If the tuggle encounters a perimeter wall along the way,
it will stop and complain.
public void left ()
Turns this tuggle left by 90 degrees.
public void right ()
Turns this tuggle right by 90 degrees.
public void brushDown ()
Lowers this tuggle's brush. When the brush is lowered, the tuggle leaves a trail when it moves.
public void brushUp ()
Raises this tuggle's brush. When the brush is raised, the tuggle leaves no trail when it moves.
public void pickUpBagel ()
Picks up the bagel that is in the tuggle's cell.
Complains if there is no bagel in the tuggle's cell.
public void dropBagel ()
Drops one bagel in the tuggle's cell.
Complains if a bagel is already present in that cell.
public Color getCellColor ()
Returns the color of the cell under this tuggle.
public void paintCell (Color c)
Paints the cell under this tuggle with color c
.
public Color getColor ()
Returns the color of this tuggle.
public void setColor (Color c)
Changes the color of this tuggle to be the color c
.
public Point getPosition ()
Returns a point that indicates the current position of this tuggle in
the grid. Modifying the returned point will not affect the
tuggle's position.
public void setPosition (int x, int y)
Changes the position of this tuggle to be the grid location (x,y).
Complains if (x,y) is not in the grid.
public void setPosition (Point p)
Changes the position of this tuggle to be the grid location whose x-
and y-coordinates are given by the point p
.
Complains if p
is not in the grid. Subsequent
modifications of p
will not affect the tuggle's
position.
public Direction getHeading ()
Returns the heading of this tuggle.
public void setHeading (Direction d)
Changes the heading of this tuggle to be the direction d
.
public boolean getBrush ()
Returns the brush state of this tuggle (true
if the brush
is down, false
otherwise).
public void setBrush (boolean b)
Changes the brush state of this tuggle to be the boolean
b
. A value of true
means that the brush
will be down after the call.
public String toString ()
Returns a string representation of this tuggle that
has the form
[Tuggle@(x,y) facing dir with color (r,g,b) and brush bs]
where
EAST
, NORTH
, WEST
,
and SOUTH
);
up
or down
.