def move_to_location(move): ''' This maps a 'move' label to an x and y increment. ''' mapper = { STAY: ZERO, LEFT: Position(-1, 0), RIGHT: Position(1, 0), UP: Position(0, 1), DOWN: Position(0, -1) } return mapper[move]
def __init__(self): # Randomly choose a ping frequency - we shouldn't always be pinging at the same time as the other Goody self.ping_frequency = random.randint(10, 20) self.position = Position(0, 0) # We define the origin as the place we start from # containers and variables for the things we've learned self.maze_data = MazeData() self.last_goody_position = None self.last_goody_position_stale = True self.last_baddy_position = None self.last_baddy_position_stale = True self.last_ping_response_turn_count = 0 # In units of turns self.turn_count = 0 self.steps = [] # A history of steps, used for backtracking # When pathfinding, path is a deque of directions to move in, and path_destination is where we're heading self.path = None self.path_destination = None self.state = Explorer.exploring # Set the initial state # A map from state to the method that will be used to choose what to do self._decision_maker = {Explorer.exploring: self.explore, Explorer.backtracking: self.backtrack, Explorer.pathfinding: self.pathfind}
def find_best(self, target): """ Given the map it outputs the path to the adjacent uknown which is closest to the target square First find the adjacent unkowns """ adjacent_unkowns = [] for i in self.knowledge.keys(): if self.knowledge[i] == True: for j in [-1, +1]: for k in [-1, +1]: if i + Position(j, k) not in self.knowledge.keys(): adjacent_unkowns.append(i + (j, k)) best_unkown = adjacent_unkowns[0] best_dist = self.vector_len_2(best_unkown - target) for i in adjacent_unkowns[1:]: if self.vector_len_2(i - target) < self.vector_len_2(best_unkown - target): best_unkown = i best_dist = self.vector_len_2(i - target) return best_unkown
def next_move(self, mapy, best_unkown): mapy2 = mapy.copy() mapy2[best_unkown] = True n = len(mapy2.keys()) A = [[True for _ in range(n)] for _ in range(n)] listy = list(mapy2.keys()) for i in range(n): for j in range(n): if self.vector_len_2(listy[i] - listy[j]) == 1 and mapy2[ listy[i]] and mapy2[listy[j]]: A[i][j] = True else: A[i][j] = False x = listy.index(Position(0, 0)) y = listy.index(best_unkown) path = self.path_alg(A, x, y, n) nexts = listy[path[1]] return Position(nexts[0], nexts[1])
def take_turn(self, obstruction, ping_response): ''' Ignore any ping information, just choose a random direction to walk in, or ping ''' for direction in [UP, DOWN, LEFT, RIGHT]: # print(direction, obstruction[direction]) self.knowledge[self.me + STEP[direction]] = obstruction[direction] print(self.knowledge) if ping_response is not None: self.last_ping_response = ping_response self.last_ping_time = 0 if self.last_ping_time is None or self.last_ping_time > 30: # If we don't know where the other goody is, then send a ping so that we can find out. print("Pinging to find our friend and foe") return PING import sys # print(self.last_ping_response) friend, = [ player for player in self.last_ping_response.keys() if isinstance(player, Goody) and player is not self ] foe, = [ player for player in self.last_ping_response.keys() if isinstance(player, Baddy) ] # For the four possible moves, find the resulting distance to our friend after each one: other = self.last_ping_response[friend] target_location = Position(other.x // 2, other.y // 2) #self.last_ping_response[friend]/2 len_and_dirs = self.move_towards(obstruction, other) self.last_ping_time += 1 # print(self.last_ping_time) move = len_and_dirs[0][0] # move = self.BFS(self.me, self.find_best(target_location)) # move = self.next_move(self.knowledge, self.find_best(target_location)) # if not obstruction[move]: # self.me += STEP[move] import os os.system('clear') b = '{"baddy wins", 257, "goodies win": 743}' print(b) return move
def __init__(self): self.position = Position(0, 0) # We define the origin as the place we start from # containers and variables for the things we've learned self.maze_data = MazeData() self.last_goody_position = defaultdict(lambda: None) self.last_goody_position_stale = defaultdict(lambda: True) self.last_ping_response_turn_count = 0 # In units of turns self.turn_count = 0 self.steps = [] # A history of steps, used for backtracking # When pathfinding, path is a deque of directions to move in, and path_destination is where we're heading self.path = None self.path_destination = None self.state = Ninja.exploring # Set the initial state # A map from state to the method that will be used to choose what to do self._decision_maker = {Ninja.exploring: self.explore, Ninja.backtracking: self.backtrack, Ninja.pathfinding: self.pathfind}
def __init__(self): self.last_ping_response = None self.last_ping_time = None self.me = Position(0, 0) self.knowledge = {self.me: True}