Lab 4 Structured Data
Purpose: The purpose of this lab is to practice designing and programming with structured data. We will also assign partners in this lab.
Textbook references: Chapter 9: Designing with Self-Referential Data Definitions
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 6 with your partner. It’s due on Monday so you are going to have to do it soon.
Exercise 3 Read the policy page to find out what to do if you are having problems with your partner. It is very important that you tell us if you are struggling to work with your partner. Otherwise we can’t help you resolve the problem.
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.
Exercise 4 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? Remember to follow all the steps of the design recipe for data.
Exercise 5 Define any constants you think you may need. What sorts of things stay the same throughout the game? Think about the images you are using as well as the non-visual parts of the game (for example, does the player get faster over time or stay the same speed?).
Exercise 6 Design your program. Remember to follow the steps laid out in the textbook. We have covered two of the steps in the above exercises. What comes next?
Before You Go...
If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance. We love to teach and you will learn. It’s symbiotic!
Fighting Back
Update your world program so that you can fight back against the zombie. You can shoot a single bullet at the zombie 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 7 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? Be sure to update all the steps of the data design recipe.
Exercise 8 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!