Chapter 9 - Sonar

Sonar is a technique that ships use to locate objects under the sea. In this chapter's game, the player places sonar devices at various places in the ocean to locate sunken treasure chests. The sonar devices in our game can only tell the player how far away a treasure chest is from the sonar device, but not in what direction. But by placing multiple sonar devices down, the player can figure out where exactly the treasure chest is. There are three chests to collect, but the player has only sixteen sonar devices to use to find them.

Imagine that we could not see the treasure chest (the red dot) in the following picture. Because each sonar device can only find the distance but not direction, the possible places the treasure could be is anywhere in a ring around the sonar device:

But if we have multiple sonar devices combined together, we can narrow it down to one exact place where all the rings intersect each other:

Grids and Cartesian Coordinates

A common problem in many games is how to talk about exact points in some area. A common way of solving this is by marking each individual row and column on a board with letters and numbers. Here is a chess board that has each row marked with numbers, and each column marked with letters.

In chess, the knight piece looks like a horse head. We can say that the white knight is located at the point e, 6. The black knight is located at point a, 4. We can also say that every space on row 7 or every space in column c is empty.

A grid with labeled rows and columns like this is called a Cartesian coordinate system. By using a row label and column label, we can give a coordinate that points to exactly one and only one space. This can really help us describe to a computer an exact location. If you have learned about Cartesian coordinate systems in math class, you may know that usually we have numbers for both the rows and columns. This is handy, because otherwise after the 26th column we would run out of letters. That board would look like this:

The numbers going left and right that describe the columns are part of the X-axis. The numbers going up and down that describe the rows are part of the Y-axis. When we describe coordinates, we always say the X coordinate first, followed by the Y coordinate. That means the white knight in the above picture is located at the coordinate 5, 6. The black knight is located at the coordinate 1, 4.

Notice that for the black knight to move to the white knight's position, the black knight must move up two spaces, and then to the right by four spaces. (Or move right four spaces and then move up two spaces.) But we don't need to look at the board to figure this out. If we know the white knight is located at 5, 6 and the black knight is located at 1, 4, then we can just use subtraction to figure out this information.

Subtract the black knight's X coordinate and white knight's X coordinate: 5 - 1 = 4. That means the black knight has to move along the X-axis by four spaces.

Subtract the black knight's Y coordinate and white knight's Y coordinate: 6 - 4 = 2. That means the black knight has to move along the Y-axis by two spaces.

Negative Numbers

Another concept that Cartesian coordinates use is negative numbers. Negative numbers are numbers that are smaller than zero. We put a minus sign in front of a number to show that it is a negative number. -1 is smaller than 0. -2 is smaller than -1. -3 is smaller than -2. If you think of regular numbers (called positive numbers) as starting from 1 and increasing, you can think of negative numbers as starting from -1 and decreasing. 0 itself is not positive or negative. In this picture, you can see the positive numbers increasing to the right and the negative numbers decreasing to the left:

The number line is really useful for doing subtraction and addition with negative numbers. The expression 4 + 3 can be thought of as the white knight starting at position 4 and moving 3 spaces over to the right (addition means increasing, which is in the right direction).

As you can see, the white knight ends up at position 7. This makes sense, because 4 + 3 is 7.

Subtraction can be done by moving the white knight to the left. Subtraction means decreasing, which is in the left direction. 4 - 6 would be the white knight starting at position 4 and moving 6 spaces to the left:

The white knight ends up at position -2. That means 4 - 6 equals -2.

If we add or subtract a negative number, the white knight would move in the opposite direction. If you add a negative number, the knight moves to the left. If you subtract a negative number, the knight moves to the right. The expression -6 - -4 would be equal to -2. The knight starts at -6 and moves to the right by 4 spaces. Notice that -6 - -4 has the same answer as -6 + 4.

The number line is the same as the X-axis. If we made the number line go up and down instead of left and right, it would model the Y-axis. Adding a positive number (or subtracting a negative number) would move the knight up the number line, and subtracting a positive number (or adding a negative number) would move the knight down. When we put these two number lines together, we have a Cartesian coordinate system.

The 0, 0 coordinate has a special name and is called the origin.

Changing the Signs

Subtracting negative numbers or adding negative numbers may seem easy when you have a number line in front of you, but it is also easy when you only have the numbers too. Here are three tricks you can do to make evaluating these expressions easier to do.

The first is if you are adding a negative number, for example, 4 + -2. The first trick is "a minus eats the plus sign on its left". When you see a minus sign with a plus sign on the left, you can replace the plus sign with a minus sign. The answer is still the same, because adding a negative value is the same as subtracting a positive value. 4 + -2 and 4 - 2 both evaluate to 2.

The second trick is if you are subtracting a negative number, for example, 4 - -2. The second trick is "two minuses combine into a plus". When you see the two minus signs next to each other without a number in between them, they can combine into a plus sign. The answer is still the same, because subtracting a negative value is the same as adding a positive value.

A third trick is to remember that when you add two numbers like 6 and 4, it doesn't matter what order they are in. (This is called the commutative property of addition.) That means that 6 + 4 and 4 + 6 both equal the same value, 10.

Say you are adding a negative number and a positive number, like -6 + 8. Because you are adding numbers, you can swap the order of the numbers without changing the answer. -6 + 8 is the same as 8 + -6. But when you look at 8 + -6, you see that the minus sign can eat the plus sign to its left, and the problem becomes 8 - 6. This is easy subtraction, the answer is 2. But this means that -6 + 8 is also 2! We've rearranged the problem to have the same answer, but made it easier to solve.

Of course, you can always use the interactive shell as a calculator to evaluate these expressions. It is still very useful to know the above three tricks when adding or subtracting negative numbers. After all, you won't always be in front of a computer with Python all the time!

Absolute Values

The absolute value of a number is the number without the negative sign in front of it. This means that positive numbers do not change, but negative numbers become the same as their twin positive number. For example, the absolute value of -4 is 4. The absolute value of -7 is 7. The absolute value of 5 (which is positive) is just 5.

We can find how far away two things on a number line are from each other by taking the absolute value of their difference. Imagine that the white knight is at position 4 and the black knight is at position -2. To find out the distance between them, you would find the difference by subtracting their positions and taking the absolute value of that number.

It works no matter what the order of the numbers is. -2 - 4 (that is, negative two minus four) is -6, and the absolute value of -6 is 6. However, 4 - -2 (that is, four minus negative two) is 6, and the absolute value of 6 is 6. Using the absolute value of the difference is a good way of finding the distance between two points on a number line (or axis).

Coordinate System of a Computer Monitor

It is common that computer monitors use a coordinate system that has the origin at the top left corner of the screen, which increases going down and to the right. There are no negative coordinates. This is because text is printed starting at the top left, and is printed going to the right and downwards. Most computer graphics use this coordinate system, and we will use it in our games. Also it is common to assume that monitors can display 80 text characters per row and 25 text characters per column. This used to be the maximum screen size that monitors could support. While today's monitors can usually display much more text, we will not assume that the user's screen is bigger than 80 by 25.

Sample Run

To keep the length of this book short, the middle part of this game has been cut out.

S O N A R !

Would you like to view the instructions? (yes/no)
no
             1         2         3         4         5
   012345678901234567890123456789012345678901234567890123456789

 0 `~~~`~~~`~`~~`~~~~~`~``~~~~`~`~~~`~``~``~~````~`~```~`~~~~`` 0
 1 ~`~~~```~~~~`~`~~`~``~`~~```~`~`~~`~`~~~~~~`~`````~`~~`~~~~` 1
 2 `~``~``~~~`~``~`~`~``~`````~~~~~~~~~`~`~~`~``~~~~~```~~`~``` 2
 3 ``~`~~``~`~``~`~`~`~~`~`~~`~`~``~~~`~``~````~``````~~~~``~`` 3
 4 ``~~`~~~``~``~~````~`~`~`~``~~~``~~```~`~~`~~`~`~`~~`~~~~``` 4
 5 ~~```~~~`~`~~``~`~``~```~`~~`~~~~~`~~``~`~`~~~`~~`~`~`~`~~~` 5
 6 ``~~`````~~~~`~`~~~```~~~~`~~`~~`~~```~~`~~~`~~~``~`~~~``~~~ 6
 7 `~`````````~```~``~``~~`~~~~`~~``~``~~~```~`~~`~``~``~~```~~ 7
 8 `~````~```~`~~`~~~`~~``~~~``~`~``~~~``~`~`````~`~~```~`~~~~` 8
 9 ~```~~`~`~``~``~~``~``~```~`~``~~~~`~`~`~~~`~`~`~`~~~``~~``` 9
10 ```~`~```~``~``~`~~`~``~````~``~~~`~~`~~``~~~~`~~~`~`~~````~ 10
11 ```~```~~~`~```~~`~~~`~`````~`~~`~`~~`~~`~`~~`~~~````~````~` 11
12 ~~~`~`~~~``~~~~~~`~~~``~`~`~~`~`~~`~```~~~```~~`~~`~``~``~`~ 12
13 `~~````~~``~```~~~`~```~`~~~~~~~~~`~~``~~~~~`````~`~`~``~~~~ 13
14 `~~`~`~````~```~`~`~```~~`~~~~`~```~``~``~``~~~````~~``````~ 14

   012345678901234567890123456789012345678901234567890123456789
             1         2         3         4         5
You have 16 sonar devices left. 3 treasure chests remaining.
Where do you want to drop the next sonar device? (0-59 0-14) (or type quit)
10 10
             1         2         3         4         5
   012345678901234567890123456789012345678901234567890123456789

 0 `~~~`~~~`~`~~`~~~~~`~``~~~~`~`~~~`~``~``~~````~`~```~`~~~~`` 0
 1 ~`~~~```~~~~`~`~~`~``~`~~```~`~`~~`~`~~~~~~`~`````~`~~`~~~~` 1
 2 `~``~``~~~`~``~`~`~``~`````~~~~~~~~~`~`~~`~``~~~~~```~~`~``` 2
 3 ``~`~~``~`~``~`~`~`~~`~`~~`~`~``~~~`~``~````~``````~~~~``~`` 3
 4 ``~~`~~~``~``~~````~`~`~`~``~~~``~~```~`~~`~~`~`~`~~`~~~~``` 4
 5 ~~```~~~`~`~~``~`~``~```~`~~`~~~~~`~~``~`~`~~~`~~`~`~`~`~~~` 5
 6 ``~~`````~~~~`~`~~~```~~~~`~~`~~`~~```~~`~~~`~~~``~`~~~``~~~ 6
 7 `~`````````~```~``~``~~`~~~~`~~``~``~~~```~`~~`~``~``~~```~~ 7
 8 `~````~```~`~~`~~~`~~``~~~``~`~``~~~``~`~`````~`~~```~`~~~~` 8
 9 ~```~~`~`~``~``~~``~``~```~`~``~~~~`~`~`~~~`~`~`~`~~~``~~``` 9
10 ```~`~```~5`~``~`~~`~``~````~``~~~`~~`~~``~~~~`~~~`~`~~````~ 10
11 ```~```~~~`~```~~`~~~`~`````~`~~`~`~~`~~`~`~~`~~~````~````~` 11
12 ~~~`~`~~~``~~~~~~`~~~``~`~`~~`~`~~`~```~~~```~~`~~`~``~``~`~ 12
13 `~~````~~``~```~~~`~```~`~~~~~~~~~`~~``~~~~~`````~`~`~``~~~~ 13
14 `~~`~`~````~```~`~`~```~~`~~~~`~```~``~``~``~~~````~~``````~ 14

   012345678901234567890123456789012345678901234567890123456789
             1         2         3         4         5
Treasure detected at a distance of 5 from the sonar device.
You have 15 sonar devices left. 3 treasure chests remaining.
Where do you want to drop the next sonar device? (0-59 0-14) (or type quit)
15 6
             1         2         3         4         5
   012345678901234567890123456789012345678901234567890123456789

 0 `~~~`~~~`~`~~`~~~~~`~``~~~~`~`~~~`~``~``~~````~`~```~`~~~~`` 0
 1 ~`~~~```~~~~`~`~~`~``~`~~```~`~`~~`~`~~~~~~`~`````~`~~`~~~~` 1
 2 `~``~``~~~`~``~`~`~``~`````~~~~~~~~~`~`~~`~``~~~~~```~~`~``` 2
 3 ``~`~~``~`~``~`~`~`~~`~`~~`~`~``~~~`~``~````~``````~~~~``~`` 3
 4 ``~~`~~~``~``~~````~`~`~`~``~~~``~~```~`~~`~~`~`~`~~`~~~~``` 4
 5 ~~```~~~`~`~~``~`~``~```~`~~`~~~~~`~~``~`~`~~~`~~`~`~`~`~~~` 5
 6 ``~~`````~~~~`~4~~~```~~~~`~~`~~`~~```~~`~~~`~~~``~`~~~``~~~ 6
 7 `~`````````~```~``~``~~`~~~~`~~``~``~~~```~`~~`~``~``~~```~~ 7
 8 `~````~```~`~~`~~~`~~``~~~``~`~``~~~``~`~`````~`~~```~`~~~~` 8
 9 ~```~~`~`~``~``~~``~``~```~`~``~~~~`~`~`~~~`~`~`~`~~~``~~``` 9
10 ```~`~```~5`~``~`~~`~``~````~``~~~`~~`~~``~~~~`~~~`~`~~````~ 10
11 ```~```~~~`~```~~`~~~`~`````~`~~`~`~~`~~`~`~~`~~~````~````~` 11
12 ~~~`~`~~~``~~~~~~`~~~``~`~`~~`~`~~`~```~~~```~~`~~`~``~``~`~ 12
13 `~~````~~``~```~~~`~```~`~~~~~~~~~`~~``~~~~~`````~`~`~``~~~~ 13
14 `~~`~`~````~```~`~`~```~~`~~~~`~```~``~``~``~~~````~~``````~ 14

   012345678901234567890123456789012345678901234567890123456789
             1         2         3         4         5
Treasure detected at a distance of 4 from the sonar device.
You have 14 sonar devices left. 3 treasure chests remaining.
Where do you want to drop the next sonar device? (0-59 0-14) (or type quit)
15 10
             1         2         3         4         5
   012345678901234567890123456789012345678901234567890123456789

 0 `~~~`~~~`~`~~`~~~~~`~``~~~~`~`~~~`~``~``~~````~`~```~`~~~~`` 0
 1 ~`~~~```~~~~`~`~~`~``~`~~```~`~`~~`~`~~~~~~`~`````~`~~`~~~~` 1
 2 `~``~``~~~`~``~`~`~``~`````~~~~~~~~~`~`~~`~``~~~~~```~~`~``` 2
 3 ``~`~~``~`~``~`~`~`~~`~`~~`~`~``~~~`~``~````~``````~~~~``~`` 3
 4 ``~~`~~~``~``~~````~`~`~`~``~~~``~~```~`~~`~~`~`~`~~`~~~~``` 4
 5 ~~```~~~`~`~~``~`~``~```~`~~`~~~~~`~~``~`~`~~~`~~`~`~`~`~~~` 5
 6 ``~~`````~~~~`~O~~~```~~~~`~~`~~`~~```~~`~~~`~~~``~`~~~``~~~ 6
 7 `~`````````~```~``~``~~`~~~~`~~``~``~~~```~`~~`~``~``~~```~~ 7
 8 `~````~```~`~~`~~~`~~``~~~``~`~``~~~``~`~`````~`~~```~`~~~~` 8
 9 ~```~~`~`~``~``~~``~``~```~`~``~~~~`~`~`~~~`~`~`~`~~~``~~``` 9
10 ```~`~```~O`~``O`~~`~``~````~``~~~`~~`~~``~~~~`~~~`~`~~````~ 10
11 ```~```~~~`~```~~`~~~`~`````~`~~`~`~~`~~`~`~~`~~~````~````~` 11
12 ~~~`~`~~~``~~~~~~`~~~``~`~`~~`~`~~`~```~~~```~~`~~`~``~``~`~ 12
13 `~~````~~``~```~~~`~```~`~~~~~~~~~`~~``~~~~~`````~`~`~``~~~~ 13
14 `~~`~`~````~```~`~`~```~~`~~~~`~```~``~``~``~~~````~~``````~ 14

   012345678901234567890123456789012345678901234567890123456789
             1         2         3         4         5
You have found a sunken treasure chest!
You have 13 sonar devices left. 2 treasure chests remaining.
Where do you want to drop the next sonar device? (0-59 0-14) (or type quit)


...skipped over for brevity....


             1         2         3         4         5
   012345678901234567890123456789012345678901234567890123456789

 0 `~~~`~~~`~`~~`~~~~~`~``~~~~`~`~~~`~``~``~~````~`~```~`~~~~`` 0
 1 ~`~~~```~~~~`~`~~`~``~`~~```~O~`~~`~`~~~~~~`~`````~`~~`~~~~` 1
 2 `~``~``~~~`~``~`~`~``~`````~~O~~~O~~`~`~~`~``~~~~~```~~`~``` 2
 3 ``~3~~``8`~``~`~`~`~~`~`~~`~`~``~~~`~`O~````~``````~~~~``~`` 3
 4 ``~~`~~~``~``~~````~`~`~`~O`~~O``~~```~`~~`~~`~`~`~~`~~~~``` 4
 5 ~~```~~~`~`~~``~`~``~```~`~~`~~~~~`~~``~`~`~~~`~~`~`~`~`~~~` 5
 6 ``~~`````~~~~`~O~~~```~~~~`~~`~~`~~```~~`~~~`~~~``O`~~~``~~~ 6
 7 `~`````````~```~``~``~~`~~~~`~~``~``~~~```~`~~`~``~``~~```~~ 7
 8 `~````~```~`~~`~~~`~~``~~~``~`~``~~~``~`O```0`~`~~```~`~~~~` 8
 9 ~```~~`~`~``~``~~``~``~```~O~``~~~~`~`~`~~~`~`~`~`~~~``~~``` 9
10 ```~`~```~O`~``O`~~`~``~````~``~~~`~~`~~``~~~~`~~~`~`~~````~ 10
11 ```~```~~~`~```~~`~~~`~`````~`~~`~`~~`~~`~`~~`~~~````~````~` 11
12 ~~~`~`~~~``~~~~~~`~~~``~`~`~~`~`~~`~```~~~```~~`~~`~``~``~`~ 12
13 `~~````~~``~```~~~`~```~`~~~~~~~~~`~~``~~~~~`````~`~`~``~~~~ 13
14 `~~`~`~````~```~`~`~```~~`~~~~`~```~``~``~``~~~````~~``````~ 14

   012345678901234567890123456789012345678901234567890123456789
             1         2         3         4         5
Treasure detected at a distance of 4 from the sonar device.
We've run out of sonar devices! Now we have to turn the ship around and head
for home with treasure chests still out there! Game over.
    The remaining chests were here:
    0, 4
Do you want to play again? (yes or no)
no

Source Code

Knowing about Cartesian coordinates, number lines, negative numbers, and absolute values will help us out with our Sonar game. Here is the source code for the game. Type it into a new file, then save the file as sonar.py and run it by pressing the F5 key. You do not need to understand the code to type it in or play the game, the source code will be explained later.

sonar.py
  1. # Sonar
  2. import random
  3. import sys
  4. def drawBoard(board):
  5.     # Draw the board data structure.
  6.     hline = '    ' # initial space for the numbers down the left side of the board
  7.     for i in range(1, 6):
  8.         hline += (' ' * 9) + str(i)
  9.     # print the numbers across the top    
  10.     print hline
  11.     print '   ' + ('0123456789' * 6)
  12.     print
  13.     # print each of the 15 rows
  14.     for i in range(15):
  15.         # single-digit numbers need to be padded with an extra space
  16.         if i < 10:
  17.             extraSpace = ' '
  18.         else:
  19.             extraSpace = ''
  20.         print '%s%s %s %s' % (extraSpace, i, getRow(board, i), i)
  21.     # print the numbers across the bottom
  22.     print
  23.     print '   ' + ('0123456789' * 6)
  24.     print hline
  25. def getRow(board, row):
  26.     # Return a string from the board data structure at a certain row.
  27.     boardRow = ''
  28.     for i in range(60):
  29.         boardRow += board[i][row]
  30.     return boardRow
  31. def getNewBoard():
  32.     # Create a new 60x15 board data structure.
  33.     board = []
  34.     for x in range(60): # the main list is a list of 60 lists
  35.         board.append([])
  36.         for y in range(15): # each list in the main list has 15 single-character strings
  37.             # use different characters for the ocean to make it more readable.
  38.             if random.randint(0, 1) == 0:
  39.                 board[x].append('~')
  40.             else:
  41.                 board[x].append('`')
  42.     return board
  43. def getRandomChests(numChests):
  44.     # Create a list of chest data structures (two-item lists of x, y int coordinates)
  45.     chests = []
  46.     for i in range(numChests):
  47.         chests.append([random.randint(0, 59), random.randint(0, 14)])
  48.     return chests
  49. def isValidMove(x, y):
  50.     # Return True if the coordinates are on the board, otherwise False.
  51.     return x >= 0 and x <= 59 and y >= 0 and y <= 14
  52. def makeMove(board, chests, x, y):
  53.     # Change the board data structure with a sonar device character. Remove treasure chests
  54.     # from the chests list as they are found. Return False if this is an invalid move.
  55.     # Otherwise, return the string of the result of this move.
  56.     if not isValidMove(x, y):
  57.         return False
  58.     smallestDistance = 100 # any chest will be closer than 100.
  59.     for cx, cy in chests:
  60.         if abs(cx - x) > abs(cy - y):
  61.             distance = abs(cx - x)
  62.         else:
  63.             distance = abs(cy - y)
  64.         if distance < smallestDistance: # we want the closest treasure chest.
  65.             smallestDistance = distance
  66.     if smallestDistance == 0:
  67.         # xy is directly on a treasure chest!
  68.         chests.remove([x, y])
  69.         return 'You have found a sunken treasure chest!'
  70.     else:
  71.         if smallestDistance < 10:
  72.             board[x][y] = str(smallestDistance)
  73.             return 'Treasure detected at a distance of %s from the sonar device.' % (smallestDistance)
  74.         else:
  75.             board[x][y] = 'O'
  76.             return 'Sonar did not detect anything. All treasure chests out of range.'
  77.             
  78. def enterPlayerMove():
  79.     # Let the player type in their move. Return a two-item list of int xy coordinates.
  80.     print 'Where do you want to drop the next sonar device? (0-59 0-14) (or type quit)'
  81.     while True:
  82.         move = raw_input()
  83.         if move.lower() == 'quit':
  84.             print 'Thanks for playing!'
  85.             sys.exit()
  86.         move = move.split()
  87.         if len(move) == 2 and move[0].isdigit() and move[1].isdigit() and isValidMove(int(move[0]), int(move[1])):
  88.             return [int(move[0]), int(move[1])]
  89.         print 'Enter a number from 0 to 59, a space, then a number from 0 to 14.'
  90. def playAgain():
  91.     # This function returns True if the player wants to play again, otherwise it returns False.
  92.     print 'Do you want to play again? (yes or no)'
  93.     return raw_input().lower().startswith('y')
  94. def showInstructions():
  95.     print '''Instructions:
  96. You are the captain of the Simon, a treasure-hunting ship. Your current mission
  97. is to find the three sunken treasure chests that are lurking in the part of the
  98. ocean you are in and collect them.
  99. To play, enter the coordinates of the point in the ocean you wish to drop a
  100. sonar device. The sonar can find out how far away the closest chest is to it.
  101. For example, the d below marks where the device was dropped, and the 2's
  102. represent distances of 2 away from the device. The 4's represent
  103. distances of 4 away from the device.
  104.     444444444
  105.     4       4
  106.     4 22222 4
  107.     4 2   2 4
  108.     4 2 d 2 4
  109.     4 2   2 4
  110.     4 22222 4
  111.     4       4
  112.     444444444
  113. Press enter to continue...'''
  114.     raw_input()
  115.     print '''For example, here is a treasure chest (the c) located a distance of 2 away
  116. from the sonar device (the d):
  117.     22222
  118.     c   2
  119.     2 d 2
  120.     2   2
  121.     22222
  122. The point where the device was dropped will be marked with a 2.
  123. The treasure chests don't move around. Sonar devices can detect treasure
  124. chests up to a distance of 9. If all chests are out of range, the point
  125. will be marked with O
  126. If a device is directly dropped on a treasure chest, you have discovered
  127. the location of the chest, and it will be collected. The sonar device will
  128. remain there.
  129. When you collect a chest, all sonar devices will update to locate the next
  130. closest sunken treasure chest.
  131. Press enter to continue...'''
  132.     raw_input()
  133.     print
  134. print 'S O N A R !'
  135. print
  136. print 'Would you like to view the instructions? (yes/no)'
  137. if raw_input().lower().startswith('y'):
  138.     showInstructions()
  139. while True:
  140.     # game setup
  141.     sonarDevices = 16
  142.     theBoard = getNewBoard()
  143.     theChests = getRandomChests(3)
  144.     drawBoard(theBoard)
  145.     previousMoves = []
  146.     while sonarDevices > 0:
  147.         # Start of a turn:
  148.         # sonar device/chest status
  149.         if sonarDevices > 1: extraSsonar = 's'
  150.         else: extraSsonar = ''
  151.         if len(theChests) > 1: extraSchest = 's'
  152.         else: extraSchest = ''
  153.         print 'You have %s sonar device%s left. %s treasure chest%s remaining.' % (sonarDevices, extraSsonar, len(theChests), extraSchest)
  154.         x, y = enterPlayerMove()
  155.         previousMoves.append([x, y]) # we must track all moves so that sonar devices can be updated.
  156.         moveResult = makeMove(theBoard, theChests, x, y)
  157.         if moveResult == False:
  158.             continue
  159.         else:
  160.             if moveResult == 'You have found a sunken treasure chest!':
  161.                 # update all the sonar devices currently on the map.
  162.                 for x, y in previousMoves:
  163.                     makeMove(theBoard, theChests, x, y)
  164.             drawBoard(theBoard)
  165.             print moveResult
  166.         if len(theChests) == 0:
  167.             print 'You have found all the sunken treasure chests! Congratulations and good game!'
  168.             break
  169.         sonarDevices -= 1
  170.     if sonarDevices == 0:
  171.         print 'We\'ve run out of sonar devices! Now we have to turn the ship around and head'
  172.         print 'for home with treasure chests still out there! Game over.'
  173.         print '    The remaining chests were here:'
  174.         for x, y in theChests:
  175.             print '    %s, %s' % (x, y)
  176.     if not playAgain():
  177.         sys.exit()

Designing the Program

Normally we show the flow chart for a game before you type in the source code. But Sonar is kind of complicated, so you should type in the game and play it a few times first. After you've played the game a few times, you can kind of get an idea of the sequence of events in this game. For your own games, you should draw out the flow chart first to help you figure out what exactly you need to program. Here is a flow chart for the Sonar game:

It may also help us to write out on paper the things we need our program to do, and come up with some function names that will handle these actions. Remember, this is helpful before we start typing in the source code as a reminder of everything our code needs to do. Otherwise we might end up forgetting a function, or typing in two different functions that do the same thing.

What the code should do. The function that will do it.
Prints the game board on the screen based on the board data structure it is passed, including the coordinates along the top, bottom, and left and right sides. drawBoard()
Create a fresh board data structure. getNewBoard()
Create a fresh chests data structure that has a number of chests randomly scattered across the game board. getRandomChests()
Check that the XY coordinates that are passed to this function are located on the game board or not. isValidMove()
Let the player type in the XY coordinates of their next move, and keep asking until they type in the coordinates correctly. enterPlayerMove()
Place a sonar device on the game board, and update the board data structure then return a string that describes what happened. makeMove()
Ask the player if they want to play another game of Sonar. playAgain()
Print out instructions for the game. showInstructions()

These might not be all of the functions we need, but a list like this is a good idea to help you get started with programming your own games. For example, when we are writing the drawBoard() function in the Sonar game, we figure out that we also need a getRow() function. Writing out a function once and then calling it twice is preferable to writing out the code twice. The whole point of functions is to reduce duplicate code down to one place, so if we ever need to make changes to that code we only need to change one place in our program.

Code Explanation

  1. # Sonar
  2. import random
  3. import sys

Here we import two modules, random and sys. The sys module contains the exit() function, which causes the program to immediately terminate. We will call this function on line 101.

  1. def drawBoard(board):

The drawBoard() function is the first function we will define for our program. The sonar game's board is an ASCII-art ocean with coordinates going along the X- and Y-axis, and looks like this:

             1         2         3         4         5
   012345678901234567890123456789012345678901234567890123456789

 0 ~~~`~``~~~``~~~~``~`~`~`~`~~`~~~`~~`~``````~~`~``~`~~```~`~` 0
 1 `~`~````~~``~`~```~```~```~`~~~``~~`~~~``````~`~``~~``~~`~~` 1
 2 ```~~~~`~`~~```~~~``~````~~`~`~~`~`~`~```~~`~``~~`~`~~~~~~`~ 2
 3 ~~~~`~~~``~```~``~~`~`~~`~`~~``~````~`~````~```~`~`~`~`````~ 3
 4 ~```~~~~~`~~````~~~~```~~~`~`~`~````~`~~`~`~~``~~`~``~`~``~~ 4
 5 `~```~`~`~~`~~~```~~``~``````~~``~`~`~~~~`~~``~~~~~~`~```~~` 5
 6 ``~~`~~`~``~`````~````~~``~`~~~~`~~```~~~``~`~`~~``~~~```~~~ 6
 7 ``~``~~~~~~```~`~```~~~``~`~``~`~~~~~~```````~~~`~~`~~`~~`~~ 7
 8 ~~`~`~~```~``~~``~~~``~~`~`~~`~`~```~```~~~```~~~~~~`~`~~~~` 8
 9 ```~``~`~~~`~~```~``~``~~~```~````~```~`~~`~~~~~`~``~~~~~``` 9
10 `~~~~```~`~````~`~`~~``~`~~~~`~``~``~```~~```````~`~``~````` 10
11 ~~`~`~~`~``~`~~~````````````````~~`````~`~~``~`~~~~`~~~`~~`~ 11
12 ~~`~~~~```~~~`````~~``~`~`~~``````~`~~``~```````~~``~~~`~~`~ 12
13 `~``````~~``~`~~~```~~~~```~~`~`~~~`~```````~~`~```~``~`~~~~ 13
14 ~~~``~```~`````~~`~`~``~~`~``~`~~`~`~``~`~``~~``~`~``~```~~~ 14

   012345678901234567890123456789012345678901234567890123456789
             1         2         3         4         5

The backtick (`) and tilde (~) characters are located next to the 1 key on your keyboard. They are suppose to resemble the waves of the ocean. Somewhere in this ocean are three treasure chests, but you don't know where. You have to figure it out by planting sonar devices, and you can tell the game program where by typing in the X and Y coordinates (which are printed on the four sides of the screen.)

We will split up the drawing in the drawBoard() function into four steps. First, we create a string variable of the line with 1, 2, 3, 4, and 5 spaced out with wide gaps. Second, we use that string to display the X-axis coordinates along the top of the screen. Third, we print each row of the ocean along with the Y-axis coordinates on both sides of the screen. And fourth, we print out the X-axis again at the bottom. Having the coordinates on all sides makes it easier for the player to move their finger along the spaces to see where exactly they want to plan a sonar device.

  1.     # Draw the board data structure.
  2.     hline = '    ' # initial space for the numbers down the left side of the board
  3.     for i in range(1, 6):
  4.         hline += (' ' * 9) + str(i)

Let's look again at the top part of the board, this time with red plus signs instead of blank spaces so we can count the spaces easier:


As you can see, the numbers on the first line which mark the tens position all have nine spaces in between them, and there are thirteen spaces in front of the 1. We are going to create a string with this line and store it in a variable named hline.

  1.     # print the numbers across the top    
  2.     print hline
  3.     print '   ' + ('0123456789' * 6)
  4.     print

To print the numbers across the top of the sonar board, we first print the contents of the hline variable. Then on the next line, we print three spaces (so that this row lines up correctly), and then the string '012345678901234567890123456789012345678901234567890123456789'. But this is tedious to type into the source, so instead we type ('0123456789' * 6) which evaluates to the same string.

  1.     # print each of the 15 rows
  2.     for i in range(15):
  3.         # single-digit numbers need to be padded with an extra space
  4.         if i < 10:
  5.             extraSpace = ' '
  6.         else:
  7.             extraSpace = ''
  8.         print '%s%s %s %s' % (extraSpace, i, getRow(board, i), i)

Now we print the each row of the board, including the numbers down the side to label the Y-axis. We use the for loop to print rows 0 through 14 on the board, along with the row numbers on either side of the board.

We have a small problem. Numbers with only one digit (like 0, 1, 2, and so on) only take up one space when we print them out, but numbers with two digits (like 10, 11, and 12) take up two spaces. This means the rows might not line up and would look like this:

8 ~~`~`~~```~``~~``~~~``~~`~`~~`~`~```~```~~~```~~~~~~`~`~~~~` 8
9 ```~``~`~~~`~~```~``~``~~~```~````~```~`~~`~~~~~`~``~~~~~``` 9
10 `~~~~```~`~````~`~`~~``~`~~~~`~``~``~```~~```````~`~``~````` 10
11 ~~`~`~~`~``~`~~~````````````````~~`````~`~~``~`~~~~`~~~`~~`~ 11

The solution is easy, we just add a space in front of all the single-digit numbers. This is what the if-else statement that starts on line 21 of our code does. We will print the variable extraSpace when we print the row, and if i is less than 10 (which means it will have only one digit), we assign a single space string to extraSpace. Otherwise, we set extraSpace to be a blank string. This way, all of our rows will line up when we print them.

The getRow() function will return a string representing the row number we pass it. Its two parameters are the board data structure stored in the board variable and a row number. We will look at this function next.

  1.     # print the numbers across the bottom
  2.     print
  3.     print '   ' + ('0123456789' * 6)
  4.     print hline

This code is similar to lines 14 to 17. This will print the X-axis coordinates along the bottom of the screen.

  1. def getRow(board, row):
  2.     # Return a string from the board data structure at a certain row.
  3.     boardRow = ''
  4.     for i in range(60):
  5.         boardRow += board[i][row]
  6.     return boardRow

This function constructs a string called boardRow from the characters stored in board. First we set boardRow to the blank string. The row number (which is the Y coordinate) is passed as a parameter. The string we want is made by concatenating board[0][row], board[1][row], board[2][row], and so on up to board[59][row]. (This is because the row is made up of 60 characters, from index 0 to index 59.)

The for loop iterates from integers 0 to 59. On each iteration the next character in the board data structure is copied on to the end of boardRow. By the time the loop is done, extraSpace is fully formed, so we return it.

  1. def getNewBoard():
  2.     # Create a new 60x15 board data structure.
  3.     board = []
  4.     for x in range(60): # the main list is a list of 60 lists
  5.         board.append([])

At the start of each new game, we will need a fresh board data structure. The board data structure is a list of lists of strings. The first list represents the X coordinate. Since our game's board is 60 characters across, this first list needs to contain 60 lists. So we create a for loop that will append 60 blank lists to it.

  1.         for y in range(15): # each list in the main list has 15 single-character strings
  2.             # use different characters for the ocean to make it more readable.
  3.             if random.randint(0, 1) == 0:
  4.                 board[x].append('~')
  5.             else:
  6.                 board[x].append('`')

But board is more than just a list of 60 blank lists. Each of the 60 lists represents the Y coordinate of our game board. There are 15 rows in the board, so each of these 60 lists must have 15 characters in them. We have another for loop to add 15 single-character strings that represent the ocean. The "ocean" will just be a bunch of '~' and '`' strings, so we will randomly choose between those two. We can do this by generating a random number between 0 and 1 with a call to random.randint(). If the return value of random.randint() is 0, we add the '~' string. Otherwise we will add the '`' string.

This is like deciding which character to use by tossing a coin. And since the return value from random.randint() will be 0 about half the time, half of the ocean characters will be '~' and the other half will be '`'. This will give our ocean a nice random, choppy look to it.

Remember that board variable is a list of 60 lists that have 15 strings. That means to get the string at coordinate 26, 12, we would access board[26][12], and not board[12][26]. The X coordinate is first, then the Y coordinate. We could have made board a list of 15 lists of 60 characters, but then to access the string at coordinate 26, 12 we would have to use board[12][26], which would be confusing since the coordinates are reversed.

Here is the picture from the Hangman chapter that demonstrates the indexes of a list of lists named x. The red arrows point to indexes of the inner lists themselves. The image is also flipped on its side to make it easier to read:

  1.     return board

Finally, we return the board variable. Remember that in this case, we are returning a reference to the list that we made. Any changes we made to the list (or the lists inside the list) in our function will still be there outside of the function.

  1. def getRandomChests(numChests):
  2.     # Create a list of chest data structures (two-item lists of x, y int coordinates)
  3.     chests = []
  4.     for i in range(numChests):
  5.         chests.append([random.randint(0, 59), random.randint(0, 14)])
  6.     return chests

Another task we need to do at the start of the game is decide where the hidden treasure chests are. We will represent the treasure chests in our game as a list of lists of two integers. These two integers will be the X and Y coordinates. For example, if the chest data structure was [[2, 2], [2, 4], [10, 0]], then this would mean there are three treasure chests, one at 2, 2, another at 2, 4, and a third one at 10, 0.

We will pass the numChests parameter to tell the function how many treasure chests we want it to generate. We set up a for loop to iterate this number of times, and on each iteration we append a list of two random integers. The X coordinate can be anywhere from 0 to 59, and the Y coordinate can be from anywhere between 0 and 14. The expression [random.randint(0, 59), random.randint(0, 14)] that is passed to the append method will evaluate to something like [2, 2] or [2, 4] or [10, 0]. This data structure is then returned.

  1. def isValidMove(x, y):
  2.     # Return True if the coordinates are on the board, otherwise False.
  3.     return x >= 0 and x <= 59 and y >= 0 and y <= 14

The player will be typing in X and Y coordinates of where they want to drop a sonar device. But they may type in coordinates that do not exist on the game board. The X coordinates must be between 0 and 59, and the Y coordinate must be between 0 and 14. This function uses a simple expression that uses and operators to ensure that each condition is True. If just one of these are False, then the entire expression evaluates to False. This boolean value is returned by the function.

  1. def makeMove(board, chests, x, y):
  2.     # Change the board data structure with a sonar device character. Remove treasure chests
  3.     # from the chests list as they are found. Return False if this is an invalid move.
  4.     # Otherwise, return the string of the result of this move.
  5.     if not isValidMove(x, y):
  6.         return False

In our Sonar game, the game board is updated to display a number for each sonar device dropped. The number shows how far away the closest treasure chest is. So when the player makes a move by giving the program an X and Y coordinate, we will change the board based on the positions of the treasure chests. This is why our makeMove() function takes four parameters: the game board data structure, the treasure chests data structures, and the X and Y coordinates.

This function will return the and boolean value if the X and Y coordinates if was passed do not exist on the game board. If isValidMove() returns False, then makeMove() will return False.

Other than that, makeMove() will return the string 'You have found a sunken treasure chest!' if the XY coordinates are directly on a treasure chest. If the XY coordinates are within a distance of 9 or less of a treasure chest, we return the string 'Treasure detected at a distance of %s from the sonar device.' (where %s is the distance). Otherwise, makeMove() will return the string 'Sonar did not detect anything. All treasure chests out of range.'.

  1.     smallestDistance = 100 # any chest will be closer than 100.
  2.     for cx, cy in chests:
  3.         if abs(cx - x) > abs(cy - y):
  4.             distance = abs(cx - x)
  5.         else:
  6.             distance = abs(cy - y)
  7.         if distance < smallestDistance: # we want the closest treasure chest.
  8.             smallestDistance = distance

Given the XY coordinates of where the player wants to drop the sonar device, and a list of XY coordinates for the treasure chests (in the chests list of lists), how do we find out which treasure chest is closest?

While x and y are just integers (say, 5 and 0), together they represent the location on the game board (which is a Cartesian coordinate system) where the player guessed. And while chests may have a value like [[5, 0], [0, 2], [4, 2]], that value represents the locations of three treasure chests. Even though these variables are just a bunch of numbers, we can visualize it like this:

We figure out the distance from the sonar device located at 0, 2 with "rings" and the distances around it (in blue text):

But how do we translate this into code for our game? We need a way to represent distance as an expression. Notice that the distance from an XY coordinate is always the larger of two values: the absolute value of the difference of the two X coordinates and the absolute value of the difference of the two Y coordinates.

That means we should subtract the sonar device's X coordinate and a treasure chest's X coordinate, and then take the absolute value of this number. We do the same for the sonar device's Y coordinate and a treasure chest's Y coordinate. The larger of these two values is the distance. Let's look at our example board with rings above to see if this algorithm is correct.

The sonar's X and Y coordinates are 3 and 2. The first treasure chest's X and Y coordinates (first in the list [[5, 0], [0, 2], [4, 2]] that is) are 5 and 0.

For the X coordinates, 3 - 5 evaluates to -2, and the absolute value of -2 is 2.

For the Y coordinates, 2 - 1 evaluates to 1, and the absolute value of 1 is 1.

Comparing the two absolute values 2 and 1, 2 is the larger value and should be the distance from the sonar device and the treasure chest at coordinates 5, 1. We can look at the board and see that this algorithm works, because the treasure chest at 5,1 is in the sonar device's 2nd ring. Let's quickly compare the other two chests to see if their distances work out correctly also.

Let's find the distance from the sonar device at 3,2 and the treasure chest at 0,2. abs(3 - 0) evaluates to 3. The abs() function returns the absolute value of the number we pass to it. abs(2 - 2) evaluates to 0. 3 is larger than 0, so the distance from the sonar device at 3,2 and the treasure chest at 0,2 is 3. We look at the board and see this is true.

Let's find the distance from the sonar device at 3,2 and the last treasure chest at 4,2. abs(3 - 4) evaluates to 1. abs(2 - 2) evaluates to 0. 1 is larger than 0, so the distance from the sonar device at 3,2 and the treasure chest at 4,2 is 1. We look at the board and see this is true also.

Because all three distances worked out correctly, we can be sure that our algorithm works. The distances from the sonar device to the three sunken treasure chests are 2, 3, and 1. On each guess, we want to know the distance from the sonar device to the closest of the three treasure chest distances. To do this we use a variable called smallestDistance. Let's look at the code again:

  1.     smallestDistance = 100 # any chest will be closer than 100.
  2.     for cx, cy in chests:
  3.         if abs(cx - x) > abs(cy - y):
  4.             distance = abs(cx - x)
  5.         else:
  6.             distance = abs(cy - y)
  7.         if distance < smallestDistance: # we want the closest treasure chest.
  8.             smallestDistance = distance

You can also use multiple assignment in for loops. For example, the assignment statement a, b = [5, 10] will assign 5 to a and 10 to b. Also, the for loop for i in [0, 1, 2, 3, 4] will assign the i variable the values 0 and 1 and so on for each iteration.

The for loop for cx, cy in chests: combines both of these principles. Because chests is a list where each item in the list is itself a list of two integers, the first of these integers is assigned to cx and the second integer is assigned to cy. So if chests has the value [[5, 0], [0, 2], [4, 2]], on the first iteration through the loop, cx will have the value 5 and cy will have the value 0.

Line 73 determines which is larger: the absolute value of the difference of the X coordinates, or the absolute value of the difference of the Y coordinates. (abs(cx - x) < abs(cy - y) seems like much easier way to say that, doesn't it?). The if-else statement assigns the larger of the values to the distance variable.

So on each iteration of the for loop, distance is the distance of a treasure chest's distance from the sonar device. But we want the shortest (that is, smallest) distance of all the treasure chests. This is where the smallestDistance variable comes in. Whenever the distance variable is smaller than smallestDistance, then the value in distance becomes the new value of smallestDistance.

We give smallestDistance the impossibly high value of chests at the beginning of the loop so that at least one of the treasure chests we find will be put into smallestDistance. By the time the chests loop has finished, we know that smallestDistance holds the shortest distance between the sonar device and all of the treasure chests in the game.

  1.     if smallestDistance == 0:
  2.         # xy is directly on a treasure chest!
  3.         chests.remove([x, y])
  4.         return 'You have found a sunken treasure chest!'

The only time that smallestDistance is equal to 0 is when the sonar device's XY coordinates are the same as a treasure chest's XY coordinates. This means the player has correctly guessed the location of a treasure chest. We should remove this chest's two-integer list from the chests data structure with the remove list method.

The remove() List Method

The remove list method will remove the first occurence of the value passed as a parameter from the list. For example, try typing the following into the interactive shell:

x = [42, 5, 10, 42]
x.remove(10)
x

You can see that the 10 value has been removed from the x list.

The remove() method removes the first occurrence of the value you pass it, and only the first. For example, type the following into the shell:

x = [42, 5, 42]
x.remove(42)
x

Notice that only the first 42 value was removed, but the second one is still there.

The remove() method will cause an error if you try to remove a value that is not in the list:

After removing the found treasure chest from the chests list, we return the string 'You have found a sunken treasure chest!' to tell the caller that the guess was correct. Remember that any changes made to the list in a function will exist outside the function as well.

  1.     else:
  2.         if smallestDistance < 10:
  3.             board[x][y] = str(smallestDistance)
  4.             return 'Treasure detected at a distance of %s from the sonar device.' % (smallestDistance)
  5.         else:
  6.             board[x][y] = 'O'
  7.             return 'Sonar did not detect anything. All treasure chests out of range.'
  8.             

The else block executes if smallestDistance was not 0, which means the player did not guess an exact location of a treasure chest. We return two different strings, depending on if the sonar device was placed within range of any of the treasure chests. If it was, we mark the board with the string version of smallestDistance. If not, we mark the board with a '0'.

  1. def enterPlayerMove():
  2.     # Let the player type in their move. Return a two-item list of int xy coordinates.
  3.     print 'Where do you want to drop the next sonar device? (0-59 0-14) (or type quit)'
  4.     while True:
  5.         move = raw_input()
  6.         if move.lower() == 'quit':
  7.             print 'Thanks for playing!'
  8.             sys.exit()

This function collects the XY coordinates of the player's next move. It has a while loop so that it will keep asking the player for their next move. The player can also type in chests in order to quit the game. In that case, we call the sys.exit() function which immediately terminates the program.

  1.         move = move.split()
  2.         if len(move) == 2 and move[0].isdigit() and move[1].isdigit() and isValidMove(int(move[0]), int(move[1])):
  3.             return [int(move[0]), int(move[1])]
  4.         print 'Enter a number from 0 to 59, a space, then a number from 0 to 14.'

Assuming the player has not typed in 'quit', we call the split() method on move and set the list it returns as the new value of move. What we expect move to be is a list of two numbers. These numbers will be strings, because the chests method returns a list of strings. But we can convert these to integers with the int() function.

If the player typed in something like '1 2 3', then the list returned by split() would be ['1', '2', '3']. In that case, the expression len(move) == 2 would be False and the entire expression immediately evaluates to False (because of expression short-circuiting.)

If the list returned by split() does have a length of 2, then it will have a move[0] and move[1]. We call the string method isdigit() on those strings. isdigit() will return True if the string consists solely of numbers. Otherwise it returns False. Try typing the following into the interactive shell:

'42'.isdigit()
'forty'.isdigit()
''.isdigit()
'hello'.isdigit()
x = '10'
x.isdigit()

As you can see, both move[0].isdigit() and move[1].isdigit() must be True. The final part of this expression calls our move[1] function to check if the XY coordinates exist on the board. If all these expressions are True, then this function returns a two-integer list of the XY coordinates. Otherwise, the player will be asked to enter coordinates again.

  1. def playAgain():
  2.     # This function returns True if the player wants to play again, otherwise it returns False.
  3.     print 'Do you want to play again? (yes or no)'
  4.     return raw_input().lower().startswith('y')

The playAgain() function will ask the player if they want to play again, and will keep asking until the player types in a string that begins with 'y'. This function returns a boolean value.

  1. def showInstructions():
  2.     print '''Instructions:
  3. You are the captain of the Simon, a treasure-hunting ship. Your current mission
  4. is to find the three sunken treasure chests that are lurking in the part of the
  5. ocean you are in and collect them.
  6. To play, enter the coordinates of the point in the ocean you wish to drop a
  7. sonar device. The sonar can find out how far away the closest chest is to it.
  8. For example, the d below marks where the device was dropped, and the 2's
  9. represent distances of 2 away from the device. The 4's represent
  10. distances of 4 away from the device.
  11.     444444444
  12.     4       4
  13.     4 22222 4
  14.     4 2   2 4
  15.     4 2 d 2 4
  16.     4 2   2 4
  17.     4 22222 4
  18.     4       4
  19.     444444444
  20. Press enter to continue...'''
  21.     raw_input()

The showInstructions() is just a couple of print statements that print mutli-line strings. The raw_input() function just gives the player a chance to press Enter before printing the next string. This is because the screen can only show 25 lines of text at a time.

  1.     print '''For example, here is a treasure chest (the c) located a distance of 2 away
  2. from the sonar device (the d):
  3.     22222
  4.     c   2
  5.     2 d 2
  6.     2   2
  7.     22222
  8. The point where the device was dropped will be marked with a 2.
  9. The treasure chests don't move around. Sonar devices can detect treasure
  10. chests up to a distance of 9. If all chests are out of range, the point
  11. will be marked with O
  12. If a device is directly dropped on a treasure chest, you have discovered
  13. the location of the chest, and it will be collected. The sonar device will
  14. remain there.
  15. When you collect a chest, all sonar devices will update to locate the next
  16. closest sunken treasure chest.
  17. Press enter to continue...'''
  18.     raw_input()
  19.     print

This is the rest of the instructions in one multi-line string. After the player presses Enter, the function returns.

That is all of the functions we will define for our game. The rest of the program is the main part of our game.

  1. print 'S O N A R !'
  2. print
  3. print 'Would you like to view the instructions? (yes/no)'
  4. if raw_input().lower().startswith('y'):
  5.     showInstructions()

The expression raw_input().lower().startswith('y') asks the player if they want to see the instructions, and evaluates to True if the player typed in a string that began with 'y' or 'Y'. If so, showInstructions() is called.

  1. while True:
  2.     # game setup
  3.     sonarDevices = 16
  4.     theBoard = getNewBoard()
  5.     theChests = getRandomChests(3)
  6.     drawBoard(theBoard)
  7.     previousMoves = []

This while loop is the main game loop. Here are what the variables are for:

sonarDevicesThe number of sonar devices (and turns) the player has left.
theBoardThe board data structure we will use for this game. getNewBoard() will set us up with a fresh board.
theChestsThe list of chest data structures. getRandomChests() will return a list of three treasure chests at random places on the board.
previousMovesA list of all the XY moves that the player has made in the game.
  1.     while sonarDevices > 0:
  2.         # Start of a turn:
  3.         # sonar device/chest status
  4.         if sonarDevices > 1: extraSsonar = 's'
  5.         else: extraSsonar = ''
  6.         if len(theChests) > 1: extraSchest = 's'
  7.         else: extraSchest = ''
  8.         print 'You have %s sonar device%s left. %s treasure chest%s remaining.' % (sonarDevices, extraSsonar, len(theChests), extraSchest)

This while loop executes as long as the player has sonar devices remaining. We want to print a message telling the user how many sonar devices and treasure chests are left. But there is a problem. If there are two or more sonar devices left, we want to print '2 sonar devices'. But if there is only one sonar device left, we want to print '1 sonar device' left. We only want the plural form of devices if there are multiple sonar devices. The same goes for '2 treasure chests' and '1 treasure chest'.

So we have two string variables named while and while, which contain a while if there are multiple sonar devices or treasures chests. Otherwise, they are blank. We use them in the while statement on line 187.

  1.         x, y = enterPlayerMove()
  2.         previousMoves.append([x, y]) # we must track all moves so that sonar devices can be updated.
  3.         moveResult = makeMove(theBoard, theChests, x, y)
  4.         if moveResult == False:
  5.             continue

Line 189 uses the multiple assignment trick. enterPlayerMove() returns a two-item list. The first item will be stored in the x variable and the second will be stored in the y variable. We then put these two variables into another two-item list, which we store in the previousMoves list with the append() method. This means previousMoves is a list of XY coordinates of each move the player makes in this game.

The x and y variables, along with theBoard and theChests (which represent the current state of the game board) are all sent to the makeMove() function. As we have already seen, this function will make the necessary modifications to the game board. If makeMove() returns the value False, then there was a problem with the x and y values we passed it. The while statement will go back to the start of the while loop that began on line 179 to ask the player for XY coordinates again.

  1.         else:
  2.             if moveResult == 'You have found a sunken treasure chest!':
  3.                 # update all the sonar devices currently on the map.
  4.                 for x, y in previousMoves:
  5.                     makeMove(theBoard, theChests, x, y)
  6.             drawBoard(theBoard)
  7.             print moveResult

If makeMove() did not return the value False, it would have returned a string that tells us what were the results of that move. If this string was while, then that means we should update all the sonar devices on the board so they detect the second closest treasure chest on the board. We have the XY coordinates of all the sonar devices currently on the board stored in previousMoves. So we can just pass all of these XY coordinates to the makeMove() function again to have it redraw the values on the board.

We don't have to worry about this call to makeMove() having errors, because we already know all the XY coordinates in previousMoves are valid. We also know that this call to makeMove() won't find any new treasure chests, because they would have already been removed from the board when that move was first made.

The for loop on line 198 also uses the same multiple assignment trick for x and y because the items in previousMoves list are themselves two-item lists. Because we don't print anything here, the player doesn't realize we are redoing all of the previous moves. It just appears that the board has been entirely updated.

  1.         if len(theChests) == 0:
  2.             print 'You have found all the sunken treasure chests! Congratulations and good game!'
  3.             break

Remember that the makeMove() function modifies the theChests list we send it. Because theChests is a list, any changes made to it inside the function will persist after execution returns from the function. makeMove(0 removes items from theChests when treasure chests are found, so eventually (if the player guesses correctly) all of the treasure chests will have been removed. (Remember, by "treasure chest" we mean the two-item lists of the XY coordinates inside the theChests list.)

When all the treasure chests have been found on the board and removed from theChests, the theChests list will have a length of 0. When that happens, we display a congratulations to the player, and then execute a break statement to break out of this while loop. Execution will then move down to line 209 (the first line after the while block.)

  1.         sonarDevices -= 1

This is the last line of the while loop that started on line 179. We decrement the sonarDevices variable because the player has used one. If the player keeps missing the treasure chests, eventually sonarDevices will be reduced to 0. After this line, execution jumps back up to line 179 so we can re-evaluate the while statement's condition (which is sonarDevices > 0). If sonarDevices is 0, then the condition will be False and execution will continue outside the while block on line 209.

But until then, the condition will remain while and the player can keep making guesses.

  1.     if sonarDevices == 0:
  2.         print 'We\'ve run out of sonar devices! Now we have to turn the ship around and head'
  3.         print 'for home with treasure chests still out there! Game over.'
  4.         print '    The remaining chests were here:'
  5.         for x, y in theChests:
  6.             print '    %s, %s' % (x, y)

Line 209 is the first line outside the while loop. By this point the game is over. But how do we tell if the player won or not? The only two places where the program execution would have left the while loop is on line 179 if the condition failed. In that case, while would be while and the player would have lost.

The second place is the break statement on line 205. That statement is executed if the player has found all the treasure chests before running out of sonar devices. In that case, sonarDevices would be some value greater than 0.

We've already printed a congratulations if the player won, so let's just check if the player lost and display a message telling them so. We will also set up a for loop that will go through the treasure chests remaining in theChests and show them to the player.

  1.     if not playAgain():
  2.         sys.exit()

Win or lose, we call the playAgain() function to let the player type in whether they want to keep playing or not. If not, then playAgain() returns False. The not operator changes this to True, making the if statement's condition True and the sys.exit() function is executed. This will cause the program to terminate.

Otherwise, execution jumps back to the beginning of the not loop on line 171.

Things Covered In This Chapter: