Designing world states

The 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:

Developing a wish list for big-bang programs

Questions to ask about any possible big-bang program:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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?)
             ...)

Resources

The Racket Documentation is not the best way to learn big-bang. There are better places in Part One of the second edition: