Problem 3

The problem 3 is “In this exercise, your job is to get Karel to create a checkerboard pattern of beepers inside an empty rectangular world, as illustrated in the following before-and-after diagram:”

Karels checkboard
Karels checkboard

The way that I viewed the problem is that the robot (Karel) had to move along the either in Up / Down or Left / Right and place a beeper every other step, so in basics if he moves up, he will move up 1 place see if he is able to move any more, if not turn right and if the front is not blocked move forward and right again to go back down the aisle next to the previous one we have just been up and whilst moving up and down the aisle to place a beeper every other step ( so need to keep check of the steps) or you could do the same process but going left and right instead, e.g. move right first to the end of that aisle and then go up if able to and then turn around and move left along the aisle above the previous aisle.

Here is my implementation of both ideas, again the source code for the assignment 1 is attached above in the zip file and also the PDF with the full assignment details.

/*
 * File: CheckerboardKarel.java
 * ----------------------------
 * When you finish writing it, the CheckerboardKarel class should draw
 * a checkerboard using beepers, as described in Assignment 1.  You
 * should make sure that your program works for all of the sample
 * worlds supplied in the starter folder.
 */
 
import stanford.karel.*;
 
public class CheckerboardKarel extends SuperKarel {
 
	private boolean putBeeper = true;
 
	private void placeBeeperAndMove()
	{
		while (true)
		{
			if (putBeeper) {
				putBeeper();
				putBeeper = false;
			} else
				putBeeper = true;
 
			if (frontIsBlocked()) break;
			move();
		}
	}
 
	private void goUpAndDown()
	{
		boolean turnLeft = true;
 
		while (true)
		{
			if (turnLeft) 
				turnLeft();
			else 
				turnRight();
			placeBeeperAndMove();
			turnLeft = (turnLeft == true ? false : true);
			if (turnLeft)
				turnLeft();
			else
				turnRight();
			if (frontIsBlocked()) break;
			move();
		}
	}
 
	public void goLeftAndRight()
	{
		boolean turnLeft = true;
 
		while (true)
		{
			placeBeeperAndMove();
			if (turnLeft) 
				turnLeft();
			else 
				turnRight();
			if (frontIsBlocked()) break;
			move();
			if (turnLeft)
				turnLeft();
			else
				turnRight();
			turnLeft = (turnLeft == true ? false : true);
		}
	}
 
	// You fill in this part
	public void run()
	{
		// go up and down 
		//goUpAndDown();
		// go left and right
		goLeftAndRight();
	}
}

54 thoughts on “Problem 3”

  1. Here’s my version … it works but looks nothing like as nice as yours.

    /*
     * File: CheckerboardKarel_m27.java
     * ----------------------------
     * Accepts an unknown world and places Beepers on every other junction on both axes. 
     * Mechanistically repeats the simple core: putBeeper()+move()+move() to populate each world using
     * method.distributeBeepers(). Following an initiating putBeeper() call, an if/else construct coordinates 
     * the moves along and between streets to complete the repeating core. This allows fall-through recursion.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel_m27 extends SuperKarel {
     
    	/* main method*/
    	public void run(){		
    		distributeBeepers();
    	}
     
     
    	/* method: distributeBeepers()
    	 * Beeper distribution function built using recursive nested if,else conditionals rather than while() loops
    	 * preconditions: Karel facing East (at 1,1) in an unknown world
    	 * postcondition: World populated with bleepers on alternate junctions on both axes
    	 **/
    	private void distributeBeepers() {
    		putBeeper(); 
     
    		if ( frontIsClear() ) {
    			move();
     
    			if ( frontIsClear() ) {
    				move();			
    				distributeBeepers();
     
    			} else {
    				negotiateJunction();
    			}
     
    		} else {
    			negotiateJunction();
     
    			if ( frontIsClear() ) {
    				move();	
     
    			} else {
    				negotiateJunction();
    			}
    		}		
     
     
    		if( frontIsClear()) {
    			distributeBeepers();
    		} else {
    			if( rightIsClear() && leftIsClear() ) {
    				distributeBeepers();
    			}
    		}				
    	}
     
     
    	/* method: negotiateJunction() 
    	 * action: turns Karel North, checks frontIsClear, and moves to next street
    	 * preconditions: Karel at end of street, facing (East || West), frontIsBlocked
    	 * postconditions: Karel has moved to next street if it exists and isFacingAlongStreet
    	 * */
    	private void negotiateJunction() {
    		turnToNextStreet();
    		if( frontIsClear() ) {
    			moveToNextStreet();
    		}
    	}
     
    	/* method: turnToNextStreet();
    	 * action: turns Karel North to face next street
    	 * precondition: Karel at end of street, frontIsBlocked
    	 * postcondition: Karel at end of street, facingNorth
    	 * */
    	private void turnToNextStreet(){
    		turnNorth();
    	}
     
    	/* method: moveToNextStreet()  
    	 * action: moves Karel North one street and turns to face along street
    	 * precondition: Karel at end of street, facingNorth
    	 * postCondition: Karel advanced North to next street, and is facing along it
    	 * */
    	private void moveToNextStreet() {
    		move();
    		turnIntoStreet();
    	}
     
     
    	/* method: turnNorth()
    	 * action: turns Karel to face North
    	 * precondition: Karel at end of street, facing (East || West)
    	 * postcondition: Karel facing North
    	 * */
    	private void turnNorth() {
     
    		if( facingEast() ) { 
    			turnLeft();
    		}
     
    		if( facingWest() ) {
    			turnRight();
    		}
    	}
     
    	/* method: turnIntoStreet()
    	 * action: turns Karel to face along a street
    	 * precondition: Karel at start of street, facingNorth
    	 * postcondition: Karel at start of street, facingAlongStreet
    	 * */
    	private void turnIntoStreet(){
     
    		if ( leftIsBlocked() ) {
    			turnRight();
    		}
     
    		if( rightIsBlocked() ) {
    			turnLeft();
    		}
    	}
    }
  2. hi matdodds, there is no real prefect answer, as long as the program does what is asked then that is prefect answer :)..

    the cs106a course is fun 🙂

  3. Another version using help from the lecture files

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run(){
    		cleanRow();
    		while (leftIsClear()){
    			repositionForRowToWest();
    			cleanRow();
    			if (rightIsClear()){
    				repositionForRowToEast();
    				cleanRow();
    			} else {
    				turnAround();
    			}
     
    		}
    	}
     
    	private void cleanRow() {
    		putBeeper();
    		while(frontIsClear()){
    			moveOn();
    			moveOnPutBeeper();	
    		}
     
    	}
     
    	private void repositionForRowToWest() {
    		if(beepersPresent()){
    		turnLeft();
    		move();
    		turnLeft();
    		move();
    		}else{
    		turnLeft();
    		move();
    		turnLeft();
    		}
    	}
     
    	private void repositionForRowToEast() {
    		if(beepersPresent()){
    			turnRight();
    			move();
    			turnRight();
    			move();
    			}else{
    			turnRight();
    			move();
    			turnRight();
    			}
    	}
     
    	private void moveOn(){
    	if(frontIsClear()){
    		move();
    		}
    	}
    	private void moveOnPutBeeper(){
    		if(frontIsClear()){
    			move();
    			putBeeper();
    		}
    	}
    }
  4. Subhadeep’s is the best I’ve seen here, and does follow the lecture notes. Remember, we aren’t supposed to use any variables, and it’s generally not good practice to put a “break” in any statement. Break is also not part of Karel’s commands or methods. I’m going to play with this some more and see if I can work out a more efficient algorithm.

  5. Hi Wendy, yeah, Subhadeeps does not use any of the java commands, but the Karel’s ones. Shame on me!!. I consider myself hand slapped !!.

    Seriously you are right, in general coding the “break” is not a good way of doing things, while … is better or do … while depending on the setup of the variables that you are wanting to setup.

    I shall do a better version without using the java commands.

  6. here you go, funny thing is is that it is better code than before :).

     
    	public void run()
    	{
    		while (frontIsClear())
    		{
    			while (!frontIsBlocked())
    			{
    				putBeeper();
    				move();
    				if (frontIsClear())
    					move();
    			}
    			turnLeft();
    			if (frontIsClear())
    			{
    				move();
    				turnLeft();
    			}
    			while (!frontIsBlocked())
    			{
    				putBeeper();
    				move();
    				if (frontIsClear())
    					move();
    			}
    			turnRight();
    			if (frontIsClear())
    			{
    				move();
    				turnRight();
    			}
    		}
    	}
  7. That is much more efficient, but doesn’t work for odd-sized worlds or 1 column wide rows. That drove me crazy. Here is my program, and instead of putting beepers, I painted them actually red and black.

    public void run(){
     
    		checkerRow();		
    		//after row is checkered, determine which direction to turn.
    		while (leftIsClear())
    		{
    			turnToWest();
    			checkerRow();
    			if (rightIsClear()){
    				turnToEast();
    				checkerRow();
    			}
    			else{
    				turnAround();
    			}
    		}
    	}
     
    	public void checkerRow(){		
    		//for single column worlds, don't drop beeper(paint red) when facing West.
    		if (frontIsBlocked() && facingWest())	
    		{}
    		else{			
    			paintCorner(RED);
    			while(frontIsClear()){
    				moveUp();				
    				moveUpAndDrop();
    			}
    		}
     
    	}
     
    	private void moveUpAndDrop() {
    		if(frontIsClear()){
    			moveUp();
    			paintCorner(RED);
    		}		
    	}
     
    	private void moveUp() {
    		if(frontIsClear()){
    			move();
    			paintCorner(BLACK);
    		}		
    	}
     
    	//determine whether or not to start next row with a beeper.
    	private void turnToWest(){
    		if (cornerColorIs(RED)){
    			turnLeft();
    			moveUp();
    			turnLeft();
    			moveUp();
    			}
    		else
    		{
    			turnLeft();			
    			moveUp();			
    			turnLeft();			
    		}
    	}
     
    	private void turnToEast(){
    		if (cornerColorIs(RED)){
    			turnRight();			
    			moveUp();
    			turnRight();			
    			moveUp();
    		}
    		else
    		{
    			turnRight();			
    			moveUp();
    			turnRight();			
    		}		
    	}
     
     
    }
  8. shall have to correct that then.. not working in funny sized grids, was doing the part 4 and did not check against the different sized grids :(.

  9. This one is abit more long winded, but does all cases that I checked, 1*8, 7*7, 8*1, 8*8, 6*5, 40*40

     
    	public void run()
    	{
    		putBeeper();
    		if (frontIsBlocked())
    			turnLeft();
    		while (frontIsClear())
    		{
    			while (!frontIsBlocked())
    			{
    				move();
    				if (frontIsClear())
    					if (!beepersPresent())
    					{
    						move();
    						putBeeper();
    					}
    					else
    						move();
    			}
    			turnLeft();	
    			if (frontIsClear())
    			{
    				if (!beepersPresent())
    				{
    					move();
    					putBeeper();
    					turnLeft();
    				}
    				else
    				{
    					move();
    					if (frontIsClear())
    					{
    						turnLeft();
    						move();
    					}
    				}
    			}
    			while (!frontIsBlocked())
    			{
    				if (!beepersPresent())
    				{
    					putBeeper();
    					move();
    				}
    				else
    					move();
    				if (frontIsClear())
    					move();
    			}
    			turnRight();
    			if (frontIsClear())
    			{
    				if (!beepersPresent())
    				{
    					move();
    					putBeeper();
    				}
    				else
    					move();
    				turnRight();
    			}
    		}
  10. I know my code has some bad form in it – take for instance:

    public void checkerRow(){		
    		//for single column worlds, don't drop beeper(paint red) when facing West.
    		if (frontIsBlocked() && facingWest())	
    		{}
    		else{
    			paintCorner(RED);
    			while(frontIsClear()){
    				moveUp();				
    				moveUpAndDrop();
    			}
    		}
     
    	}

    I’ve tried switching it around so I didn’t have the “if (frontIsBlocked() && facingWest())
    {}

    because that just means “do nothing”…however, it doesn’t seem to work if I put in just the “else” part as the “if”. I’ll have to do a little more debugging.

    These exercises are pretty fun, as is watching the videos.

  11. BTW, how do I get that nice, formatted code I see here? Mine doesn’t indent by using just the tag. Thanks!

  12. Yeah Wendy, the exercises are fun and also watching the video, he is a good bloke to teach IT stuff, I am doing the CS106B course now and also find that interesting as well :). the Lady is very interesting too in the videos :).

    I would say for the code, I am using a <pre lang=”java”> … </pre> tags to do the code.

    Also what could be a good idea for the code

    if (! (frontIsBlocked() && facingWest()))

    notice the ! which means not, so if we are not front is blocked and facing west., just a idea.

    have fun.. that is what I am doing 🙂

  13. Here is my version (I’ve tried to keep it easy to understand):

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public boolean switchState(boolean state) {
    		if(state == true) {
    			return false;
    		} else {
    			return true;
    		}
    	}
     
    	public void draw(boolean state) {
    		if(state == true) {
    			putBeeper();
    		}
    	}
     
    	private boolean goNorth() {
    		while(!facingNorth()) {
    			turnLeft();
    		}
    		if(frontIsClear()) {
    			move();
    			return true;
    		}
    		return false;
    	}
     
    	private boolean moveToNextSpot() {
    		if(frontIsClear()) {
    			move();
    		} else if(goNorth()) {
    			if(leftIsClear()) {
    				turnLeft();
    			} else {
    				turnRight();
    			}
    		} else {
    			return false;
    		}
    		return true;
    	}
     
    	public void run() {
    		boolean state = true;
    		do {
    			draw(state);
    			state = switchState(state);
    		} while(moveToNextSpot());
    	}
     
    }
  14. Mine:

    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run() {
    		placeBeepers();
    		while(leftIsClear()) {
    			switchRowToWest();
    			placeBeepers();
    			if(rightIsClear()) {
    				switchRowToEast();
    				placeBeepers();
    			} else {
    				turnAround();
    			}
    		}
    	}
     
    	private void placeBeepers() {
    		if(!(noBeepersPresent() && facingWest() && frontIsBlocked())) {
    			putBeeper();
    		}
    		while(frontIsClear()) {
    			moveIfPossible();
    			if(noBeepersPresent() && !frontIsBlocked()) {
    				moveAndPlaceBeeper();
    			}
    		}
    	}
     
    	private void switchRowToWest() {
    		if(noBeepersPresent()) {
    			turnLeft();
    			moveIfPossible();
    			turnLeft();
    		} else {
    			turnLeft();
    			moveIfPossible();
    			turnLeft();
    			moveIfPossible();
    		}
    	}
     
    	private void switchRowToEast() {
    		if(noBeepersPresent()) {
    			turnRight();
    			moveIfPossible();
    			turnRight();
    		} else {
    			turnRight();
    			moveIfPossible();
    			turnRight();
    			moveIfPossible();
    		}
    	}
     
    	private void moveIfPossible() {
    		if(frontIsClear()) {
    			move();
    		}
    	}
     
    	private void moveAndPlaceBeeper() {
    		if(frontIsClear()) {
    			move();
    			putBeeper();
    		}
    	}
    }
  15. This was my take on it:

     
    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run(){
     
    		fillRow();
    		while (frontIsClear()) {
    			if (beepersPresent()){
    				move();
    				turnFromWall();
    				if (frontIsClear()){
    					move();
    					fillRow();
    				} else {
    					turnNorth();
    				}
    			} else {
    				move();
    				turnFromWall();
    				fillRow();
    			}
    		}
    	}
     
    	private void fillRow(){
    		putBeeper();
    		while(frontIsClear()){
    			move();
    			if (frontIsClear()){
    				move();
    				putBeeper();
    			}
     
    		}
    		turnNorth();
    	}
     
    	private void turnNorth(){
    		turnLeft();
    		if (facingSouth()){
    			turnAround();
    		}
    	}
     
    	private void turnFromWall(){
    		if (leftIsBlocked()){
    			turnRight();
    		} else {
    			turnLeft();
    		}
    	}
     
    }

    It’s good to have a way to get feedback on these since it’s not exactly like we can turn them in to be graded…

  16. I think from previous posts that if someone does like it, or not, then it kinder gets a mention :). which is very nice since that helps everyone :). keep up the fun of programming everyone 🙂

  17. Another version.

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
    /*
     * Karel creates the checker board for the first row moving eastward. After creating the pattern for the first row,
     * if a beeper is present at the east corner is means the world has an odd width. Or else, it has an even width.
     * Karel creates the pattern for the remaining world depending on whether the width is odd or even.
     */
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run(){
    		createPatternMovingEast();
    		if(beepersPresent())
    			createRemainingPatternForWorldWithOddWidth();
    		else
    			createRemainingPatternForWorldWithEvenWidth();
    	}
     
    	/*
    	 * This method creates a row of the checker board pattern every time Karel is moving eastward.
    	 */
    	private void createPatternMovingEast(){
    		putBeeper();
    		while(frontIsClear()){
    			move();
    			if(frontIsClear()){
    				move();
    				putBeeper();
    			}
    		}
    	}
     
    	/*
    	 * This method creates the remaining pattern for the checker board when the width of the world is odd. that is,
    	 * when the right corner of the first row has a beeper after the pattern for the first row is created.
    	 */
    	private void createRemainingPatternForWorldWithOddWidth(){
    		while(leftIsClear()){
    			repositionToCreatePatternMovingWest();
    			createPatternMovingWestWhenWidthIsOdd();
    			if(rightIsClear()){
    				repositionToCreatePatternMovingEast();
    				createPatternMovingEast();
    			}else{
    				turnAround();	//so that the left is no longer clear and the loop can be exit
    			}
    		}
    	}
     
    	/*
    	 * This method creates the remaining pattern for the checker board when the width of the world is even. that is,
    	 * when the right corner of the first row does not have a beeper after the pattern for the first row is created.
    	 */
    	private void createRemainingPatternForWorldWithEvenWidth(){
    		while(leftIsClear()){
    			repositionToCreatePatternMovingWest();
    			createPatternMovingWestWhenWidthIsEven();
    			if(rightIsClear()){
    				repositionToCreatePatternMovingEast();
    				createPatternMovingEast();
    			}else{
    				turnAround();	//so that the left is no longer clear and the loop can be exit
    			}
    		}
    	}
     
    	/*
    	 * pre-condition:	Karel is at the east end of a row facing east and its left is clear
    	 * post-condition:	Karel is at the east end of the row above and facing west
    	 */
    	private void repositionToCreatePatternMovingWest(){
    		turnLeft();
    		move();
    		turnLeft();
    	}
     
    	/*
    	 * pre-condition:	Karel is at the west end of a row facing west and its right is clear
    	 * post-condition:	Karel is at the west end of the row above and facing east
    	 */
    	private void repositionToCreatePatternMovingEast(){
    		turnRight();
    		move();
    		turnRight();
    	}
     
    	/*
    	 * Creates the patterns moving westward when the width of the world is odd
    	 */
    	private void createPatternMovingWestWhenWidthIsOdd(){
    		while(frontIsClear()){
    			move();
    			putBeeper();
    			if(frontIsClear()){
    				move();
    			}
    		}
    	}
     
    	/*
    	 * creates the pattens moving westward when the width of the world is even
    	 */
    	private void createPatternMovingWestWhenWidthIsEven(){
    		putBeeper();
    		while(frontIsClear()){
    			move();
    			if(frontIsClear()){
    				move();
    				putBeeper();
    			}
    		}
    	}
     
    }
  18. My take

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
    /*
     * Karel creates the checker board pattern for the first row moving eastward. After creating the pattern for the first row,
     * if a beeper is present at the east corner, it means the world has an odd width. Or else, it has an even width.
     * Karel creates the pattern for the remaining world depending on whether the width is odd or even.
     */
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run(){
    		createPatternMovingEast();
    		if(beepersPresent())
    			createRemainingPatternForWorldWithOddWidth();
    		else
    			createRemainingPatternForWorldWithEvenWidth();
    	}
     
    	/*
    	 * This method creates checker board pattern for the first row and will also be used to create the pattern for every row in
    	 * which Karel is moving from west to east.
    	 */
    	private void createPatternMovingEast(){
    		putBeeper();
    		while(frontIsClear()){
    			move();
    			if(frontIsClear()){
    				move();
    				putBeeper();
    			}
    		}
    	}
     
    	/*
    	 * This method creates the remaining pattern for the checker board when the width of the world is odd. that is,
    	 * when the right corner of the first row has a beeper after the pattern for the first row is created.
    	 */
    	private void createRemainingPatternForWorldWithOddWidth(){
    		while(leftIsClear()){
    			repositionToCreatePatternMovingWest();
    			createPatternMovingWestWhenWidthIsOdd();
    			if(rightIsClear()){
    				repositionToCreatePatternMovingEast();
    				createPatternMovingEast();
    			}else{
    				turnAround();	//so that the left is no longer clear and the loop can be exit
    			}
    		}
    	}
     
    	/*
    	 * This method creates the remaining pattern for the checker board when the width of the world is even. that is,
    	 * when the right corner of the first row does not have a beeper after the pattern for the first row is created.
    	 */
    	private void createRemainingPatternForWorldWithEvenWidth(){
    		while(leftIsClear()){
    			repositionToCreatePatternMovingWest();
    			createPatternMovingWestWhenWidthIsEven();
    			if(rightIsClear()){
    				repositionToCreatePatternMovingEast();
    				createPatternMovingEast();
    			}else{
    				turnAround();	//so that the left is no longer clear and the loop can be exit
    			}
    		}
    	}
     
    	/*
    	 * pre-condition:	Karel is at the east end of a row facing east and its left is clear
    	 * post-condition:	Karel is at the east end of the row above and facing west
    	 */
    	private void repositionToCreatePatternMovingWest(){
    		turnLeft();
    		move();
    		turnLeft();
    	}
     
    	/*
    	 * pre-condition:	Karel is at the west end of a row facing west and its right is clear
    	 * post-condition:	Karel is at the west end of the row above and facing east
    	 */
    	private void repositionToCreatePatternMovingEast(){
    		turnRight();
    		move();
    		turnRight();
    	}
     
    	/*
    	 * Creates the patterns moving westward when the width of the world is odd
    	 */
    	private void createPatternMovingWestWhenWidthIsOdd(){
    		while(frontIsClear()){
    			move();
    			putBeeper();
    			if(frontIsClear()){
    				move();
    			}
    		}
    	}
     
    	/*
    	 * creates the pattens moving westward when the width of the world is even
    	 */
    	private void createPatternMovingWestWhenWidthIsEven(){
    		putBeeper();
    		while(frontIsClear()){
    			move();
    			if(frontIsClear()){
    				move();
    				putBeeper();
    			}
    		}
    	}
     
    }
  19. Its stuck in the first loop. What am I missing. thanks in advance for your help.

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * pre-condition: Karel is in the S-W corner of the world facing east in a 
     * beeperless world.
     * post-condition: the screen has one single beeper on every other space, on odd numbered avenues 
     * for odd numbered streets, and beepers on even numbered avenues for even 
     * numbered streets.
    */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	/*
    	 *  
    	 */
    	public void run() {
    		distributeEast();
    		distributeWest();
    	}
    		/*
    		 * Distribute east beepers.
    		 */
    		private void distributeEast() {
    			if (facingEast()) {
    				while (frontIsClear()) {
    					putBeeper();
    					move();
    					if (frontIsBlocked()) {
    						turnLeft();
    						move();
    					}
    				}
    			}
    		}
     
    		private void distributeWest() {
    			if (facingWest()) {
    				while (frontIsClear()) {
    					move();
    					putBeeper();
    					if (frontIsBlocked()) {
    						turnRight();
    						move();
    					}
    				}
    			}
    		}
    }
  20. What do you think of my program? I feel like it’s not very elegant.

     * File: CheckerboardKarel.java
     * ----------------------------
     * The CheckerboardKarel class draws
     * a checker board using beepers.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	/*
    	 * Shows the program's starting point.
    	 */
    	public void run() {
    		if (frontIsClear()) {
    			fillRow();
    			while (frontIsClear()) {
    				nextRow();
    				fillRow();
    			}
    		} else {
    			fillColumn();
    		}
    	}
    	/*
    	 * Karel starts at the first dark square of the row and places a
    	 * beeper there. Karel travels across the row, placing a beeper
    	 * on every other square. Karel stops when he reaches the wall.
    	 * Finally, Karel turns so that he is facing north. 
    	 */
    	private void fillRow() {
    	putBeeper();
    	while (frontIsClear()) {
    		if (beepersPresent()) {
    			move();
    		} else {
    			move();
    			putBeeper();
    		}
    	}
    	if (facingEast()) {
    		turnLeft();
    	} else {
    		turnRight();
    	}
    }
    	/*
    	 * Karel starts facing north at the last square of a row.  If
    	 * Karel is on a square with a beeper and his left is 
    	 * blocked, he moves, turns right, and moves. If Karel is on a 
    	 * square without a beeper and his left is blocked,he moves and 
    	 * turns left. If Karel is on a square with a beeper and his 
    	 * right is blocked, he moves, turns left, and moves. If Karel 
    	 * is on a square without a beeper and his right is blocked, he 
    	 * moves and turns left. Karel ends at the first square that 
    	 * get a beeper in the next row. 
    	 */
    	private void nextRow() {
    		if (leftIsBlocked()) {
    			if (beepersPresent()) {
    				move();
    				turnRight();
    				move();
    			} else {
    				move();
    				turnRight();
    			}
    		} else {
    			if (beepersPresent()) {
    				move();
    				turnLeft();
    				move();
    			} else {
    				move();
    				turnLeft();
    			}
    		}
    	}
    	/*
    	 * Karel starts facing east. Karel turns left and places a
    	 * beeper. Then, he travels up, placing a beeper on every other 
    	 * square.
    	 */
    	private void fillColumn() {
    		turnLeft();
    		putBeeper();
    		while (frontIsClear()) {
    			if (beepersPresent()) {
    				move();
    			} else {
    				move();
    				putBeeper();
    			}
    		}
    	}
    }
  21. hi toobrawnlittlebrain, I am guessing it is going around in circles ? you may need to place the second part of the loop under the while loop, so that it can get to the second function call ?

  22. import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run() {
    		// world is 1 column wide
    		if (frontIsBlocked()) {
    			turnLeft();
    		}
    		while (frontIsClear()) {
    			fillLine();
    		}
    	}
     
    	private void fillLine() {
    		putBeeper();
    		move();
    		if (frontIsClear()) {
    			move();
     
    			// end of the line (still facing east)
    			if (frontIsBlocked()) {
    				putBeeper();
     
    				if (leftIsClear()) {
    					nextRow();
    					move();
    				}
    			}
    		} else {
    			nextRow();
    		}
    	}
     
    	private void nextRow() {
    		if (facingEast()) {
    			if (leftIsClear()) {
    				turnLeft();
    				move();
    				turnLeft();
    			}
    		} else {
    			if (rightIsClear()) {
    				turnRight();
    				move();
    				turnRight();
    			}
    		}
    	}
     
    }
  23. hmmm… here is my code.. I can’t quite figure out how to make it loop until it hits the northernmost wall, so i just used the rowLoop(); a few times until i got just the right number to not go a second time at the top row. If its not in the right code format, im sorry, but i’ve never posted code online before :/

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
    	public void run() {
    		rowLoop();
    		rowLoop();
    		rowLoop();
    		rowLoop();
    	}
    /*oneRow();
    	 * Pre-Condition: facing away from a wall with 
    	 * no wall in front.
    	 * Post-Condition: facing a wall with a line
    	 * of dashed beepers in the back
    	 */
    private void oneRow() {
    	while (frontIsClear()) {
    		move();
    		putBeeper();
    		if (frontIsClear()) {
    			move();
    		}
     
    	}
    }
    /*nextRowL();
     * Pre-Condition facing a wall after filling a row
     * to the right.
     * Mid-condition: no wall to the north - moved one
     * corner north.
     * Post condition: one corner north of Pre-Condition,
     * facing the opposite direction.
     */
    private void nextRowL() {
    	turnLeft();
    	if (frontIsClear()) {
    		move();
    	}
    	turnLeft();
    }
    /*nextRowR();
     * Pre-Condition facing a wall after filling a row
     * to the left.
     * Mid-condition: no wall to the north - moved one
     * corner north.
     * Post condition: one corner north of Pre-Condition,
     * facing the opposite direction.
     */
    private void nextRowR() {
    	turnRight();
    	if (frontIsClear()) {
    		move();
    	}
    	turnRight();
    }
    private void rowLoop() {
    	oneRow();
    	if (frontIsBlocked()) {
    		nextRowL();
    	}
    	oneRow();
    	if (frontIsBlocked()) {
    		nextRowR();
    	}
    }
     
    }
  24. This was actually a lot harder than I thought it would be! So here’s the source for anyone else that struggled. This will work in ALL world sizes, and will not produce errors etc. at the end of loops for hitting walls and such…

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run(){
    		if(frontIsBlocked()){             // This means if column is 1 block wide, it will just turn left, and carpetBomb vertically
    			turnLeft();
    		}
    		while(frontIsClear()){
    			carpetBomb();				// The carpet bomb needs to be non direction specific, as it could be going vertically
    		}
    	}
    	private void carpetBomb(){			// The carpet bomb needs to be non direction specific, as it could be going vertically
    		putBeeper();
    		move();
    		// If front is clear move, else, change direction
    		if(frontIsClear()){
    			move();
    			if(frontIsBlocked()){
    				putBeeper();
     
    				changeDirection();
    				if(frontIsClear()){
    					move();
    				} else {
    					// You've hit a wall dear
    				}
    			}
    		} else {
    			changeDirection();
    		}
    	}
     
    	private void changeDirection(){
    		if(facingEast()){
    			turnLeft();
    			if(frontIsClear()){
    				move();
    				turnLeft();
    			} else {
    				// You've hit the wall and you're done
    			}
    		} else {
    			if(facingWest()){
    				turnRight();
    				if(frontIsClear()){
    					move();
    					turnRight();
    				} else {
    					// You've hit the wall and you're done
    				}
    			} else {
    				// do nothing, you've probably gone vertically and hit the wall, which means you're done.
    			}
    		}
    	}
    }
  25. this is my version

    /*
    * File: CheckerboardKarel.java
    * ----------------------------
    * When you finish writing it, the CheckerboardKarel class should draw
    * a checkerboard using beepers, as described in Assignment 1.  You
    * should make sure that your program works for all of the sample
    * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
     
    	public void run() {
    		putBeeper();
    		walkAndPutBeeper();
    		while (frontIsClear()) {
    			stepForward(); 
    			turnDirectionPassRow();
    			toDefaultStart();
    			walkAndPutBeeper();
    		}
    	}
     
    	private void walkAndPutBeeper() {
    		while (frontIsClear()) {
    			if (frontIsClear()) {
    				move();
    				if (frontIsClear()) {
    					move();
    					while (noBeepersPresent()) {
    						putBeeper();
    					}
    				}
    			}
    		}
    		turnDirectionInRow();
    	}
     
    	private void stepForward() {
    		if (noBeepersPresent()) {
    			move();
    			putBeeper();
    		} else {
    			move();
    		}
    	}
     
    	private void turnDirectionPassRow() {
    		if (leftIsClear()) {
    			turnLeft();
    		} else {
    			turnRight();
    		}
    	}
     
     
    	private void turnDirectionInRow() {
    		if (facingWest()) {
    			turnRight();
    		} else {
    			turnLeft();
    		}
    	}
     
    	private void toDefaultStart(){
    		if(frontIsClear()){
    			if (noBeepersPresent()) {
    				move();
    				putBeeper();
    			} else {
    			}
    		}
    	}
    }
  26. For sadistic reasons, I decided to take this iTunesU class which has so far been a lot of fun. After completing my version, I decided to google to see what others came up with as this was more challenging that I had anticipated. Here is the version I created that works in all worlds. Feel free to critique since it’s the only feedback I can get.

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	// You fill in this part
     
    	public void run() {
    		putBeeper();
    		while (frontIsClear() || leftIsClear()) {
    			fillRow();
    			if (leftIsClear()) {
    				nextStreet();
    			}
     
    		}
    }
     
    /**
     * fill row with beepers in every other corner
     */
    	private void fillRow() {
    		while (frontIsClear()) {
    			move();
    			if (frontIsClear()) {
    				move();
    				putBeeper();
    			}
    		}
    		if (leftIsClear()) {
    			returnToFirstAve();
    		}
    	}
     
    /**
     * Return Karel back to 1st Avenue
     */
    	private void returnToFirstAve() {
    		turnAround();
    		while (frontIsClear()) {
    			move();
    		}
    		turnAround();
    	}
     
     
    /**
     * Move Karel to next street
     */
    	private void nextStreet() {
    		if (beepersPresent()) {
    			turnLeft();
    			move();
    			turnRight();
    			if (frontIsClear()) {
    				move();
    				putBeeper();
    			}
    		}
    		else {
    			turnLeft();
    			move();
    			turnRight();
    			putBeeper();
    		}
    	}
    }
  27. /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel 
    {
    	private int step=0; // if the step value mod 2 = 0 then put the beeper.
     
    	public void run()
    	{ while (!frontIsBlocked() && !facingNorth() )
    		{
    			if (step%2 == 0) putBeeper();
    			move();
    			step++;
     
    			if (frontIsBlocked() && facingEast())
    			{ 
    				if (step%2 == 0) putBeeper();
    				turnLeft();
    		  		move();
    				step++;
    				turnLeft();
    			}
     
    			if (frontIsBlocked() && facingWest())
    			{	
    				if (step%2 == 0) putBeeper();
    				turnRight();
    				move();
    				step++;
    				turnRight();
    			}
     
    		} 
    	}
     
    }
  28. It will be much easier and elegent to use the step counts karel have walked at any given position,
    if the step count can be divided by 2 (mod 2 = 0) then karel shold put the beeper otherwise go a step furthur.
    if karel face the wall when he face east or west he should make a turn until he face the wall at the north.

  29. /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * Here's my solution to the Checker board problem. 
     * If the first row ends up with a beeper than the next row should start w/out.
     * If it's a one column world Karel goes to the North.
     */
     
    import stanford.karel.*;
     
    	public class CheckerboardKarel extends SuperKarel {
     
    	// Complete the Checker board.
    	public void run (){
     
    		if (frontIsClear()) {
    			while (frontIsClear()) {
    				moveToEast();
    				moveUpTurnWest();
    				moveToWest();
    				moveUpTurnEast();
    		} 
    		} else {
    			moveToNorth();
    		}
     
    }
    	private void moveToEast() {
    		// TODO Auto-generated method stub
    		putBeeper();
    		while(frontIsClear()){
    			move();
    			if(frontIsClear()){
    				move();
    				putBeeper();
    			}
    		}
    	}
    	private void moveToNorth() {
    		// TODO Auto-generated method stub
    		turnLeft();
    		while (frontIsClear()) {
    			putBeeper();
    			move();
    			if (frontIsClear()){
    			move();
    			}
    		}
    	}
    	private void moveToWest() {
    		// TODO Auto-generated method stub
    		if (noBeepersPresent()) {
    			while(frontIsClear()){
    				move();
    				if(frontIsClear()){
    					putBeeper();
    					move();
    				}
    			}
    		}	else {
    			while (frontIsClear()) {
    				move();
    				if (frontIsClear()){
    				move();
    				putBeeper();
    				}
    		}
    		}
    	}	
     
    	public void moveUpTurnWest() {
    		// TODO Auto-generated method stub
    		if (beepersPresent()) {
    			turnNorth();
    			if (frontIsClear()) {
    				move();
    				turnWest();
    			}
    		} else {
    			turnNorth();
    			if (frontIsClear()) {
    				move();
    				turnWest();
    				putBeeper();
    			}	
    		}
    	}
     
    	public void moveUpTurnEast() {
    		// TODO Auto-generated method stub
    		turnNorth();
    		if (frontIsClear()) {
    		move();
    		turnEast();
    		}
    	}
     
    	public void turnNorth() {
    		while (notFacingNorth()) {
    			turnLeft();
    		}
    	}
    	public void turnEast(){
    		while (notFacingEast()) {
    			turnLeft();
    		}
    	}
    	public void turnWest(){
    		while (notFacingWest()) {
    			turnLeft();
    		}
    	}
    	public void turnSouth(){
    		while (notFacingSouth()) {
    			turnLeft();
    		}
    	}
    }
  30. import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	// It is assumed that Karel starts facing east
    	// The logic must be bulletproof in the world where rows and columns are even OR odd
     
    	public void run(){
    		//this if statement is necessary for situations where it is 1 x 1 and 1 x n 
    		if (frontIsBlocked()){
    			turnLeft();
    		}
    		//this will execute Karel to fill in every other block until he hits a wall. 
    		while (frontIsClear()){
    			fillRow();	
    		}
    	}
    	//this needs to go on off on off whether it is odd or even number 
    	public void fillRow(){
    		putBeeper();
    		move();
    		//karel put 1 beeper and moved. now he is at the second block and does not know if there
    		//are blocks blocking his way.  therefore, he needs to check if the front is blocked again.
    		// if the front is clear, he will move one block. else, he will check his surroundings
    		if (frontIsClear()){
    			move();
    			//now there is no Beeper behind him.  he does not need to put a beeper down.  he will now check if there is a wall
    			//if there is a wall, then he will need to put a beeper down then Check his surroundings
    			if (frontIsBlocked()){
    				putBeeper();
    				if (facingEast()){
    					validateEast();
    				}
    				else {
    					validateWest();
    				}
    			}
    		}
    		//else if (frontIsBlocked()){
    		else {
    			if (facingEast()){
    				validateEast();
    			}
    			else {
    				validateWest();
    			}
    		}
    	}
     
    	//this is the method for checking his surrounding IF he is facing east
    	public void validateEast(){
    		turnLeft(); //first, he needs to turn north to see if there is anything in his way.
    		//he needs to see if there is a beeper underneath him.
    		//this if will make him turn around and set himself up to fill another row for an odd column situation
    		if (beepersPresent()){
    			if (frontIsClear()){
    				move();
    				turnLeft();
    				move();
    			}
    			else{	
    			}	
    		}
    		//if there isn't a beeper underneath karel, he is going to move up one, then turn west. this will
    		//set him up in a position to start filling westward in an even column situation.
    		else {
    			if (frontIsClear()){
    				move();
    				turnLeft();
    			}
    			else{	
    			}
    		}
     
     
    	}
     
    	//this is the method for checking his surrounding IF he is facing West
    	public void validateWest(){
    		turnRight();//first, he needs to turn north to see if there is anything in his way.
    		//he needs to see if there is a beeper underneath him.
    		//this if will make him turn around and set himself up to fill another row for an odd column situation
    		if (beepersPresent()){
    			if (frontIsClear()){
    				move();
    				turnRight();
    				move();
    			}
    			else{	
    			}	
    		}
    		//if there isn't a beeper underneath karel, he is going to move up one, then turn west. this will
    		//set him up in a position to start filling westward in an even column situation.
    		else {
    			if (frontIsClear()){
    				move();
    				turnRight();
    			}
    			else{	
    			}
    		}
    	}
    }
  31.  
    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	/*
    	 * Karel creates pattern
    	 */
    	public void run() {
    		if (frontIsClear()) {
    			passRow();
    			while (leftIsClear()) {
    				changePatternWest();
    				passRow();
    				if (rightIsClear()) {
    					changePatternEast();
    					passRow();
    				} else {
    					turnAround();
    				}
    			}
    		} else {
    			turnLeft();
    			passRow();
    		}
    	}
     
    	private void passRow() {
    		putBeeper();
    		while (frontIsClear()) {
    			move();
    				if (frontIsClear()) {
    					move();
    					putBeeper();
    				} 
    		}
    	}
     
    	private void changePatternWest() {
    		if (beepersPresent()) {
    			repositionForRowToWest();
    			if (frontIsClear()) {
    				move();
    			} 
    		} else {
    			repositionForRowToWest();
    		}
    	}
     
    	private void changePatternEast() {
    		if (beepersPresent()) {
    			repositionForRowToEast();
    			if (frontIsClear()) {
    				move();
    			} 
    		} else {
    			repositionForRowToEast();
    		}
    	}
     
    	/* Reposition Karel at far East wall to face West on next row */
    	private void repositionForRowToWest() {
    		turnLeft();
    		move();
    		turnLeft();
    	}
     
    	/* Reposition Karel at far West wall to face East on next row */
    	private void repositionForRowToEast() {
    		turnRight();
    		move();
    		turnRight();
    	}
     
    }
  32. /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CopyOfCheckerboardKarel extends SuperKarel {
     
    	/* method: main method
    	 * action: check if in a single column, and if so, turn north
    	 *         fill streets with complementary patterns
    	 * pre-condition: karel is facing east at position 1,1
    	 * post-condition: karel is facing north in an upper right or left corner
    	 *                 in a world filled with a checker board pattern using beepers
    	 */
    	public void run() {
    		if (frontIsBlocked()) {
    			turnNorth();
    		}
    		while (frontIsClear()) {
    			fillPatternA();
    			fillNextPattern();
    		}
    	}
    	/* method: fillPatternA()
    	 * action: place beepers in a [beeper][no beeper][beeper] pattern
    	 * pre-condition: karel is at the east or west end of a street facing away from
    	 *                the wall
    	 * post-condition: karel is facing north at the opposite of the street, having
    	 *                 populated it with a checkered pattern
    	 */
    	private void fillPatternA() {
    		putBeeper();
    		while (frontIsClear()) {
    			move();
    			if (frontIsClear()) {
    				move();
    				putBeeper();
    			}
    		}
    		turnNorth();
    	}
     
    	/* method: fillPatternB()
    	 * action: place beepers in a [no beeper][beeper][no beeper] pattern
    	 * pre-condition: karel is at the east or west end of a street facing away from
    	 *                the wall
    	 * post-condition: karel is facing north at the opposite of the street, having
    	 *                 populated it with a checkered pattern
    	 */
    	private void fillPatternB() {
    		while (frontIsClear()) {
    			move();
    			putBeeper();
    			if (frontIsClear()) {
    				move();
    			}
    		}
    		turnNorth();
    	}
     
    	/* method: fillNextPattern()
    	 * action: move to the next higher street and fill with a checkered pattern
    	 *         complementary to the previous
    	 * pre-condition: karel is facing north at the end of a street already filled
    	 *                with a checkered pattern
    	 * post-condition: karel is facing away from the wall at the next higher street, having
    	 *                 populated the previous with a pattern complementary to the previous 
    	 *                 street before that
    	 */
    	private void fillNextPattern() {
    		if (frontIsClear()) {
    			if (beepersPresent()) {
    				nextRow();
    				fillPatternB();
    			} else {
    				nextRow();
    				fillPatternA();
    			}
    		}
    		nextRow();
    	}
     
    	/* method: turnNorth()
    	 * action: turn Karel to face north
    	 * pre-condition: Karel is facing an arbitrary direction
    	 * post-condition: Karel is facing north
    	 */
    	private void turnNorth() {
    		while (notFacingNorth()) {
    			turnRight();
    		}
    	}
     
    	/* method: nextRow()
    	 * action: move Karel to the next higher street
    	 * pre-condition: Karel is at the end of a street
    	 * post-condition: Karel is at the next higher street and facing away from the wall
    	 */
    	private void nextRow() {
    		turnNorth();
    		if (frontIsClear()) {
    			move();
    			faceRow();
    		}
    	}
     
    	/* method: faceRow()
    	 * action: turn Karel so that it is facing away from the wall
    	 * pre-condition: Karel must be at one end of a street
    	 * post-condition: Karel is facing away from the wall at the same end of the street
    	 */
    	private void faceRow() {
    		turnNorth();
    		if (rightIsBlocked()) {
    			turnLeft();
    		} else if (leftIsBlocked()) {
    			turnRight();
    		}
    	}
    }
  33. import stanford.karel.*;
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run(){
    		while(frontIsClear()){
    			putBeeper();
    			move();
    			if(frontIsBlocked()&amp;&amp;facingEast()){
    				turnLeft();
    				if(frontIsBlocked()){
    					break;
    				}
    				move();
    				turnLeft();
    			}
    			else if(frontIsBlocked()&amp;&amp;facingWest()){
    				turnRight();
    				if(frontIsBlocked()){
    				break;
    				}
    				move();
    				turnRight();
     
    			}
    			else if(frontIsBlocked()&amp;&amp;facingNorth()){
    				break;
    			}
    			else{
    				move();
    				if(frontIsBlocked()&amp;&amp;facingEast()){
     
    					turnLeft();
    					putBeeper();
    					if(frontIsBlocked()&amp;&amp;facingNorth()){
    						break;
    					}
    					move();
    					turnLeft();
    					move();
    				}
    			}	
     
    		}
    	}
    }
  34. import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run() {
    		createCheckerboard();	// Creates a checkerboard pattern in any world	
    	}
     
     
    	private void createCheckerboard() {		// Creates a checkerboard pattern
    		if (frontIsClear()) {
    			while (facingEast()) {
    				putRowEast();
    				repositionWest();
    				while (facingWest()) {
    					putRowWest();
    					repositionEast();
    				}
    			}
    		} else {
    			turnLeft();
    			putRowNorth(); 		// Added for 1 x 8 grid
    		}
    	}	
     
     
    	private void putRowEast() { 	// Puts a beeper on every other intersection going East
    		while (frontIsClear()) {	
    			putBeeper();
    			move();
    			while (frontIsClear()) {
    				move();
    				putBeeper();
    				if (frontIsClear()) {
    				move();
    				} else {
    					repositionWest();
    				}
    			}
    		}
    	}
     
     
    	private void repositionWest() {		// Gets Karel ready for new row to West
    		if (leftIsClear()) {	
    			turnLeft();
    			move();
    			turnLeft();
    		} else {
    		}
    	}
     
    	private void putRowWest() { 	// Puts a beeper on every other intersection going West
    		while (frontIsClear()) {	
    			putBeeper();
    			if (frontIsClear()) {	
    				move();
    				while (frontIsClear()) {
    					move();
    					putBeeper();
    					if (frontIsClear()) {
    					move();
    					} else {
    						repositionEast();
    					}
    				}
    			} else {
    				repositionEast();
    			}
    		}
    	}
     
     
    	private void repositionEast() {		// Gets Karel ready for new row to East
    		if (rightIsClear()) {
    			turnRight();
    			move();
    			turnRight();
    		} else {
     
    		}
    	}
     
     
    	private void putRowNorth() { 	// Added for 1 x 8 grid
    		while (frontIsClear()) {	
    			putBeeper();
    			move();
    			while (frontIsClear()) {
    				move();
    				putBeeper();
    				if (frontIsClear()) {
    				move();
    				} else {
    					turnRight();
    				}
    			}
    		}
    	}
     
    }

    it cant do the 7 x7 grid ahhhhhhh!!!!! :@

  35. My idea was to start with a base row and build off that. Only being able to use the Karel stuff definitely made this more challenging.

    			
    /*
    * Maybe a bit redundant but i included some checks so it would work in any environment-
     
    */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
    	public void run () {
    		buildOddColumn () ;
    		while (frontIsClear ()) {
    		move () ;
    		alternatingCheckerBoard () ;
    		}
    	}
     
    	/*
    	 * Builds the first checkerboard column where (1,1) begins
    	 * with a beeper and every other corner up the column contains
    	 * a Beeper.
    	 */
    	private void buildOddColumn () {
    		turnLeft () ;
    		putBeeper () ;
    		while (frontIsClear()) {
    			move () ;
    			if (frontIsClear ()) {
    				move () ;
    				putBeeper ();
    			}
    		}
    		turnAround () ;
    		if (frontIsClear()) {
    			move () ;
    			if (beepersPresent()) {
    				movetoWall () ;
    				} else { 
    					turnAround () ;
    					move () ;
    					if (noBeepersPresent()) {
    					putBeeper () ;
    					}
    			turnAround () ;
    			movetoWall() ;
    			}
    		}
    		turnLeft () ;
    	}
     
    	/*
    	 * Builds the opposite of the OddColumn.
    	 */
    	private void buildEvenColumn () {
    		turnLeft () ;
    		while (frontIsClear()) {
    			move () ;
    			putBeeper ();
    			if (frontIsClear()) {
    				move () ;
    			}
    		}
    		turnAround () ;
    		movetoWall () ;
    		turnLeft () ;
    	}
     
    	/*
    	 * If front is clear Karel will advance until blocked
    	 * by a wall.
    	 */
    	private void movetoWall () {
    		while (frontIsClear()) {
    				move () ;
    			}
    	}
     
    	/*
    	 * Method to carry out the building of alternating checker-
    	 * board columns. After building a column, Karel backtracks
    	 * to check for the presence of a Beeper at that point.
    	 * If a Beeper is present, Karel advances to the next column
    	 * and builds EvenColumn.
    	 */
    	private void alternatingCheckerBoard () {
    		turnAround () ;
    		move () ;
    		if (beepersPresent()) {
    			turnAround () ;
    			move () ;
    			buildEvenColumn () ;
    		}
    			else {
    				turnAround () ;
    				move () ;
    				buildOddColumn () ;
    			}
    		}
     
    }
  36. It was hard to me (too).
    After solving by myself looked for others’ solutions.
    I very like Jamie Shepherd’s (December 14, 2010 at 5:11 pm) and Frosh’s (November 22, 2010 at 4:14 am) solutions.

    Here is my version:

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run() {
    		fill();
    	}
     
    	private void fill() {
    		if (frontIsBlocked()) {
    			startLoneColumn();
    		}
     
    		while(frontIsClear()) {
    			put();
    			go();
     
    			if (frontIsBlocked()) {
    				finishRow();
    			}
     
    		}
    	}
     
    	private void startLoneColumn() {
    		if (leftIsBlocked()) {
    			put();
    		} else {
    			turnLeft();
    		}
    	}
     
    	private void put() {
    		putBeeper();
    		go();
    	}
     
    	private void go() {
    		if(frontIsClear()) {
    			move();
    		} else {
    			goTop();
    		}
    	}
     
    	private void goTop() {
    		if(facingEast()) {
    			turnLeft();
    			if (frontIsClear()) {
    				move();
    				turnLeft();
    			}
    		} else {
    			if (facingWest()) { 
    				turnRight();
    				if (frontIsClear()) {
    					move();
    					turnRight();
    				}
    			}
    		}
    	}
     
    	private void finishRow() {
    		if (facingEast()) {
    			put();
    			if (leftIsClear()) {
    				go();
    			} 
    		} else {
    			if (facingWest()) {
    				put();
    				if (rightIsClear()) {
    					go();
    				} 
    			} else { // facingNorth - finishColumn
    				if (leftIsBlocked()) {
    					if (rightIsBlocked()) {
    						finishLoneColumn();
    					}
    				}
    			} 
    		}
    	}
     
    	private void finishLoneColumn() {
    		turnAround();
    		if (frontIsClear()) {
    			move();
    			turnAround();
    			if (noBeepersPresent()) {
    				move();
    				put();
    			} else {
    				move();
    			}
    		} else {
    			turnAround();
    		}
     
    	}
     
    }
  37. My Version 😀

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    public void run(){
    		while(frontIsClear()) {
    			bieberbeeper();
    		}
    }
     
     
     
    	private void bieberbeeper(){
    		if(noBeepersPresent()){
    		putBeeper();
    		}		
    			if(frontIsClear()){
    				move();
     
    					if(frontIsClear()){
    						move();
    						putBeeper();
    					}	
     
    					else{
    						deadend();
    					}
    			}
    	}
     
     
     
    	private void deadend() {
    		if(frontIsBlocked()){
    			faceNorth();
    			moveintostreet();
    		}
    	}
     
    	private void moveintostreet() {
    		if(frontIsClear()){
    			move();
    			turnintostreet();
    		}
    	}
     
    	private void turnintostreet() {
    		if(rightIsBlocked()){
    			turnLeft();
    		}
     
    		if(leftIsBlocked()){
    			turnRight();
    		}
    	}
     
     
    	private void faceNorth() {
    		if(facingEast()){
    			turnLeft();
    		}
     
    		if(facingWest()){
    			turnRight();
    		}
    	}
     
     
     
     
     
     
     
     
     
     
     
    }
  38. Wow – Well I have to say I am very impressed with Cyvre’s code!

    After reading your code Cyvre then looking at mine, I see I have rather a lot to learn and tidy up! Even with the lack of code comments I found it very easy to read and understand.

    My efforts appear to be flooded with if statements

    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	// You fill in this part
    	/*
    	 * Main method for Karel to place beepers in a chequered fashion for all rectangular and square worlds.
    	 * Karel always starts facing East
    	 */
    	public void run() {
    		checkForSingleStreet();
    		putBeeper();
    		if (frontIsBlocked()) {
    			nextAvenueFaceWest();
    		}
    		while (facingEast()) {
    			placeBeepersEast();
    		while (facingWest()) {	
    			placeBeepersWest();
    		}
    		}
    	}
     
    /*
     * Karel checks to see if he is to place the beepers down a single street
     */
    private void checkForSingleStreet() {
    	if (frontIsBlocked()) {
    		turnLeft();
    		putBeeper();
    		while (facingNorth()) {
    	if (leftIsBlocked()) {
    		spaceOutBeepers();
    			} else {
    				turnRight();
    			}	
    		}
    	}
    }
    /*
     *Karel checks for blockages before placing a series of beepers with spaces in between them.
     */
    	private void placeBeepersEast() {
    		if (frontIsBlocked()) {
    			nextAvenueFaceWest();
    		}
    		spaceOutBeepers();
    	}
    /*
    * Karel places the beepers leaving a space between each one.
    */ 
    	 private void spaceOutBeepers() {
    		if (frontIsClear()) {
    			move();
    			if (frontIsClear()) {
    				move();
    				putBeeper();
    			}	
    		}	
    	}
     
    /*
     * Karel is blocked and is facing West.
     * Karel checks status and moves onto the next avenue and faces in the opposite direction
     */
    	private void nextAvenueFaceWest() {
    			if (frontIsBlocked()) {
    				if (leftIsClear()) {
    					if (noBeepersPresent()) {
    						turnLeft();
    						move();
    						turnLeft();
    						putBeeper();
    						} else {
    							if (frontIsBlocked()) {
    								if (beepersPresent()) {
    									turnLeft();
    									if (frontIsClear()) {
    										move();
    										turnLeft();
    										move();
    										putBeeper();
    							} else {	
    							}
    						}	
    					}
    				}
    			}
    		}	
    	}
     
    	/*
    	 * 
    	 */
    	private void placeBeepersWest() {
    		if (frontIsBlocked()) {
    			nextAvenueFaceEast();
    		}
    		spaceOutBeepers();
    	}
     
    	/*
    	 * Karel is blocked and is facing West.
    	 * Karel takes the appropriate steps to move onto the next avenue and face in the opposite direction
    	 */
    		private void nextAvenueFaceEast() {
    			if (frontIsBlocked()) {
    				if (rightIsClear()) {
    					if (noBeepersPresent()) {
    						turnRight();
    					if (frontIsClear()) {
    						move();
    						turnRight();
    					putBeeper();
    					} else {
    						if (frontIsBlocked()) {
    							if (beepersPresent()) {
    								turnRight();
    								if (frontIsClear()) {
    									move();
    									turnRight();
    									move();
    									putBeeper();
    								} else {	
    								}
    							}
    						}
    					}
    				}
    			}	
    		}
    	}	
    }

    Live and Learn I guess!

  39. Nps, you just need to add in <pre lang=”java”>….</pre> around the code, thanks for sharing.

  40. And here is my version:

     /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
     
    import stanford.karel.*;
     
    public class CheckerboardKarel extends SuperKarel {
     
    	public void run() {
    		spam();
    	}
     
    	private void spam() {
    		if (frontIsClear()) {
    			putBeepers();
    		} else {
    			while (!frontIsClear()) {
    				turnLeft();
    			}
    			putBeepers();	
    		}
    	}
     
    	private void putBeepers() {
    		putBeeper();
    		while (frontIsClear()) {
    			move();
    			while (frontIsClear()) {
    				move();
    				putBeepers();
    				if (!frontIsClear() &amp;&amp; beepersPresent()) {
    					turnODD();
    				} else if (!frontIsClear() &amp;&amp; !beepersPresent()){
    					turnEven();
    				}else if (!frontIsClear() &amp;&amp; !leftIsClear() || !rightIsClear()){ 
    					stop(); 
    				}
    			}
    		}
    	}
     
    	private void turnODD() {
    		turnNorth();
    		if (frontIsClear()) {
    			move();
    			if(rightIsClear()) {
    				turnEast();
    				move();
    				putBeepers();
    			}else{
    				turnWest();
    				move();	
    				putBeepers();
    			}
    		}else{ stop(); }
    	}
     
    	private void turnEven() {
    		turnNorth();
    		if (frontIsClear()) {
    			move();
    			if(rightIsClear()) {
    				turnEast();
    				putBeepers();
    			}else{
    				turnWest();
    				putBeepers();
    			}
    		}else{ stop(); }
    	}
     
    	private void stop() {
    		turnNorth();
    	}
     
    	private void turnNorth() {
    		while (!facingNorth()) {
    			turnRight();
    		}
    	}
     
    	private void turnWest() {
    		while (!facingWest()) {
    			turnRight();
    		}
    	}
     
    	private void turnEast() {
    		while (!facingEast()) {
    			turnRight();
    		}
    	}
    }
  41. Here’s what I did, haven’t written a program for 25 years (FORTRAN!)

    /*
    * File: CheckerboardKarel.java
    * —————————-
    * CheckerboardKarel class draws a checkerboard using beepers,
    * as described in Assignment 1. Karel starts in the southwest
    * corner of the world, facing east. Program works for various worlds.
    */

    import stanford.karel.*;

    public class CheckerboardKarel extends SuperKarel {
    public void run() {

    putBeeper();

    if (frontIsBlocked()) {
    turnLeft();
    }

    while (frontIsClear()) {
    move();
    if (frontIsClear()) {
    move();
    putBeeper();

    if (frontIsBlocked()) {
    moveToNextStreet();
    if (frontIsClear()) {
    move();
    putBeeper();
    }

    }

    } else {
    moveToNextStreet();
    if (frontIsClear()) {
    putBeeper();
    }
    }

    }
    }

    /**
    * Moves to the next street facing opposite direction
    */
    private void moveToNextStreet() {
    if (facingEast()) {
    turnLeft();
    if (frontIsClear()) {
    move();
    turnLeft();
    }
    } else {
    turnRight();
    if (frontIsClear()) {
    move();
    turnRight();
    }
    }
    }
    }

Leave a Reply

Your email address will not be published.