def decide(self, board, players, num): me = players[num] forward = apply_direction(me.position, me.direction) f = not board.is_wall(forward) left = apply_direction(me.position, new_direction(me.direction, player.TURN_LEFT)) l = not board.is_wall(left) right = apply_direction(me.position, new_direction(me.direction, player.TURN_RIGHT)) r = not board.is_wall(right) if f and l and r: return util.choose_randomly([ (0.001, player.TURN_LEFT), (0.998, player.GO_FORWARD), (0.001, player.TURN_RIGHT), ]) elif f and l or f and r: return util.choose_randomly([ (0.999, player.GO_FORWARD), (0.001, player.TURN_LEFT if l else player.TURN_RIGHT), ]) elif l and r: return util.choose_randomly([ (0.5, player.TURN_LEFT), (0.5, player.TURN_RIGHT), ]) elif l: return player.TURN_LEFT elif r: return player.TURN_RIGHT else: return player.GO_FORWARD
def minmax(self, board, players, iteration): me, opponent = players if iteration < self.depth: best_outcome = (UNDEFINED,) best_action = None for action in [player.GO_FORWARD, player.TURN_LEFT, player.TURN_RIGHT]: new_dir = new_direction(me.direction, action) new_pos = apply_direction(me.position, new_dir) if board.is_wall(new_pos): outcome = outcome_lose(iteration) else: board.place_wall(new_pos) new_me = SimplePlayer(new_dir, new_pos) opponents_action, opponents_outcome = self.minmax(board, (opponent, new_me), iteration + 1) outcome = invert_outcome(opponents_outcome) board.remove_wall(new_pos) if self.compare_outcomes(outcome, best_outcome) > 0: best_outcome = outcome best_action = action return (best_action, best_outcome) else: #temp_board = board.copy() #temp_board.remove_wall(me.position) #temp_board.remove_wall(opponent.position) #my_space = temp_board.fill(me.position) #opponents_space = temp_board.fill(opponent.position) #if opponents_space == 0: #opponents_space == my_space my_space = 0 opponents_space = 0 return player.GO_FORWARD, outcome_unknown(my_space, opponents_space)
def decide(self, board, players, num): me = players[num] forward = apply_direction(me.position, me.direction) if not board.is_wall(forward): return player.GO_FORWARD left_free = right_free = 0 left = apply_direction(me.position, new_direction(me.direction, player.TURN_LEFT)) if not board.is_wall(left): temp_board = board.copy() left_free = temp_board.fill(left) right = apply_direction(me.position, new_direction(me.direction, player.TURN_RIGHT)) if not board.is_wall(right): temp_board = board.copy() right_free = temp_board.fill(right) if left_free > right_free: return player.TURN_LEFT else: return player.TURN_RIGHT