Advertisement
Guest User

Untitled

a guest
May 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. class Utility:
  2. __FINISHING_HEXES = {
  3. 'r': {(3, -3), (3, -2), (3, -1), (3, 0)},
  4. 'g': {(-3, 3), (-2, 3), (-1, 3), (0, 3)},
  5. 'b': {(-3, 0), (-2, -1), (-1, -2), (0, -3)},
  6. }
  7.  
  8.  
  9. def get_total_score(self, board, colour):
  10. final = []
  11.  
  12. # creating all the position of other pieces
  13. pieces = set()
  14. for key in board:
  15. if key != colour:
  16. pieces.update(board[key])
  17.  
  18. # calulating the scaore of each piece of ours
  19. for i in board[colour]:
  20. total = 0
  21. # if (len(board[colour])+self._EXITS[colour]) >= 4:
  22. # The steps to the goals with exiting move
  23. total += -100 * (self.get_shortest_hex_distance(i, colour) + 1)
  24. # else:
  25. # total -= -100*(self.get_shortest_att_distance(i,colour,board))
  26.  
  27. for sur in self.get_surr(i, i, True):
  28. if sur in board[colour]:
  29. # score for the own colour near this piece
  30. total += 5
  31. if self.can_be_jump(board, sur, i):
  32. # score for we can jump over our own piece
  33. new_pos = (i[0] + (i[0] - sur[0]), i[1] + (i[1] - sur[1]))
  34. if self.is_closer(new_pos, sur, colour):
  35. # score for if we jump over this piece we are closer to the goal
  36. total += 2
  37. if (sur[0] + (sur[0] - i[0]), sur[1] + (sur[1] - i[1])) in pieces:
  38. # score for protecting our own piece, in case of get eaten by other piece
  39. total += 10
  40. if sur in pieces:
  41. # negetive score for other pieces around us
  42. if self.can_be_jump(board, i, sur):
  43. # Score for we can eat other pieces
  44. if (len(board[colour]) + self._EXITS[colour]) >= 4:
  45. total += 10
  46. else:
  47. # increased the score for eating other if we dont have inough piece to win
  48. total += 20
  49. new_pos = (sur[0] + (sur[0] - i[0]), sur[1] + (sur[1] - i[1]))
  50. if self.is_closer(new_pos, sur, colour):
  51. # if we eat this piece our own piece is closer to the goal
  52. total += 10
  53. cannot_be_jump = True
  54. # Score of eating others and we are safe
  55. for surr2 in self.get_surr(new_pos, new_pos, True):
  56. if surr2 in pieces:
  57. if self.can_be_jump(board, surr2, new_pos):
  58. cannot_be_jump = False
  59. break
  60. if cannot_be_jump:
  61. total += 100
  62.  
  63. if self.can_be_jump(board, sur, i):
  64. # we may get eat by other colour
  65. total -= 15
  66. if (sur[0] + (sur[0] - i[0]), sur[1] + (sur[1] - i[1])) in pieces:
  67. # we block others way from jumping
  68. total += 25
  69. final.append(total)
  70.  
  71. # only calulate the best four score of our piece, since we only need to win exiting four pieces
  72.  
  73. final_score = 0
  74. index = 0
  75. for i in sorted(final, reverse=True):
  76. if index < 4:
  77. final_score += i
  78. index += 1
  79.  
  80. # the score of each piece
  81. # the more piece we have , the higher score we got
  82. final_score += 50 * len(final)
  83.  
  84. return final_score
  85.  
  86. @staticmethod
  87. def get_shortest_hex_distance(self, a, colour):
  88. min = 37
  89. for items in self.__FINISHING_HEXES[colour]:
  90. new_dis = self.get_hex_distance(a, items)
  91. if (new_dis < min):
  92. min = new_dis
  93. return min
  94.  
  95. @staticmethod
  96. def get_hex_distance(self, a, b):
  97. return max(abs(a[0] - b[0]), abs(a[1] - b[1]), abs((-1 * a[0] - a[1]) - (-1 * b[0] - b[1])))
  98.  
  99. @staticmethod
  100. def can_be_jump(self,board,a,b):
  101. new_pos = (b[0] + (b[0] - a[0]),b[1] + (b[1] - a[1]))
  102. if self.on_board(new_pos[0],new_pos[1]):
  103. for key in board:
  104. if new_pos in board[key]:
  105. return False
  106. return True
  107. else:
  108. return False
  109.  
  110. @staticmethod
  111. def get_surr(self,currentpos,pos,show_all_pos):
  112. hex = []
  113. if self.on_board(pos[0],pos[1] - 1):
  114. hex.append((pos[0],pos[1] - 1))
  115. if self.on_board(pos[0] + 1,pos[1] - 1):
  116. hex.append((pos[0] + 1,pos[1] - 1))
  117. if self.on_board(pos[0] + 1,pos[1]):
  118. hex.append((pos[0] + 1,pos[1]))
  119. if self.on_board(pos[0],pos[1] + 1):
  120. hex.append((pos[0],pos[1] + 1))
  121. if self.on_board(pos[0] - 1,pos[1] + 1):
  122. hex.append((pos[0] - 1,pos[1] + 1))
  123. if self.on_board(pos[0] - 1,pos[1]):
  124. hex.append((pos[0] - 1,pos[1]))
  125. if not show_all_pos:
  126. hex.remove(currentpos)
  127. return hex
  128.  
  129.  
  130. def is_closer(self, a, b, colour):
  131. return self.get_shortest_hex_distance(a, colour) < self.get_shortest_hex_distance(b, colour)
  132.  
  133.  
  134. def on_board(self, x, y):
  135. if x == 0 and -3 <= y <= 3:
  136. return True
  137. elif x == -1 and -2 <= y <= 3:
  138. return True
  139. elif x == -2 and -1 <= y <= 3:
  140. return True
  141. elif x == -3 and 0 <= y <= 3:
  142. return True
  143. elif x == 1 and -3 <= y <= 2:
  144. return True
  145. elif x == 2 and -3 <= y <= 1:
  146. return True
  147. elif x == 3 and -3 <= y <= 0:
  148. return True
  149. else:
  150. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement