![]() |
CS111 TuggleWorld Contract |
An instance of the TuggleWorld class represents
a text-based world that may be inhabited by tuggles and their bagels.
Conceptually, each TuggleWorld has the following abstract state variables:
numTuggles tuggles
that live in the grid. These can be referenced by an index in the
range [1 .. numTuggles].
cols] and y is in the
range [1..rows].
Unlike BuggleWorld, TuggleWorld is
not an applet. Instances of TuggleWorld are
constructed directly by the programmer.
Constructor Method
public TuggleWorld (int r, int c, int numTuggles)
Constructs an instance of TuggleWorld with r rows,
c columns, and numTuggles tuggles.
Complains if r ≤ 0, c ≤ 0,
or numTuggles < 0.
Public Instance Methods
public int numTuggles ()
Return the number of tuggles in this world.
public Tuggle getTuggle (int i)
Return the ith tuggle in this world, where
i is an index between 1 and numTuggles(),
inclusive.
public String toString ()
Return a string representation of this world's grid.
The contents of cells are separated horizontally
by the characters
+-+and vertically by the characters
+ | +For example, a empty grid with 3 rows and 4 columns is displayed as
+-+-+-+-+ | | | | | +-+-+-+-+ | | | | | +-+-+-+-+ | | | | | +-+-+-+-+In general, a cell is represented by a 3x3 array of characters with the form
+-+ |X| +-+where
X is a single character displaying
some of the characteristics of the cell. Here are the rules
that determine the character X:
X does not show
the cell color or bagel (if any) but only
shows the heading of the
highest-indexed tuggle in the cell as follows:
Direction Character EAST '>' NORTH '^' WEST '<' SOUTH 'V'
X shows the color
and bagel (if any) as follows:
Color Character If No Bagel Character If Bagel black 'k' 'K' blue 'b' 'B' cyan 'c' 'C' green 'g' 'G' magenta 'm' 'M' red 'r' 'R' white ' ' (a space char) 'O' yellow 'y' 'Y' any other color '?' '$'
Protected Instance Methods
(These instance methods can be used only within the
TuggleWorld and Tuggle classes.)
protected int getRows ()
Return the number of rows in this world's grid.
protected int getCols ()
Return the number of columns in this world's grid.
protected boolean contains (Point p)
Return true if p is
a legal grid position in this world and
false otherwise.
protected Color getColorAt (int x, int y)
Return the color at grid location (x,y).
Complains if this location is not in the grid.
protected void setColorAt (int x, int y, Color c)
Change the color at grid location (x,y) to be c.
Complains if this location is not in the grid.
protected boolean getBagelAt (int x, int y)
Return true if there is a bagel at
grid location (x,y) and false otherwise.
Complains if this location is not in the grid.
protected void setBagelAt (int x, int y, boolean b)
Change the bagel state of grid location (x,y) to be b.
Complains if this location is not in the grid.
protected Tuggle getTuggleAt (int x, int y)
If there is at least one tuggle at (x,y), return the tuggle with the
highest index; otherwise returns null.