big-bang
ProgramsThe second edition says:
Your task is to develop a data representation for all possible states of the world.
This is the hardest part of designing a big-bang
program. Some ideas:
Draw scenes that the program might display.
What data do you need to represent the scenes?
What data remains the same in every scene? Things that don’t change shouldn’t be part of the world state. Such things should instead be made named constants using define
.
What data changes? That data becomes part of the data definition for the world state. Here are some general ideas:
A very simple program might have just one item of atomic data as its world state.
A scene with multiple elements might have a structure as its world state (definition by parts).
A program with multiple kinds of scenes, each with its own set of elements, might have a set of variants as its world state (definition by choices on top of definition by parts).
Protip: It is also possible to define the world state using definition by parts on top of definition by choice, or even definition by parts on top of definition by choice on top of definition by parts. This is one of the deepest tricks in the business, and we’ll talk about it in class.
What data are needed to draw the very first scene?
This data is a world state; in fact, it is the initial state of the world. It is sometimes called “world 0.”
A world is full of events such as mouse movement, button presses, key presses, and the passage of time. What events cause changes in scenes? One way to answer this question is with a state-transition diagram that shows a box for each choice of state and and arrows connecting related states.
If you can answer this question, and you can say how the events affect the state, then you’re ready to finish your data description and move on to your wish list.
big-bang
programsQuestions to ask about any possible big-bang
program:
Does it need to draw anything?
Yes. You will need to define a function to pass to to-draw
. That function will need a name; in the example below, it is called render
.
Does it need to respond to the mouse?
If so, you will need to define a function to pass to on-mouse
. That function will need a name; in the example below, it is called mouse-event-handler
.
Does it need to respond to the keyboard?
If so, you will need to define a function to pass to on-key
. That function will need a name; in the example below, it is called key-stroke-handler
.
Do things need to happen as time passes, even if the mouse and keyboard are untouched?
If so, you will need to define a function to pass to on-tick
. That function will need a name; in the example below, it is called clock-tick-handler
.
Does the program run indefinitely (or until killed)?
If not, you will need to define a function to pass to stop-when
. That function will need a name (which should end in a question mark); in the example below, it is called end?
.
The signatures for all these functions are explained in Section 2.6 (Designing World Programs) of the second edition textbook:
; WorldState : a data definition of your choice
; a collection of data that represents the state of the world
; render :
; WorldState -> Image
; big-bang evaluates (render cw) to obtain image of
; current world cw
; clock-tick-handler :
; WorldState -> WorldState
; for each tick of the clock, big-bang evaluates
; (clock-tick-handler cw) for current world cw to obtain
; new world
; key-stroke-handler :
; WorldState String -> WorldState
; for each key stroke, big-bang evaluates
; (key-stroke-handler cw ke) for current world cw and
; key stroke ke to obtain new world
; mouse-event-handler :
; WorldState Number Number String -> WorldState
; for each key stroke, big-bang evaluates
; (mouse-event-handler cw x y me) for current world cw,
; coordinates x and y, and mouse event me to
; obtain new world
; end? :
; WorldState -> Boolean
; after an event is processed, big-bang evaluates (end? cw)
; for current world cw to determine whether the program stops
Here is an example call to big-bang
(big-bang first-world-state
(on-tick clock-tick-handler)
(on-key key-stroke-handler)
(on-mouse mouse-event-handler)
(to-draw render)
(stop-when end?)
...)
Here is another example that uses shorter names for some of your functions:
(big-bang w0
(on-tick tock)
(on-key ke-h)
(on-mouse me-h)
(to-draw render)
(stop-when end?)
...)
The Racket Documentation is not the best way to learn big-bang
. There are better places in Part One of the second edition:
Interactive programs presents some simple big-bang
examples, but it does not explain systematically what big-bang
does.
Designing World Programs, especially Figure 10, systematically outlines the structure of every program that uses big-bang
, and it explains what big-bang
does with each piece of this structure.
A Note on Mice and Characters explains mouse-event handlers and keystroke handlers.