GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Oct-2008, 23:55
nøøbsåibo† nøøbsåibo† is offline
New Member
 
Join Date: Oct 2008
Posts: 1
nøøbsåibo† is on a distinguished road
Unhappy

Karel: draw a checkerboard using beepers


I am doing an online course through Stanford school of Engineering, and its free. Feel free to check it out, its high quality. Anyway the assignment is to make Karel the robot, which is a program in Java that can perform a small set of actions, make an empty matrix into a checkerboard. I have "beepers" which are grey diamonds, and I must put no more than one beeper on every other square. The X axis is "avenues" 1st avenue, second avenue, etc., and the Y axis is "streets". Karel can move(); turnLeft(); turnRight(); turnAround(); putBeeper(); and pickBeeper();
So far I get him/her to make the checkerboard, but when it is completed, Karel continues to move around the matrix, putting more beepers ontop of one another, resulting in an infinite loop. How do I get Karel to stop when he/she is finished at the top left of the screen?
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 {

	/* pre-condition: Karel is at the bottom left of an empty matrix
	 * post-condition: the screen has one single beeper on every other
	 * space, on odd numbered avenues for odd numbered streets,
	 * and beepers on even numbered avenues for even numbered streets.
	 */ 
	
	public void run()  {
		makeOddRow();
		makeEvenRow();
		
	}

	private void makeOddRow()  {
		putBeeper();
		while (frontIsClear())  {
			move();
			while (frontIsClear())  {
				move();
				makeOddRow();
			}
			if (leftIsClear())  {
				turnLeft();
				move();
				turnLeft();
				makeEvenRow();
			}
		
		}
		 
	}
	
	private void makeEvenRow()  {
		putBeeper();
		while (frontIsClear())  {
			move();
			while (frontIsClear())  {
				move();
				makeEvenRow();
			}
		}
		turnRight();
		if (frontIsClear())  {
			move();
			turnRight();
			makeOddRow();
		}
		
	}
	
}




and this the description of the world Karel occupies:
Dimension: 8, 8
BeeperBag: INFINITY
Karel: (1, 1) East
Speed: 0.75
  #2  
Old 07-Dec-2008, 16:42
drnelson77 drnelson77 is offline
New Member
 
Join Date: Dec 2008
Posts: 1
drnelson77 is on a distinguished road

Re: Karel: draw a checkerboard using beepers


Hello,

I got Karel to stop after a couple runs around but couldn't get it to stop in the top left corner on his first run...

I used the code you created, (which was admirable by the way) and added a couple lines down at the bottom.

JAVA Code:
//checker karel...

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel 

{
	public void run()  {
		makeOddRow();
		makeEvenRow();
		
		
	}

	private void makeOddRow()  {
		putBeeper();
		while (frontIsClear())  {
			move();
			while (frontIsClear())  {
				move();
				makeOddRow();
			}
			if (leftIsClear())  {
				turnLeft();
				move();
				turnLeft();
				makeEvenRow();
			}
		
		}
		
	}
	
	private void makeEvenRow()  {
		putBeeper();
		while (frontIsClear())  {
			move();
			while (frontIsClear())  {
				move();
				makeEvenRow();
			}
			
		}
	turnRight();
	if (frontIsClear())  {
		move();
		turnRight();
		makeOddRow();
		while (facingNorth()){
			if (frontIsBlocked()){
				stopKarel();
			}
		}
		
		}
	}

private void stopKarel(){
	while (frontIsClear()){
		move();}
}}
Last edited by admin : 07-Dec-2008 at 21:33. Reason: Please insert your example Java codes between [JAVA] and [/JAVA] tags
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Barcode reader code problem copa_21 C Programming Language 5 13-Oct-2006 00:40
problem with code arikeri C++ Forum 3 06-Mar-2005 00:22
Insert problem in linked list with two function code Kay Chan C++ Forum 1 03-Sep-2004 09:52
problem with C code belludandy C Programming Language 1 26-Jan-2004 21:28
Code problem (a newbie question) monkster87 C++ Forum 3 11-Aug-2003 12:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:02.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.