Extended Exercise: Exploring Places


~


rawhtml60

<#48347#>Figure: A tour of a university<#48347#>


Early computer games asked players to find their way through dangerous mazes and caves. The player wanders from cave to cave, finds treasures, encounters creatures of all kinds, fights, kisses, picks up energy, and eventually reaches paradise. This section lays out the basics of such a game, using our iterative approach to program design. Our tour takes place at one of the scariest places of all: campus. A campus consists of buildings, one more dangerous than the other. Each building has a name and is connected to a bunch of other buildings. The player is always in a building. We refer to this building as the <#48349#>current location<#48349#>. To find out more about the location, the player can ask for a picture of the building and for the list of connections. The player can also move to one of the connected buildings by issuing a <#68824#><#48350#>go<#48350#><#68824#> command.
<#48353#>Exercise 37.5.1<#48353#> Provide structure and data definitions for buildings. Include a picture field in the structure. A campus is a list of buildings. Define a sample campus. See figure~#figricetour#48355> for a small example.~ external Solution<#68825#><#68825#> <#48361#>Exercise 37.5.2<#48361#> Develop a program that permits a player to move through the sample campus of exercise~#extourdata#48363>. The program should support at least three services:
  1. <#68826#><#48365#>show-me<#48365#><#68826#>, which produces the picture of the current location: see figure~#figricetour2#48366>;
  2. <#68827#><#48367#>where-2-go<#48367#><#68827#>, which produces the list of connected buildings;
  3. <#68828#><#48368#>go<#48368#><#68828#>, which changes the current location of the player.
If the player issues the command <#68829#><#48370#>(go<#48370#>\ <#48371#>s)<#48371#><#68829#> and <#68830#><#48372#>s<#48372#><#68830#> is not connected to the current location, the function must report an error. Develop other functions as necessary or as desired.~ external Solution<#68831#><#68831#>

~


rawhtml61

<#48380#>Figure: Take the tour<#48380#>


Players of early maze games could also gather objects and trade objects at the various sites. Specifically, the player had a bag for carrying objects, and each site contained objects that the player could trade for things in the bag.
<#48384#>Exercise 37.5.3<#48384#> Modify the tour program so that each building contains one object. Furthermore, the player should have a <#48386#>bag<#48386#> that contains (at most) one object. At each location, the player can pick up an object, if the bag is empty, or swap the object in the bag for the object in the building otherwise. Modify the program further so that the player can carry an arbitrary number of objects in the bag.~ external Solution<#68832#><#68832#>
The three exercises in this section illustrate how maze games work. From here it is easy to experiment with various flavors of the game. Taking a walk from one building to another may take some energy, and the player may have only a finite amount of energy. Creatures may fight or kiss the player, which consumes or replenishes the player's energy level. Use your imagination to extend the game and have friends take the tour.