Results 1 to 2 of 2
  1. #1
    Drewbiedoo is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default CheckerboardKarel issues

    Hi all,

    I'm working through Stanford's online Programming Methodologies class. I'm stuck on the 3rd part of assignment 1:
    CheckerboardKarel issues-checkerboardkarel.png

    My code works great for any world with an even number of columns (8x8, 8x1, 40x40), but when I run it in a 7x7 world, Karel makes a wrong left turn and doubles the beepers on the bottom row before hitting a wall:
    CheckerboardKarel issues-screenshot.png

    Here's my code - any suggestions (regarding my problem or coding technique) are very welcome:

    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 {
    
    /*
     * Precondition: Karel is at SW corner of a blank board
     * Postcondition: The board has beepers set in a checkerboard configuration
     */
    	public void run() {
    		oddRow();
    		while (leftIsClear()) {
    			moveToEven();
    			evenRow();
    			if (rightIsClear()) {
    				moveToOdd();
    				oddRow();
    			} else {
    				turnAround();
    			}
    		}
    	}
    	
    /* Method to place alternating beepers on odd rows */
    	private void oddRow() {
    		putBeeper();
    		if (frontIsClear()) {
    			move();
    		}
    		while (frontIsClear()) {
    			move();
    			putBeeper();
    			if (frontIsClear()) {
    			move();
    			} else {
    				moveToEven();
    			}
    		}
    	}
    	
    /* Method to place alternating beepers on even rows */
    	private void evenRow() {
    		if (frontIsBlocked()) {
    			moveToOdd();
    		} else {
    			putBeeper();
    			move();
    		}
    		while (frontIsClear()) {
    			move();
    			putBeeper();
    			if (frontIsClear()) {
    			move();
    			} else {
    				moveToOdd();
    			}
    		}
    	}
    	
    /* Method to move Karel from odd row to even row */
    	private void moveToEven() {
    		turnLeft();
    		move();
    		turnLeft();
    	}
    
    /* Method to move Karel from even row to odd row */
    	private void moveToOdd() {
    		turnRight();
    		move();
    		turnRight();
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: CheckerboardKarel issues

    You have one problem to solve: make your robot move over its entire 'world'; at each square it arrives (at position (r,c)) drop a 'beeper' there if r+c is even.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. CheckerBoardKarel - Stanford CS106A Assignment 1
    By mattdotmac in forum New To Java
    Replies: 6
    Last Post: 07-15-2012, 01:58 PM
  2. GUI Issues
    By larry_d1990 in forum Advanced Java
    Replies: 2
    Last Post: 03-17-2011, 02:35 PM
  3. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  4. JFrame issues
    By ishdevil in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 08:26 AM
  5. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 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
  •