import java.awt.*; public class RandomBagelWorld extends BuggleWorld { Randomizer rand = new Randomizer(); String parameterNames [] = {"side", "bagels"}; ParameterFrame params; public void setup() { params = new ParameterFrame("If World Parameters", 200, 100, parameterNames); params.setIntParam("side", 9); params.setIntParam("bagels", 25); params.show(); } public void resetButtonHook() { // Extend default state initialization to add bagels trail as well. int side = params.getIntParam("side"); int bagels = params.getIntParam("bagels"); setDimensions(side, side); placeBagels(bagels, side); } public void placeBagels(int bagels, int n) { if (bagels > 0) { int x = rand.intBetween(1,n); int y = rand.intBetween(1,n); if (isBagelAt(new Point(x,y))) { //System.out.println("lose (" + x + ", " + y + ")"); // We lose; try again! placeBagels(bagels, n); } else { // We win; place a bagel //System.out.println("win (" + x + ", " + y + ")"); addBagel(new Point(x,y)); placeBagels(bagels - 1, n); } } } public Direction eastOrWest () { int dir = rand.intBetween(1,2); if (dir == 1) { return Direction.EAST; } else { return Direction.WEST; } } public Point randomPoint (int n) { return new Point (rand.intBetween(1,n), rand.intBetween(1,n)); } }