CS111 Lab 7 - Recursion II
SmileWorld
Spring Break is coming up, so we're all set to smile!
In fact, smiles have gone out of control in PictureWorld.
We started with two different smiley faces, shown below:
smileyFace1(Color.black,Color.yellow) |
smileyFace2(Color.magenta,Color.black) |
We let them simmer with the CS111 recursion lecture notes, and guess what happened?
We got a recursive mess! There are three recursive patterns to untangle. They are shown
at different levels of recursion below. It will be easiest to solve these problems by
looking at the different levels of recursion available in the SmileWorld applet in the
Test folder. Note that the base case for each pattern is always available
in the test applet.
The following Pictures were defined for invoking the patterns:
smile1 |
smile2 |
smile3 |
smile4 |
smileys1 -- recursion with two Picture parameters
smileys1(5,smile1,smile2)
smileys2 -- recursion with four Picture parameters
smileys2(6,smile1,smile2,smile3,smile4)
smileys3 -- recursion with three Color parameters
smileys3(6,Color.black,Color.green,Color.blue)
HungryWorld
Now that we've exercised our jaws by smiling, it's time to start thinking
about eating! This problem deals with a buggle that has the following behavior.
The buggle starts out in the middle of the first row of a grid facing
NORTH with its brush up. The grid cells are randomly populated
with a user-specified number of bagels. The bagels are initially placed
so that none are in the column in which the buggle initially faces. The
buggle moves row-by-row from the bottom row to the top row, and
performs the following action at every row:
- If there are more bagels to the left of the buggle than to the
right of the buggle on the current row, eat all of the bagels to
the left. In place of each eaten bagel, leave a red square.
- If there are more bagels to the right of the buggle than to
the left of the buggle on the current row, eat all of the bagels
to the right. In place of each eaten bagel, leave a red square.
- If the same number of bagels are to the left and to the right
of the buggle on the current row, do not eat any of them.
Initial grid configuration |
Final grid configuration |
Your solution must include the following four methods. You may feel
free to define auxiliary methods, as necessary. Note that it may be
easiest to define these methods in the reverse order than that given
in order to test each method one at a time.
public void eatRows();
This method applies the eatRow
method at every row in the
grid as the buggle moves from the bottom row to the top row.
public void eatRow();
This method implements the row-eating behavior described by the three
bullets above.
public void eatBagels();
This method eats all the bagels between the buggle and the wall it is facing.
A colored square is left in every cell that originally contained a bagel.
Calling this method should not change the state of the buggle.
public int countBagels();
This method returns the number of bagels between the buggle and the wall it is facing.
Calling this method should not change the state of the buggle.
To see a working applet, look in the Test folder.
Open the HungryWorld.html file from the Test folder with
the Applet Viewer. Experiment with the HungryWorld to make sure you
understand the task.