Results 1 to 8 of 8
  1. #1
    top7gunn is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default CS106a assignment 3 help

    I have began Stanford's free online CS106a class and am new to programming. I worked through the problem for assignment 3 (to have Karel the robot drop "beepers" forming a checkerboard pattern across the world he is introduced in). We are guaranteed he will start in the bottom left, and that there are no walls in the world, but the code must work in a world of any size. I believe I figured that much out but for some reason, I can not get the program to call my returnToCorner method. This is supposed to return Karel to the top left square and have him facing East. I'm sure it's just a simple change that I can't see because I have been staring at it so long, but any help would be appreciated! P.S. my code is nowhere close to elegant, but I'll get there...

    /*
    * 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();
    while(frontIsClear()){
    while(facingEast()) {
    makeAve();
    if(leftIsClear()) {
    turn();
    }
    }
    while(facingWest()) {
    makeAve();
    if(rightIsClear()) {
    turn();
    }
    }
    }
    returnToCorner();
    }

    private void makeAve() {
    while(frontIsClear()){
    if(beepersPresent()) {
    move();
    if(frontIsClear()){
    move();
    putBeeper();
    }
    }
    if(noBeepersPresent()) {
    if(frontIsClear()) {
    move();
    putBeeper();
    }
    }
    }
    }

    private void turn() {
    if(facingEast()) {
    turnFromEast();
    } else {
    turnFromWest();
    }
    }

    private void turnFromEast() {
    turnLeft();
    if(noBeepersPresent()) {
    if(frontIsClear()) {
    move();
    putBeeper();
    }
    }else {
    move();
    }
    turnLeft();
    }

    private void turnFromWest() {
    turnRight();
    if(noBeepersPresent()) {
    if(frontIsClear()) {
    move();
    putBeeper();
    }
    } else {
    move();
    }
    turnRight();
    }

    private void returnToCorner() {
    if(facingEast()) {
    turnAround();
    while(frontIsClear()) {
    move();
    }
    turnAround();
    }
    if(facingWest()) {
    turnAround();
    }
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: CS106a assignment 3 help

    When posting code, please use the code tags to preserve formatting. This is pretty impossible to read.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    top7gunn is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: CS106a assignment 3 help

    Quote Originally Posted by KevinWorkman View Post
    When posting code, please use the code tags to preserve formatting. This is pretty impossible to read.
    My fault! This is my first time posting on the forums...let me try again.

    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() {
    		putBeeper();
    		while(frontIsClear()){
    			while(facingEast()) {
    				makeAve();
    				if(leftIsClear()) {
    					turn();
    				}
    			}
    			while(facingWest()) {
    				makeAve();
    				if(rightIsClear()) {
    					turn();
    				}
    			}
    		}
    		returnToCorner();
    	}
    	
    	private void makeAve() {
    		while(frontIsClear()){	
    			if(beepersPresent()) {
    				move();
    				if(frontIsClear()){
    					move();
    					putBeeper();
    				}
    			}
    			if(noBeepersPresent()) {
    				if(frontIsClear()) {
    					move();
    					putBeeper();
    				}
    			}
    		}
    	}
    	
    	private void turn() {
    		if(facingEast()) {
    			turnFromEast();
    		} else {	
    			turnFromWest();
    		}
    	}
    	
    	private void turnFromEast() {
    		turnLeft();
    		if(noBeepersPresent()) {
    			if(frontIsClear()) {
    				move();
    				putBeeper();
    			}
    		}else {
    			move();
    		}
    		turnLeft();
    	}
    	
    	private void turnFromWest() {
    		turnRight();
    		if(noBeepersPresent()) {
    			if(frontIsClear()) {
    				move();
    				putBeeper();
    			}
    		} else {
    			move();
    		}
    		turnRight();
    	}
    	
    	private void returnToCorner() {
    		if(facingEast()) {
    			turnAround();
    			while(frontIsClear()) {
    				move();
    			}
    			turnAround();
    		}
    		if(facingWest()) {
    			turnAround();
    		}
    	}
    }
    Is that better?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: CS106a assignment 3 help

    Much better, thanks. But I'm not sure how this code is being run- I don't see a main method.

    The best advice I can give you is to step through this code with a debugger, or at least add a bunch of print statements, to figure out when the code's behavior differs from what you'd expect. That's all we'd be doing, and it's a skill that every programmer uses several times a day.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    top7gunn is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: CS106a assignment 3 help

    Quote Originally Posted by KevinWorkman View Post
    Much better, thanks. But I'm not sure how this code is being run- I don't see a main method.

    The best advice I can give you is to step through this code with a debugger, or at least add a bunch of print statements, to figure out when the code's behavior differs from what you'd expect. That's all we'd be doing, and it's a skill that every programmer uses several times a day.
    Thanks for the help!

    I believe this is the main method.


    Java Code:
    	public void run() {
    		putBeeper();
    		while(frontIsClear()){
    			while(facingEast()) {
    				makeAve();
    				if(leftIsClear()) {
    					turn();
    				}
    			}
    			while(facingWest()) {
    				makeAve();
    				if(rightIsClear()) {
    					turn();
    				}
    			}
    		}
    		returnToCorner();
    	}
    I don't believe there are any bugs because It runs fine. My only concern is that while it is running everything within the run method, it is doing nothing when it gets to 'returnToCorner();' and I'm not sure why. I also do not know how to step through it with a debugger or how to add the print statements. This is not actually being run in Java but instead in a format to get students thinking about ideas and less about the intricacies of Java itself. Do you see any reason it wouldn't be running returnToCorner?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: CS106a assignment 3 help

    It's tough to troubleshoot if you can't add print statements or run it through a debugger. Does it ever exit the while(frontIsClear()) loop? Does it enter the returnToCorner() method at all?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    top7gunn is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: CS106a assignment 3 help

    Quote Originally Posted by KevinWorkman View Post
    It's tough to troubleshoot if you can't add print statements or run it through a debugger. Does it ever exit the while(frontIsClear()) loop? Does it enter the returnToCorner() method at all?
    I'm thinking it isn't ever exiting the while(frontIsClear()) loop. Instead of calling the returnToCorner() method I created I replaced it with turnAround() which is built in and it did not execute that method either. I'm confused though because where 'Karel' is stopped, the front is blocked which would make me think it would exit that loop...

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: CS106a assignment 3 help

    Yeah, I'm not sure what these functions do, so it's conceivable that they have a forever loop or a wait. That's why a debugger or some print statements would come in handy- we have to identify where the code is getting stuck before we can figure out why.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 11
    Last Post: 05-12-2013, 06:20 PM
  2. Stanford CS106a breakout assignment
    By Newbieprogrammer in forum New To Java
    Replies: 3
    Last Post: 08-14-2012, 10:18 AM
  3. CheckerBoardKarel - Stanford CS106A Assignment 1
    By mattdotmac in forum New To Java
    Replies: 6
    Last Post: 07-15-2012, 01:58 PM
  4. StandfordU CS106a free version - assignment 1
    By SaraInMontreal in forum New To Java
    Replies: 5
    Last Post: 02-08-2012, 03:02 PM
  5. Breakout Assignment from CS106a
    By tech8462 in forum New To Java
    Replies: 13
    Last Post: 10-09-2011, 03:38 AM

Posting Permissions

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