On this page:
Shaping Up
Before You Go...
Defying Gravity
6.6

Lab 2 Conditional Data

lab!

Purpose: The purpose of this lab is to practice defining and programming with conditional data.

Textbook references: Chapter 3: How To Design Programs Chapter 4: Intervals, Enumerations, and Itemizations

Shaping Up

Goals: To design programs over enumerations.

Starter Code: This is a data definition of a Shape.
; A Shape is one of:
; - "circle"
; - "square"
; - "triangle"
; and represents a kind of shape

Exercise (Reviewed) 1 ** Finish following the design recipe for this data definition (examples and a template).

Exercise (Reviewed) 2 ** Design a function draw which draws a Shape. Make use of your handy template from the past exercise, and don’t forget to follow every step of the design recipe for functions (signature, purpose statement, tests, code).

Exercise 3 * Design a function draw/scene which overlays the image of a Shape on an empty-scene of a fixed size. Do you need to follow the Shape template here? How many tests does it need?

Exercise 4 ** Design a function next-shape which consumes a Shape and outputs the "next" Shape (any order is fine, so long as next-shape "cycles through" all the shapes.)

Exercise 5 *** Compose the draw/scene and next-shape functions in a big-bang animation that cycles through all of the shapes. Ask a member of the course staff for help if you’re stuck on big-bang syntax/usage. Using big-bang takes some practice, which is why we’re here!

Exercise 6 * Is your animation headache-inducingly fast? Slow it down by giving your on-tick clause a number that follows next-shape. The default is 1/28 seconds per tick; providing the on-tick handler with 1/2 would cause it to tick twice per second, whereas 2 would cause it to tick once every two seconds.

Exercise 7 ** Wrap your call to big-bang in a main function, which takes a Shape and uses that shape as the initial state. Don’t forget to give it a signature and purpose statement, but you cannot test the main program. Why not?

Exercise 8 * Launch your animation by calling your main function from the interactions window.

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!

Defying Gravity

Goals: To design programs over intervals.

Countless crazes have swept the nation over the course of history. From planking to democracy, America is truly a land of fads. What fad grips us in its promise of adventure, scenic vistas, and defiance of gravity? Climbing, of course!

We’re going to create a game that simulates climbing, with varying difficulty in the vertical terrain. Our worldstate will be represented by the height of the climber, and we will construct a data definition that helps us design a program that simulates this daring climb.

Exercise 9 * Define three constants, EASY-UP, MEDIUM-UP, and HARD-UP which will define how many pixels our brave climber can move in a single movement in terrains of varying climbing difficulty. The easier the terrain, the more our climber can move. Also, define the constant HEIGHT which will determine how tall our mountain is.

Exercise 10 ** Design a data definition Terrain which itemizes three non-overlapping, adjacent intervals. The smallest should start at 0 and the largest should end at whatever you defined HEIGHT to be. The three intervals represent three increasingly difficult sections of the terrain, and each interval can be as long as you want it to be, so long as you follow the above specifications.

Remember to follow all four steps of the design recipe for data (data definition, interpretation, examples, template).

Exercise 11 ** Design a function place-terrain which takes a Terrain (representing the height of a climber) and draws someone (who can be represented by your favorite shape) on a scene that is HEIGHT tall. If you’re feeling fancy, split your scene into three different-colored rectangles, one for each section of the terrain.

Remember that the coordinate system of images has (0, 0) at the top left and not bottom left!

Exercise 12 ** Design a function climb, which takes a Terrain (representing the height of the climber) and a KeyEvent and moves a climber up the mountain if they key event is "up". Otherwise, they drop back down to 0. Sad!

Exercise 13 * Design a function reached-the-top? which takes a Terrain and determines if it is equal to HEIGHT.

Exercise 14 *** Compose your functions into a big-bang program wrapped in a main/climb function. This function can ignore its input, as the start state should always be 0; when inputs are ignored we name them _ by convention.

Unlike our last program, this one ends when a specific event happens (the climber reaches the top). What big-bang handler stops programs? Again, ask a course staff member if you’re having trouble with big-bang.