On this page:
Partner Up!
Code Review
Meanwhile...
Surviving the Apocalpyse
Additional practice:   Fighting Back
6.6

Lab 3 Structural Integrity

home work!

Purpose: The purpose of this lab is to give you some hands-on experience with designing interactive programs and programs that process structured data.

Textbook references: Chapter 2.5: Programs, Chapter 3: How to Design Programs, Chapter 6: Itemizations and Structures

Partner Up!

Goals: Get a lab partner. Begin a beautiful friendship.

As you walk into the lab or sometime directly after the lab starts your TAs will pair you up! This person is your partner for the next 2 weeks. Say hello! Ask them their favorite color! Tell them a deep dark secret about your life!

Exercise 1 Exchange contact information with your partner. You will need to get together a lot so you are going to need to be able to talk to each other. Please give them a method of communicating with you that you will check regularly (as in, don’t just give them your email if you never check your email).

Exercise 2 Arrange a time to start working on homework 4 with your partner. REMEMBER: Homework 3 (due Thursday) should be completed on your own.

If you are ever having trouble with your partner please contact a staff member as soon as possible. We can’t help you resolve the issue if we don’t know it’s happening. All conversations about partner problems are confidential. Problems with a partner can include (but are not limited to):
  • My partner has never contacted me.

  • I have contacted my partner but never received a response.

  • My partner is never able to meet with me.

  • My partner promised to meet with me and then bailed at the last minute or just failed to show up.

  • My partner met with me to work on the homework but did not contribute during the meeting.

  • My partner was rude or inappropriate during our meeting.

  • My partner did the entire assignment behind my back without telling me.

Code Review

Today we will begin our code reviews. Your TAs will select a random student from the pool of volunteers to go to the front of the classroom and walk through their coding and design decisions. Keep in mind that it is good to make mistakes here because you can learn from them. There is no shame in not being perfect. Mistakes you have made can help you and all your fellow classmates. We guarantee that someone else made the same mistake and wants to know how to fix it.

Exercise 3 Applaud wildly for your classmates (or yourself) after the code review. It is VERY difficult to present code in front of a large audience, and especially if you are the first one to do so. It takes an incredible amount of bravery and a willingness to learn from your own mistakes.

Meanwhile...

If you are waiting for the code reviews to begin you should work on these exercises. If they are already done when you get to this section that’s fine too. You can save these exercises as extra practice for later and skip straight to the fun part (below) or you can work on these first if you prefer.

Exercise 4 Complete Exercise 72 from the textbook. Note: this will involve reading some of the preceding text. Otherwise you will have no idea what you are doing.

Exercise 5 Complete Exercise 76 from the textbook. You will have to make some assumptions here. You will find that this is a common theme in computer programming: when problems are ambiguous you will have to make a reasonable assumption and document it.

Exercise 6 Complete Exercise 80 from the textbook.

Exercise 7 Complete Exercise 103 from the textbook. This involves itemizations which you just learned about in lecture.

Surviving the Apocalpyse

In today’s lab we will work on designing a little game where you are being attacked by a zombie. Your program should take in the initial position of the player. The zombie will be initially placed at a random location on the screen. Then, every time the clock ticks the zombie will move towards the player. Here’s some code that moves one position towards another position at a given speed:

; move-towards : Posn Posn Number -> Posn
; Move position 1 towards position 2 at speed
(check-expect (move-towards (make-posn 0 0) (make-posn 8 6) 5) (make-posn 4 3))
(check-expect (move-towards (make-posn 1 2) (make-posn 1 2) 1) (make-posn 1 2))
(define (move-towards p1 p2 speed)
  (if (< (distance p1 p2) speed) p2
      (make-posn (+ (posn-x p1) (/ (* (- (posn-x p2) (posn-x p1)) speed) (distance p1 p2)))
                 (+ (posn-y p1) (/ (* (- (posn-y p2) (posn-y p1)) speed) (distance p1 p2))))))
 
; distance : Posn Posn -> Number
; Produces the distance between the two positions
(check-expect (distance (make-posn 0 0) (make-posn 8 6)) 10)
(check-expect (distance (make-posn 1 2) (make-posn 1 2)) 0)
(define (distance p1 p2)
  (inexact->exact
   (sqrt (+ (sqr (- (posn-x p1) (posn-x p2)))
            (sqr (- (posn-y p1) (posn-y p2)))))))

The player can run away using the arrow keys on the keyboard. Every second that they stay alive they get 1 point. However, you should not change the tick rate to achieve this. The game ends when the player gets eaten by the zombie. The player is eaten by the zombie if the distance between the player and the zombie is less than the radius of the player. After the game ends you should display a screen that shows the player their score.

Step 1: What stays the same?

Exercise 8 Define constants to represent the things that are not changing in the world. This can include graphical constants (images you will need throughout the program) but can also include non-graphical constants (e.g. the speed of the player).

Step 2: What changes?

Exercise 9 Design a data definition to represent the state of the zombie game. What are the things that change as the game progresses? What kinds of data can you use to represent them? Ask a staff member to double check your data definition before you continue.

Exercise 10 Provide the template(s) for the data definition(s) you designed in the previous exercise. Recall that a template shows you the shape of functions that take in this type of data.

Step 3: Which handlers do we need?

Exercise 11 Write down the signatures and purpose statements for the handler functions you need. This program requires more handler functions than we have ever written in lab before!

Step 4: Design your handlers

Exercise 12 Design the handlers you decided on in the last step. Remember to follow all the steps of the function design recipe. These steps will help you ensure that your functions all work together the way you expect.

Step 5: Put it all together!

Exercise 13 Design a function that uses big-bang to run your zombie game. Your function should take in the initial position of the player and should randomly generate the initial position of the zombie (the random function will come in handy here). Try the game! What’s the highest score you can get?

Additional practice: Fighting Back

Update your world program so that you can fight back against the zombie. You can shoot a single bullet at the zombie by pressing the space bar, but only if you don’t already have a bullet flying around. If you successfully shoot the zombie you win the game but if it eats you first, you lose.

Exercise 14 Update your data definitions to account for this new information. What sorts of things do we need to keep track of now that we didn’t before? How can we represent these things?

Exercise 15 Update any functions that take in this new data. Note that you don’t have to update any functions that are still taking in the same data definition. This is a very important thing to remember about re-factoring your code (which is what we are doing in this exercise). Don’t throw away code if you can re-use it!