Change the board data structure with a sonar device character. Remove treasure chests from the chests list as they are found. Return False if this is an invalid move. Otherwise, return the string of the result of this move.
(board, chests, x, y)
| 75 | return x >= 0 and x <= 59 and y >= 0 and y <= 14 |
| 76 | |
| 77 | def makeMove(board, chests, x, y): |
| 78 | """Change the board data structure with a sonar device character. |
| 79 | Remove treasure chests from the chests list as they are found. |
| 80 | Return False if this is an invalid move. Otherwise, return the |
| 81 | string of the result of this move.""" |
| 82 | smallestDistance = 100 # Any chest will be closer than 100. |
| 83 | for cx, cy in chests: |
| 84 | distance = math.sqrt((cx - x) * (cx - x) + (cy - y) * (cy - y)) |
| 85 | |
| 86 | if distance < smallestDistance: # Use the closest chest. |
| 87 | smallestDistance = distance |
| 88 | |
| 89 | smallestDistance = round(smallestDistance) |
| 90 | |
| 91 | if smallestDistance == 0: |
| 92 | # xy is directly on a treasure chest! |
| 93 | chests.remove([x, y]) |
| 94 | return 'You have found a sunken treasure chest!' |
| 95 | else: |
| 96 | if smallestDistance < 10: |
| 97 | board[(x, y)] = str(smallestDistance) |
| 98 | return 'Treasure detected at a distance of {} from the sonar device.'.format(smallestDistance) |
| 99 | else: |
| 100 | board[(x, y)] = 'X' |
| 101 | return 'Sonar did not detect anything. All treasure chests out of range.' |
| 102 | |
| 103 | def askForPlayerMove(previousMoves): |
| 104 | """Returns an (x, y) tuple of the player's move.""" |