/* FILE NAME: BagelWorldRecSolns.java * AUTHOR: Stella * DATE: Oct 3-10 2007 * COMMENTS: Defines some recursive methods * on Buggle World, to be used in lab. * MODIFICATION HISTORY: First version of the file contains * bagelLine() and bagelRect(), written for the purpose of * the first lecture on Recursion. * Notice that bagelLine() can be useful for -and is used in- * several of the other methods too. * * */ import java.awt.*; public class BagelWorldRecSolns extends BuggleWorld { public static void main (String[] args) { runAsApplication(new BagelWorldRecSolns(), "BagelWorldRecSolns"); } public void run() { BagelBuggleRec betty = new BagelBuggleRec(); betty.brushUp(); //we need to move the buggle one pos forward before starting, //b/c of the way bagelLine(n) works. //See comments in that method. betty.setPosition(new Location(2,1)); betty.spiral(17); betty.brushDown(); } } class BagelBuggleRec extends Buggle { //make a triangle of bagels //Draw the base. //Then move up one row, and draw a triangle with one less base. public void triangle(int base) { if (base == 0) return; bagelLine(base); left(); forward(); right(); triangle(base-1); left(); backward(); right(); } //Draws a spiral of bagels. The first side of the outside //spiral is "length" long. //"length" should be 2^n + 1, where n is an integer. //This implementation can be shorter, if one defines the //right helper method(s). //I only used the bagelLine() which has been already defined. public void spiral(int length) { //System.out.println("Inside spiarl; " + "n=" + length); if (length <= 4) return; //draw the bottom side (West to East) //System.out.println("drawing the bottom side (West to East)"); bagelLine(length); forward(length-1); left(); forward(); //draw the right side (South to North) //System.out.println("drawing the right side (South to North)"); bagelLine(length-1); forward(length-2); left(); forward(); //draw the top side (East to West) //System.out.println("drawing the top side (East to West)"); bagelLine(length-1); forward(length-2); left(); forward(); //draw the left side (North to South) //System.out.println("drawing the left side (North to South)"); bagelLine(length-3); forward(length-4); left(); forward(); dropBagel(); forward(); spiral(length-4); } //*************************************************************** //Write a recursive method, skip(), that produces a row of bagels in every //other cell on a grid. skip() takes one integer parameter, n, // that is the number of bagels that are dropped in a row. //The buggleÕs position and heading are not changed after the invocation //of the method //write all your code within the skip() method (no auxiliary methods used). //Do not use paintCell(). public void skip(int n) { //The idea here is this: //Do one bagel first, then the method recursively on a problem that has //size (n-1) if (n == 0) return; //do nothing if (n == 1){ dropBagel(); forward(); brushUp(); backward(); brushDown(); } else { dropBagel(); forward(2); skip(n-1); backward(2); } } public void skip2 (int length) { //The idea here is different: //Take care of the problem for size (length-1). //Then, go to the end of the dropped bagels, and take care of //dropping the last bagel. Of course, at the end, you'll have to //come back to the start. if (length == 0) return; // do nothing //in any other case: skip2(length-1); forward(2*(length-1)); dropBagel(); backward(2*(length-1)); } //*************************************************************** //Places a bagel "distance" cells forward from the buggle's current position. //The cells between the buggle's position and the bagel's position will be //colored the same color as the buggle's color. //The position and heading of the buggle does not change //after the method is invoked. public void throwBagel (int distance) { if (distance == 0) { dropBagel(); } else { forward(); throwBagel(distance - 1); backward(); } } //*************************************************************** public void bagelRect(int w, int h) { if (h == 0) { // base case // nothing to do } else { // recursive case bagelLine(w); // draw bottom line left(); // move to the forward(); // next row right(); bagelRect(w, h - 1); // draw subrectangle left(); // undo state changes backward(); right(); } } //bagelLine() //It produces a line of n bagels, forward from the buggle's //position. //The state of the buggle does not change after the method's //invocation. //Interesting side-effect: //The cell at distance n+1 is visited during the method execution! public void bagelLine(int n) { if (n == 0) return; dropBagel(); forward(); bagelLine(n - 1); backward(); } }