Jump to content

Review my code please

- - - - -

  • Please log in to reply
No replies to this topic

#1
chmza

chmza

    Newbie

  • Members
  • Pip
  • 5 posts
Hello!

I'm new to this forum as I am just starting to learn my first programming language, Java. I'm following the Stanford Programming methodology course CS106A which I'm sure many of you are aware of.

I plan on submitting my answers to the assignments here for peer review to make sure I'm on the right path. Is this the correct forum to do it in? If it's not please let me know and I will post in the appropriate place next time.

Anyways, onto my first bit of code.

This is for Problem #3 of assignment #1:

"In this exercise, your job is to get Karel to create a checkerboard pattern of beepers inside
an empty rectangular world, as illustrated in the following before-and-after diagram:


This problem has a nice decomposition structure along with some interesting algorithmic
issues. As you think about how you will solve the problem, you should make sure that
your solution works with checkerboards that are different in size from the standard 8x8
checkerboard shown in the example. Odd-sized checkerboards are tricky, and you should
make sure that your program generates the following pattern in a 5x3 world:

Another special case you need to consider is that of a world which is only one column
wide or one row high. The starter folder contains several sample worlds that test these
special cases, and you should make sure that your program works for each of them."


I solved the problem with the code below, I just want to get your opinions on how I did with my decomposition / any other thoughts.

* 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 {


	/* This program lays down one initial row of checkers and then checks

	 * the final square in the row for a beeper to determine if there is 

	 * an even or odd ammount of columns on the board.  It then runs the 

	 * corresponding program from odd or even. Pre-Conditions are that Karel

	 * needs to be in the bottom left corner of the board.  Post-Conditions

	 * need to have karel in the far right square of the board facing East

	 * @see stanford.karel.SuperKarel#run()

	 */

	

	public void run() {

		layRowA();

		if (beepersPresent()) {

			runOddColumnsBot();

		} else {

			runEvenColumnsBot();}

		}

	

	

	

	/*This lays a row of checkers starting with a checker on the first

	 * square.  Pre-Conditions are that Karel is on a blank row facing 

	 * down the row, away from the wall. Post-Conditions are that Karel

	 * is at the end of a row that has already been checkered, facing the 

	 * wall either to the East or West. */

	

	private void layRowA() {

		putBeeper();

		while (frontIsClear()) {

			move();

			if (frontIsClear()) {

				move();

				putBeeper();

			}

		}

	}

	

	

	/*this is the method that is run if there is an odd number of columns

	 * on the checkerboard. Pre-Conditions are that Karel is on the far East

	 * end of the checkerboard, facing the East wall on a row of checkers he's

	 * just placed.  Post-Conditions are that Karel is facing East on either 

	 * the East or West side of a row.

	 */

	private void runOddColumnsBot() {

		while (leftIsClear()) {

			turnAndFaceWest();

			layRowB();

			if (rightIsClear()) {

				turnAndFaceEast();

				layRowA();

			} else {

				turnAround();

			}

		}

	}

	

	/*This lays a row of checkers starting without a checker on the first

	 * square.  Pre-Conditions are that Karel is on a new row, facing either

	 * East or West but away from the wall.  Post-Conditions will have Karel 

	 * at the end of a row he just laid checkers on, faced against either the

	 * East or West wall.

	 */

	private void layRowB() {

		while (frontIsClear()) {

			move();

			putBeeper();

			if (frontIsClear()) {

				move();

			}

		}

	

	}


	/* This method makes Karel move up one row and face the west.  Pre-Conditions

	 * are that Karel must be on the eastern most side of a row facing the wall.

	 * Post-Conditions have Karel one spot above where he started facing East.

	 */

	private void turnAndFaceWest() {

		turnLeft();

		move();

		turnLeft();

	}

	

	

	/*This method makes Karel move up one row and face the east.  Pre-Conditions

	 * are that Karel must be on the western most side of a row facing the wall.

	 * Post-Conditions have Karel one spot above where he started facing West.

	 */

	private void turnAndFaceEast() {

		turnRight();

		move();

		turnRight();

	}

	

	

	/*this is the method that is run if there is an even number of columns

	 * on the checkerboard. Pre-Conditions are that Karel is on the far East

	 * end of the checkerboard, facing the East wall on a row of checkers he's

	 * just placed.  Post-Conditions are that Karel is facing East on either 

	 * the East or West side of a row.

	 */

	private void runEvenColumnsBot() {

		while (leftIsClear()) {

			turnAndFaceWest();

			layRowA();

			if (rightIsClear()) {

				turnAndFaceEast();

				layRowA();

			} else {

				turnAround();

			}

		}

	}

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users