This activity teaches students about state machine programming as they create a simple game. The goal is to make Cozmo view the light cubes in the sequence 1-2-3. Along the way, students will also learn about the "in-frame" tile and the "once" tile.
Cut out one set of labels (cheeseburger, fries, and kitty litter images) and paste them to the tops of cubes 1, 2, and 3, respectively.
Print a copy of the "Cheeseburger state diagram" worksheet for each student or team of students.
1. Introducing The "in-frame" TileThe "see" tile looks for things on Cozmo's world map. Once Cozmo sees a cube and puts it on the world map, it stays on the world map. Even if Cozmo moves so that the object is no longer in the camera frame, it's still on the world map -- unless it gets bumped or picked up and returns to the dock. If we want to ask whether an object is in the current camera frame, not just on the world map, we must add an "in-frame" tile to the "see" rule. Demonstrate this with the following program:
Experiment: Show Cozmo a cube while running this program and he will repeatedly play a sound. Block his view with a hand and the sound will stop; the rule is grayed. The cube remains on the world map, but it's grayed as well, since it's not currently visible. Remove the hand and the sound starts again.
2. Tilting the HeadWith the Let Me Drive idiom, we can use the right stick to move Cozmo's head up and down. Try it with this program:
Experiment: Use the right stick to prevent Cozmo from seeing cubes by forcing him to look up before we drive him around. When you want him to see a cube, tilt his head down.
3. Game Logic: Reacting to the CheeseburgerNow we're going to code the first step of the game. Cozmo will announce that the game is starting. And when he sees cube 1 (the cheeseburger) we want him to switch to page 2. Click here if you need to learn about how to switch pages in Calypso. Then try this program:
Experiment: Start out with cube 1 behind the robot. When you run the program, turn Cozmo so he sees the cube, and he will switch to page 2 and say "cheeseburger".
The state machine diagram for our program looks like this:
4. More Game Logic: Reacting to Fries and Kitty LitterNow we want to extend the game to include the fries and kitty litter steps. Exercise: see if you can add rules to pages 2-4 to look for cube 2 and say "fries", and then look for cube 3 and say "kitty litter". Remember that each page need its own "Let Me Drive" rule. On page 4, after Cozmo says "kitty litter", the player should win the game. (The "win" tile can be found in the "game" submenu.) Here is the solution:
Now the state machine diagram looks like this:
5. Losing If The Wrong Object Is ViewedWe can make the game more challenging by adding ways to lose. Suppose at the start of the game Cozmo sees fries or kitty litter when he should only be seeing the cheeseburger. We can make a losing page (let's use page 5) for this situation:
Note: the rules for cubes 2 and 3 must precede the rule for cube 1 because if we have fries or kitty litter in view, that should cause a loss even if we also see the cheeseburger. According to the Third Law, putting these rules first will give them priority over the rule that switches to page 2 upon seeing the cheeseburger. Once we've seen the cheeseburger, we switch to page 2 where we announce the cheeseburger and wait to see the fries. But the cheeseburger may still be in view, so we don't want it to trigger a loss. But if we see the kitty litter, that's definitely a loss. So page 2 should look like this:PAGE 1: WHEN DO say "start" once WHEN see cube2 in-frame DO switch to page 5 WHEN see cube3 in-frame DO switch to page 5 WHEN see cube1 in-frame DO switch to page 2 WHEN gamepad DO move PAGE 5: WHEN DO play "cuckoo" WHEN timer 4 seconds DO (game) end
Similarly, on page 3 we've just announced the fries (cube 2) and may still be seeing them while we wait to see the kitty litter (cube 3), but if we see the cheeseburger (cube 1) again, we should lose.PAGE 2: WHEN DO say "cheeseburger" once WHEN see cube3 in-frame DO switch to page 5 WHEN see cube2 in-frame DO switch to page 3 WHEN gamepad DO move
Here are the complete rules for the modified game:PAGE 3: WHEN DO say "fries" once WHEN see cube1 in-frame DO switch to page 5 WHEN see cube3 in-frame DO switch to page 4 WHEN gamepad DO move
Testing the GameHere is a cleaned up version of the state machine diagram for the complete game:
To thoroughly test the game we should try out every path through the state diagram. This will require multiple runs. Here is a procedure you can follow to ensure a complete test:
Playing CompetitivelyTwo people can play the game competitively, as follows. The first player arranges the cubes around the robot however they like. The can put cubes right behind Cozmo, or stack one cube on top of another one. Anything is fair game, except that the fries and kitty litter cannot be in view when the game starts or else that would be an immediate loss. Once the first player is done arranging things, the second student starts the program and tries to make Cozmo see the cubes in the proper order. If there is a stack of cubes, they may have to knock things over in order to get Cozmo to see the cubes in the order they want. After they win or lose, the students switch roles and play again.
Extension: Multiple Loser PagesThe simple version of the game uses a single page (page 5) to announce when a player has lost. We could make the game more engaging by having Cozmo announce the object that caused the loss, e.g., page 5 could say "oh no, I saw fries", while page 6 could say "too bad, i saw kitty litter", and page 7 could say "sorry, I saw cheeseburger again". This is an easy extension to implement. Just add the appropriate "WHEN DO say" and "WHEN timer" rules to each page, and modify pages 1, 2, and 3 to switch to pages 5, 6, or 7 when Cozmo sees something he shouldn't have. Another easy extension is to add an "express frustrated" action to the loser behavior. Replace the "DO end" action on of the three each loser pages with a "switch to page 8" action, and have page 8 do "express frustrated" followed by a timer and "DO end".
Make Your Own Game[Notes to be expanded soon:] Use "feel" instead of "see in-frame". Drive the robot into the cube. Use mama/papa/baby bear. Color the cube when you bump it. Make your own graphics to label the cubes.
Extensions: (1) Let them change the wording and the order of the cubes, e.g., cube 2 is first and the robot says "mama bear", cube 3 is next and the robot says "papa bear", cube 1 is last and the robot says "baby bear". (2) Add code to color each cube green while saying the word. (3) Combination lock 1: code a sequence like 1-3-1-2 to win, and use "when feel" instead of "when see". A wrong input should play a cuckoo sound and return to state 1. Show students how to pick the lock by guessing combinations. (4) Combination lock 2: build a pick-proof lock by not revealing failure until after the fourth digit is input. Now there are 81 (3^4) separate combinations. How many would there be if the sequene was 6 digits? Back to top of page Back to New Brighton Curriculum