Skip to content

Instantly share code, notes, and snippets.

@NatashaTheRobot
Created October 28, 2011 04:18
Show Gist options
  • Save NatashaTheRobot/1321608 to your computer and use it in GitHub Desktop.
Save NatashaTheRobot/1321608 to your computer and use it in GitHub Desktop.
This is the solution the MidpointFindingKarel problem in the online Stanford CS 106A class
/*
* 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 () {
putEndBeepers();
while (frontIsClear()) {
takeLastBeeperWest();
takeLastBeeperEast();
}
}
private void putEndBeepers() {
move();
putBeeper();
while (frontIsClear()) {
move();
}
turnAround();
move();
putBeeper();
}
private void takeLastBeeperWest() {
if (facingWest()) {
move();
if (noBeepersPresent()) {
putBeeper();
}
turnAround();
move();
pickBeeper();
turnAround();
move();
move();
}
detectBeeper();
turnAround();
}
private void takeLastBeeperEast() {
if (facingEast()) {
pickBeeper();
move();
if(noBeepersPresent()) {
putBeeper();
}
move();
detectBeeper();
turnAround();
}
}
private void detectBeeper() {
while (noBeepersPresent()) {
if(frontIsClear()) {
move();
}
if(frontIsBlocked()) {
turnAround();
while(frontIsClear()) {
if(noBeepersPresent()) {
move();
}
}
}
}
}
}
@channelchannel
Copy link

So on CodeHS, we are allowed no variables.
There is a fairly simple recursive way to do it but since we are allowed for loops, I re purpose the loop control variables in this example.
public class MidpointKarel extends SuperKarel
{

public void run()
{
    for(int i = 0; i < 1000; i++)
    {
        if (frontIsClear())
        {
            move();
        }
        else
        {
            turnAround();
            for (int j = i/2; j > 0; j--)
            {
                move();
            }
            turnAround();
            break;
        }
    }
    putBall();
}

}

@16dragon16
Copy link

16dragon16 commented May 18, 2017

On CodeHS, here is the way that I used without variables, including in for loops. Basically I go to the top middle, then go down and place a ball and turn to face the right way.

public class MidpointKarel extends SuperKarel
{
    public void run()
    {
        while(frontIsClear())
        {
            turnLeft();
            if(frontIsClear())
            {
                move();
            }
            if(frontIsClear())
            {
                move();
            }
            else
            {
                break;
            }
            turnRight();
            move();
        }
        while(notFacingSouth())
        {
            turnLeft();
        }
        while(frontIsClear())
        {
            move();
        }
        putBall();
        turnLeft();
    }
}

@jacksea1024
Copy link

jacksea1024 commented Aug 26, 2017

public class MidpointFinderKarel extends SuperKarel {
	public void run() {
		//	move from bothsides to mid. step by step
		/*	pre-condition: Karel travel between two beepers, 
		 *              collect the beeper it found, and 
		 *              put one 1Ave closer to mid.
		 *	post-condition: Karel have both beepers by bothsides,
		 *                it stand on the mid corner.          
		*/
		//	set pre-condition
		
		//	put beepers on bothsides.
		putBeeper();
		while (frontIsClear()) {
			move();
		}
		putBeeper();
		turnAround();
		move();
		
		/*  travel between two beepers, 
		 *  collect the beeper it found, and 
		 *	put one 1Ave closer to mid.
		 */ 
		while (frontIsClear()){
			while (noBeepersPresent()) {
				move();
			}
			pickBeeper();
			turnAround();
			move();
			putBeeper(); //put beeper on mid.
			move();      //move to next Ave. 
			//This is when to tell if find midpoint.
			if (beepersPresent()) {
				pickBeeper();
				turnAround();
				move();
				if (facingEast()) {
					turnRight();
				} else {
					turnLeft();
				}
			}
		}
	}
}

@gwerrry
Copy link

gwerrry commented Mar 23, 2021

thank you

@Silverclaw17
Copy link

Silverclaw17 commented Sep 23, 2022

Screenshot 2022-09-23 9 43 07 AM

I repurposed the for loops to technically not use variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment