Problem 2

The basics of problem 2 is “Karel has been hired to repair the damage done to the Quad in the 1989 earthquake. In particular, Karel is to repair a set of arches where some of the stones (represented by beepers, of course) are missing from the columns supporting the arches”, which means that you have to replace empty places with a beeper!. The rules are:

Karel 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.
  • The columns are exactly four units apart, on 1st, 5th, 9th Avenue, and so forth.
  • The end of the columns is marked by a wall immediately after the final column. This wall section appears after 13th Avenue in the example, but your program should work for any number of columns.
  • The top of the column is marked by a wall, but Karel cannot assume that columns are always five units high, or even that all columns are the same height.
  • Some of the corners in the column may already contain beepers representing stones that are still in place. Your program should not put a second beeper on these corners.

Remember to change the run configurations as from the previous post on how to setup the environment to the StoneMasonKarel for the main class, here is the my java file for this problem 2.

My way of fixing the stones is to go up the supporting line and replace the stones (beepers) and then come back down again and move along 4 positions if able to, and repeat, since in the rules you the height of the building can change which is why I come back down again from the top of the buildings.

/*
 * 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 {
 
	// move up the row and find replace any missing "stones"
	// checking to make sure that we have not reached the top
	private void replaceStones()
	{
		// check for a stone or wall! move in that direction
		while (true)
		{
			if (!beepersPresent())
				putBeeper();
			if (frontIsBlocked()) break;
			move();
		}
	}
 
	private void comeBackDown()
	{
		turnAround();
		while (!frontIsBlocked())
			move();
	}
 
	// need to go up and down move 4 and then up and down
	private void moveAlong()
	{
		while (true)
		{
			turnLeft();
			replaceStones();
			comeBackDown();
			turnLeft();
			if (!frontIsBlocked())
				for (int i =0; i < 4; i++)
					move();
			else
				break;
		}
 
	}
 
	public void run()
	{
		moveAlong();
	}
}

again the zile file attached is the whole assignment 1 problems, so you can download that for the full source code and the PDF assignment details.

7 thoughts on “Problem 2”

  1. My version::

    /*
     * 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 {
     
    	//Main Program
    	public void run(){
    		while (frontIsClear()){
    			doWork();
    		}
    			doWork();
    	}
     
     
    	// Move Up Until Hits a Wall and Place Beepers
    	private void moveUp(){
    		turnLeft();
    		while (frontIsClear()){
    			placeBeepers();
    			move();
    		}
    	}
     
    	//Move Down to its starting position
    	private void moveDown(){
    		turnLeft();
    		turnLeft();
    		while (frontIsClear()){
    			placeBeepers();
    			move();
    		}
    		turnLeft();
    	}
     
     
    	//Place Blocks where not present
    	private void placeBeepers(){
    		if (noBeepersPresent()){
    			putBeeper();
    		}
    	}
     
    	//Front Move Until Hits Wall
    	private void frontMove(){
    		if (frontIsClear()){
    			move();
    			move();
    			move();
    			move();
    		}
    	}
     
    	//	Combined Command for doing work
    	private void doWork(){
    		moveUp();
    		moveDown();
    		frontMove();
    		}
    }
  2. My Version:

    [code]
    /*
    * File: StoneMasonKarel.java
    * ————————–
    * The StoneMasonKarel subclass fills in all missing Beepers in a column,
    * moves to the beginning of the next column if front isn’t blocked and repeats
    * until front is blocked and breaks.
    */

    import stanford.karel.*;

    public class StoneMasonKarel extends SuperKarel {

    public void turnNorth() {
    while(!facingNorth()) {
    turnLeft();
    }
    }

    public void turnSouth() {
    while(!facingSouth()) {
    turnLeft();
    }
    }

    public void turnEast() {
    while(!facingEast()) {
    turnLeft();
    }
    }

    public void goNorthAndFillInBeepers() {
    turnNorth();
    while(true) {
    if(noBeepersPresent()) {
    putBeeper();
    }
    if(frontIsBlocked()) {
    break;
    }
    move();
    }
    }

    public void goSouthAndTurnEast() {
    turnSouth();
    while(frontIsClear()) {
    move();
    }
    turnEast();
    }

    private void goToNextColumn() {
    for(int i=0; i<4; i++) {
    move();
    }
    }

    public void run() {
    while(true) {
    goNorthAndFillInBeepers();
    goSouthAndTurnEast();
    if(frontIsClear()) {
    goToNextColumn();
    } else {
    break;
    }
    }
    }

    }
    [/code]

  3. Mine:

    import stanford.karel.*;
     
    public class StoneMasonKarel extends SuperKarel {
     
    	private static final int COLUMN_DISTANCE = 4;
     
    	public void run() {
    		while(frontIsClear()) {
    			repairColumn();
    			moveToNextColumn();
    		}
    		repairColumn();
    	}
     
    	private void repairColumn() {
    		turnLeft();
    		while(frontIsClear()) {
    			placeBeepers();
    			move();
    		}
    		placeBeepers();
    		turnAround();
    		moveToWall();
    		turnLeft();
    	}
     
    	private void moveToNextColumn() {
    		for(int i = 0; i < COLUMN_DISTANCE; i++) {
    			move();
    		}
    	}
     
    	private void placeBeepers() {
    		if(noBeepersPresent()) {
    			putBeeper();
    		}
    	}
     
    	private void moveToWall() {
    		while(frontIsClear()) {
    			move();
    		}
    	}
    }
  4. My take:

    /*
     * File: StoneMasonKarel.java
     * --------------------------
     *This program used Karel to repair the quad by going through each column
     *and putting beepers at corners where none is present
     */
     
    import stanford.karel.*;
     
    public class StoneMasonKarel extends SuperKarel {
    	/*
    	 * There is always at least one column to repair so Karel starts by repairing the first one.
    	 * If its front is blocked, it means it is at the end of the world so it stops. Else it moves
    	 * to the next column, repairs it and then it keeps repeating the process until its front is blocked
    	 */
    	public void run(){
    			while (true){
    				repairColumn();
    				if(frontIsBlocked()) break;
    				moveToNextColumn();
    			}
    	}
     
    	/*
    	 * pre-condition: Karel is at the bottom of a repaired column facing east
    	 * post-condition: Karel is at the bottom of the next column to be repaired facing east
    	 */
    	private void moveToNextColumn(){
    		for(int i = 0; i &lt; 4; i++)
    			move();
    	}
     
    	/*
    	 * Karel repairs a column by checking and repairing every corner as it moves up the column.
    	 * It then descends when it reaches the top of the column
    	 */
    	private void repairColumn(){
    		turnLeft();
    		while(true){
    			checkAndRepairCorner();
    			if(frontIsBlocked()) break;
    			move();
    		}
    		turnAround();
    		descendColumn();
    		turnLeft();
    	}
     
    	/*
    	 * Karel repairs a corner by putting a beeper in the corner if one is not already present
    	 */
    	private void checkAndRepairCorner(){
    		if(noBeepersPresent())
    			putBeeper();
    	}
     
    	/*
    	 * pre-condition:Karel is at the top of a repaired column facing south
    	 * post-condition: Karel is at the bottom of a repaired column facing south
    	 */
    	private void descendColumn(){
    		while(frontIsClear())
    			move();
    	}
     
    }
  5. There are soo many ways of doing it:) that is the fun part of programming :).. nice one GrandT

  6. /*
     * StoneMasonKarel.java
     * Problem no. 2 - Karel has been hired to repair the damage done to the Quad in the 1989 earthquake in assignment 1
     * By Kuldeep Rathod from Bangalore, India
     * */
    import stanford.karel.*;
     
    public class StoneMasonKarel extends SuperKarel{
     
    	public void run()
    	{
     
    		do
    		{
     
    		if(facingEast())
    			oneastfacing();
     
    		for(int i=0;i&lt;4;i++)
    			move();
     
     
    		if(frontIsBlocked() &amp;&amp; facingEast())
    			oneastfacing();
     
    		}while(frontIsClear());
     
     
     
    	}
     
    	private void dropbeeper()
    	{
    		while(frontIsClear())
    		{
    			if(beepersInBag())
    			{
    				if(beepersPresent())
    				{
    					move();
    				}else
    				{
    					putBeeper();
    					move();
    				}
    			}
    		}
    	}
     
    	private void oneastfacing()
    	{
    		turnLeft();
    		dropbeeper();
    		turnAround();
    		dropbeeper();
    		turnLeft();
     
    	}
     
    }
  7. Hey guys, some of your solutions are good, but Subhadeep Gayen, your method is fine, but there is one slight problem.

    In the task in the handout, it says “Some of the corners in the column may already contain beepers representing stones
    that are still in place. Your program should not put a second beeper on these corners.”

    So we are not allowed to place a second beeper 😛

    My version does not place a second beeper 😀 all you had to do was add a little bit of code!

    if(noBeepersPresent()){	
    					putBeeper();
    					move();
    				}
     
     
     
    ################################################################################
     
     
    /*
     * 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(){
     
    		while(frontIsClear()){
    			repair();
    		}
    	}
     
    		private void moveup() {
    			turnLeft();	
    			while(frontIsClear()){
    				if(noBeepersPresent()){	
    					putBeeper();
    					move();
    				}
    				else{
    					move();
    				}
    			} 
    			turnRight();
    		}
     
    		private void movedown() {
    			turnRight();
    			while(frontIsClear()){
    				if(noBeepersPresent()){
    					putBeeper();
    					move();
    				}
    				else{
    					move();
    					}
    				}
    			turnLeft();
    		}
     
    		private void tocolumn() {
    			if(frontIsClear()){
    			move();
    			move();
    			move();
    			move();
    			}
    			else{
    				//do nothing
    			}
    		}
     
    		private void repair() {
    			moveup();
    			tocolumn();
    			movedown();
    			tocolumn();
    		}
     
     
    }

Leave a Reply

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