On this page:
1 Introduction(s)
2 Purpose
3 Explain Chip to your Partner
4 Possibility of a Pause
6.12

Lab 5: Stop and Smell the Sheep

1 Introduction(s)

You’ll work in labs in pairs. Find someone to work with for this first lab and introduce yourself.

Make sure at least one of you have a laptop to work on for this lab.

The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. We’ll have you switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.

2 Purpose

In this lab, you’ll practice
  • communicating your own program designs to others,

  • understanding programs designed by others,

  • adapting existing programs, and

  • using itemizations involving structures.

3 Explain Chip to your Partner

In this lab, you will be adapting one of your two solutions to Exercise 3 to add some functionality. The first step, however, is to understand both solutions.

Open both you and your partners solution to the Chip exercise and take turns explaining how you designed the program. Focus on giving rationales for the decisions you made. Go through each problem and discuss any parts you felt were particular tricky or you struggled with.

When you’re done, select one of the solutions. It doesn’t matter which since you should both understand both programs.

4 Possibility of a Pause

Revise your solution to the Chip problem to add the ability to pause the game. When paused, Chip should stop moving and a text message indicating the game is paused should be overlayed on top of the scene. When unpaused, the game should resume exactly how it was before: Chip is moving is the same direction, with the same speed, and from the same location as when the game was paused.

There are many ways to represent this information, but for this lab, use the following data definition (in addition to ones given to you in the exercise) to guide your design:

;; A PPC (possibly paused Chip) is one of:
;; - (make-paused Chip)
;; - Chip
(define-struct paused (chip))
 
;; Interpretation: (make-paused c) represents the pausing of a Chip
;; game in state c.  Otherwise the game is unpaused.

The world state should now be represented as a PPC instead of a Chip and your program should be revised accordingly. For example, your main function should become something like this:

;; main : Integer -> PPC
;; Launch Chip at given position, moving right slowly
(define (main x)
  (big-bang (make-chip x #true 1)
    [on-tick ppc-move]
    [to-draw ppc-draw]
    [on-key ppc-key]))

where the signatures of ppc-move, ppc-draw, and ppc-key should now be, respectively:
  • PPC -> PPC,

  • PPC -> Image, and

  • PPC KeyEvent -> PPC.

The ppc-key function should toggle the paused/unpaused state of the game whenever the user presses the space key.

Lab problem 1: Template for PPCs

Write down a template for PPC consuming functions.

Lab problem 2: Revising for PPCs

Revise your program to work with PPCs. (Hint: you may want to add new PPC functions rather than modifying your Chip functions to work on PPCs.)