Problem 4

This is the problem 4 “As an exercise in solving algorithmic problems, program Karel to place a single beeper at the center of 1st Street. For example, if Karel starts in the world”. In solving this problem, you may count on the following facts about the world:

  • Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers in its bag.
  • The initial state of the world includes no interior walls or beepers.
  • The world need not be square, but you may assume that it is at least as tall as it is wide.
  • Your program, moreover, can assume the following simplifications:
    1. If the width of the world is odd, Karel must put the beeper in the center square.
    2. If the width is even, Karel may drop the beeper on either of the two center squares.
  • It does not matter which direction Karel is facing at the end of the run.

To me the answer is when you move from one side to the other and count the number of steps, and then once you hit the right side wall, go back length /2 steps and then place the beeper, and below is my answer in code, once again the zip file attached is the full assignment 1 with each problem and also the PDF file which includes the assignment in full.

/*
 * File: MidpointFindingKarel.java
 * -------------------------------
 * When you finish writing it, the MidpointFindingKarel class should
 * leave a beeper on the corner closest to the center of 1st Street
 * (or either of the two central corners if 1st Street has an even
 * number of corners).  Karel can put down additional beepers as it
 * looks for the midpoint, but must pick them up again before it
 * stops.  The world may be of any size, but you are allowed to
 * assume that it is at least as tall as it is wide.
 */
 
import stanford.karel.*;
 
public class MidpointFindingKarel extends SuperKarel {
 
	private int middleI =0;
 
	// find the middle by move across until hitting the end
	private void findTheMiddle()
	{
		while (!frontIsBlocked())
		{
			move();
			middleI++;
		}
 
	}
 
	// move half the way back, and place the beeper
	private void placeBeeperInTheMiddle()
	{
		for (int i=0; i < (middleI /2); i++)
			move();
		putBeeper();
	}
 
	// You fill in this part
	public void run()
	{
		findTheMiddle();
		turnAround();
		placeBeeperInTheMiddle();
	}
}

28 thoughts on “Problem 4”

  1. This is my version, yours is so small and good one 🙂

    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	//spreadBeepers = spread beepers on the full row, pickBeepers = Picks up beeper until center reached
    	public void run(){
    		spreadBeepers();
    		pickBeepers();
    	}
     
    	private void spreadBeepers(){
    		while(frontIsClear()){
    			putBeeper();
    			move();
    		}
    		putBeeper();
    	}
     
    	//pickLastBeepers = picks last beeper on both ends , pickNextBeepers = goes on picking beeper 1 at every end until finished
    	private void pickBeepers(){
    		pickLastBeepers();
    		pickNextBeepers();
    	}
     
    	private void pickLastBeepers(){
    		for(int i=0; i&lt;2; i++){
    			turnAround();
    			while(frontIsClear()){
    				move();
    			}
    			pickBeeper();
    		}
    	}
     
    	private void pickNextBeepers(){
    		turnAround();
    		while(noBeepersPresent()){
    		beepBeep();
    		}		
    	}
     
    	// this is the main loop for picking up beeper
    	private void beepBeep(){
    		move();
    		while(beepersPresent()){
    			move();
    		}
    		turnAround();
    		move();
    		if(beepersPresent()){
    		pickBeeper();
    		}else{
    			putBeeper();
    			if(facingWest()){
    				turnAround();
    			}
    		}
    	}
    }
  2. Here is my version with just using the Karel commands and nothing else, e.g. no for loops etc. basically what I do is place beepers across the bottom and then pick one up at each end until there is no beeper next to the beeper that we are on.. e.g. the centre.

     
    	public void lookNorth()
    	{
    		while (!facingNorth())
    			turnLeft();
    	}
     
    	public void lookSouth()
    	{
    		while (!facingSouth())
    			turnLeft();
    	}
     
    	public void lookEast()
    	{
    		while (!facingEast())
    			turnLeft();
    	}
     
    	public void lookWest()
    	{
    		while (!facingWest())
    			turnLeft();
    	}
     
    	// You fill in this part
    	public void run()
    	{
    		while (frontIsClear())
    		{
    			putBeeper();
    			move();
    		}
    		putBeeper();
    		while (beepersPresent())
    		{
    			if (facingEast())
    			{
    				lookNorth();
    				move();
    				lookWest();
    				while (frontIsClear())
    					move();
    				lookSouth();
    				move();
    				lookEast();
    				while (!beepersPresent())
    					move();
    				// check for beeper in the next cell
    				move();
    				// not finished
    				if (beepersPresent())
    				{
    					turnAround();
    					move();
    					pickBeeper();
    					turnAround();
    					move();
    					turnAround();
    				}
    			}
    			else
    			{
    				lookNorth();
    				move();
    				lookEast();
    				while (frontIsClear())
    					move();
    				lookSouth();
    				move();
    				lookWest();
    				while (!beepersPresent())
    					move();
    				pickBeeper();
    				move();
    				turnAround();
    			}			
    		}
    	}
  3. /*
     * File: MidpointFindingKarel.java
     * -------------------------------
     * When you finish writing it, the MidpointFindingKarel class should
     * leave a beeper on the corner closest to the center of 1st Street
     * (or either of the two central corners if 1st Street has an even
     * number of corners).  Karel can put down additional beepers as it
     * looks for the midpoint, but must pick them up again before it
     * stops.  The world may be of any size, but you are allowed to
     * assume that it is at least as tall as it is wide.
     */
     
    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
    	public void run() {
    		spreadBeepers();
    		pickBeepers();
    	}
     
    	private void spreadBeepers() {
    		while(frontIsClear()) {
    			putBeeper();
    			move();
    		}
    		putBeeper();
     
    	}	
     
    	private void pickBeepers() {
    		turnAround();
    		pickBeeper();
    		while(frontIsClear()) move();
    		turnAround();
    		pickBeeper();
    		while(noBeepersPresent()) {
    			move();
    			while(beepersPresent()) move();
    			turnAround();
    			move();
    			if(beepersPresent()) pickBeeper();
    			else putBeeper();
    		}
    	}
    }
  4. shall have to sort that out then :). was trying to do it so that the Karel robot did some funky moves 🙂

  5. Hopefully this will sort out the issue of working in both even/odds, it basically moves the beepers into the middle of the line.

    	public void run()
    	{
    		putBeeper();
    		while (frontIsClear())
    			move();
    		turnAround();
    		move();
    		putBeeper();
    		move();
    		while (!beepersPresent())
    		{
    			while (!beepersPresent())
    				move();
    			pickBeeper();
    			turnAround();
    			move();
    			putBeeper();
    			move();
    		}
    		pickBeeper();
    }

    my mantra should be.. test. test. and test again.

  6. Mine:

    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	public void run() {
    		placeBeepers();
    		retrieveBeepers();
    	}
     
    	private void placeBeepers() {
    		while(frontIsClear()) {
    			putBeeper();
    			move();
     
    		}
    		putBeeper();
    	}
     
    	private void retrieveBeepers() {
    		retrieveOuterBeepers();
    		retrieveInnerBeepers();
    	}
     
    	private void retrieveOuterBeepers() {
    		turnAround();
    		pickBeeper();
    		moveToWall();
    		turnAround();
    		pickBeeper();
    		move();
    	}
     
    	private void retrieveInnerBeepers() {
    		while(beepersPresent()) {
    			move();
    			if(noBeepersPresent()) {
    				turnAround();
    				move();
    				pickBeeper();
    				move();
    			}
    		}
    		turnAround();
    		move();
    		putBeeper();
    	}
     
    	private void moveToWall() {
    		while(frontIsClear()) {
    			move();
    		}
    	}
    }
  7. nice one Jeromy and Safi and Subhadeep, it is great when there is some many ways to solve a puzzle 🙂

  8. nice guys. i like safi’s solution.

    /*
     * File: MidpointFindingKarel.java
     * -------------------------------
     * When you finish writing it, the MidpointFindingKarel class should
     * leave a beeper on the corner closest to the center of 1st Street
     * (or either of the two central corners if 1st Street has an even
     * number of corners).  Karel can put down additional beepers as it
     * looks for the midpoint, but must pick them up again before it
     * stops.  The world may be of any size, but you are allowed to
     * assume that it is at least as tall as it is wide.
     */
     
    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
    	public void run() {
    	establishPosition();
    	moveBack();
    	returnHome();
    	}
     
    	/* precondition: karel is facing east on 1st st &amp; 1st ave
    	 * postcondition: karel is facing east on the end of 1st street with one beeper under him
    	 * establishes the beginning and end of the street by placing a beeper at the corner of both ends
    	 */ private void establishPosition() {
    		if (noBeepersPresent()) {
    		putBeeper();
    		}
    		while (frontIsClear()) {
    			move();
    		}
    		putBeeper();
    	}
     
    	/* precondition: karel is facing either the end or the beginning of 1st street with a beeper under him
    	 * postcondition: karel is facing the opposite way, one avenue ahead of the original position, with a beeper under him
    	 * karel picks up the marker/beeper, turns around to face the other beeper, moves forward once, and places another beeper
    	 */ private void moveBack() {
    		if (beepersPresent()) {
    			pickBeeper();
    			turnAround();
    			move();
    			placeMarker();
    		}
    		moveToNext();
    	 }
     
     
    	/* precondition: there is a beeper under karel and he is facing the other marker
    	 * postcondition:karel is facing the marker he put
    	 * 
    	 */ private void moveToNext() {
    		while (frontIsClear()) {
    			move();
    			if (beepersPresent()) {
    				moveBack();
    			}
    		}
    	}
    	/* precondition: there are no beepers under karel
    	 * postcondition: there is a beeper under karel
    	 * places a marker/beeper if none are present
    	 */ private void placeMarker() {
    		 if (noBeepersPresent()) {
    				putBeeper();
    				}
    	 }
    	 /* precondition: karel is facing a wall and not on the same corner as the middle marker
    	  * postcondition: karel is on the corner of the middle marker
    	  * while karel is on either the beginning or end of 1st street, he will turn around and move until he reaches the middle marker
    	  */ private void returnHome() {
    		  if (frontIsBlocked()) {
    			  turnAround();
    		  }
    		  while (noBeepersPresent()) {
    			  move();
    		  }
    	  }
     
    }
  9. Here’s my version using a little recursion:

     
    /*
     * File: MidpointFindingKarel.java
     * -------------------------------
     * When you finish writing it, the MidpointFindingKarel class should
     * leave a beeper on the corner closest to the center of 1st Street
     * (or either of the two central corners if 1st Street has an even
     * number of corners).  Karel can put down additional beepers as it
     * looks for the midpoint, but must pick them up again before it
     * stops.  The world may be of any size, but you are allowed to
     * assume that it is at least as tall as it is wide.
     */
     
    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	public void run (){
    		putBeeper();
    		if(frontIsClear()) {
    			moveToWall();
    			turnAround();
    			findCenter();
    		}
    	}
     
    	private void findCenter() {
    		move();
    		if (noBeepersPresent()) {
     
    			putBeeper();
    			move();
    			moveToBeeper();
    			pickBeeper();
    			turnAround();
    			findCenter();
    		}
     
    	}
     
    	private void moveToBeeper() {
    		while (noBeepersPresent()) {
    			move();
    		}
     
    	}
     
    	private void moveToWall() {
    		while(frontIsClear()) {
    			move();
    		}
    	}
     
    }
  10. I went a completely different way i assumed the hint about as least as tall as it is wide meant to go for the diagonal approach

    /*
     * File: MidpointFindingKarel.java
     * -------------------------------
     * When you finish writing it, the MidpointFindingKarel class should
     * leave a beeper on the corner closest to the center of 1st Street
     * (or either of the two central corners if 1st Street has an even
     * number of corners).  Karel can put down additional beepers as it
     * looks for the midpoint, but must pick them up again before it
     * stops.  The world may be of any size, but you are allowed to
     * assume that it is at least as tall as it is wide.
     */
     
    import stanford.karel.*;
     
    /*
     * Name: 
     * Section Leader: 
     */
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	public void run() {
    		placeDiagonalBeepers();
    		moveToLeft();
    		checkDiagonalBeepers();
    		moveToBottom();
    		placeCentreBeeper();
    		moveToLeft();
    		cleanUpBeepers();
    		moveToBottom();
    		moveToCentre();
    	}
     
    	private void placeDiagonalBeepers() {
    		while (true) {
    			putBeeper();
    			if (frontIsBlocked()) {break;}
    			move();
    			turnLeft();
    			move();
    			turnRight();
    		}
    	}
     
    	private void moveToLeft() {
    		turnAround();
    		moveToWall();
    		turnAround();
    	}
     
    	private void moveToWall() {
    		while(frontIsClear()) {
    			move();
    		}
    	}
     
    	private void checkDiagonalBeepers() {
    		while(noBeepersPresent()) {
    			move();
    			if (beepersPresent()) {break;}
    			turnRight();		
    			move();
    			turnLeft();
    		}
    	}
     
    	private void moveToBottom() {
    		turnRight();
    		moveToWall();
    		turnLeft();
    	}
     
    	private void placeCentreBeeper() {
    		putBeeper();
    	}
     
    	private void cleanUpBeepers() {
    		while (true) {
    			pickBeeper();
    			if (frontIsBlocked()) {break;}
    			move();
    			turnLeft();
    			move();
    			turnRight();
    		}
    	}
     
    	private void moveToCentre() {
    		turnAround();
    		while (noBeepersPresent()) {
    			move();
    		}
    		turnAround();
    	}
    }
  11. Nice one Dave, as long as it works that is all that matters 🙂 and of course you are having fun along the way 🙂

  12. My take: a different Algorithm

    /*File: MidpointFindingKarel.java
     * ---------------------------
     */
     
    import stanford.karel.*;
     
    public class MidpointFindingkarel extends SuperKarel {
     
    	/*
    	 * Karel lays down a checker board pattern of beepers on the bottom row. If the world has an odd width, the number of beepers
    	 * laid down will correspond to the midpoint of the row. If the world has an even width, the midpoint is between two beepers
    	 * and the number of beepers laid down corresponds to the beeper on the left side of the midpoint when counting from left to right.
    	 * Since karel cannot count, it will move all the beepers back to the starting point, lay them down in a row with no spaces in between
    	 * and then pick them up until it gets to the last one which will represent the midpoint 
    	 * 
    	 */
    	public void run(){
    		laydownCheckerboardPatternOnFirstRow();
    		moveAllBeepersBackToStartingPoint();
    		laydownBeepersInARow();
    		pickUpAllBeepersOnTheRow();
    		moveBackToMidpoint();
    	}
     
    	/*
    	 * Lays down a checker board pattern of beepers on the first row
    	 * first method to be called in main
    	 * post-condition:	Karel is at the east corner of the bottom row and facing east
    	 */
    	private void laydownCheckerboardPatternOnFirstRow(){
    		putBeeper();
    		while(frontIsClear()){
    			move();
    			if(frontIsClear()){
    				move();
    				putBeeper();
    			}
    		}
    	}
     
    	/*
    	 * Moves all beepers to the starting point by continously moving one pile to the next until it reaches the west corner of the bottom row
    	 * pre-condition:	Karel is at the east end of the bottom row and facing east after laying down a checker board pattern on the row
    	 * post-condition: karel is at the west end of the bottom row and facing west after moving all beepers back to the starting point
    	 */
    	private void moveAllBeepersBackToStartingPoint(){
    		turnAround();
    		if(beepersPresent()){
    			movePileToNextTillTheStartPoint();
    		}
    		else{
    			move();
    			movePileToNextTillTheStartPoint();
    		}
    	}
     
    	/*
    	 * Moves one Pile to the next until it reaches the starting point
    	 * pre-condition:	karel is at the on the last beeper laid on the first row and facing west
    	 * post-condition: 	karel is at the bottom west corner facing east with all beepers on thesame corner
    	 */
    	private void movePileToNextTillTheStartPoint(){
    		while(frontIsClear())
    			moveOnePileToTheNext();
    	}
     
    	/*
    	 * Moves one pile to the next by continuously moving one beeper to the next pile and returning to check whether
    	 * there are more beepers present. When there no more beepers present, it moves to the next pile
    	 */
    	private void moveOnePileToTheNext(){
    		while(beepersPresent()){
    			pickBeeper();
    			takeTwoSteps();
    			putBeeper();
    			goBackAndCheck();
    		}
    		takeTwoSteps();
    	}
     
    	/*
    	 * move karel two steps. ie from one pile to the next
    	 */
    	private void takeTwoSteps(){
    		for(int i = 0; i&lt;2; i++)
    			move();
    	}
     
    	/*
    	 * Goes back to the previous pile to check if there are any more beepers left 
    	 */
    	private void goBackAndCheck(){
    		turnAround();
    		takeTwoSteps();
    		turnAround();
    	}
     
    	/*
    	 * Lays down a row of beepers by continously taking one from the pile and moving it to the next empty spot until there is
    	 * no beeper left on the starting point. This means the row of beepers will end at a corner which is step east of the midpoint
    	 * 
    	 * pre-condition:	Karel is at the bottom west corner of the world facing west after all beepers have beepers have
    	 * 					been moved back to the starting point
    	 * post-contition:  Karel is at the bottom west corner of the world facing east after laying down a row of beepers
    	 * 					
    	 */
    	private void laydownBeepersInARow(){
    		turnAround();
    		while(beepersPresent()){
    			moveOneBeeperToNextEmptyCorner();
    			returnAndCheck();
    		}
    	}
     
    	/*
    	 * Picks one beeper from the pile at the starting point and moves it to the next empty corner
    	 */
    	private void moveOneBeeperToNextEmptyCorner(){
    		pickBeeper();
    		move();
    		if(noBeepersPresent()){
    			putBeeper();
    		}else {
    			while(beepersPresent()){
    				move();
    			}
    			putBeeper();
    		}
    	}
     
    	/*
    	 * Returns to the starting point to check whether there are any more beepers to lay down on the row
    	 * pre-condition: 	Karel is on the currently last beeper laid and facing east.
    	 * post-condition: 	Karel is on the startng point and facing east.
    	 */
    	private void returnAndCheck(){
    		turnAround();
    		while(frontIsClear()){
    			move();
    		}
    		turnAround();
    	}
     
    	/*
    	 * Picks up all the beepers laid down on the row.
    	 * pre-condition:	Karel is at the starting point and facing east with a row of beepers to pick up
    	 * post-condition:	Karel is two steps east of the midpoint after cleaning the row of beepers
    	 */
    	private void pickUpAllBeepersOnTheRow(){
    		move();
    		while(beepersPresent()){
    			pickBeeper();
    			move();
    		}
    	}
     
    	/*
    	 * pre-condition:	Karel is two steps east of the midpoint after cleaning a row of beepers
    	 * post-condition:	Karel is at the midpoint with one beeper on thesame corner
    	 */
    	private void moveBackToMidpoint(){
    		turnAround();
    		takeTwoSteps();
    		turnAround();
    		putBeeper();
    	}
    }
  13. I don’t know if anyone is still looking at this, but here’s my approach using recursion (basic idea: Karel takes one step back for every two steps forward). I’m sure there’s a way to optimize this even more.

    /*
     * File: MidpointFindingKarel.java
     * -------------------------------
     * When you finish writing it, the MidpointFindingKarel class should
     * leave a beeper on the corner closest to the center of 1st Street
     * (or either of the two central corners if 1st Street has an even
     * number of corners).  Karel can put down additional beepers as it
     * looks for the midpoint, but must pick them up again before it
     * stops.  The world may be of any size, but you are allowed to
     * assume that it is at least as tall as it is wide.
     */
     
    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	public void run() {
    		findMidpoint();
    		putBeeper();
    	}
     
    	private void findMidpoint() {
    		if(frontIsClear()) {
    			if(frontIsClear()) {
    				move();
    				if(frontIsClear()) {
    					move();
    				} else {
    					turnAround();
    				}
    				if(frontIsClear()) {
    					if(facingEast()) {
    						findMidpoint();
    					}
    				} else {
    					turnAround();
    				}
    			} else {
    				turnAround();
    			}
    			move();
    		}
     
    	}
     
    }
  14. Mine isn’t as clean as everybody’s
    in addition, I had to look at everybody’s code to get it right 🙁

    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	public void run (){
    		fillRow();
    		findMidpoint();
    		goPickExtraBeeper();
    	}
    	public void fillRow(){
    		putBeeper();
    		while (frontIsClear()){
    			move();
    		}
    		putBeeper();
    	}
    	public void findMidpoint(){
    		turnAround();
    		while (frontIsClear()){
    			move();
    			if (beepersPresent()){
    				pickBeeper();
    				turnAround();
    				move();
    				putBeeper();
    			}
    		}
    	}
    	public void goPickExtraBeeper(){
    		turnAround();
    		while (noBeepersPresent()){
    			move();
    		}
    		pickBeeper();
    	}
    }
  15. I don’t see many using variables as counters for this program. Is there a ruling we aren’t supposed to use a lot of variables?

  16. Hi Zig, do not think so regarding the counters, you can use as many or little as you want to 🙂

  17. My contribution..thks to others for the code ideas..
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	public void run() {
    		placebeepers();
     
    		if (frontIsClear()) {
    			placecentrebeeper();
    		} else {
    			turnAround();
    			turnLeft();
    			move();
    		}
     
     
     
     
    	}
     
    	private void placebeepers() {
    		while (frontIsClear() &amp;&amp; noBeepersPresent()) {
    			putBeeper();
    			move();
    		}
    		putBeeper();
     
    		turnAround();
    	}
     
    	private void placecentrebeeper() {
    		while (frontIsClear() &amp;&amp; beepersPresent()) {
    			move();
    		}
    		turnAround();
     
    		if (noBeepersPresent()) move();
     
    		if (beepersPresent()) {
    			pickBeeper();
    			move();
    			placecentrebeeper();
    		} else {
    			putBeeper();
    			move();
    		}
     
     
     
    	}
    }
  18. Thanks for all the ideas on this page, I’ve been working through the stanford course on itunesU which is amazing. This is the first code I’ve written since 10 Print “Hello” 20 Goto 10 when I was 6 years old 🙂 I copied your examples for problems 2 and 3 and was then able to work this one out on my own. Cheers 🙂

    			
    
  19. Hey, mine is slightly different, I filled the whole square round the edges to find the middle, then head to the bottom, pick up the beeper from the middle square, and run the program again to invert the states. Uses only the Karel commands we were taught…

     
    /*
     * File: MidpointFindingKarel.java
     * -------------------------------
     * When you finish writing it, the MidpointFindingKarel class should
     * leave a beeper on the corner closest to the center of 1st Street
     * (or either of the two central corners if 1st Street has an even
     * number of corners).  Karel can put down additional beepers as it
     * looks for the midpoint, but must pick them up again before it
     * stops.  The world may be of any size, but you are allowed to
     * assume that it is at least as tall as it is wide.
     */
     
    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	// The program works by Karel filling the outside of the world again and again
    	// until he reaches the middle. This allows him to find the middle. Then he can 
    	// turn right and head to the bottom, and pick up the beeper at the target 1st 
    	// street square. Then he heads back to the start, and the program runs again
    	// , this time inverting the beeper state of each square (including the empty
    	// target square) and cleaning up. 
     
    	public void run() { 
    		fillUp();
    		cleanUp();
    	}
     
    	// This is the first half of the program, which fills the world and empties
    	// the target square
    	private void fillUp() {
    		firstThreeSides(); // fills (inverts) the outside 3 sides
    		fillWorld(); // fills the rest of the world to the middle
    		moveToBottom(); // turns and heads to bottom
    		pickBeeper(); // empties target square
    		moveBackToStart(); // heads back to start and turns around
    	}
     
    	// This is the second half of the program, which empties the world and refills
    	// the target square
    	private void cleanUp() {
    		firstThreeSides(); // empties (inverts) the outside 3 sides
    		fillWorldInverted(); // empties the rest of the world to the middle
    		moveToBottom(); // turns and heads to the target square
    		turnLeft(); // turns to face East as in brief
     
    	}
     
    	// This fills (inverts) the outside 3 sides
    	private void firstThreeSides() {
    		invertBeeper(); // fills the first square
    		outerRow(); // fills a row till end
    		turnLeft(); //turns left for next row
    		outerRow();
    		turnLeft();
    		outerRow();
    		turnLeft();
    		move(); // steps Karel forward one for fillWorld() command
    	}
     
    	// This fills the rest of the world to the middle
    	private void fillWorld() {
    		while (noBeepersPresent()) { // only put a beeper down in empty squares
    			invertBeeper(); // put one down
    			move();	// and move on
    		}
    		turnLeft(); //at end of blank part of row, turn
    		move(); // and move round to the next blank inner circle, then....
    		turnLeft();
    		move();
    		if (beepersPresent()) { // this is for when you get to the middle
    			turnLeft(); // does a circle back to central square
    			move();
    			turnLeft();
    		}
    		else { //.... turn right and repeat filling
    			turnRight();
    			fillWorld();
    		}
    	}
     
    	// This makes Karel go for the middle to the bottom
    	private void moveToBottom() {
    		if (facingEast()) { //These just allow for odd/even worlds, to stop Karel
    			turnRight(); // from going to the top instead of the bottom
    		}
    		if (facingWest()) {
    			turnLeft();
    		}
    		while (frontIsClear()) { // once he's facing the right way, move to bottom
    			move();
    		}
    	}
     
    	// This moves Karel back to the start position
    	private void moveBackToStart() {
    		turnRight(); // turn to face start
    		while (frontIsClear()) {
    			move(); // and move all the way there
    		}
    		turnAround(); // then turn around ready to go again
    	}
     
    	// This is the opposite of the fillWorld() command, and just allows the
    	// program to follow the same path as that command when the world is full 
    	// of beepers rather than empty. * marks changed bits
    	private void fillWorldInverted() {
    		while (beepersPresent()) { // *
    			invertBeeper();
    			move();	
    		}
    		turnLeft();
    		move();
    		turnLeft();
    		move();
    		if (noBeepersPresent()) { // *
    			turnLeft();
    			move();
    			turnLeft();
    		}
    		else {
    			turnRight();
    			fillWorldInverted(); // *
    		}
    	}
     
    	// This is for the outer rows, and fills them to the end
    	private void outerRow() {
    		while (frontIsClear()) {
    			move();
    			invertBeeper();
    		}
    	}	
     
    	// This changes the beepers state from present to not present, or from 
    	// not present to present.
    	private void invertBeeper() {
    		if (beepersPresent()) {
    			pickBeeper();
    		} else {
    			putBeeper();
    		}
    	}
    }
  20. hi there,
    here is mine. really simple when you see it…

    			

    I guess
    1) paving the streets with beepers
    2) go back and forth and pick the last beepers
    will eventually move Karel to midpoint without any needs of marking, thus.. no height is needed
    no variable should be allowed I guess :/? it is out of the scope of Karel.

     
    /*
     * File: MidpointFindingKarel.java
     * -------------------------------
     * When you finish writing it, the MidpointFindingKarel class should
     * leave a beeper on the corner closest to the center of 1st Street
     * (or either of the two central corners if 1st Street has an even
     * number of corners).  Karel can put down additional beepers as it
     * looks for the midpoint, but must pick them up again before it
     * stops.  The world may be of any size, but you are allowed to
     * assume that it is at least as tall as it is wide.
     */
     
    import stanford.karel.*;
     
    public class MidpointFindingKarel extends SuperKarel {
     
    	// You fill in this part
    	public void run() {
    		putBeeper();
    		while (frontIsClear()) {
    			move();
    			putBeeper();
    		}
    		turnAround();
    		while (beepersPresent()) {
    			while (frontIsClear()) {
    				move();
    			}
    			turnAround();
    			while (noBeepersPresent()) {
    				move();
    			}
    			pickBeeper();
    			if (frontIsClear()) {
    				move();
    			}
    		}
    		turnAround();
    		if (frontIsClear()) {
    			move();
    			putBeeper();
    		} else {
    			putBeeper();
    		}			
    	}
     
    }

Leave a Reply

Your email address will not be published. Required fields are marked *