Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. # from globals import *
  2.  
  3. class Grid:
  4. def __init__(self, max_width, max_height):
  5. self.max_width = max_width
  6. self.max_height = max_height
  7. self.MAX_SLOT_COUNT = self.GetSize()
  8.  
  9. self.__Initialize()
  10.  
  11. def __Initialize(self):
  12. self.grid = {}
  13.  
  14. self.Clear()
  15.  
  16. def Clear(self):
  17. for i in range(self.MAX_SLOT_COUNT):
  18. self.grid[i] = 0
  19.  
  20. def IsEmpty(self, pos, height):
  21. row = pos / self.max_width
  22.  
  23. if row + height > self.max_height:
  24. return False
  25.  
  26. for y in range(height):
  27. index = pos + (y * self.max_width)
  28.  
  29. if self.grid[index]:
  30. return False
  31.  
  32. return True
  33.  
  34. def FindBlank(self, height):
  35. if height > self.max_height:
  36. return -1
  37.  
  38. for row in range(self.max_height):
  39. for column in range(self.max_width):
  40. index = row * self.max_width + column
  41.  
  42. if self.IsEmpty(index, height):
  43. return index
  44.  
  45. return -1
  46.  
  47. def Put(self, pos, height, value = True):
  48. if not self.IsEmpty(pos, height):
  49. return False
  50.  
  51. for y in range(height):
  52. index = pos + (y * self.max_width)
  53. self.grid[index] = value
  54.  
  55. def Print(self):
  56. for row in range(self.max_height):
  57. for column in range(self.max_width):
  58. print("%d" % (self.grid[row * self.max_width + column]), end=" ")
  59.  
  60. print("")
  61.  
  62. def GetSize(self):
  63. return self.max_width * self.max_height
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement