player.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. while foods < qt:
  36. x = randrange(self.board.width)
  37. y = y_offset + randrange(y_range)
  38. if not self.board.has_food(x, y):
  39. if self.set_food(x, y):
  40. foods += 1
  41. def remove_food(self, x, y):
  42. food_remove = False
  43. x0, y0 = int(x - self.food_size / 2), int(y - self.food_size / 2)
  44. for x_size in range(self.food_size):
  45. for y_size in range(self.food_size):
  46. if not self.use_circle or (x_size - self.food_size / 2) ** 2 + (y_size - self.food_size / 2) ** 2 <= \
  47. (self.food_size / 2 - 0.5) ** 2:
  48. if self.board.inside(x0 + x_size, y0 + y_size) and not self.board.is_touched(x0 + x_size,
  49. y0 + y_size):
  50. self.board.remove_food(x0 + x_size, y0 + y_size)
  51. food_remove = True
  52. if not food_remove:
  53. print("Blob already found it !")
  54. def set_food(self, x, y):
  55. food_put = False
  56. x0, y0 = int(x - self.food_size / 2), int(y - self.food_size / 2)
  57. for x_size in range(self.food_size):
  58. for y_size in range(self.food_size):
  59. if not self.use_circle or (x_size - self.food_size / 2) ** 2 + (y_size - self.food_size / 2) ** 2 <= \
  60. (self.food_size / 2 - 0.5) ** 2:
  61. if self.board.inside(x0 + x_size, y0 + y_size) and not self.board.is_touched(x0 + x_size,
  62. y0 + y_size):
  63. self.board.set_food(x0 + x_size, y0 + y_size)
  64. food_put = True
  65. if not food_put:
  66. print("There is blob there !")
  67. return False
  68. return True
  69. def check_blob_cover(self):
  70. return self.board.get_cover(1), self.board.get_cover(2)
  71. def clean_board(self):
  72. y_range = ceil(self.board.height/2)
  73. if self.clean_top:
  74. y_offset = 0
  75. else:
  76. y_offset = int(self.board.height/2)
  77. for x in range(self.board.width):
  78. for y in range(y_range):
  79. self.board.reset(x, y_offset + y)
  80. self.blob.reset(x, y_offset + y)
  81. self.clean_top = not self.clean_top