Results 1 to 7 of 7
  1. #1
    mattdotmac is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default CheckerBoardKarel - Stanford CS106A Assignment 1

    Hi,

    I recently started following the Programming methodology course on iTunesU, and I'm stuck on the 3rd part (CheckerboardKarel) of Assignment 1. The objective is to get Karel to create a checkerboard pattern of beepers inside an empty rectangular world. When I run it, all goes to plan until 3 rows are filled, then it stops. I've tried putting a whileFrontIsClear in various places, but to no avail. Below is the code. Any help is much appreciated :)

    Matt
    __________

    Java Code:
    /*
     * 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() {
    		
    		putInitialRow() ;
    		
    		determineDirection() ;
    				
    	}
    		
    	
    
    	/*
    	 * Puts down the first row of checkers. Post condition is the most easterly column in row 1, facing east.
    	 */
    	private void putInitialRow() {
    		putBeeper() ;
    		if (frontIsBlocked()) {
    			turnLeft() ;
    		}
    		while (frontIsClear()) {
    			move() ;
    			if (frontIsClear()) {
    				move() ;
    				putBeeper() ;
    			}
    		}
    			
    	}
    
    	/*
    	 * Determines which direction Karel is facing in order to choose the correct direction to continue.
    	 */
    	private void determineDirection() {
    		if (facingEast()) {
    			if (beepersPresent()) {
    				turnLeft() ;
    				move() ;
    				turnLeft() ;
    				putOddRowWest() ;
    				
    		
    			} else { 
    				turnLeft() ;
    				move() ;
    				turnLeft() ;
    				putEvenRowWest() ;
    				
    			}
    		}	
    		if (facingWest()) {
    			if (beepersPresent()) {
    				turnRight() ;
    				move() ;
    				turnRight() ;
    				putOddRowEast() ;
    			
    			} else {
    				turnRight() ;
    				move() ;
    				turnRight() ;
    				putEvenRowEast() ;
    			}	
    		}
    			
    	}
    	
    
    	private void putOddRowWest() {
    	/* Puts a row of checkers to the west that begins with a blank because there is a beeper directly to the south.*/	
    		if (frontIsBlocked()) {
    			determineDirection() ;
    		}	
    		while (frontIsClear()) {
    			move() ;
    			putBeeper() ;
    			if (frontIsClear()) {
    				move() ;
    			}	
    		}
    		
    	}
    
    	private void putEvenRowWest() {
    	/* Puts a row of checkers to the west that begins with a beeper because there is no beeper directly to the south.*/
    		putBeeper() ;
    		if (frontIsBlocked()) {
    			determineDirection() ;
    		}
    		while (frontIsClear()) {
    			move() ;
    			if (frontIsClear()) {
    				move() ;
    				putBeeper() ;
    			}
    		}
    		
    	}
    
    	private void putOddRowEast() {
    	/* Puts a row of checkers to the east that begins with a blank because there is a beeper directly to the south.*/
    		if (frontIsBlocked()) {
    			determineDirection() ;
    		}	
    		while (frontIsClear()) {
    			move() ;
    			putBeeper() ;
    			if (frontIsClear()) {
    				move() ;
    			}	
    		}
    	}
    
    	private void putEvenRowEast() {
    	/* Puts a row of checkers to the east that begins with a beeper because there is no beeper directly to the south.*/
    		putBeeper() ;
    		if (frontIsBlocked()) {
    			determineDirection() ;
    		}
    		while (frontIsClear()) {
    			move() ;
    			if (frontIsClear()) {
    				move() ;
    				putBeeper() ;
    			}
    		}
    			
    	}
    
    
    
    }
    Last edited by mattdotmac; 07-09-2012 at 02:55 AM. Reason: added code tags

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: CheckerBoardKarel - Stanford CS106A Assignment 1

    Quote Originally Posted by wikipedia.org
    karel is an educational programming language for beginners, created by richard e. Pattis in his book karel the robot: A gentle introduction to the art of programming. Pattis used the language in his courses at stanford university, california. The language is named after karel Čapek, a czech writer who introduced the word robot.

    a program in karel is used to control a simple robot named karel that lives in an environment consisting of a grid of streets (left-right) and avenues (up-down). Karel understands five basic instructions: Move (karel moves by one square in the direction he is facing), turnleft (karel turns 90 left), putbeeper (karel puts a beeper on the square he is standing at), pickbeeper (karel lifts a beeper off the square he is standing at), and turnoff (karel switches himself off, the program ends). Karel can also perform boolean queries about his immediate environment, asking whether there is a beeper where he is standing, whether there are barriers next to him, and about the direction he is facing. A programmer can create additional instructions by defining them in terms of the five basic instructions, and by using conditional control flow statements if and while with environment queries, and by using the iterate construct.
    @Matt: "karel moves by one square in the direction he is facing", but in your code you have situations that Karel sometimes moves by two squares, and put a beeper but if Karel moves by two squares he should put two beepers:

    Java Code:
    		while(frontIsClear()) {
    			move();
    			if(frontIsClear()) {
    				move();
    				putBeeper();
    			}
    		}
    Please put your code in code tags: [code] [forward_slash_character code]

  3. #3
    mattdotmac is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: CheckerBoardKarel - Stanford CS106A Assignment 1

    @cselic: Sorry I wasn't very clear in my explaining :P

    The task is for karel to put the beepers down so that they form this checkered pattern:

    CheckerBoardKarel - Stanford CS106A Assignment 1-images.jpeg

    This is what the program looks like before it is run:

    CheckerBoardKarel - Stanford CS106A Assignment 1-screen-shot-2012-07-09-00.59.50.png

    And this is what it looks like when it stops working:

    CheckerBoardKarel - Stanford CS106A Assignment 1-screen-shot-2012-07-09-00.59.27.png


    It is written to work for rectangular boxes of any size.

    Thanks again,

    Matt

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: CheckerBoardKarel - Stanford CS106A Assignment 1

    ^I think that your code in this class is good, but what about code in SuperKarel class? Can you give us code of this class, or at least some part.

    Do you have in your SuperKarel class data that sets dimesions of the table?
    Last edited by cselic; 07-09-2012 at 03:44 AM.

  5. #5
    psprof is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: CheckerBoardKarel - Stanford CS106A Assignment 1

    It is clear from your code that you have no loop present, so it runs through the program once and stops. In your initial Run statement, you only have 2 lines of code:

    putInitialRow() ;

    determineDirection() ;

    Then, you are calling up subroutines within your subroutines.
    1) This is bad practice at this point because the program gets too confusing. You should basically have one program that runs at the highest level and calls single components. When possible do not allow these components to call other components because it will get confusing quickly.
    2) There is no loop. You need some way of running highest portion of the program (above) more than once so that you get multiple lines of beepers. In addition, you have to be careful not to run into an error where you try to walk through a wall.

    Good luck.

  6. #6
    mattdotmac is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: CheckerBoardKarel - Stanford CS106A Assignment 1

    Thank you for this, your advice is really helpful. I'll have another go.

    Matt

  7. #7
    psprof is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: CheckerBoardKarel - Stanford CS106A Assignment 1

    Well, I am just watching lecture 3 now and it seems that you can have as many of these decompositions as you like. So, I guess that is not a big deal (my point 1 above). I wrote this same program for the course, which I am also entertaining myself with at the moment. The loop problem is still there, though (point 2). Good luck with it! I think the checkerboard was hardest for me as well.

Similar Threads

  1. Stanford cs106a
    By D.good in forum Introductions
    Replies: 1
    Last Post: 02-04-2012, 07:18 PM
  2. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 05:43 PM
  3. Stanford CS106A, my work- your thoughts?
    By chmza in forum New To Java
    Replies: 2
    Last Post: 12-02-2010, 06:53 PM
  4. BigInteger Rational (CS106A-Stanford university)
    By ccie007 in forum New To Java
    Replies: 16
    Last Post: 10-01-2010, 08:16 PM
  5. Class exercise CS106A (Stanford university)
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 09-11-2010, 02:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •