Lab 2 Enumerations and Structures
Purpose: The purpose of this lab is to practice the use and definition of enumerations and structure types.
Textbook references: Chapter 5: Adding Structure, Chapter 4.3: Enumerations, Chapter 4.5: Itemizations
Opening doors
A door can either be open, closed, or locked. Initially your program will take in a representation of one of these states.
The user can open a closed door by pressing the "o" key on their keyboard. You cannot open a locked door, and attempting to open an already open door will do nothing.
The user can close an open door by pressing the "c" key on their keyboard. Attempting to close an already closed (or closed and locked) door will do nothing.
The user can lock a closed door by pressing the "l" key on their keyboard. Attempting to lock an open door or an already locked door will do nothing.
The user can unlock a locked door by pressing the "u" key on their keyboard. Attempting to unlock a closed door that is already unlocked, or an open door, will do nothing.
Step 1: What stays the same?
One thing that stays the same in this program (and many others) is the images. We can define images of open, closed, and locked doors to use throughout our program. Here are some examples of what those images might look like:
Exercise 1 Insert these images into your program and define them as constants. Use names that tell you something about the image such as DOOR-CLOSED-IMAGE.
Step 2: What changes?
The only thing changing in this program is the state of the door: whether it is open, closed, or locked. Remember that we want to separate data from the images of that data, so we can’t just use the images from the previous step as our data. A good way to keep track of the state of the door would be the following data definition:
; A DoorState is one of: ; - "closed" ; - "locked" ; - "open"
Exercise 2 Write down the template for a DoorState. Recall that the template shows you the shape of functions that take in this type of data.
Step 3: Which handlers do we need?
As always we will need a to-draw clause. Based on the explanation of the program, it changes when we press a certain key. What does that tell us about the handlers we need?
Exercise 3 Write down the signatures of the handler functions you will need. If you are having trouble you can take a look at the documentation or ask a staff member to help.
Step 4: Design your handlers
Exercise 4 Design the function that draws the DoorState. Which handler does this belong to?
Exercise 5 Design the function which changes the DoorState based on user input. Which handler does this belong to?
Step 5: Put it all together!
Exercise 6 Design the function door-simulator which, given an initial DoorState, runs the door simulation using big-bang. Give it a try! Be sure you can’t lock an open door or open a locked door.
Structural Integrity
Recall from lecture that a structure is a type of data that contains other data. For example, we might have a structure for a book which contains information about its title, author, and price.
Exercise 7 Define a structure for an employee. An employee has a first name, a last name, an hourly wage, and a social security number. What kinds of data can we use to represent these fields?
Exercise 8 List out the functions given by the structure definition for an employee. Remember that there are three kinds of functions given to you: a constructor, some selectors, and a predicate.
Exercise 9 Define an example of an Employee.
Exercise 10 Write the template for an Employee. Remember that a template shows you the shape of functions that take in this kind of data.
Going Batty
In this part of the lab we will design a cute little program where a bat moves towards some fruit. The program should take the initial location of the bat. The fruit should be in the lower right corner of the screen. Every tick the bat should move towards the fruit by moving either down or right. If the bat has moved as far down as possible it will move to the right. If it has moved as far to the right as possible it should move down. Otherwise it should move randomly either down or right. The program will end when the bat reaches the fruit and will display some kind of win screen to let people know that he made it and will not starve after all.
If this all sounds very confusing, below is a video of a finished program. Yours doesn’t have to look exactly like this but it should be similar.
Step 1: What stays the same?
Exercise 11 Define some constants to represent what stays the same in this program.
Step 2: What changes?
Exercise 12 Design a data definition to represent what changes in the program. Check with a staff member to make sure you are on the right track before you move on. Trying to design a program with the wrong data definition can lead you down a very dark and dangerous path.
Step 3: Which handlers do we need?
Exercise 13 Write down the signatures of the handlers we need for this program. Keep in mind that the program stops automatically when the bat reaches the food. Which handler can we use to make that happen?
Step 4: Design your handlers
Exercise 14 Design the handlers you decided on in the last step. One thing to remember is that stop-when can take two functions: one tells the program when to stop and the other tells the program what to draw when it stops. Take a look at the documentation for more information.
Step 5: Put it all together!
Exercise 15 Design a function that uses big-bang to run your bat program. Try it out! Make sure the bat stays on the screen and that your program correctly displays the happy success screen when it ends.