def compute_distance_field(self, entity_type): """ Function computes and returns a 2D distance field Distance at member of entity_list is zero Shortest paths avoid obstacles and use four-way distances """ grid_height, grid_width = self.get_grid_height(), self.get_grid_width() visited = poc_grid.Grid(grid_height, grid_width) distance_field = [ [grid_height * grid_width for dummy_row in range(grid_width)]\ for dummy_col in range(grid_height)] entity_list = [] if entity_type == ZOMBIE: entity_list = self._zombie_list else: entity_list = self._human_list boundary = poc_queue.Queue() for row, col in entity_list: boundary.enqueue((row, col)) visited.set_full(row, col) distance_field[row][col] = 0 while len(boundary) != 0: current_cell = boundary.dequeue() neighbors = self.four_neighbors(current_cell[0], current_cell[1]) for neighbor in neighbors: if self.is_empty(neighbor[0], neighbor[1]) and\ visited.is_empty(neighbor[0], neighbor[1]): visited.set_full(neighbor[0], neighbor[1]) boundary.enqueue((neighbor[0], neighbor[1])) distance_field[neighbor[0]][neighbor[1]] =\ distance_field[current_cell[0]][current_cell[1]] + 1 return distance_field
def compute_distance_field(self, entity_type): """ Function computes a 2D distance field Distance at member of entity_queue is zero Shortest paths avoid obstacles and use distance_type distances """ grid_width = poc_grid.Grid.get_grid_width(self) grid_height = poc_grid.Grid.get_grid_height(self) visited = poc_grid.Grid(grid_height, grid_width) distance_field = [[ grid_width * grid_height for dummy_col in range(grid_width) ] for dummy_row in range(grid_height)] boundary = poc_queue.Queue() # For cells in the queue, initialize visited to be FULL and distance_field to be zero if entity_type == ZOMBIE: for item in self._zombie_list: boundary.enqueue(item) elif entity_type == HUMAN: for item in self._human_list: boundary.enqueue(item) for item in boundary: visited.set_full(item[0], item[1]) distance_field[item[0]][item[1]] = 0 while len(boundary) != 0: cell = boundary.dequeue() neighbors = self.four_neighbors(cell[0], cell[1]) for neighbor in neighbors: if self.is_empty(neighbor[0], neighbor[1]) and visited.is_empty( neighbor[0], neighbor[1]): visited.set_full(neighbor[0], neighbor[1]) boundary.enqueue(neighbor) distance_field[neighbor[0]][ neighbor[1]] = distance_field[cell[0]][cell[1]] + 1 return distance_field
def compute_distance_field(self, entity_type): """ Function computes a 2D distance field Distance at member of entity_queue is zero Shortest paths avoid obstacles and use distance_type distances """ visited = poc_grid.Grid(self.get_grid_height(), self.get_grid_width()) visited.clear() distance_field = [([self.get_grid_width() * self.get_grid_height()] * self.get_grid_width()) for dummy_i in range(self.get_grid_height())] boundary = poc_queue.Queue() for dummy_item in (self.zombies() if entity_type == ZOMBIE else self.humans()): boundary.enqueue(dummy_item) visited.set_full(dummy_item[0], dummy_item[1]) distance_field[dummy_item[0]][dummy_item[1]] = 0 while len(boundary) != 0: current_cell = boundary.dequeue() #for dummy_neighbors in self.four_neighbors(current_cell[0],current_cell[1]) if entity_type == ZOMBIE else self.eight_neighbors(current_cell[0],current_cell[1]): for dummy_neighbors in self.four_neighbors(current_cell[0], current_cell[1]): if self.is_empty(dummy_neighbors[0], dummy_neighbors[1]): if visited.is_empty(dummy_neighbors[0], dummy_neighbors[1]): visited.set_full(dummy_neighbors[0], dummy_neighbors[1]) boundary.enqueue(dummy_neighbors) if distance_field[dummy_neighbors[0]][ dummy_neighbors[1]] > distance_field[ current_cell[0]][current_cell[1]] + 1: distance_field[dummy_neighbors[0]][ dummy_neighbors[1]] = distance_field[ current_cell[0]][current_cell[1]] + 1 return distance_field
def move_humans(self, zombie_distance): """ Function that moves humans away from zombies, diagonal moves are allowed """ width = self.get_grid_width() height = self.get_grid_height() visited = poc_grid.Grid(height, width) obstacle = len(zombie_distance) * len(zombie_distance[0]) #print obstacle human_list = [] for human in self.humans(): neighbors = visited.eight_neighbors(human[0], human[1]) best_moves = {} best_moves[zombie_distance[human[0]][human[1]]] = human for neighbor in neighbors: if zombie_distance[neighbor[0]][neighbor[1]] != obstacle: if zombie_distance[neighbor[0]][ neighbor[1]] > zombie_distance[human[0]][human[1]]: best_moves[zombie_distance[neighbor[0]][ neighbor[1]]] = neighbor human_list.append(best_moves[max(best_moves)]) self._human_list = human_list
def compute_distance_field(self, entity_type): """ Function computes a 2D distance field Distance at member of entity_queue is zero Shortest paths avoid obstacles and use distance_type distances """ visited = poc_grid.Grid(self.get_grid_height(), self.get_grid_width()) for row in range(self.get_grid_height()): for col in range(self.get_grid_width()): if not self.is_empty(row, col): visited.set_full(row, col) distance_field = [[self.get_grid_height() * self.get_grid_width() \ for dummy_col in range(self.get_grid_width())] \ for dummy_row in range(self.get_grid_height())] entity_cells = self._zombie_list \ if entity_type is ZOMBIE \ else self._human_list boundary = poc_queue.Queue() for entity_cell in entity_cells: visited.set_full(entity_cell[0], entity_cell[1]) distance_field[entity_cell[0]][entity_cell[1]] = 0 boundary.enqueue(entity_cell) while len(boundary) > 0: current = boundary.dequeue() neighbors = self.four_neighbors(current[0], current[1]) distance = distance_field[current[0]][current[1]] + 1 for neighbor in neighbors: if visited.is_empty(neighbor[0], neighbor[1]): if distance < distance_field[neighbor[0]][neighbor[1]]: distance_field[neighbor[0]][neighbor[1]] = distance visited.set_full(neighbor[0], neighbor[1]) boundary.enqueue(neighbor) return distance_field
def compute_distance_field(self, entity_type): """ Function computes and returns a 2D distance field Distance at member of entity_list is zero Shortest paths avoid obstacles and use four-way distances """ visited = poc_grid.Grid(self._grid_height, self._grid_width) distance_field = [[ self._grid_height * self._grid_width for dummy_col in range(self._grid_width) ] for dummy_row in range(self._grid_height)] boundary = poc_queue.Queue() if entity_type == HUMAN: for human in self.humans(): boundary.enqueue(human) visited.set_full(human[0], human[1]) distance_field[human[0]][human[1]] = 0 elif entity_type == ZOMBIE: for zombie in self.zombies(): boundary.enqueue(zombie) visited.set_full(zombie[0], zombie[1]) distance_field[zombie[0]][zombie[1]] = 0 while len(boundary) > 0: current_cell = boundary.dequeue() for neighbor in visited.four_neighbors(current_cell[0], current_cell[1]): if visited.is_empty(neighbor[0], neighbor[1]) and self.is_empty( neighbor[0], neighbor[1]): visited.set_full(neighbor[0], neighbor[1]) boundary.enqueue((neighbor[0], neighbor[1])) distance_field[neighbor[0]][neighbor[1]] = distance_field[ current_cell[0]][current_cell[1]] + 1 return distance_field
def compute_distance_field(self, entity_type): """ Function computes and returns a 2D distance field Distance at member of entity_list is zero Shortest paths avoid obstacles and use four-way distances """ # grid height visited = poc_grid.Grid(self._grid_height, self._grid_width) distance_field = [ x[:] for x in [[self._grid_width * self._grid_height] * self._grid_width] * self._grid_height ] boundary = poc_queue.Queue() if entity_type == HUMAN: entity_type = self._human_list elif entity_type == ZOMBIE: entity_type = self._zombie_list for entity in entity_type: boundary.enqueue(entity) visited.set_full(entity[0], entity[1]) distance_field[entity[0]][entity[1]] = 0 while boundary.__len__() > 0: current_cell = boundary.dequeue() neighbor_cells = self.four_neighbors(current_cell[0], current_cell[1]) for nebor in neighbor_cells: if visited.is_empty(nebor[0], nebor[1]) and self.is_empty( nebor[0], nebor[1]): visited.set_full(nebor[0], nebor[1]) boundary.enqueue(nebor) distance_field[nebor[0]][nebor[1]] = distance_field[ current_cell[0]][current_cell[1]] + 1 return distance_field
def compute_distance_field(self, entity_type): """ Function computes and returns a 2D distance field Distance at member of entity_list is zero Shortest paths avoid obstacles and use four-way distances """ visited = poc_grid.Grid(self._grid_height, self._grid_width) distance_field = [[self._grid_height * self._grid_width \ for dummy_col in range(self._grid_width)] \ for dummy_row in range(self._grid_height)] boundary = poc_queue.Queue() if entity_type == ZOMBIE: for zombies in self.zombies(): boundary.enqueue(zombies) elif entity_type == HUMAN: for humans in self.humans(): boundary.enqueue(humans) for item in boundary: visited.set_full(item[0], item[1]) distance_field[item[0]][item[1]] = 0 while len(boundary) > 0: cell = boundary.dequeue() neighbors = self.four_neighbors(cell[0], cell[1]) for neighbor in neighbors: if visited.is_empty(neighbor[0], neighbor[1]) and self.is_empty( neighbor[0], neighbor[1]): visited.set_full(neighbor[0], neighbor[1]) boundary.enqueue(neighbor) distance_field[neighbor[0]][ neighbor[1]] = distance_field[cell[0]][cell[1]] + 1 return distance_field
def compute_distance_field(self, entity_type): """ Function computes and returns a 2D distance field Distance at member of entity_list is zero Shortest paths avoid obstacles and use four-way distances """ grid_height = poc_grid.Grid.get_grid_height(self) grid_width = poc_grid.Grid.get_grid_width(self) visited = poc_grid.Grid(grid_height, grid_width) distance_field = [[ grid_height*grid_width \ for dummy_col in range(grid_width)] \ for dummy_row in range(grid_height)] boundary = poc_queue.Queue() if entity_type == ZOMBIE: for zombie in self._zombie_list: boundary.enqueue(zombie) else: for human in self._human_list: boundary.enqueue(human) for cell in boundary: visited.set_full(cell[0], cell[1]) distance_field[cell[0]][cell[1]] = 0 while len(boundary) != 0: current_cell = boundary.dequeue() for neighbor in visited.four_neighbors(current_cell[0], current_cell[1]): if visited.is_empty(neighbor[0], neighbor[1]) and self.is_empty(neighbor[0], neighbor[1]): visited.set_full(neighbor[0], neighbor[1]) boundary.enqueue(neighbor) distance_field[neighbor[0]][neighbor[1]] = distance_field[current_cell[0]][current_cell[1]] + 1 return distance_field
def compute_distance_field(self, entity_type): """ Function computes a 2D distance field Distance at member of entity_queue is zero Shortest paths avoid obstacles and use distance_type distances """ visited = poc_grid.Grid(poc_grid.Grid.get_grid_height(self), poc_grid.Grid.get_grid_width(self)) distance_field = [[ poc_grid.Grid.get_grid_height(self) * poc_grid.Grid.get_grid_width(self) for dummy_col in range(poc_grid.Grid.get_grid_width(self)) ] for dummy_row in range(poc_grid.Grid.get_grid_height(self))] boundary = poc_queue.Queue() if entity_type == ZOMBIE: for zombie in self._zombie_list: boundary.enqueue(zombie) else: for human in self._human_list: boundary.enqueue(human) for cell in boundary: visited.set_full(cell[0], cell[1]) distance_field[cell[0]][cell[1]] = 0 while boundary: current_cell = boundary.dequeue() distance = distance_field[current_cell[0]][current_cell[1]] + 1 neighbors = self.four_neighbors(current_cell[0], current_cell[1]) for neighbor in neighbors: if visited.is_empty(neighbor[0], neighbor[1]): visited.set_full(neighbor[0], neighbor[1]) if self.is_empty(neighbor[0], neighbor[1]): distance_field[neighbor[0]][neighbor[1]] = distance boundary.enqueue(neighbor) return distance_field
def compute_distance_field(self, entity_type): """ Function computes and returns a 2D distance field Distance at member of entity_list is zero Shortest paths avoid obstacles and use four-way distances """ visited = poc_grid.Grid(self.get_grid_height(), self.get_grid_width()) distance_field = [[self.get_grid_width() * self.get_grid_height() for dummy_col in range( self.get_grid_width())] for dummy_row in range(self.get_grid_height())] # Create a queue, boundary, that is a *copy* of either the zombie list or the human list. For cells in the queue, initialize visited to be FULL and distance_field to be zero. We recommend that you use our Queue class. boundary = poc_queue.Queue() if entity_type == ZOMBIE: for zombie in self.zombies(): boundary.enqueue(zombie) elif entity_type == HUMAN: for human in self.humans(): boundary.enqueue(human) else: print "Invaid entity type in compute_distance_field()!" return for entity in boundary.__iter__(): visited.set_full(entity[0],entity[1]) distance_field[entity[0]][entity[1]] = 0 current_cell = None while boundary.__len__() > 0: print "--------------------------" # print "boundary queue:", boundary current_cell = boundary.dequeue() print "current_cell:", current_cell print "boundary queue:", boundary if entity_type == "ZOMBIE": neighbors = self.eight_neighbors(current_cell[0], current_cell[1]) else: neighbors = self.four_neighbors(current_cell[0], current_cell[1]) print "\nneighbors of ", current_cell, ":" print neighbors for neighbor_cell in neighbors: print "neighbor_cell:", neighbor_cell if visited.is_empty(neighbor_cell[0],neighbor_cell[1]) and self.is_empty(neighbor_cell[0],neighbor_cell[1]): visited.set_full(neighbor_cell[0],neighbor_cell[1]) boundary.enqueue(neighbor_cell) distance_field[neighbor_cell[0]][neighbor_cell[1]] = self.manhattan_distance(neighbor_cell[0],current_cell[0], neighbor_cell[1], current_cell[1]) # distance_field = self.create_distance_field([neighbor_cell, current_cell]) print "distance_field[neighbor_cell[0]][neighbor_cell[1]]:", distance_field[neighbor_cell[0]][neighbor_cell[1]] print "\nvisited:\n", visited print "distance_field:" self.print_field(distance_field) print "\nvisited:\n", visited print "\ndistance_field:\n" self.print_field(distance_field) return distance_field