board.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import numpy as np
  2. class Board:
  3. MAX_BLOB = 255.0
  4. MIN_BLOB = 0.0
  5. INIT_FOOD = 100
  6. def __init__(self, width, height):
  7. self.width = width
  8. self.height = height
  9. self.dropped_blob = np.zeros(shape=(width, height), dtype=float)
  10. self.foods = np.zeros(shape=(width, height), dtype=float)
  11. self.touched = np.zeros(shape=(width, height), dtype=bool)
  12. def save(self):
  13. stream = str(self.width) + ' ' + str(self.height) + '\n'
  14. for y in range(self.height):
  15. for x in range(self.width):
  16. saved_node = "{:d},{},{} ".format(self.touched[x, y], self.foods[x, y], self.dropped_blob[x, y])
  17. stream += saved_node
  18. stream = stream.rstrip(' ')
  19. stream += '\n'
  20. return stream.rstrip('\n')
  21. def load(self, filename):
  22. with open(filename, 'r') as file:
  23. dim = file.readline()
  24. dims = dim.split(' ')
  25. if dims[0] != self.width and dims[1] != self.height:
  26. self.__init__(int(dims[0]), int(dims[1]))
  27. y = 0
  28. for line in file:
  29. nodes = line.split(' ')
  30. if len(nodes) != self.width:
  31. print("Error with given height !" + str(len(nodes)))
  32. x = 0
  33. for node in nodes:
  34. values = node.split(',')
  35. if len(values) != 3:
  36. print("Error with packaged values !")
  37. self.touched[x, y] = values[0] == '1'
  38. self.foods[x, y] = float(values[1])
  39. self.dropped_blob[x, y] = float(values[2])
  40. x += 1
  41. y += 1
  42. def has_food(self, x, y):
  43. return self.inside(x, y) and self.foods[x, y] > 0
  44. def set_food(self, x, y, value=INIT_FOOD):
  45. if not self.foods[x, y] > 0:
  46. self.foods[x, y] = value
  47. def remove_food(self, x, y):
  48. if self.foods[x, y] > 0:
  49. self.foods[x, y] = 0
  50. def update_blob(self, x, y, change_value):
  51. if self.inside(x, y):
  52. self.touched[x, y] = True
  53. self.dropped_blob[x, y] = max(Board.MIN_BLOB, min(self.dropped_blob[x, y] + change_value, Board.MAX_BLOB))
  54. return True
  55. else:
  56. return False
  57. def eat_food(self, x, y, change_value):
  58. if self.foods[x, y] > 0:
  59. if self.foods[x, y] - change_value >= 0:
  60. self.foods[x, y] -= change_value
  61. else:
  62. change_value = self.foods[x, y]
  63. self.foods[x, y] = 0
  64. else:
  65. change_value = 0
  66. return change_value, self.foods[x, y] <= 0
  67. def get_blob(self, x, y):
  68. if self.inside(x, y):
  69. return self.dropped_blob[x, y]
  70. else:
  71. return None
  72. def inside(self, x, y):
  73. return 0 <= x < self.width and 0 <= y < self.height
  74. def is_touched(self, x, y):
  75. if self.inside(x, y):
  76. return self.touched[x, y]
  77. else:
  78. return False
  79. def get_cover(self, half_board=0):
  80. if half_board == 1:
  81. val = np.sum(self.touched[:, 0:int(self.height/2)]) * 2
  82. elif half_board == 2:
  83. val = np.sum(self.touched[:, int(self.height/2):self.height]) * 2
  84. else:
  85. val = np.sum(self.touched)
  86. return val / self.height / self.width * 100
  87. def get_blob_total(self):
  88. total = 0
  89. for x in range(self.width):
  90. for y in range(self.height):
  91. total += self.dropped_blob[x, y]
  92. return total / self.height / self.width / self.MAX_BLOB * 100
  93. def manage_blob(self, value, min_food_value=MIN_BLOB):
  94. for x in range(self.width):
  95. for y in range(self.height):
  96. if self.touched[x, y]:
  97. if not (self.foods[x, y] > 0 and self.dropped_blob[x, y] <= min_food_value):
  98. self.update_blob(x, y, -value)
  99. def reset(self, x, y):
  100. if self.inside(x, y):
  101. self.touched[x, y] = False
  102. self.dropped_blob[x, y] = 0
  103. self.foods[x, y] = 0
  104. def compare(self, board):
  105. if board.height != self.height and board.width != self.width:
  106. print("Size don't match !")
  107. return None
  108. board_comp = Board(self.width, self.height)
  109. for x in range(self.width):
  110. for y in range(self.height):
  111. board_comp.foods[x, y] = board.foods[x, y] - self.foods[x, y]
  112. board_comp.touched[x, y] = self.touched[x, y] == board.touched[x, y]
  113. board_comp.dropped_blob[x, y] = board.dropped_blob[x, y] - self.dropped_blob[x, y]
  114. return board_comp