class LooterRobot extends BasicRobot{ public boolean worthySetting() { //determine wether a beeper stack is to be processed return !myworld.frontIsClear(xLoc(),yLoc()+1,east) } public void processStack() { //take all beppers, and count them for later use int numOfBeepers; while (nextToABeeper()){ pickBeeper(); numOfBeepers++; } //walk along the wall until reaching the first cleared // on the right spot turnLeft(); while (!myworld.rightIsClear(xLoc(),yLoc(),north)) move(); //drop all beepers but one while(numOfBeepers > 1) { putBeeper(); numOfBeepers--; } //return to main line and head east turnLeft(); turnLeft(); while (frontIsClear()) move(); turnLeft(); } public void endSequence() { //return to (1,1) and head East turnLeft(); turnLeft(); while (frontIsClear()) move(); turnLeft(); turnLeft(); //drop l00t while (anyBeepersInBag()) putBeeper(); //go to sleep turnOff(); } public static void main(String[ ] args) { // Assuming karel already exists and of type LooterRobot // Facing unknown direction // LooterRobot karel = new LooterRobot(1,1, ); //make sure that Karel is east-headed. while (!karel.facingEast()) karel.turnLeft( ); // get Karel to move until the front is blocked, // checking for a beeper on each step and invoking // the proper sequence of methods while (karel.frontIsClear) { karel.move(); if (karel.nextToABeeper()) { if (karel.worthySetting()) { karel.processStack(); } } } karel.endSequence(); } }