concept cave in category python

appears as: caves, cave, caves, cave
Hello! Python

This is an excerpt from Manning's book Hello! Python.

Let’s start with the “setup” part of the program . You’re storing a list of numbers in the program, each of which represents a cave. Don’t worry too much about the first line—you’ll learn more about the import statement in chapter 3. The choice function will return one of the caves, picked at random, and you use it to place the wumpus and the player in their starting positions. Note the loop at the end that you use to tell if the player and the wumpus are in the same spot—it wouldn’t be a fun game if the player got eaten right away!

The introductory text tells the player how the game works. You use the len() function to tell how many caves there are. This is useful because you may want to change the number of caves at a later point, and using a function like this means you only have to change things in one place when you define the list of caves.

Your main game loop is where the game starts. When playing the game, the program gives the player details of what the player can see, asks the player to enter a cave, checks to see whether the player has been eaten, and then starts over at the beginning. while loops will loop as long as their condition is true, so while True: means “loop over and over again without stopping” (you’ll handle the stopping part in a minute).

The first if statement tells the player where the player is and prints a warning if the wumpus is only one room away (“I smell a wumpus!”). Note how you’re using the player_location and wumpus_location variables. Because they’re numbers, you can add to and subtract from them. If the player is in cave 3, and the wumpus is in cave 4, then the player_location == wumpus_location - 1 condition will be true, and Python will display the message.

You then ask the player which cave the player wants next . You do some checking to see that the player has put in the right sort of input. It has to be a number, and it has to be one of the caves. Note also that the input will be a string, not a number, so you have to convert it using the int() function. If it doesn’t match what you need, you display a message to the player.

What this tells you is that cave 0 (don’t forget that lists start with their index at 0) links to caves 2, 3, and 7; cave 1 links to caves 5, 6, and 12; and so on. Because the caves are generated randomly, your numbers will be different, but the overall structure will be the same. The number of the cave is the same as its index in the list so that Python can easily find the exits later. Let’s replace section 1 of listing 2.1 with the following listing so that it sets up your new and improved cave system.

Listing 2.4. Setting up your caves

You’re still using a range function to generate the list of caves, but you’ve changed the range so that it starts at 0 instead of 1, to match the indexes of your list. Then you make an empty list for each of the caves that you’re supposed to have. At this point, it’s a list of unconnected caves.

Connecting caves is straightforward—when you create a one-way tunnel, you add another one way tunnel back the way you came. Every time you say caves[a].append[b], you also say caves[b].append[a]. The program looks something like the following listing.

Listing 2.6. Creating a linked cave network

First, create a list of caves that you haven’t visited, and visit cave 0 . You loop until unvisited_caves is empty ; that is, there are no unvisited caves left. You pick one that has fewer than three tunnels to other caves . If you link 1 cave to 10 others, the game will be too hard, because it will be difficult or impossible to work out which tunnel leads to the wumpus.

is where you’re building the cave. You pick a random unvisited cave, put a tunnel in the old cave to the new one, and then link from the new one back to the old one. This way you know that players can find their way back. In figure 2.2, you’re adding cave 3 to your structure— it will get linked to one of either cave 0, 1, or 2.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest