On this page:
Partner Up!
Stuck In The Middle
Before You Go...
Shaking Things Up
6.6

Lab 3 Structured Data

lab!

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 3: How to Design Programs Chapter 4: Intervals, Enumerations, and Itemizations Chapter 6: Adding Structure

Partner Up!

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

Exercise 1 TAs will announce and display partner assignments; find your lab (and homework) partner.

Exercise 2 Exchange contact info with your lab partner. For the next few weeks you are effectively married, so it’s a good idea to be able to contact each other.

Exercise 3 Follow the instructions here to request your team on the handin server.

Stuck In The Middle

Goals: Use structures to write a simple but engaging game.

We are going to write a "click the midpoint" game. It will begin by showing players two points on a screen. Once they click on the screen, it will show them how the actual midpoint of the game compares to where they clicked.

Starter Code: Below is a full data definition that will enable us to design this game. Since before and after the user clicks require two different behaviors, the data has two clauses.

; A MidpointGame is one of:
; - (make-pre-click Posn Posn)
; - (make-post-click Posn Posn Posn)
(define-struct pre-click [p1 p2])
(define-struct post-click [p1 p2 p-click])
; and represents the two points to be clicked between
; as well as the point where the player clicked (once they do)
 
; A Posn is a (make-posn Number Number)
(define POSN-0 (make-posn 0 0))
(define POSN-10 (make-posn 10 10))
(define POSN-CLICK (make-posn 6 6))
 
(define MPG-PRE (make-pre-click POSN-0 POSN-10))
(define MPG-POST (make-post-click POSN-0 POSN-10 POSN-CLICK))
 
; mg-temp : MidpointGame -> ?
(define (mg-temp mg)
  (cond [(pre-click? mg) (... (posn-temp (pre-click-p1 mg))
                              (posn-temp (pre-click-p2 mg)))]
        [(post-click? mg) (... (posn-temp (post-click-p1 mg))
                               (posn-temp (post-click-p2 mg))
                               (posn-temp (post-click-pclick mg)))]))
 
; posn-temp : Posn -> ?
(define (posn-temp p)
  (... (posn-x p) (posn-y p)))

Exercise (Reviewed) 4 ** Design the function which will be given to this game’s on-mouse handler; it should transition from a pre-click game to a post-click game when the user clicks.

WARNING: The signature for mouse handlers is wrong on your big-bang card. It should read as follows:

; handle-mouse : World Number Number MouseEvent -> World

The two numbers are the x and y coordinates of the mouse event, respectively. Our apologies.

Exercise 5 ** Design a function, midpoint, which computes the midpoint of two Posns.

Exercise 6 ** Design a function, game-midpoint, which takes a MidpointGame and determines the midpoint of the two static dots.

Exercise 7 ** Design a function distance which computes the distance between two Posns. The distance between two points is the square root of the sum of the squares of the differences in their x and y coordinates.

Exercise 8 * Define visual constants for your program. A width, a height, a background image, a circle for the initial points, a circle for where the player clicks, and a circle for the midpoint will all come in handy.

Exercise 9 ** Design a function which takes in an image, a posn, and another image and places the first image on the second one at that position.

Exercise 10 ** Design a function which takes two posns, a color and an image and draws a line between those two points in that color. Hint: scene+line.

Exercise 11 *** Design a function which takes a MidpointGame and draws it. In a post-click game, a line should connect the two initial points as well as one that connects where the player clicked and the actual midpoint. Be sure to use the constants you defined.

Exercise 12 Did you exchange contact info with your partner yet? Do so if you didn’t.

Exercise 13 ** Design a function user-error which takes a MidpointGame and determines how the distance from the user’s click to the actual midpoint. The function should error if the user has not clicked yet.

Exercise 14 ** Design a function random-pre-click which ignores its input (remember, when a function ignores its input we name that input _) and creates a MidpointGame with two randomly placed posns (within the scene width and height, of course).

But wait! You are about to write a random function, so how can you test it? Turns out we’re in luck; check-random was designed for exactly this purpose.

Exercise 15 ** Design a main function that launches the game. The input can be ignored and the initial start state is random. The output should be the distance in pixels between where the player clicked and where the actual midpoint of the two initial dots are. The program should stop-when the person clicks.

Hint: we do not need to design a new function to give to the stop-when clause. What function already exists?

Exercise 16 * Do you not see the world change after you click? Give your stop-when clause a second input, the function which draws your game, and try again. This tells big-bang to draw the final state of the world.

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!

Also, exchange contact info with your partner if you haven’t yet.

Shaking Things Up

Goals: Making the game more interesting.

Exercise 17 ** While this is a somewhat fun game, it is currently too easy. People have too much time to fine-tune their midpoint estimation. Design the function next-location which takes a MidpointGame. If the given game is in the pre-click state, produce a new, randomly generated game in the same state. Otherwise, output the given world as-is.

Exercise 18 * Add this function to your big-bang’s on-tick clause. Your main function should now be forced to take in a number which will dictate the rate of how many seconds until the points switch places (so passing in the number 2 will cause it to change every two seconds, and passing in the number 1/2 will cause it to change twice per second). Be sure to update main’s signature and purpose statement with this new specification.

Exercise 19 ** Manually starting the game over and over again is a bit tedious. Design a function which takes a MidpointGame and a KeyEvent and launches a new game if the game was already finished, otherwise it leaves it as is.

Exercise 20 * Remove the stop-when clause from your world and put in the function you just designed to your on-key handler.

Exercise 21 *** There would be many ways to extend this game, such as reporting the mean distance from the midpoint every time the player clicked instead of just the last one. How would you have to change the data definition of the world to accommodate for such a computation? Would it be possible with our current design paradigms? Give your hypothesis to a course staff member and see what they have to say, and feel free to modify the game to your heart’s content.