Results 1 to 6 of 6
  1. #1
    stormed is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Question Karel- debugging a problem and can't solve it o.o

    Hi I've been using the karel "spin-off" language of java as a precursor to learning real java and I faced a problem that I cannot solve.

    The question can be seen here as problem 3 on page 4 out of 5. What occurs is Karel is able to complete the pattern up to the third row but once she moves up to the next row she continues to move straight as if it skipped specific lines of code.

    The code is below.
    Java Code:
    import stanford.karel.*;
    
    public class CheckerboardKarel extends SuperKarel {
    
    public void run() {
    turnRight();
    if (facingSouth()) {
    	turnLeft();
    	while (frontIsClear()) {
    	putBeeper();
    	move();
    		if (frontIsClear()) {
    			move(); 
    			while (frontIsBlocked()) {
    				while (noBeepersPresent()) {
    				putBeeper();
    				gobackHome();
    					
    				} }
    		}
    		gobackHome();
    			if (frontIsBlocked()) {
    				if (facingWest()) {
    					nextRowA(); } }
    invertedChecker();	
    gobackHome();
    	if (frontIsBlocked()) {
    		if (facingWest()) {
    			nextRowB(); }
    noninvertedChecker();
    }
    invertedChecker();
    }
    	}
    	}
    private void gobackHome() {
    if (frontIsBlocked()) {
    	turnAround();
    		while (frontIsClear()) {
    		move(); }
    			}
    }
    private void invertedChecker() {
    if (facingNorth()) {
    	turnRight();
    	while (frontIsClear()) {
    		move();
    		putBeeper();
    			if (frontIsClear()) {
    				move(); 
    				while (frontIsBlocked()) {
    					while (noBeepersPresent()) {
    					putBeeper(); }
    gobackHome();
    	if (frontIsBlocked()) {
    		if (facingWest()) {
    			nextRowB();
    		}
    	}
    					
    				}
    			}
    	}
    }
    }
    private void nextRowA() {
    turnRight();
    move();
    }
    private void nextRowB() {
    turnRight();
    move();
    turnAround();
    }
    
    private void noninvertedChecker() {
    if (facingSouth()) {
    	turnLeft();
    	while (frontIsClear()) {
    	putBeeper();
    	move();
    		if (frontIsClear()) {
    			move(); 
    			while (frontIsBlocked()) {
    				while (noBeepersPresent()) {
    				putBeeper();
    				gobackHome();
    					
    				} }
    		}
    		gobackHome();
    			if (frontIsBlocked()) {
    				if (facingWest()) {
    					nextRowA(); } }
    	}
    }
    	}
    
    }
    Last edited by stormed; 07-01-2012 at 02:04 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Karel- debugging a problem and can't solve it o.o

    This isn't really Java, so we might not be able to help. We certainly can't help without seeing what your error messages are.

  3. #3
    stormed is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: Karel- debugging a problem and can't solve it o.o

    Quote Originally Posted by Fubarable View Post
    This isn't really Java, so we might not be able to help. We certainly can't help without seeing what your error messages are.
    Hm yes I thought so, however this is from the coding methodology class of Stanford (course notes offered online and I'm using those). course code being CS106A, I've seen threads in the past on this board relating to it.

    No there are no error messages whatsoever, it is just as I explained in the original post. Karel continues a pattern as seen in the problem example (within the link) that I coded myself up until the third row however the fourth row it just goes north and continues to lay beepers in the wrong direction. I'm very new to coding so sorry if it seems disorganized, I don't usually provide descriptions for different methods until I'm finished the actual coding.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Karel- debugging a problem and can't solve it o.o

    A couple of things may help: If there is public documentation for the Karel and SuperKarel classes, post a link. (And if there isn't, tell your teacher there should be.)

    Secondly, please edit your post and correct the indentation. Generally I'm pretty tolerant of formatting (even when people, inexplicably, don't do things the same way I do) but your placement of the closing braces is crazy. Put them each on their own line, lining up directly below the opening brace they match. This isn't a trivial matter: from what you describe you are dealing with some sort of logic error and the nesting of the code matters a lot.

  5. #5
    maslow is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Re: Karel- debugging a problem and can't solve it o.o

    pbrockway2: There is documentation on Karel (i.e., Karel the Robot Learns Java by Eric Roberts), it can be found here (PDF)

    stormed: While it may not be as helpful to you, you may want to thing in bits at first (i.e., just get Karel to put a beeper down and then move)

    Java Code:
     // Programmer notes/definitions
    // putBeeper - Karel puts a beeper down
    // beepersInBag - Karel checks to see if he has any beepers in his bag
    // facingEast - Karel determines his direction
    // frontIsClear - Karel checks to see if anything is blocking his way
    // forwardHo - Karel checks to see if anything is blocking his way, if nothing is he continues moving
    
    while (beepersInBag() { 
    while (facingEast() { 
    while (frontIsClear() {
    	putBeeper(); 
    	forwardHo(); 
    	forwardHo();
    		}
    	}
    }
    
    private void forwardHo () {
    	if (frontIsClear()) {
    		move();
    	}
    }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Karel- debugging a problem and can't solve it o.o

    @maslow, thanks.

Similar Threads

  1. cant solve my problem .needs some help(C++):(y):
    By seongchog in forum New To Java
    Replies: 2
    Last Post: 12-03-2011, 09:49 PM
  2. Solve JVM Problem
    By pedroffbarbosa in forum Advanced Java
    Replies: 8
    Last Post: 11-29-2011, 12:35 AM
  3. Problem getting karel to turn north? he turns back east?
    By jmorgangrice in forum New To Java
    Replies: 6
    Last Post: 07-24-2011, 09:18 PM
  4. Help me to solve problem
    By mansoorhacker in forum Forum Guides
    Replies: 8
    Last Post: 01-24-2009, 07:29 PM
  5. Help me to solve problem
    By mansoorhacker in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 09:15 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
  •