Day and Night

The sky in each world is some color in the day and gray at night.

This is a “get familiar with universe” animation. There not be anything complicated about it. Even on a whiteboard it can be done in 45-60 minutes.

Setup

During the daytime, the sky on Earth is blue, while the sky on Mars is reddish. Other worlds might have other sky colors. During the nighttime, it gets dark so the sky in every world is dark gray. (This is easier to look at than pitch black.)

Model

The structure w that holds the state of a single world has two items:

  • sky-color - a color - the color of the sky in the day
  • is-day? - a boolean - true means it is daytime

If you prefer, you can abbreviate the fields as sky and day?.

(define-struct w (sky-color is-day?))

Exercises:

  • Make a variable m1 that represents the Earth during the day.
  • Make a variable m2 that represents Mars at night. (What color should the sky be? You decide.)
  • Make a variable m3 that represents a world with a third sky color during the day.

Message

Use the string “day” or “night” to tell worlds it is now daytime or nighttime.

Draw Handler

Make a rectangle that is sky-color when is-day?, otherwise dark gray.

Note: sky-color and SKY-COLOR are short ways of writing “the value inside the sky-color field of the model”.

Key Handler

There are four action keys:

  • “t” makes your world daytime
  • “f” makes your world nighttime
  • “d” makes it daytime for everyone
  • “n” makes it nighttime for everyone

Write check-expects showing what happens in one of the worlds from your Model section when you press each of the keys above.

Receive Handler

You need a receive handler to respond to messages.

In general, the receive handler runs in a particular world to respond to the messages that are delivered to that world.

The signature is:

receive-handler : model(old) message -> model(new)

Referring to the variables from the Model section above:

  • Write a check-expect that shows what happens when your world m1 receives the message “night”.
  • Write another check-expect that shows m2 receiving the message “day”.
  • Write another check-expect using m3.

Advice

Begin with check-expects for everything.

You will encounter everything difficult just writing checks.

After you believe your checks are correct, write code for each function.

Advanced

  • 24 ticks for day, 24 ticks for night
  • very advanced: color fades to dark gray in 4 (or more) steps