Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1.     def makeMove(board, chests, x, y):
  2.         # Change the board data structure with a sonar device character. Remove treasure chests from the chests list as they are found.
  3.         # Return False if this is an invalid move.
  4.         # Otherwise, return the string of the result of this move.
  5.         smallestDistance = 100 # Any chest will be closer than 100.
  6.         for cx, cy in chests: # For 'value 1' and 'value 2' in each index of the list
  7.             # If each index didn't have two values, this wouldn't work.
  8.             # This isn't a nested loop or anything.
  9.             distance = math.sqrt((cx - x) * (cx - x) + (cy - y) * (cy - y))
  10.  
  11.             if distance < smallestDistance: # We want the closest treasure chest.
  12.                 smallestDistance = distance
  13.  
  14.         smallestDistance = round(smallestDistance)
  15.  
  16.         if smallestDistance == 0:
  17.             # xy is directly on a treasure chest!
  18.             chests.remove([x,y])
  19.             return 'You have found a sunken treasure chest!'
  20.         else:
  21.             if smallestDistance < 10:
  22.                 board[x][y] = str(smallestDistance)
  23.                 return 'Treasure detected at a distance of %s from the sonar device' % (smallestDistance)
  24.             else:
  25.                 board[x][y] = 'X'
  26.                 return 'Sonar did not detect anything. All treasure chests out of range.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement