def utility(state: State, maximizing_player): """ utility and heuristic function """ best_move_score = -1 #######################[Goal]######################### is_current_player_stuck = is_stuck(state, state.player_type) other_player = RIVAL if state.player_type == PLAYER else PLAYER # Check if stuck if is_current_player_stuck: if state.player_type == PLAYER: state.players_score[state.player_type] -= state.penalty_score else: state.players_score[state.player_type] += state.penalty_score return state.players_score[ state.player_type] - state.players_score[other_player] ###################################################### # Else #-------------------------------------------------- ################# Available Steps ################# #-------------------------------------------------- player_available_steps = availables(state.board, state.locations[PLAYER]) h1 = 4 - player_available_steps h4 = player_available_steps #-------------------------------------------------- ################# Fruits Distance ################# #-------------------------------------------------- h2 = -1 if state.fruits_ttl > 0 and len(state.fruits_dict) > 0: min_fruit_dist = float('inf') for fruit_loc in state.fruits_dict: curr_fruit_dist = Manhattan(state.locations[state.player_type], fruit_loc) # Check what is the closest fruit reachable if curr_fruit_dist < min_fruit_dist and curr_fruit_dist <= state.fruits_ttl: other_player_fruit_dist = Manhattan( state.locations[other_player], fruit_loc) if curr_fruit_dist < other_player_fruit_dist: min_fruit_dist = curr_fruit_dist max_dist = len(state.board) + len(state.board[0]) h2 = (max_dist * 10.0 / min_fruit_dist) + 1 if min_fruit_dist < float('inf') else -1 #-------------------------------------------------- ################# Reachable Squrs ################# #-------------------------------------------------- reachables_player = reachables(state.board, state.locations[PLAYER]) reachables_rival = reachables(state.board, state.locations[RIVAL]) h3 = reachables_player - reachables_rival # We want more for us #-------------------------------------------------- ################# Combine it all. ################# #-------------------------------------------------- if not state.half_game(): w = 0.8 if h2 > 0 else 1 best_move_score = w * (h1 - h3) + (1 - w) * h2 else: w = 0.7 if h2 > 0 else 1 best_move_score = w * (h4 + h3) + (1 - w) * h2 best_move_score += state.players_score[state.player_type] return best_move_score
def make_move(self, time_limit, players_score): """Make move with this Player. input: - time_limit: float, time limit for a single turn. output: - direction: tuple, specifing the Player's movement, chosen from self.directions """ start_time = time.time() d = 1 reach_the_end = False best_direction = None chosen_state = None time_limit = (2 * self.game_time * float(self.player_turns - self.turns + 1)) / ( (self.player_turns + 1) * self.player_turns) time_limit += self.spaire_time if time_limit >= 5: TIME_ESTIMATION = 0.9 else: TIME_ESTIMATION = 0.85 #print(f'Time limit: {time_limit}') while not reach_the_end: # and d < len(state.board)*len(state.board[0]): iter_time_limit = TIME_ESTIMATION * (time_limit - (time.time() - start_time)) #print(f'>>>Iter time: {iter_time_limit}') state = State(get_directions(), self.board, self.locations, self.fruits_on_board_dict, PLAYER, players_score, self.penalty_score, self.fruits_ttl, self.turns) try: _, best_direction, reach_the_end, chosen_state = self.alphabeta.search( state, d, True, iter_time_limit, alpha=float('-inf'), beta=float('inf')) d += 1 except Exception as e: self.spaire_time = time_limit - (time.time() - start_time) break # Set new location if best_direction == None: best_direction = self.get_random_move() self.set_player_location(best_direction) self.turns += 1 return best_direction
def make_move(self, time_limit, players_score): """Make move with this Player. input: - time_limit: float, time limit for a single turn. output: - direction: tuple, specifing the Player's movement, chosen from self.directions """ start_time = time.time() d = 1 # Make the initial state: reach_the_end = False best_direction = None chosen_state = None if time_limit >= 5: TIME_ESTIMATION = 0.9 else: TIME_ESTIMATION = 0.85 while not reach_the_end: iter_time_limit = TIME_ESTIMATION * (time_limit - (time.time() - start_time)) state = State(get_directions(), self.board, self.locations, self.fruits_on_board_dict, PLAYER, players_score, self.penalty_score, self.fruits_ttl, self.turns) try: _, best_direction, reach_the_end, chosen_state = self.minimax.search( state, d, True, iter_time_limit) d += 1 except Exception as e: break # Set new location if best_direction == None: best_direction = self.get_random_move() self.set_player_location(best_direction) self.turns += 1 return best_direction
def make_move(self, time_limit, players_score): """Make move with this Player. input: - time_limit: float, time limit for a single turn. output: - direction: tuple, specifing the Player's movement, chosen from self.directions """ d = 2 reach_the_end = False best_direction = None chosen_state = None time_limit = float('inf') while not reach_the_end: # and d < len(state.board)*len(state.board[0]): state = State(get_directions(), self.board, self.locations, self.fruits_on_board_dict, PLAYER, players_score, self.penalty_score, self.fruits_ttl, self.turns) try: _, best_direction, reach_the_end, chosen_state = self.alphabeta.search( state, d, True, time_limit, alpha=float('-inf'), beta=float('inf')) break #d += 1 except Exception as e: break # Set new location if best_direction == None: best_direction = self.get_random_move() self.set_player_location(best_direction) self.turns += 1 return best_direction