player.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from math import ceil
  2. from random import randrange
  3. import json
  4. from simulation.board import Board
  5. class Player:
  6. def __init__(self, board, blob, default_config):
  7. """
  8. :type blob: Blob_Manager
  9. :type board: Board
  10. """
  11. self.board = board
  12. self.blob = blob
  13. with open(default_config, 'r') as file:
  14. d = json.load(file)
  15. self.clean_top = d['clean_top']
  16. self.food_size = d['food_size']
  17. self.use_circle = d['use_food_circle']
  18. def save(self):
  19. d = dict()
  20. d['clean_top'] = self.clean_top
  21. d['food_size'] = self.food_size
  22. d['use_food_circle'] = self.use_circle
  23. return json.dumps(d, indent=4, sort_keys=True)
  24. def set_random_food(self, qt, random_top=None):
  25. if random_top is None: # Randomize over all the board
  26. y_offset = 0
  27. y_range = self.board.height
  28. elif random_top: # Randomize over the half top board
  29. y_offset = 0
  30. y_range = ceil(self.board.height / 2)
  31. else: # Randomize over the half bottom board
  32. y_offset = int(self.board.height / 2)
  33. y_range = ceil(self.board.height / 2)
  34. foods = 0
  35. foods_list = []
  36. while foods < qt:
  37. x = randrange(self.board.width)
  38. y = y_offset + randrange(y_range)
  39. if not self.board.has_food(x, y):
  40. if self.set_food(x, y):
  41. foods += 1
  42. foods_list.append((x, y))
  43. return foods_list
  44. def remove_food(self, x, y):
  45. food_remove = False
  46. x0, y0 = int(x - self.food_size / 2), int(y - self.food_size / 2)
  47. for x_size in range(self.food_size):
  48. for y_size in range(self.food_size):
  49. if not self.use_circle or (x_size - self.food_size / 2) ** 2 + (y_size - self.food_size / 2) ** 2 <= \
  50. (self.food_size / 2 - 0.5) ** 2:
  51. if self.board.inside(x0 + x_size, y0 + y_size) and not self.board.is_touched(x0 + x_size,
  52. y0 + y_size):
  53. self.board.remove_food(x0 + x_size, y0 + y_size)
  54. food_remove = True
  55. if not food_remove:
  56. # print("Blob already found it !")
  57. return False
  58. return True
  59. def set_food(self, x, y, force=False, value=Board.INIT_FOOD):
  60. food_put = False
  61. x0, y0 = int(x - self.food_size / 2), int(y - self.food_size / 2)
  62. for x_size in range(self.food_size):
  63. for y_size in range(self.food_size):
  64. if not self.use_circle or (x_size - self.food_size / 2) ** 2 + (y_size - self.food_size / 2) ** 2 <= \
  65. (self.food_size / 2 - 0.5) ** 2:
  66. if self.board.inside(x0 + x_size, y0 + y_size):
  67. if force or not self.board.is_touched(x0 + x_size, y0 + y_size):
  68. self.board.set_food(x0 + x_size, y0 + y_size, value)
  69. food_put = True
  70. if not food_put:
  71. # print("There is blob there !")
  72. return False
  73. return True
  74. def check_blob_cover(self):
  75. return self.board.get_cover(1), self.board.get_cover(2)
  76. def clean_board(self):
  77. y_range = ceil(self.board.height/2)
  78. if self.clean_top:
  79. y_offset = 0
  80. else:
  81. y_offset = int(self.board.height/2)
  82. for x in range(self.board.width):
  83. for y in range(y_range):
  84. self.board.reset(x, y_offset + y)
  85. self.blob.reset(x, y_offset + y)
  86. self.clean_top = not self.clean_top