Advertisement
Charmander

neighbors

Oct 5th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1.  
  2. def findNeighbors(width, height, tile):
  3.     width = width
  4.     height = height
  5.     tile = tile
  6.  
  7.  
  8.     row = (tile - 1) // width
  9.     col = (tile - 1) % width
  10.     tile1 = tile - width - 1
  11.     tile2 = tile - width
  12.     tile3 = tile - width + 1
  13.     tile4 = tile - 1
  14.     tile5 = tile + 1
  15.     tile6 = tile + width - 1
  16.     tile7 = tile + width
  17.     tile8 = tile + width + 1
  18.     if width * height == 1:
  19.         return ""
  20.     elif width * height == 2:
  21.         if tile == 1:
  22.             return "2"
  23.         else:
  24.             return "1"
  25.     else:
  26.         if row == 0:
  27.             if col == 0:
  28.                 return str(tile5) + " " + str(tile7) + " " + str(tile8) + " "
  29.             if col == width - 1:
  30.                 return str(tile4) + " " + str(tile6) + " " + str(tile7) + " "
  31.             else:
  32.                 return str(tile4) + " " + str(tile5) + " " + str(tile6) + " " + str(tile7) + " " + str(tile8) + " "
  33.         elif row == height - 1:
  34.             if col == 0:
  35.                 return str(tile2) + " " + str(tile3) + " " + str(tile5) + " "
  36.             if col == width - 1:
  37.                 return str(tile1) + " " + str(tile2) + " " + str(tile4) + " "
  38.             else:
  39.                 return str(tile1) + " " + str(tile2) + " " + str(tile3) + " " + str(tile4) + " " + str(tile5) + " "
  40.         elif col == 0:
  41.             return str(tile2) + " " + str(tile3) + " " + str(tile5) + " " + str(tile7) + " " + str(tile8) + " "
  42.         elif col == width - 1:
  43.             return str(tile1) + " " + str(tile2) + " " + str(tile4) + " " + str(tile6) + " " + str(tile7) + " "
  44.         else:
  45.             return str(tile1) + " " + str(tile2) + " " + str(tile3) + " " + str(tile4) + " " + str(tile5) + " " + str(tile6) + " " + str(tile7) + " " + str(tile8) + " "
  46.  
  47.     #END YOUR CODE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement