Results 1 to 7 of 7
- 07-08-2012, 11:28 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
CheckerBoardKarel - Stanford CS106A Assignment 1
Hi,
I recently started following the Programming methodology course on iTunesU, and I'm stuck on the 3rd part (CheckerboardKarel) of Assignment 1. The objective is to get Karel to create a checkerboard pattern of beepers inside an empty rectangular world. When I run it, all goes to plan until 3 rows are filled, then it stops. I've tried putting a whileFrontIsClear in various places, but to no avail. Below is the code. Any help is much appreciated :)
Matt
__________
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() { putInitialRow() ; determineDirection() ; } /* * Puts down the first row of checkers. Post condition is the most easterly column in row 1, facing east. */ private void putInitialRow() { putBeeper() ; if (frontIsBlocked()) { turnLeft() ; } while (frontIsClear()) { move() ; if (frontIsClear()) { move() ; putBeeper() ; } } } /* * Determines which direction Karel is facing in order to choose the correct direction to continue. */ private void determineDirection() { if (facingEast()) { if (beepersPresent()) { turnLeft() ; move() ; turnLeft() ; putOddRowWest() ; } else { turnLeft() ; move() ; turnLeft() ; putEvenRowWest() ; } } if (facingWest()) { if (beepersPresent()) { turnRight() ; move() ; turnRight() ; putOddRowEast() ; } else { turnRight() ; move() ; turnRight() ; putEvenRowEast() ; } } } private void putOddRowWest() { /* Puts a row of checkers to the west that begins with a blank because there is a beeper directly to the south.*/ if (frontIsBlocked()) { determineDirection() ; } while (frontIsClear()) { move() ; putBeeper() ; if (frontIsClear()) { move() ; } } } private void putEvenRowWest() { /* Puts a row of checkers to the west that begins with a beeper because there is no beeper directly to the south.*/ putBeeper() ; if (frontIsBlocked()) { determineDirection() ; } while (frontIsClear()) { move() ; if (frontIsClear()) { move() ; putBeeper() ; } } } private void putOddRowEast() { /* Puts a row of checkers to the east that begins with a blank because there is a beeper directly to the south.*/ if (frontIsBlocked()) { determineDirection() ; } while (frontIsClear()) { move() ; putBeeper() ; if (frontIsClear()) { move() ; } } } private void putEvenRowEast() { /* Puts a row of checkers to the east that begins with a beeper because there is no beeper directly to the south.*/ putBeeper() ; if (frontIsBlocked()) { determineDirection() ; } while (frontIsClear()) { move() ; if (frontIsClear()) { move() ; putBeeper() ; } } } }
Last edited by mattdotmac; 07-09-2012 at 02:55 AM. Reason: added code tags
- 07-09-2012, 01:26 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 11
Re: CheckerBoardKarel - Stanford CS106A Assignment 1
Originally Posted by wikipedia.org
Java Code:while(frontIsClear()) { move(); if(frontIsClear()) { move(); putBeeper(); } }
- 07-09-2012, 03:09 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: CheckerBoardKarel - Stanford CS106A Assignment 1
@cselic: Sorry I wasn't very clear in my explaining :P
The task is for karel to put the beepers down so that they form this checkered pattern:
This is what the program looks like before it is run:
And this is what it looks like when it stops working:
It is written to work for rectangular boxes of any size.
Thanks again,
Matt
- 07-09-2012, 03:40 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 11
Re: CheckerBoardKarel - Stanford CS106A Assignment 1
^I think that your code in this class is good, but what about code in SuperKarel class? Can you give us code of this class, or at least some part.
Do you have in your SuperKarel class data that sets dimesions of the table?Last edited by cselic; 07-09-2012 at 03:44 AM.
- 07-15-2012, 10:34 AM #5
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Re: CheckerBoardKarel - Stanford CS106A Assignment 1
It is clear from your code that you have no loop present, so it runs through the program once and stops. In your initial Run statement, you only have 2 lines of code:
putInitialRow() ;
determineDirection() ;
Then, you are calling up subroutines within your subroutines.
1) This is bad practice at this point because the program gets too confusing. You should basically have one program that runs at the highest level and calls single components. When possible do not allow these components to call other components because it will get confusing quickly.
2) There is no loop. You need some way of running highest portion of the program (above) more than once so that you get multiple lines of beepers. In addition, you have to be careful not to run into an error where you try to walk through a wall.
Good luck.
- 07-15-2012, 02:17 PM #6
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: CheckerBoardKarel - Stanford CS106A Assignment 1
Thank you for this, your advice is really helpful. I'll have another go.
Matt
- 07-15-2012, 02:58 PM #7
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
Re: CheckerBoardKarel - Stanford CS106A Assignment 1
Well, I am just watching lecture 3 now and it seems that you can have as many of these decompositions as you like. So, I guess that is not a big deal (my point 1 above). I wrote this same program for the course, which I am also entertaining myself with at the moment. The loop problem is still there, though (point 2). Good luck with it! I think the checkerboard was hardest for me as well.
Similar Threads
-
Stanford cs106a
By D.good in forum IntroductionsReplies: 1Last Post: 02-04-2012, 07:18 PM -
CS106A Stanford University
By Learning Java in forum New To JavaReplies: 116Last Post: 07-09-2011, 05:43 PM -
Stanford CS106A, my work- your thoughts?
By chmza in forum New To JavaReplies: 2Last Post: 12-02-2010, 06:53 PM -
BigInteger Rational (CS106A-Stanford university)
By ccie007 in forum New To JavaReplies: 16Last Post: 10-01-2010, 08:16 PM -
Class exercise CS106A (Stanford university)
By ccie007 in forum New To JavaReplies: 2Last Post: 09-11-2010, 02:47 AM
Bookmarks