board.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import numpy as np
  2. class Board:
  3. MAX_BLOB = 255
  4. DECREASE_BLOB = 0.1
  5. def __init__(self, width, height):
  6. self.width = width
  7. self.height = height
  8. self.board_array = np.empty(shape=(width, height), dtype=object)
  9. for x in range(self.width):
  10. for y in range(self.height):
  11. self.board_array[x, y] = Square()
  12. def save(self):
  13. stream = ''
  14. for y in range(self.height):
  15. for x in range(self.width):
  16. saved_node = self.board_array[x, y].save() + " "
  17. stream += str(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. self.board_array[x, y].load(node)
  35. x += 1
  36. y += 1
  37. def has_food(self, x, y):
  38. return self.inside(x, y) and self.board_array[x, y].food
  39. def update_blob(self, x, y, change_value):
  40. if self.inside(x, y):
  41. self.board_array[x, y].update_blob(change_value)
  42. return True
  43. else:
  44. return False
  45. def get_blob(self, x, y):
  46. if self.inside(x, y):
  47. return self.board_array[x, y].blob
  48. else:
  49. return None
  50. def inside(self, x, y):
  51. return 0 <= x < self.width and 0 <= y < self.height
  52. def is_touched(self, x, y):
  53. if self.inside(x, y):
  54. return self.board_array[x, y].touched
  55. else:
  56. return False
  57. def next_turn(self, food_lock=True):
  58. for x in range(self.width):
  59. for y in range(self.height):
  60. if self.board_array[x, y].touched:
  61. if not (food_lock and self.board_array[x,y].food):
  62. self.board_array[x, y].update_blob(-Board.DECREASE_BLOB)
  63. def reset(self, x, y):
  64. if self.inside(x, y):
  65. self.board_array[x, y] = Square()
  66. class Square:
  67. def __init__(self):
  68. self.food = False
  69. self.touched = False
  70. self.blob = 0
  71. def update_blob(self, change_value):
  72. self.touched = True
  73. self.blob += change_value
  74. self.blob = max(0, min(self.blob, Board.MAX_BLOB))
  75. def save(self):
  76. return format(self.touched, 'd') + "," + format(self.food, 'd') + "," + str(self.blob)
  77. def load(self, node):
  78. values = node.split(',')
  79. if len(values) != 3:
  80. print("Error with packaged values !")
  81. self.touched = values[0] == '1'
  82. self.food = values[1] == '1'
  83. self.blob = float(values[2])