Skip to main content Link Menu Expand (external link) Copy Copied

Lab 01

Partners

At the start of lab, you should have received a partner assignment. This will be your homework partner for the first half semester. Make sure to introduce yourself and set up a regular meeting time to work on the homework. It’s important to start early…

Your lab partner will change every week so that you can get to know your fellow computer scientists. Your homework partner will probably only change once; this is so that you may develop a rapport and consistent working schedule.

If you have not received a partner assignment by now, speak to your TA immediately.

Pet Rock and The Nested Boxes

Aaron is very protective of his pet rock, "King Paimon". He carefully guards it by placing it in layers of boxes of various colors, each with their own size.

To represent this psychologically dubious scenario, we are going to use a list to represent the (potentially 0) layers of boxes, in the order of from the largest box to "King Paimon" itself. Note: in this imaginary world, larger boxes can fit inside smaller ones, and negative box size is permitted.

  1. Write the data definition for PRS (i.e. Pet Rock Storage) describing the scenario, along with other data definitions you might need. What should your base case be?
  2. Define examples and design a template for PRS.
  3. Design a function largest-box-size which gives the size of the largest box in a PRS. If the pet rock is contained in no boxes, largest-box-size should produce 0.
  4. Design a function which determines if a box of a particular color is in a PRS.

Switch pair programming roles

  1. Design a function which takes a PRS and increases all of its boxes sizes by 1.
  2. Design a function which takes a PRS and a number and discards all boxes bigger than that size.
  3. Design the function well-stored? which ensures a PRS is comprised of successively strictly smaller boxes.

Stuck in the middle

Switch pair programming roles

We are going to write a “click the midpoint” game. It will begin by showing players two points on a screen. The player is then expected to guess their midpoint by clicking on the screen. Once the player clicks, it will show them how the actual midpoint compares to where they clicked.

First, we will start be representing the static elements of the game.

  1. Design a data definition, called Point, which represents a point on a 2D-plane.
  2. Design a data definition, called Board, which represents the 2D board on which the midpoint-game will be played. The board should include the locations of the two points.
  3. Define constant images to represent the board points, their midpoint, and the point the user chooses. These will come in handy later.
  4. Design a function called place-point, which takes a Point, an image representing that point, and a background image, and places the point’s image on top of the background image at the point’s coordinates.
  5. Design a function called draw-board, which renders the board you designed in the previous question.

Switch pair programming roles

Next, we will work on designing the calculations necessary for playing the game.

  1. Design a function called midpoint, which computes the midpoint of two Points.
  2. Design a function called board-midpoint, which computes the midpoint of the points on a given Board.
  3. Define a function called distance, which computes the distance between two points. The distance between two points is the square root of the sum of the squares of the differences in their x and y coordinates.
  4. Design a function user-error determining the distance from the user-given Point to the midpoint of the Board.

Switch pair programming roles

Now, we would like to work on rendering the Board on the screen.

At this time, we can render every part of the game, and what’s left is to complete the main logic of the game itself. We need to first represent the state of the game. There are two things to keep track in the game state:

  • the Board;
  • the current state of user input.

We have already designed the Board. How would we design the user state? The only event we care about here is a single click on the board, but also note that the user might not have immediately clicked their mouse.

  1. Design a data definition for the state of user input, and use this data definition to complete the data definition for the GameState.
  2. Design a function to check whether a GameState has user-input (i.e., to check if the user has already clicked)
  3. Design a function, draw-state, which takes a GameState and renders the board and a dot (different from the board dots) for the potential user input.
  4. Design a function, draw-state/midpoint, which renders exactly the same state as the previous function, but also includes another dot at the midpoint between the two dots on the board.
  5. Design a function, initial-board, which takes a height and width and generates a board with randomly positioned Points. How can you test on random values? check-random is your friend.
  6. Design a function, initial-state, which generates an initial GameState.

Switch pair programming roles

  1. Design a function to handle when the user has clicked their guess. See the documentation for big-bang’s on-mouse for the structure.
  2. Design a main function that launches the game. The input should be the height and width of the desired board and the initial start state is random. The program should stop-when the player clicks.
  3. Do you see the world change after you click? If not, give your stop-when clause a second input, the function which draws the board, the true midpoint, and where the player clicked, and try again. This tells big-bang to draw the final state of the world before exiting.