CS106A Assignment 1

The Stanford Honor Code requires that students do not share code or copy code or use code without citing it. With that in mind, here’s the code from the very first program of the course.  I’m not fixing the formatting.

For the remaining programs, I’m only going to document my troubles and tribulations.  What I might do is insert the final program as a java applet.  However, unless I can obfuscate the code so you cannot copy it, that’s not going to happen.

Problem 1: Collect Newspaper

/*
 * File: CollectNewspaperKarel.java
 * ——————————–
 * At present, the CollectNewspaperKarel subclass does nothing.
 * Your job in the assignment is to add the necessary code to
 * instruct Karel to walk to the door of its house, pick up the
 * newspaper (represented by a beeper, of course), and then return
 * to its initial position in the upper left corner of the house.
 */

// Ha!  After 2 days I got this up and running
// perhaps I needed to install Windows updates onto the “fresh
// Windows7.
// Will the program execute or will run do nothing?
// That is the question.  Yes!  It works.  Hoo Rayyyy.

import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

    // You fill in this part
public void run(){
    // above line needed to intialise program
    // The aim: get karel to collect newspaper.
    // How complex do we want this? Move move, collect, move move or
    // move, check, move check, check, collect, etc???
    
//move();
/*    if (frontIsClear()) {
            move();
      } else { */

// Move Karel to Newspaper.  Double check no obstruction.
// Pick up beeper, check that there is one there.
// Move back to starting position.  Reverse code somehow?
// Face east once more.
// REALLY SIMPLE non robust programming follows (and it works)}
/*
 * Paul Yeatman
 * Collect Newspaper
 * Version 1.0
 * 20170330
 *
    // Go to newspaper
    move(); // go to wall
    move();
    turnLeft(); // could be turnRight
    turnLeft();
    turnLeft();
    move();
    turnLeft();
    move();
    
    // Pick up the newspaper
    pickBeeper();
    
    // Go back to starting position and orientation
    turnLeft(); // could be turnAround
    turnLeft();
    move(); //go to wall
    move();
    move();
    turnLeft(); // could be turnRight
    turnLeft();
    turnLeft();
    move(); // go to wall
    turnLeft(); // could be turnRight
    turnLeft();
    turnLeft();
    }
    */
    
/*
 * Paul Yeatman
 * Collect Newspaper
 * Version 2.0
 * 20170331
     */
    
 // code expanded on
    // Pre-conditions:
        // House ALWAYS the same
         // Newspaper ALWAYS there and ALWAYS in same spot.

    // Post-Conditions:
        // Karel back in starting position facing East.
    
    // I’ve coded this with a mix of check and a mix of mehs.  Probably bad bad bad.
    
    // Step 1 follows: go to newspaper
    moveToWall ();
    dogLegTurn (); // turn right, move then turn left.
    
    // Step 2 follows: pick up newspaper
    moveToBeeperAndCollect (); // go out the door, checking for beeper at each step.  If beeper stop and

pick up.

    // Step 3 follows: go back to starting position and orientation
    turnAround ();
    moveToWall ();
    dogLegTurn ();
    //turnAround (); /// or check orientation
    correctOrientation (); //turnRight ();
    //moveToWall (); // check that Karel’s facing east, if not, turn and check.
    //turnRight ();
}  // closes public void run
 // closes the public class

// private voids go outside of the public class.  Think of them as functions called by ‘public void run’

private void moveToWall ()  //!! Code works !!
    {
        while (frontIsClear() )
        {
            move();}
        }

private void dogLegTurn () {  //!! Code works !!
    turnRight();
    move();
    turnLeft();
}

private void correctOrientation () {  //!! Code works !!
//check that Karel’s facing east, if not, turn left.
    while (notFacingEast() )
    {
        turnLeft();}
    }

private void moveToBeeperAndCollect () {  //!! Code works !!
    while (noBeepersPresent() )
    {
        move();
    }
    pickBeeper ();
}
}

Problem 2: Stonemason Karel

This was fairly easy given the pre-conditions.  I made Karel climb up, placing beepers where there were none.  It then climbed down, moved four corners and climbed up.  Rinse and repeat.

Problem 3: Create a Checkerboard

This was a bit trickier.  Originally I was going to recycle code from Problems 1 and 2.  Karel was going to ascend placing beepers and then descend, move to the right one and place the beepers.  Very inefficient.  I ditched that idea for what I thought of originally – making Karel run left to right (a beeperrun), check for a beeper before moving up a line after determining if it was to the east or west, turning and performing another beeper run.

After finally getting the program to work (6 hours of code, test, code test), Karel would correctly place the beepers and then headbutt the wall.  An additional if statement on the move rows statement fixed that up.

I’m thinking of doing what I used to do with my Commodore 64.  Write the code out on paper and use arrows so I can better visualize the program flow.  It would have sped up the code creation for this problem

Problem 4: Find The Middle Bottom of a Grid

Here Karel needed to place a beeper at the bottom center of the grid (or to one of the other of the central columns if an even numbered grid width.  Karel cannot count.

I knew what I wanted to do right way on this one:

  • place a line of beepers (DropBeepers)
  • take a beeper off each end until no more beepers then put a beeper down (CollectBeepers).

I recycled the orientEast code I’d written for problem 3 as Karel needed to be looking east at the end.

I spent more time debugging the looping on this than I did programming it.

The final code was tested on the following grid sizes:

// default map: works
// 1×1 map: works (not needed according to instructions).
// 2×2 map: works
// 3×3 map: works
// 2×7 map: works
// 6×11 map: works

Summary

Five lectures worth and about 10 hours of coding/debugging with half of this trying to get Eclipse to work.