On this page:
Shrinking Balloons
Designing Chip the Cheap Sheep
Lists
Extra Problems

Lab 3h Worlds and Lists

home work!

Purpose The purpose of this lab is to give you some hands-on experience with designing [interactive] programs, programs that process structures and unions (itemizations) of data, and lists. Students should know how to define constants, create data and structure definitions, use templates, create the main function for world programs, and create/process wish lists. Students should be familiar with how to make and use lists, and the list template.

image

Shrinking Balloons

See design of world programs for background

Your TA has created an elaborate backstory for the below exercises, and you completely ignore it before jumping straight to the instructions on how to do the exercises.

Sample Problem Create a game in which you compete against time to stop a small red circle from shrinking into nothingness. Initially, you have a size 50 red circle. On each clock tick the circle will shrink. At first the circle will shrink slowly, but as time goes by, it will shrink more quickly. Each time the player clicks the left mouse button, the circle will grow slightly.

Draw a time-indexed series of scenes from the game. Discuss transitions from one scene to next. What stays the same (dimension of game canvas)? What changes? For the former we need basic constant definitions; for the latter we need a data definition.

With the next few sample problems, your TA will break down the above problem into smaller pieces according to the design recipe for world program.

Sample Problem Add appropriate constant definitions to your definitions area. Here are some of our choosing:
(require 2htdp/image)
(require 2htdp/universe)
 
...
(define BACKGROUND (empty-scene WIDTH HEIGHT))

Sample Problem Define a structure type for the world and then develop a data definition. Recall that initially the world is a size 50 red circle. While you’re at it, define a constant initial world.

(define-struct shrinking (size time))
; A SW is a structure: (make-shrinking Number Number)
; interpretation: (make-shrinking s t) combines the size s
; of the circle in the world and the running time t of the world
 
(define initial-world (make-shrinking 50 0))
(define intermediate-world (make-shrinking 50 10))
Stop! Interpret these last two lines.

Sample Problem You know you need a rendering function. The big-bang OS applies it to the current state of the world every time it wishes to draw something. Make a wish:
; SW -> Image
; draws a red circle whose size depends on the world
(define (draw-world world)
  BACKGROUND)

You also need events to trigger transitions between game scenarios. Recall that the world changes on clock ticks and on mouse clicks. This tells you which event handling functions your big-bang needs.

From that you can create your wish list. Remember to give a signature, purpose statement, and meaningful name for each function.

; SW -> SW
; creates a new SW in which time has been
; incremented by 1 and the size has decreased
(define (shrink-world an-sw)
  an-sw)
 
; SW MouseEvent -> SW
; creates a new SW in which the size is slightly
; larger than in world, if the left mouse button is pressed.
(define (grow-world an-sw me)
  an-sw)

Sample Problem Define the main function, which “composes” the handlers and the rendering functions.
; SW -> SW
(define (main world0)
  (big-bang world0
            (to-draw draw-world)
            (on-tick shrink-world)
            (on-mouse grow-world)))
Should the game ever stop? If so, how should main express the fact?

Exercise 1 Define 3 numeric constants, SHRINK-TIME0, SHRINK-TIME1, and SHRINK-TIME2 such that

(< SHRINK-TIME0 SHRINK-TIME1 SHRINK-TIME2)

These constants represent times in the game and shrink-world uses these times to determine how quickly to shrink the world.

Exercise 2 Design the function shrink-world, which takes a SW. It increments the world’s time and shrinks the world’s size depending on how the world’s time compares with SHRINK-TIME0, SHRINK-TIME1, and SHRINK-TIME2.

Hint That’s two distinct tasks. Did you notice the "two" here?

You may choose by how much the world should shrink in each interval.

Switch roles.

Exercise 3 Design the function grow-world, which takes a SW, an x coordinate, and y coordinate, and a MouseEvent. If the mouse button has been pressed, then the returned SW should have a slightly larger size than the give one.

You may choose by how much the world’s circle should grow.

Remember that mouse events are special strings. Be sure to ignore all mouse events except "button-down".

Play your game. How long can you hold out against time? Change constants to make the game more (less) challenging.

image

Designing Chip the Cheap Sheep

Switch roles

An animation studio is working on a game called "Chip, the Cheap Sheep". They have some basic idea, and that is to have a sheep running across the screen. Here are some sheep shapes that their artist has drawn:

You should be able to drag the images from the webpage into DrRacket, once they are there you should know what to do. If not save them to the desktop, and use "Insert">"Insert Image..." from the DrRacket menu bar.

0

 

1

 

2

 

3

 

 

 

Your goal is to create a simple proof-of-concept game with Chip running from offscreen to the (x coordinate of the) point the player clicks on first. Before Chip runs, the screen shows the text

image

The following exercises take you through the design steps for world programs again.

Exercise 4 Design the function pick-chip. It takes a number between 0 and 3 and returns the corresponding image of Chip from the sequence above.

Switch roles.

Exercise 5 Draw some still frames from the game. Which aspects remain the same? Which change?

Exercise 6 Gather constant definitions for the properties of the world frames that remain the same. Here are some:
(define CHIP1 ...)
...
(define WIDTH (* 20 (image-width CHIP1)))
(define HEIGHT (* 2 (image-height CHIP1)))
(define PLAYGROUND (empty-scene WIDTH HEIGHT))
...
(define TXT (text "click the mouse to get Chip running" 33 "pink"))
...

Exercise 7 Develop a structure type definition so that your program can keep track of all the quantities that change between frames. Hints In addition to where Chip currently is, your program must know where Chip is going. After all, at the very beginning you have no clue where Chip is supposed to go.

Develop a data definition so that you can represent the state of the game. Remember that data definitions come with an interpretation and sample data.

Exercise 8 Develop a wish list. You know you need a function that renders the current state of the game. What else do you need? Which kind of events make the game transition from one state to another?

Exercise 9 Create a main function to start your world program. Here is a sketch:
; PositiveNumber -> ... class of data that big-bang manages ...
; given the animation rate, play a game of Chip, the cheap sheep
; NOTE: by supplying the animation rate you can slow down the game
(define (chip rate)
  (big-bang initial-world
            [to-draw ... what name did you choose? ... rate]
            [on-tick ... what name did you choose? ...]
            [on-mouse ... what name did you choose? ...]))
Why did we add to-draw, on-tick, and on-mouse clauses?

Exercise 10 Develop a template for functions that work on the state of the Chip game.

TAs: time for a short lecture on: comment with "#;" and copy-paste to re-use the template from now on.

Exercise 11 Design the rendering function.

Exercise 12 Design the clock tick event handler. The function takes world and returns a world where Chip is moved to the left, toward his goal. Use the function pick-chip to change the image according to where Chip currently is. Hint Check out modulo and/or remainder.

Switch roles.

Exercise 13 Design the mouse click handler. The function that takes a world, x and y coordinates, and a mouse event and returns a new world. The new world will have the mouse’s coordinates as Chip’s new destination and Chip teleported from offscreen to a location close to the right edge.

Remember that mouse events are just strings. Be sure to ignore all mouse events except "button-down".

Exercise 14 Design a function that determines whether Chip is close to his destination. We let you choose what "close" means. Then equip the chip function from above with a stop-when clause.

image

Lists

Sample Problem Design the function sum, which consumes a list of numbers and produces a number which is the sum of the numbers in the list.

; A LON is a one of:
; - empty
; - (cons Number LON)
 
; LON -> Number
; Computes the sum of a list of numbers
; Given: (cons 1 (cons 2 (cons 3 empty))) Except: 6
; Given: (cons 1 (cons 5 (cons 6 (cons 3 empty)))) Except: 15
(define (sum a-lon)
  (cond
    [(empty? a-lon) 0]
    [(cons? a-lon) (+ (first a-lon) (sum (rest a-lon)))]))
(check-expect (sum (cons 1 (cons 2 (cons 3 empty)))) 6)
(check-expect (sum (cons 1 (cons 5 (cons 6 (cons 3 empty))))) 15)

Exercise 15 For each of the following, make a data definition. Make at least 3 examples of each.
  • A list of strings

  • A list of images

  • A list of movies, where a movie has a title and a duration

  • A list of a list of numbers (this should take no more work than defining a regular list of numbers)

Exercise 16 Design the function product which consumes a list of numbers and produces the product of all the numbers in the list.

Exercise 17 Design a function join-los which consumes a list of strings and produces a string which is the concatenation of each string in the list. For example,
(define a-list-of-strings
  (cons "A"
            (cons "List"
                      (cons "Of"
                                (cons "Strings" empty)))))
 
(check-expect (join-los a-list-of-strings) "AListOfStrings")

Exercise 18 Design a function join-los-with-space which consumes a list of strings and produces a string which is the concatenation of the strings in the list interspersed with a space (" "). For example,

(check-expect (join-los-with-space a-list-of-strings) "A List Of Strings ")

Exercise 19 Design a function join-los-with-string which consumes a string and a list of strings and behaves like join-los-with-space, except it uses a given string instead of a space. For example,
(check-expect (join-los-with-string "!!!" a-list-of-strings)
              "A!!!List!!!Of!!!Strings!!!")
(check-expect (join-los-with-string " wheeeeeee " a-list-of-strings)
              "A wheeeeeee List wheeeeeee Of wheeeeeee Strings wheeeeeee ")
(check-expect (join-los-with-string " " a-list-of-strings)
              "A List Of Strings ")

Why might we say that join-los-with-string is "more powerful", "more general", or "more flexible" than join-los-with-space?

Exercise 20 Design a function create-kandinsky which consumes a list of images and overlays them all on a red rectangle.

Exercise 21 Here is the data definition of a list of stock trades:
; A LOT (list of trades) is one of:
;  empty
;  (cons (make-stock-sale Symbol Number Number) LOT)
;  (cons (make-stock-purchase Symbol Number Number) LOT)
(define-struct stock-sale (company shares price-per-share))
(define-struct stock-purchase (company shares price-per-share))

Design a function that takes a list of stock trades and returns a list of the traded companies (that is, their stock symbols).

Exercise 22 Design a function that takes a list of stock trades and a symbol and returns the total values of traded shares (both bought and sold)

Extra Problems

If you finish the problems before lab is over, try modifying your game to include a Difficulty. Define a data and structure definition for Difficulty. A Difficulty consists of 3 successively larger Numbers, representing times. Modify your data and structure definitions for your world to include a Difficulty. Modify shrink-world to use the Difficulty instead of the constants SHRINK-TIME0, SHRINK-TIME1, and SHRINK-TIME2.

When you finish that, modify the game to track how quickly the size is shrinking or growing, and change the color of the circle to blue if the size is growing faster than it is shrinking, yellow if the size is growing exactly as fast as it is shrinking, and red if the size if shrinking faster than it is growing.