Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def makeMove(board, chests, x, y):
- # 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.
- smallestDistance = 100 # Any chest will be closer than 100.
- for cx, cy in chests: # For 'value 1' and 'value 2' in each index of the list
- # If each index didn't have two values, this wouldn't work.
- # This isn't a nested loop or anything.
- distance = math.sqrt((cx - x) * (cx - x) + (cy - y) * (cy - y))
- if distance < smallestDistance: # We want the closest treasure chest.
- smallestDistance = distance
- smallestDistance = round(smallestDistance)
- if smallestDistance == 0:
- # xy is directly on a treasure chest!
- chests.remove([x,y])
- return 'You have found a sunken treasure chest!'
- else:
- if smallestDistance < 10:
- board[x][y] = str(smallestDistance)
- return 'Treasure detected at a distance of %s from the sonar device' % (smallestDistance)
- else:
- board[x][y] = 'X'
- return 'Sonar did not detect anything. All treasure chests out of range.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement