# Stanford CS106A, my work- your thoughts?

• 12-02-2010, 05:21 AM
chmza
Stanford CS106A, my work- your thoughts?
For anyone who is familiar with Karel and could give me some feedback on my coding I would be very grateful, I'm very new to programming so I could use any advice I can get.

I started watching the lectures and doing the reading for this course online and I'm doing the first assignment right now. The first two problems were fairly simple but the third one took a bit of thought. The problem calls for writing a program that will leave a checkerboard pattern of beepers on any sized board, starting from 1st ave and 1st st. There are no walls within the board and the program has to work on both a single row, and single column.

Here's a link to the assignment, scroll down to problem 3:
http://www.stanford.edu/class/cs106a/handouts/07-assignment-1.pdf

This is my solution and it works on all the boards that are provided to test on.
Does this look fairly efficient? What could be improved?

Code:

```/*  * File: CheckerboardKarel.java  * ----------------------------  * CheckerboardKarel will draw a checkerboard pattern with beepers on  * any given size open rectangle or square board when starting at  * 1st avenue and 1st street.  */ import stanford.karel.*; public class CheckerboardKarel extends SuperKarel {                 // Main routine.         public void run() {                 turnLeft();                 while(facingNorth()) {                         dropRowA();                         if(facingNorth()) {                                 dropRowB();                         }                 }         }                 // dropRowA method draws rows with a beeper on the first avenue corners         private void dropRowA() {                 turnRight();                 dropChipsA();                 turnAround();                 comeBack();                 turnRight();                 moveUp();         }                 // dropRowB method creates rows with no beeper on the first avenue corners         private void dropRowB() {                 turnRight();                 dropChipsB();                 turnAround();                 comeBack();                 turnRight();                 moveUp();         }                 // the dropChipsA method places an initial chip down then will move two         // spaces if they are open and then drop a beeper.  If Karel encounters         // a wall the method ends and the dropRowA method continues.         private void dropChipsA() {                 putBeeper();                 while(frontIsClear()) {                         move();                         if(frontIsClear()) {                                 move();                                 putBeeper();                         }                 }         }                 // the dropChipsB method moves one space if it is open, drops a beeper         // and then moves one more time if the space is open.  Like dropChipsA,         // this method will end if Karel encounters a wall and dropRowB will         // continue.         private void dropChipsB() {                 while(frontIsClear()) {                         move();                         putBeeper();                         if(frontIsClear()) {                                 move();                         }                 }         }                 // comeBack is the method to return back to it's rows' first avenue corner.         private void comeBack() {                 while(frontIsClear()) {                         move();                 }         }                 // moveUp is the final method in both dropRowA and dropRowB. Karel checks to         // see if the space in front of him (to the north) is open.  If the space is         // open Karel will move and drop the next row of chips.  If the row above         // Karel is closed, he will turn East and end the loop, stopping the program.         private void moveUp() {                 if(frontIsClear()) {                         move();                 } else {                         turnRight();                 }         } }```
• 12-02-2010, 04:38 PM
erversteeg
I am not familiar with Karel, but I get the gist of it by reading the assignment. Your code looks great! My prof always says that good code should read like a story and yours does because of excellent method names. Nice work!
• 12-02-2010, 06:53 PM
chmza
Thanks for your input! The one thing I just learned that I should have added into my comments is pre and post conditions of each methods. I'll be sure to add those in for future work.