Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Utility:
- __FINISHING_HEXES = {
- 'r': {(3, -3), (3, -2), (3, -1), (3, 0)},
- 'g': {(-3, 3), (-2, 3), (-1, 3), (0, 3)},
- 'b': {(-3, 0), (-2, -1), (-1, -2), (0, -3)},
- }
- def get_total_score(self, board, colour):
- final = []
- # creating all the position of other pieces
- pieces = set()
- for key in board:
- if key != colour:
- pieces.update(board[key])
- # calulating the scaore of each piece of ours
- for i in board[colour]:
- total = 0
- # if (len(board[colour])+self._EXITS[colour]) >= 4:
- # The steps to the goals with exiting move
- total += -100 * (self.get_shortest_hex_distance(i, colour) + 1)
- # else:
- # total -= -100*(self.get_shortest_att_distance(i,colour,board))
- for sur in self.get_surr(i, i, True):
- if sur in board[colour]:
- # score for the own colour near this piece
- total += 5
- if self.can_be_jump(board, sur, i):
- # score for we can jump over our own piece
- new_pos = (i[0] + (i[0] - sur[0]), i[1] + (i[1] - sur[1]))
- if self.is_closer(new_pos, sur, colour):
- # score for if we jump over this piece we are closer to the goal
- total += 2
- if (sur[0] + (sur[0] - i[0]), sur[1] + (sur[1] - i[1])) in pieces:
- # score for protecting our own piece, in case of get eaten by other piece
- total += 10
- if sur in pieces:
- # negetive score for other pieces around us
- if self.can_be_jump(board, i, sur):
- # Score for we can eat other pieces
- if (len(board[colour]) + self._EXITS[colour]) >= 4:
- total += 10
- else:
- # increased the score for eating other if we dont have inough piece to win
- total += 20
- new_pos = (sur[0] + (sur[0] - i[0]), sur[1] + (sur[1] - i[1]))
- if self.is_closer(new_pos, sur, colour):
- # if we eat this piece our own piece is closer to the goal
- total += 10
- cannot_be_jump = True
- # Score of eating others and we are safe
- for surr2 in self.get_surr(new_pos, new_pos, True):
- if surr2 in pieces:
- if self.can_be_jump(board, surr2, new_pos):
- cannot_be_jump = False
- break
- if cannot_be_jump:
- total += 100
- if self.can_be_jump(board, sur, i):
- # we may get eat by other colour
- total -= 15
- if (sur[0] + (sur[0] - i[0]), sur[1] + (sur[1] - i[1])) in pieces:
- # we block others way from jumping
- total += 25
- final.append(total)
- # only calulate the best four score of our piece, since we only need to win exiting four pieces
- final_score = 0
- index = 0
- for i in sorted(final, reverse=True):
- if index < 4:
- final_score += i
- index += 1
- # the score of each piece
- # the more piece we have , the higher score we got
- final_score += 50 * len(final)
- return final_score
- @staticmethod
- def get_shortest_hex_distance(self, a, colour):
- min = 37
- for items in self.__FINISHING_HEXES[colour]:
- new_dis = self.get_hex_distance(a, items)
- if (new_dis < min):
- min = new_dis
- return min
- @staticmethod
- def get_hex_distance(self, a, b):
- return max(abs(a[0] - b[0]), abs(a[1] - b[1]), abs((-1 * a[0] - a[1]) - (-1 * b[0] - b[1])))
- @staticmethod
- def can_be_jump(self,board,a,b):
- new_pos = (b[0] + (b[0] - a[0]),b[1] + (b[1] - a[1]))
- if self.on_board(new_pos[0],new_pos[1]):
- for key in board:
- if new_pos in board[key]:
- return False
- return True
- else:
- return False
- @staticmethod
- def get_surr(self,currentpos,pos,show_all_pos):
- hex = []
- if self.on_board(pos[0],pos[1] - 1):
- hex.append((pos[0],pos[1] - 1))
- if self.on_board(pos[0] + 1,pos[1] - 1):
- hex.append((pos[0] + 1,pos[1] - 1))
- if self.on_board(pos[0] + 1,pos[1]):
- hex.append((pos[0] + 1,pos[1]))
- if self.on_board(pos[0],pos[1] + 1):
- hex.append((pos[0],pos[1] + 1))
- if self.on_board(pos[0] - 1,pos[1] + 1):
- hex.append((pos[0] - 1,pos[1] + 1))
- if self.on_board(pos[0] - 1,pos[1]):
- hex.append((pos[0] - 1,pos[1]))
- if not show_all_pos:
- hex.remove(currentpos)
- return hex
- def is_closer(self, a, b, colour):
- return self.get_shortest_hex_distance(a, colour) < self.get_shortest_hex_distance(b, colour)
- def on_board(self, x, y):
- if x == 0 and -3 <= y <= 3:
- return True
- elif x == -1 and -2 <= y <= 3:
- return True
- elif x == -2 and -1 <= y <= 3:
- return True
- elif x == -3 and 0 <= y <= 3:
- return True
- elif x == 1 and -3 <= y <= 2:
- return True
- elif x == 2 and -3 <= y <= 1:
- return True
- elif x == 3 and -3 <= y <= 0:
- return True
- else:
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement