def update_fruits(self, fruits_on_board_dict): """Update your info on the current fruits on board (if needed). input: - fruits_on_board_dict: dict of {pos: value} where 'pos' is a tuple describing the fruit's position on board, 'value' is the value of this fruit. No output is expected. """ self.fruits_on_board_dict = fruits_on_board_dict for fruit_pos in utils.getFruitsOnBoard(self.board): if len(fruits_on_board_dict) == 0 or fruit_pos not in fruits_on_board_dict: self.board[fruit_pos] = 0 for fruit_pos, fruit_val in fruits_on_board_dict.items(): # assert self.board[fruit_pos] not in [-1, 1, 2] # then fruit is still a live self.board[fruit_pos] = fruit_val
def heuristic_function(self, state): # 4 parameters: curr_score, md from fruits, fruits value, is reachable fruit """ Gets the state Returns heuristic function based on below: 1.Gap between players score 2. Distance from an (3)is-reachable fruit 4. Manaheten Distance """ my_potential_score = 0 rival_potential_score = 0 for fruit_pos in utils.getFruitsOnBoard(self.board): my_dist_from_fruit = utils.mDist(state.pos, fruit_pos) rival_dist_from_fruit = utils.mDist(state.rival_pos, fruit_pos) if my_dist_from_fruit < rival_dist_from_fruit and my_dist_from_fruit <= state.fruit_life: my_potential_score += self.board[fruit_pos] elif rival_dist_from_fruit < my_dist_from_fruit and rival_dist_from_fruit <= state.fruit_life: rival_potential_score += self.board[fruit_pos] return (self.score[0] + my_potential_score) - (rival_potential_score + self.score[1])