Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Stanford's - Karel The Robot & Checkerboard Problem

843789Jul 17 2009 — edited Jul 23 2009
I am doing the free online class from Stanford CS106A. As you may know in assignment 1 there is the task of causing Karel to place 'beepers' across a world in a checkerboard pattern. It should run in any size world. Here is the link: [Stanford CS 106A|http://www.stanford.edu/class/cs106a/] ... look at Assignment 1, problem 3 (if you are interested).

Mine runs fine in any world with even numbered columns. However I just can't get it to run successfully in odd numbered column worlds.

I am required to only use a few commands available to the stanford.superkarel class. They are: move, turnRight, turnLeft, put & pick (up) beebers, turnAround. I have a number of conditions available to me as well. I can detect if my way is clear to the front, left & right, if there are beepers present or not, and which way I am facing. In this situation karel has an infintate number of 'beepers'. Of course I can use if, else, for, and, or & while statements. Anything beyond this must be my created methods from these commands and conditions.

My basic algorithm is: move along a row (called moveToWall: move, putBeeper, move & move) then turn up into next row. I turn him around one more time to stop him at end.

My error (I suspect is in the way I am thinking of my moveToWall statement) occurs when he can't make that second move (which occurs when there are an odd number of columns). The error is: Karel simply stops at end of first row with most of my 'fixes' or he runs in circles completing row 1, doing row two and then dropping down to row 1 again.

In short I am currently unable to make this work if a given row has a length with an odd number of positions (columns) in it.

So... My question is: am I on the right track... trying to fix my moveToWall statement to account for inability to move a second time? Or should I look elsewhere, more generally at my algorithm? Don't fix it for me... hints only please. Thanks in advance for your time.

Ok... THAT said... here is my 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.*;

/*
 * Name: Robert Elder
 * Section Leader: None
 * Pre-condition: no beepers in world, unknown demensions.
 * Post-condition: alternating beepers, stopping (by turning around)at top row.
 * Unfortunately so far... this only works in Worlds with even number of 
 * columns. Hmmmm need to work on that.
 */

public class CheckerboardKarel extends SuperKarel {

	public void run(){
			moveToWall();
			goUpOneRow();
			}

	/*
	 * moveToWall = move one space, then only if front is clear put a beeper
	 * and move 2 times.  Moving 2 times allows for an every other space placement
	 * of beepers.
	 */
	private void moveToWall() {
		while(frontIsClear()){
			move();
			while(frontIsClear()){
				putBeeper();
				move();
				move();
				}
			putBeeper();
			}
		}
	/*
	 * Move Karel up one row and turns him to face opposite direction
	 * and moves him down the row placing his 'beepers'
	 * This may be a place to fix my odd column problem too, but I tried with a
	 * while (frontIsClear()) condition ahead of the while (leftIsClear()) condition
	 * to no avail.  I get confused about just how local some of my statements
	 * need to be.
	 */
private void goUpOneRow() {
		while (leftIsClear()){
			repositionForRowToWest();
			moveToWall();
		if (rightIsClear()){
			repositionForRowToEast();
			moveToWall();
			}
		else {
			turnAround();
			}
		}		
	}
/*
 * repositionForRowtoEast/West = to move up a row and turn around to moveToWall
 * again... 
 */
	private void repositionForRowToEast() {
		turnRight();
		move();
		turnRight();
	}
	private void repositionForRowToWest() {
		turnLeft();
		move();
		turnLeft();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 20 2009
Added on Jul 17 2009
15 comments
4,091 views