A source of possible error in this adventure game is that the names for the rooms appear in several places. If the Dining Room is spelled “DiningRoom” in one place and “DinngRoom” (missing the second “i”) in another place, the game won’t work correctly. The more rooms you add, and the more places where you have the room names typed, the odds increase that the error will be made.
There are a couple of ways of making this error less likely to occur:
• Do not name the rooms with strings of characters. Instead, use numbers. It’s easier to type and check “4” than “DiningRoom.”
• Use a variable for DiningRoom and use that one variable for checking the location. Then it doesn’t matter if you’re using numbers or strings (and strings are much easier to read and understand). Use one of these techniques to rewrite the adventure game with fewer potential errors.
Note: The first method simply requires replacing each example of the string value for each room with a unique number, as the below:
```
#This method just prints out the description for the current rooms
def playGame ():
#The initial location of the player
location = "1"
showIntroduction() #Shows the intro text
#Continue to run the game until the exit is reached
#Each loop the current room is shown, a direction is requested,
#and a new location is determined based on the direction
while not (location == "-1") :
showRoom(location)
direction = requestString("Which direction?")
printNow("You typed: "+direction)
location = pickRoom(direction, location)
def showRoom(room):
printNow("===========")
if room =="1":
showPorch()
elif room == "2":
showEntryway()
elif room =="3":
showKitchen()
elif room=="4":
showLR()
elif room=="5":
showDR()
elif room=="6":
showStairs()
elif room=="7":
showMR()
elif room == "8":
showUW()
elif room == "9":
showTunnel()
elif room == "10":
showStorage()
etc.
```
Note: Alternatively, the second method involves the creation of global/file-level variables to store the string values of each room and then to make use of those variables in each place the string value had been used.
```
#Different room string values
porch = "Porch"
entryway = "Entryway"
kitchen = "Kitchen"
lr = "LivingRoom"
dr = "DiningRoom"
stairs = "Stairs"
mr = "MysteryRoom"
uw = "UndergroundWorld"
tunnel = "Tunnel"
storage= "Storage"
exitLoc = "Exit"
def playGame ():
#The initial location of the player
location = porch
showIntroduction() #Shows the intro text
#Continue to run the game until the exit is reached
#Each loop the current room is shown, a direction is requested,
#and a new location is determined based on the direction
while not (location == exitLoc) :
showRoom(location)
direction = requestString("Which direction?")
printNow("You typed: "+direction)
location = pickRoom(direction, location)
#This method just prints out the description for the current rooms
def showRoom(room):
printNow("===========")
if room ==porch:
showPorch()
elif room == entryway:
showEntryway()
elif room ==kitchen:
showKitchen()
elif room==lr:
showLR()
elif room==dr:
showDR()
elif room==stairs:
showStairs()
elif room==mr:
showMR()
elif room == uw:
showUW()
elif room == tunnel:
showTunnel()
elif room == storage:
showStorage()
etc
```
You might also like to view...
In the context of a storyboard-sketch, an object's ____ indicates which direction it is facing.
A. orientation B. viewpoint C. position D. pose
What is the overall best case for space complexity?
a. An algorithm that uses zero or no memory b. An algorithm that uses the same space as the input data size c. An algorithm working on data size of one element d. An algorithm working on duplicate elements
The term graphic includes all of the following EXCEPT:
A) photographs. B) line art. C) cartoon drawings. D) tables and borders.
The name of a memory location in C++ can begin with a letter or a number.
Answer the following statement true (T) or false (F)