My MidpointFindingKarel Algorithm

I am trying to solve the MidpointFindingKarel problem in the assignment 1 of Stanford’s CS106a. As of now, It is in my head but it still is not working perfectly.

I can lay down the beepers and have Karel move and pick them up one by one from one end to the other but I can’t have him stop at the middle.

  • lay down the beepers
  • move to the starting position
  • pick the beepers one at a time from one end to the other
  1. Lay down the beepers is easy. ┬áIt is just a “while” loop with the put beeper command in the body
  2. move to the starting position is just a “turnAround” and “movetowall” method
  3. This is the tough one which probably needs more decomposition. Still a work in progress.

CheckerBoardKarel – CS106a,Assignment 1,Problem 3

Eureka! I solved it! Works in all worlds. I didn’t put any comments because the code is self explanatory.

/*
 * 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(){
		turnLeft();
		for(int i=0;i<32;i++){
			putBeeper();
			skip();
		}
	}
	
	private void skip(){
		if(frontIsClear()){
			move();
			//doubleskip();
		}
			
		if(frontIsBlocked()){
			if(facingNorth()){
				if(beepersPresent()){
					skipright();
				} else {
					movedown();
				}
				
			} else {
				if(facingSouth()){
					if(beepersPresent()){
						skipleft();
					} else {
						moveup();
					}
				} 
			}
			
		} else {
			move();
		}
	}
	
	private void moveup(){
		turnLeft();
		move();
		turnLeft();
	}
	
	private void movedown(){
		turnRight();
		move();
		turnRight();
	}
	
	private void doubleskip(){
		move();
		move();
	}
	
	private void skipright(){
		moveright();
		moveright();
	}
	
	private void skipleft(){
		moveleft();
		moveleft();
	}
	
	private void moveright(){
		turnRight();
		if(frontIsBlocked()){
			moveleft();
		} else {
			move();
		}
	}
	
	private void moveleft(){
		turnLeft();
		if(frontIsBlocked()){
			moveright();
		} else {
			move();
		}
	}
	
	
	
}

Karel the Robot Assignment 1 Problem 2

It looks like I got the assignment solved. It looks a bit like spaghetti code but it works on the default world.

/*
 * File: StoneMasonKarel.java
 * --------------------------
 * The StoneMasonKarel subclass as it appears here does nothing.
 * When you finish writing it, it should solve the "repair the quad"
 * problem from Assignment 1.  In addition to editing the program,
 * you should be sure to edit this comment so that it no longer
 * indicates that the program does nothing.
 */

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

	public void run(){
		//iterate 13 times(checking 13 columns)
		for(int i=0;i<13;i++){
			check();
			fixColumn();
			keepmoving();
		}
                //pseudocode
		//go up the column
		//check if there is a beeper
		//if there is one
		//go back to the start and
		//fill in the beeper
		//go check the next column
		//repeat
	}

// move to the wall
	private void movetowall(){
		while(frontIsClear()){
			move();
		}
	}

//keep karel moving through the next column
	private void keepmoving(){
		if(frontIsBlocked()){
			if(facingNorth()){
				backtostart();
			}
		}
			
		if(facingNorth()){
			turnRight();
			move();
			//turnRight();
		}
		if(facingSouth()){
			turnLeft();
			move();
			turnLeft();
		}
		if(facingWest()){
			turnAround();
			move();
		}
		
	}

//skip a beeper
	private void skipbeeper(){
		if(beepersPresent()){
			move();
		}
	}

//fix the broken column
	private void fixColumn(){
		backtostart();
		while(frontIsClear()){
			skipbeeper();
			while(noBeepersPresent()){
				
				if(frontIsClear()){
					while(noBeepersPresent()){
						putBeeper();
						move();
					}
				} else {
					if(noBeepersPresent()){
						putBeeper();
					}
					turnAround();
					movetowall();
				}		
			}
		}		
	}

	//move to the next column
	private void nextcolumn(){
		turnLeft();
		move();
		turnLeft();

	}
	
	//back to starting position
	private void backtostart(){
		turnAround();
		movetowall();
		turnAround();
	}
	
	//check if this a column that needs fixing
	private void check(){
		if(facingEast()){
			turnLeft();
		}
		while(noBeepersPresent()){
			if(frontIsClear()){
				move();
			} else {
				turnAround();
				movetowall();
				nextcolumn();
				backtostart();
			}
		}
	}

}

Getting stuck ?!?

stuck!


* File: StoneMasonKarel.java
 * --------------------------
 * The StoneMasonKarel subclass as it appears here does nothing.
 * When you finish writing it, it should solve the &quot;repair the quad&quot;
 * problem from Assignment 1.  In addition to editing the program,
 * you should be sure to edit this comment so that it no longer
 * indicates that the program does nothing.
 */

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

	public void run(){
		while(frontIsClear()){
			turnLeft();
			movetowall();
			keepmoving();
		}
	}
	
	private void movetowall(){
		while(frontIsClear()){
			move();
		}
	}

	private void keepmoving(){
		if(notFacingEast()){
			turnRight();
		}
		nextcolumn();
	}
	
	private void fixColumn(){
		while(noBeepersPresent()){
			putBeeper();
			move();
		}
		backtostart();
	}
	
	private void nextcolumn(){
		turnRight();
		move();
		turnLeft();
//		turnLeft();
//		move();
	}
	
	private void backtostart(){
		turnAround();
		movetowall();
		turnAround();
	}
	
	private void check(){
		while(noBeepersPresent()){
			move();
		}
		//backtostart();
	}
//	private void fix(){		
//		if(beepersPresent()){
//			//backtostart();
//			fixColumn();
			
//			turnAround();
//			movetowall();
//		}
		
//	}

}



how to iterate??

The first assignment – CollectNewsPaperKarel

Posting my solution to the problem number 1 of assignment 1 of CS106a. Hoping for some feedback to this.

import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

		public void run(){
			MoveToNewsPaper();
			pickBeeper();
			MoveBackToStart();
		}


		private void MoveToNewsPaper(){
			while(frontIsClear()){
				move();
			}
			turnRight();
			move();
			turnLeft();
			move();
			
		}

		private void MoveBackToStart(){
			turnAround();
			move();
			turnRight();
			move();
			turnLeft();
			while(frontIsClear()){
				move();
			}
			turnAround();

		}
}