/* FILE NAME: HungryWorld.java * AUTHOR: Stella * DATE: Oct 18 2007 * COMMENTS: Creates a buggle World populated with bagels, in a random fashion, so * that no bagel is placed in the middle column. * Places a buggle in the middle of row one. * Sends the buggle out to eat the bagels, according to this rule: * In each row, the buggle will eat the bagels to its left or the right, only. * Specifically, it will eat the bagels to the right, if there are more bagels * to its left than its right. And it will eat the bagels to its left, * if there are more bagels to its left than its right. * Each cell that used to contain an eaten bagel, is colored red. * MODIFICATION HISTORY: */ import java.awt.*; public class HungryWorld extends RandomBagelWorld { public void run () { int side = params.getIntParam("side"); //get a HungryBuggle and initialize it HungryBuggle harry = new HungryBuggle(); harry.setPosition(new Location(side/2 + 1,1)); harry.setHeading(Direction.NORTH); harry.brushUp(); //send it off to eat the bagels harry.eatAllRows(); } //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new HungryWorld(), "HungryWorld"); } } /*****************************************************************************/ class HungryBuggle extends Buggle { public void eatAllRows() { // For each row in the grid, eats the bagels on the side of the buggle // that has more bagels. // Does nothing if the number of bagels is the same on both sides. // Starting state: In the middle of the first row, facing NORTH, brush up. // Final state: In the middle of the top row, facing NORTH, brush up. if (isFacingWall()) { eatOneRow(); return; } //if it is not facing wall eatOneRow(); forward(); // move on to continue eating more rows eatAllRows(); } public void eatOneRow() { // Eats the bagels on the side of the buggle that has more bagels. // Does nothing if the number of bagels is the same on both sides. // Starting state of the buggle: // middle of the row, brush up, facing NORTH // Does not change the state of the buggle. left(); //face to the WEST int numBagelsToLeft = countBagelsAhead(); right(); right(); //face to the EAST int numBagelsToRight = countBagelsAhead(); left(); //come back to the initial facing // Compare the numbers, and behave appropriately if (numBagelsToLeft == numBagelsToRight) return; if (numBagelsToRight > numBagelsToLeft) { right(); eatBagelsAhead(); left(); //back to initial heading } if (numBagelsToLeft > numBagelsToRight) { left(); eatBagelsAhead(); right(); //back to initial heading } } public int countBagelsAhead() { // Returns the number of bagels ahead of the buggle. // Starting state: position in the middle of the row, brush up, // facing the direction it will count the bagels (WEST or EAST). // Does not change the state of the buggle. int numBagels = 0; if (isFacingWall()) return 0; //otherwise forward(); if (isOverBagel()) numBagels = 1 + countBagelsAhead(); else // not over bagel numBagels = countBagelsAhead(); backward(); return numBagels; } public void eatBagelsAhead() { // Eats the bagels ahead of the buggle. //Leaves a red square where a bagel was found. // Starting state: position in the middle of the row, brush up, // facing the direction it will eat the bagels (WEST or EAST). // Does not change the state of the buggle. if (isFacingWall()) return; // do nothing //if it is not facing wall forward(); if (isOverBagel() ) { pickUpBagel(); paintCell(Color.red); eatBagelsAhead(); backward(); } else { //not over bagel eatBagelsAhead(); backward(); } } //This method is *not* used in this solution. //However, it might be useful to have handy for your ps6! public int numBagelsUnder() { if (isOverBagel()) return 1; else return 0; } }