For this lab, you will implement a simple game for navigating mazes. Your functions will allow a user to create a maze of interconnected caves, and also to create players that can walk around the maze looking for items. Players can carry up to two items in their pockets, and can swap an item in their pocket for the item in the cave they are currently in. Underneath the assigned problems is an example of how someone would play the game using the functions you will design for this lab.
Provide a data definitions for caves. Each cave has a name, an item sitting in the cave (such a cellphone, your academic advisor, etc), and a list of doors (other caves).
Define a variable maze
that will hold a list of
caves (it should initially be empty).
Write a function create-cave
that consumes a cave
name and an item and returns a new cave with the given information.
The new cave should have no doors. The function should also add the
cave to the maze.
Write a function connect-caves
that consumes two
caves and adds a door from each cave to the other.
Write a data definition for players that have two pockets (each can hold one item) and a current location (a cave). Use a string for (the contents of) each pocket and have it be "No item" when the pocket is empty.
Write a function create-player
that consumes a
cave and produces a player who is in the current cave and has nothing
in its pockets.
Everyone should be able to get to this point.
Write the following set of functions for exploring the player's status during the game:
where
that consumes a player and returns the name of
the cave where the player is
sees-item
that consumes a player and produces either
the item in the player's current cave or "No item" if the cave has no
item (because another player picked the item up). [HINT: you may just
want to use "No item" to represent nothing in a cave.]
left-pocket
and right-pocket
that
consume a player and produces the item in the corresponding pocket.
Players may want to know what items exist in the caves (so they
know what to look for). Write an function
items-available
that produces a list of all the items
that are currently in caves (not those in players' pockets). [Hint:
write a helper that walks over the maze variable.]
Write functions switch-left
and
switch-right
that consume a player and swap the item in
the corresponding pocket with the item in the current cave (it is okay
if either the pocket or cave has nothing, as this would correspond to
filling or emptying pockets during the game).
Write a function move-to
that consumes a player
and a cave and either moves the player to the cave (if the cave the
player is in has a door to the given cave) or a message "No door from
cave X to cave Y", where X and Y are the names of the respective
caves. Use the Scheme operator eq?
if you need to
compare whether two caves are equal.
Write a function show-next-door
that consumes a
player and produces the name of one accessible cave at a time. This
function should cycle through the names of caves for which the
player's current cave has doors (producing one name each time it is
called -- see the interaction below).
Also provide a function called go
that consumes a player and
moves the player to the last named cave produced by
show-next-door
.
Your solution must allow different players to cycle through door options in an interleaved fashion: ie, player1 asks for a door, then player2 (who is in a different cave) asks for a door, repeating until both decide to move). You may start the cycle with any available door. You may edit your data definition for players.
Show the sequence of test cases that you would use to test the cave game (don't just turn in our sample below -- figure out what else you need to test and define your own caves and test sequence). Use comments to explain what each test in the sequence is testing for.
> (define sleepcave (create-cave "home" "pillow")) > (define eatcave (create-cave "dining" "fork")) > (define exitcave (create-cave "exit" "jewels")) > (connect-caves sleepcave eatcave) > (connect-caves eatcave exitcave) > (define player1 (create-player eatcave)) > (where player1) "dining" > (sees-item player1) "fork" > (left-pocket player1) "No item" > (switch-left player1) > (left-pocket player1) "fork" > (sees-item player1) "No item" > (move-to player1 sleepcave) > (where player1) "home" > (sees-item player1) "pillow" > (switch-left player1) > (move-to player1 exitcave) "No door to cave home from cave exit" > (move-to player1 eatcave) > (move-to player1 exitcave) > (where player1) "exit" > (left-pocket player1) "pillow"
And one showing the sophisticated move functions:
> (define player2 (create-player eatcave)) > (show-next-door player2) "home" > (show-next-door player2) "exit" > (show-next-door player2) "home" > (go player2) > (where player2) "home"