def __init__(self, grid_height, grid_width, obstacle_list=None, zombie_list=None, human_list=None): """ Create a simulation of given size with given obstacles, humans, and zombies """ poc_grid.Grid.__init__(self, grid_height, grid_width) if obstacle_list != None: for cell in obstacle_list: self.set_full(cell[0], cell[1]) self._zombie_list = poc_queue.Queue() if zombie_list != None: for zombie in zombie_list: self._zombie_list.enqueue(zombie) self._human_list = poc_queue.Queue() if human_list != None: for human in human_list: self._human_list.enqueue(human)
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 """ iloczyn = self._grid_height * self._grid_width if entity_type == ZOMBIE: visited = poc_grid.Grid(self._grid_height, self._grid_width) distance_field = [[ iloczyn for dummy_col in range(self._grid_width) ] for dummy_row in range(self._grid_height)] boundary = poc_queue.Queue() for item in self.zombies(): boundary.enqueue(item) for item in boundary: visited.set_full(item[0], item[1]) distance_field[item[0]][item[1]] = 0 while boundary: 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]] = mniejsza( 1 + distance_field[cell[0]][cell[1]], distance_field[neighbor[0]][neighbor[1]]) return distance_field else: visited = poc_grid.Grid(self._grid_height, self._grid_width) distance_field = [[ iloczyn for dummy_col in range(self._grid_width) ] for dummy_row in range(self._grid_height)] boundary = poc_queue.Queue() for item in self.humans(): boundary.enqueue(item) for item in boundary: visited.set_full(item[0], item[1]) distance_field[item[0]][item[1]] = 0 while boundary: 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]] = mniejsza( 1 + distance_field[cell[0]][cell[1]], distance_field[neighbor[0]][neighbor[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()) distance_field = [[ self.get_grid_height() * self.get_grid_width() for dummy_row in range(self.get_grid_width()) ] for dummy_col in range(self.get_grid_height())] boundary = poc_queue.Queue() # TODO: check for entity type if entity_type == ZOMBIE: entity_list = self.zombies() elif entity_type == HUMAN: entity_list = self.humans() else: print "Wrong entity type provided for compute_distance_field(entity_type)." return for entity in entity_list: boundary.enqueue(entity) visited.set_full(entity[0], entity[1]) distance_field[entity[0]][entity[1]] = 0 while len(boundary) != 0: current_cell = boundary.dequeue() for neighbor_cell in self.four_neighbors(current_cell[0], current_cell[1]): 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]] = \ 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(self.get_grid_height(), self.get_grid_width()) visited.clear() distance_field = [[ self.get_grid_height() * self.get_grid_width() for dummy_x in range(self.get_grid_width()) ] for dummy_y in range(self.get_grid_height())] boundary = poc_queue.Queue() boundary_list = self._zombie_list if entity_type == ZOMBIE else self._human_list for item in boundary_list: boundary.enqueue(item) visited.set_full(item[0], item[1]) distance_field[item[0]][item[1]] = 0 while len(boundary): current_point = boundary.dequeue() neigbourd_point = self.four_neighbors(current_point[0], current_point[1]) for point in neigbourd_point: if visited.is_empty( point[0], point[1]) and not self._cells[point[0]][point[1]]: visited.set_full(point[0], point[1]) distance_field[point[0]][point[1]] = min( distance_field[point[0]][point[1]], distance_field[current_point[0]][current_point[1]] + 1) boundary.enqueue((point[0], point[1])) return distance_field
def compute_distance_field(self, entity_type): grid_height = poc_grid.Grid.get_grid_height(self) grid_width = poc_grid.Grid.get_grid_width(self) visited = [[EMPTY for dummy_col in range(grid_width)] for dummy_row in range(grid_height)] distance_field = [] for dummy_row in range(grid_height): temp_row = [] for dummy_col in range(grid_width): temp_row.append(grid_height * grid_width) distance_field.append(temp_row) boundary = poc_queue.Queue() if entity_type == ZOMBIE: for items in self._zombie_list: boundary.enqueue(items) visited[items[0]][items[1]] = FULL distance_field[items[0]][items[1]] = 0 elif entity_type == HUMAN: for items in self._human_list: boundary.enqueue(items) visited[items[0]][items[1]] = FULL distance_field[items[0]][items[1]] = 0 while len(boundary) != 0: current_cell = boundary.dequeue() for neighbor_cell in poc_grid.Grid.four_neighbors( self, current_cell[0], current_cell[1]): if visited[neighbor_cell[0]][neighbor_cell[1]] == EMPTY \ and poc_grid.Grid.is_empty (self, neighbor_cell[0], neighbor_cell[1]): visited[neighbor_cell[0]][neighbor_cell[1]] = FULL boundary.enqueue(neighbor_cell) distance_field[neighbor_cell[0]][neighbor_cell[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 """ distance_field = [[self._grid_height * self._grid_width for dummy_row in range(self._grid_width)] for dummy_col in range(self._grid_height)] # we populate the resulting array with the max distances possible (w*h) visited = [[EMPTY for dummy_row in range(self._grid_width)] for dummy_col in range(self._grid_height)] # this is where we are going to store the info about teh cells already visited boundary = poc_queue.Queue() if entity_type == ZOMBIE: for zombie in self._zombie_list: boundary.enqueue(zombie) elif entity_type == HUMAN: for human in self._human_list: boundary.enqueue(human) for cell in boundary: visited[cell[0]][cell[1]] = FULL distance_field[cell[0]][cell[1]] = 0 while len(boundary) > 0: current_cell = boundary.dequeue() current_value = distance_field[current_cell[0]][current_cell[1]] neighbors = self.four_neighbors(current_cell[0], current_cell[1]) # we are going to process the neighbours of a particular cell one at the time and increase the distance by 1 for each neighbouring ring for neighbor in neighbors: if self.is_empty(neighbor[0], neighbor[1]) and visited[neighbor[0]][neighbor[1]] == EMPTY: # if the grid cell is empty (no obstacles) and is not yet visited (a similar object is already closer) visited[neighbor[0]][neighbor[1]] = FULL boundary.enqueue(neighbor) # now the neighbor becomes a center for the neighboring cells distance_field[neighbor[0]][neighbor[1]] = current_value + 1 # for all the neighbours we increase the value of the cell by 1 return distance_field
def compute_distance_field(self, entity_type): self._height = self.get_grid_height() self._width = self.get_grid_width() self._visited = poc_grid.Grid(self._height, self._width) self._distance_field = [[ self._height * self._width for dummy_col in range(self._width) ] for dummy_row in range(self._height)] self._boundary = poc_queue.Queue() if entity_type == HUMAN: for cell in self._human_list: self._boundary.enqueue((cell[0], cell[1])) self._visited.set_full(cell[0], cell[1]) self._distance_field[cell[0]][cell[1]] = 0 elif entity_type == ZOMBIE: for cell in self._zombie_list: self._boundary.enqueue((cell[0], cell[1])) self._visited.set_full(cell[0], cell[1]) self._distance_field[cell[0]][cell[1]] = 0 while self._boundary.__len__(): current_cell = self._boundary.dequeue() neighbors = self.four_neighbors(current_cell[0], current_cell[1]) for neighbor in neighbors: if self._visited.is_empty(neighbor[0], neighbor[1]) and self.is_empty( neighbor[0], neighbor[1]): self._visited.set_full(neighbor[0], neighbor[1]) self._boundary.enqueue(neighbor) self._distance_field[neighbor[0]][ neighbor[1]] = self._distance_field[current_cell[0]][ current_cell[1]] + 1 return self._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_height = self.get_grid_height() grid_width = self.get_grid_width() #Create a 2D list distance_field of the same size as the grid and initialize each of its entries to be the product of the height times the width of the grid distance_field = [[grid_height*grid_width \ for dummy_col in range(grid_width)] \ for dummy_row in range(grid_height)] #obstacle grid, initialize its cells to be empty visited = poc_grid.Grid(grid_height, grid_width) boundary = poc_queue.Queue() if entity_type == ZOMBIE: for zom in self.zombies(): boundary.enqueue(zom) elif entity_type == HUMAN: for hum in self.humans(): boundary.enqueue(hum) for grid in boundary: visited.set_full(grid[0], grid[1]) # initialize visited to be FULL distance_field[grid[0]][grid[1]] = 0 # initialize distance_field to be zero while len(boundary) > 0: #while boundary.__len__()>0: cell = boundary.dequeue() neighbors = visited.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]] = min(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 """ distance = 1 visited = self #print visited grid_height = poc_grid.Grid.get_grid_height(self) grid_width = poc_grid.Grid.get_grid_width(self) distance_field = [[ grid_height * grid_width for dummy_col in range(grid_width) ] for dummy_row in range(grid_height)] #print distance_field boundary = poc_queue.Queue() for item0 in self._human_or_zombie[entity_type][0]: boundary.enqueue(item0) visited.set_full(item0[0], item0[1]) distance_field[item0[0]][item0[1]] = 0 #print visited while len(boundary) > 0: neighbors = [] while len(boundary) > 0: cell = boundary.dequeue() for item1 in self.four_neighbors(cell[0], cell[1]): neighbors.append(item1) for neighbor in neighbors: if visited.is_empty(neighbor[0], neighbor[1]): visited.set_full(neighbor[0], neighbor[1]) distance_field[neighbor[0]][neighbor[1]] = distance boundary.enqueue(neighbor) distance += 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 _ in range(self._grid_width)] for _ in range(self._grid_height)] boundary = poc_queue.Queue() if 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 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 while boundary: 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 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 = self.get_grid_height() grid_width = self.get_grid_width() visited = poc_grid.Grid(grid_height, grid_width) # [[visited.set_empty(row, col) for col in range(self.get_grid_width())] for row in range(self.get_grid_height())] distance_field = [[grid_height * grid_width for col in range(grid_width)] for row in range(grid_height)] boundary = poc_queue.Queue() if entity_type == ZOMBIE: entity_list = self._zombie_list else: entity_list = self._human_list for item in entity_list: boundary.enqueue(item) visited.set_full(item[0], item[1]) distance_field[item[0]][item[1]] = 0 while len(boundary) > 0: current_cell = boundary.dequeue() neighbor_cell = poc_grid.Grid.four_neighbors(self, current_cell[0], current_cell[1]) for each_cell in neighbor_cell: if visited.is_empty(each_cell[0], each_cell[1]) and self.is_empty(each_cell[0], each_cell[1]): visited.set_full(each_cell[0], each_cell[1]) distance_field[each_cell[0]][each_cell[1]] = distance_field[current_cell[0]][current_cell[1]] + 1 boundary.enqueue(each_cell) 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 """ # Height of Grid 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 """ entity_dict = {HUMAN: self._human_list, ZOMBIE: self._zombie_list} 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)] # creating a boundary for the breadth-first search boundary = poc_queue.Queue() for entity in entity_dict[entity_type]: boundary.enqueue(entity) distance_field[entity[0]][entity[1]] = 0 visited.set_full(entity[0], entity[1]) # execution of the breadth-first search while len(boundary) > 0: current_cell = boundary.dequeue() neighbors = self.four_neighbors(current_cell[0], current_cell[1]) for neighbor in neighbors: if neighbor in self._obtacle_list: continue if visited.is_empty(neighbor[0], neighbor[1]): visited.set_full(neighbor[0], neighbor[1]) distance_field[neighbor[0]][neighbor[1]] = distance_field[ current_cell[0]][current_cell[1]] + 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 """ 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 in range(grid_width)] for dummy in range(grid_height)] list_copy = [] if entity_type == HUMAN: list_copy = self._human_list else: list_copy = self._zombie_list boundary = poc_queue.Queue() for creature in list_copy: row = creature[0] col = creature[1] poc_queue.Queue.enqueue(boundary, creature) poc_grid.Grid.set_full(visited, row, col) distance_field[row][col] = 0 while boundary: current_cell = poc_queue.Queue.dequeue(boundary) neighbors = poc_grid.Grid.four_neighbors(self, current_cell[0], current_cell[1]) for neighbor in neighbors: row = neighbor[0] col = neighbor[1] if poc_grid.Grid.is_empty(visited, row, col) and \ poc_grid.Grid.is_empty(self, row, col): poc_grid.Grid.set_full(visited, row, col) poc_queue.Queue.enqueue(boundary, neighbor) distance_field[row][col] = 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.get_grid_height(), self.get_grid_width()) visited.clear() 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())] #print distance_field boundary = poc_queue.Queue() entity_func = {ZOMBIE:self.zombies(), HUMAN:self.humans()} #print entity_type for entity in entity_func[entity_type]: #print entity boundary.enqueue(entity) visited.set_full(entity[0], entity[1]) distance_field[entity[0]][entity[1]] = 0 #print distance_field while len(boundary)!=0: current_cell = boundary.dequeue() neighbors = self.four_neighbors(current_cell[0], current_cell[1]) for cell in neighbors: if visited.is_empty(cell[0], cell[1]) and self.is_empty(cell[0], cell[1]): visited.set_full(cell[0], cell[1]) boundary.enqueue(cell) distance_field[cell[0]][cell[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 = [[EMPTY for dummy_col in range(self._grid_width)]\ for dummy_row in range(self._grid_height)] distance_field = [[self._grid_width * self._grid_height \ 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._human_list: boundary.enqueue(human) elif entity_type == ZOMBIE: for zombie in self._zombie_list: boundary.enqueue(zombie) for item in boundary.__iter__(): visited[item[0]][item[1]] = FULL distance_field[item[0]][item[1]] = 0 while boundary.__len__() != 0: current_cell = boundary.dequeue() neighbors = self.four_neighbors(current_cell[0], current_cell[1]) for neighbor in neighbors: if visited[neighbor[0]][neighbor[1]] == EMPTY: visited[neighbor[0]][neighbor[1]] = FULL 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 and returns a 2D distance field Distance at member of entity_list is zero Shortest paths avoid obstacles and use four-way distances """ height = self.get_grid_height() width = self.get_grid_width() if entity_type == HUMAN: cells = self._human_list else: cells = self._zombie_list visited = poc_grid.Grid(height, width) distance_field = [[height * width for dummy_col in xrange(width)] for dummy_row in xrange(height)] boundary = poc_queue.Queue() for cell in cells: visited.set_full(cell[0], cell[1]) distance_field[cell[0]][cell[1]] = 0 boundary.enqueue(cell) while len(boundary): current_cell = boundary.dequeue() # if entity_type == HUMAN: # neighbors = visited.eight_neighbors(current_cell[0], current_cell[1]) if entity_type == HUMAN: neighbors = visited.four_neighbors(current_cell[0], current_cell[1]) else: neighbors = visited.four_neighbors(current_cell[0], current_cell[1]) for neighbor in neighbors: if visited.is_empty(neighbor[0], neighbor[1]) and self.is_empty(neighbor[0], neighbor[1]): boundary.enqueue(neighbor) distance_field[neighbor[0]][neighbor[1]] = distance_field[current_cell[0]][current_cell[1]] + 1 visited.set_full(neighbor[0], neighbor[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.__init__(self, self._grid_height, self._grid_width) distance_field = [[ self._grid_height * self._grid_width for dummy in range(self._grid_width) ] for dummy_j in range(self._grid_width)] boundary = poc_queue.Queue() if entity_type == "ZOMBIE": boundary.enqueue(self.zombies) elif entity_type == "HUMAN": boundary.enqueue(self.humans) visited[boundary.__iter__()[0]][boundary.__iter__()[1]] = FULL distance_field[boundary.__iter__()[0]][boundary.__iter__()[1]] = EMPTY curr_cell = boundary.dequeue() neighbors = poc_grid.Grid.four_neighbors(self, curr_cell[0], curr_cell[1]) for cell in neighbors: if visited[cell[0]][cell[1]] == 0: visited[cell[0]][cell[1]] = FULL boundary.enqueue(cell) distance_field[cell[0]][cell[1]] += 1 return distance_field
def compute_distance_field(self, entity_type): 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 """ width = self.get_grid_width() height = self.get_grid_height() visited_field = poc_grid.Grid(height, width) dis_field = [[width * height for dummy_col in range(width)] for dummy_row in range(height)] boundry = poc_queue.Queue() if entity_type == HUMAN: for item in self.humans(): boundry.enqueue(item) elif entity_type == ZOMBIE: for item in self.zombies(): boundry.enqueue(item) for item in boundry: visited_field.set_full(item[0], item[1]) dis_field[item[0]][item[1]] = 0 while len(boundry) != 0: cell = boundry.dequeue() neighbors = visited_field.four_neighbors(cell[0], cell[1]) for neighbor in neighbors: if visited_field.is_empty(neighbor[0], neighbor[1]) and self.is_empty( neighbor[0], neighbor[1]): visited_field.set_full(neighbor[0], neighbor[1]) boundry.enqueue(neighbor) dis_field[neighbor[0]][ neighbor[1]] = dis_field[cell[0]][cell[1]] + 1 return dis_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._grid_height, self._grid_width) distance_field = [[self._grid_height * self._grid_width] * 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() neighbor_cells = self.four_neighbors(current_cell[0], current_cell[1]) for neighbor_cell in neighbor_cells: 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]) distance_field[neighbor_cell[0]][ neighbor_cell[1]] = distance_field[current_cell[0]][ current_cell[1]] + 1 boundary.enqueue(neighbor_cell) 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 item in self._human_list: boundary.enqueue(item) else: for item in self._zombie_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 (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 = self.get_grid_height() grid_width = self.get_grid_width() self._visited = poc_grid.Grid(grid_height, grid_width) self._visited.clear() self._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: list_to_queue = list(self._zombie_list) elif entity_type == HUMAN: list_to_queue = list(self._human_list) else: return self._distance_field for element in list_to_queue: boundary.enqueue(element) for cell in boundary: self._visited.set_full(cell[0], cell[1]) self._distance_field[cell[0]][cell[1]] = EMPTY while len(boundary) > 0: current_cell = boundary.dequeue() neighbor_cells = self.four_neighbors(current_cell[0], current_cell[1]) for neighbor in neighbor_cells: if self._visited.is_empty(neighbor[0], neighbor[1]) and self.is_empty(neighbor[0], neighbor[1]): self._visited.set_full(neighbor[0], neighbor[1]) boundary.enqueue(neighbor) self._distance_field[neighbor[0]][neighbor[1]] = self._distance_field[current_cell[0]][current_cell[1]] + 1 return self._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 """ entry_list = self._human_list if entity_type == HUMAN else self._zombie_list distance_grid = [[ self._grid_height * self._grid_width for dummy_x in range(self._grid_width) ] for dummy_y in range(self._grid_height)] visited = poc_grid.Grid(self._grid_height, self._grid_width) boundary = poc_queue.Queue() for item in entry_list: boundary.enqueue(item) distance_grid[item[0]][item[1]] = 0 while len(boundary) != 0: item = boundary.dequeue() visited.set_full(item[0], item[1]) neighbors = poc_grid.Grid.four_neighbors(self, item[0], item[1]) for each_neighbor in neighbors: if visited.is_empty( each_neighbor[0], each_neighbor[1]) and poc_grid.Grid.is_empty( self, each_neighbor[0], each_neighbor[1]): visited.set_full(each_neighbor[0], each_neighbor[1]) distance_grid[each_neighbor[0]][ each_neighbor[1]] = distance_grid[item[0]][item[1]] + 1 boundary.enqueue(each_neighbor) return distance_grid
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 = [[0 for dummy_col in range(self._grid_width)] for dummy_row in range(self._grid_height)] infi = self._grid_height * self._grid_width distance_field = [[infi for dummy_col in range(self._grid_width)] for dummy_row in range(self._grid_height)] boundary = poc_queue.Queue() if entity_type == HUMAN: entity_list = self._human_list elif entity_type == ZOMBIE: entity_list = self._zombie_list for entity in entity_list: boundary.enqueue(entity) visited[entity[0]][entity[1]] = FULL distance_field[entity[0]][entity[1]] = 0 while len(boundary) != 0: current_cell = boundary.dequeue() for neighbor_cell in self.four_neighbors(current_cell[0], current_cell[1]): if visited[neighbor_cell[0]][ neighbor_cell[1]] == EMPTY and self.is_empty( neighbor_cell[0], neighbor_cell[1]): visited[neighbor_cell[0]][neighbor_cell[1]] = FULL distance_field[neighbor_cell[0]][neighbor_cell[1]] = min( distance_field[neighbor_cell[0]][neighbor_cell[1]], distance_field[current_cell[0]][current_cell[1]] + 1) boundary.enqueue(neighbor_cell) 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 """ height = poc_grid.Grid.get_grid_height(self) widht = poc_grid.Grid.get_grid_width(self) visited = [[EMPTY for dummy in range(widht)] for dummy in range(height)] distance_field = [[widht * height for dummy in range(widht)] for dummy in range(height)] queue = poc_queue.Queue() if entity_type == ZOMBIE: use_list = self._zombie_list else: use_list = self._human_list for item in use_list: queue.enqueue(item) visited[item[0]][item[1]] = FULL distance_field[item[0]][item[1]] = 0 while queue: current_cell = queue.dequeue() neighbors = poc_grid.Grid.four_neighbors(self, current_cell[0], current_cell[1]) for neighbor in neighbors: if poc_grid.Grid.is_empty(self, neighbor[0], neighbor[1]): if not visited[neighbor[0]][neighbor[1]]: queue.enqueue(neighbor) distance_field[neighbor[0]][ neighbor[1]] = distance_field[current_cell[0]][ current_cell[1]] + 1 visited[neighbor[0]][neighbor[1]] = FULL return distance_field
def compute_distance_field(self, entity_type): 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): enum_l = self._human_list[:] elif (entity_type == ZOMBIE): enum_l = self._zombie_list[:] for row in (enum_l): visited.set_full(row[0], row[1]) distance_field[row[0]][row[1]] = 0 boundary.enqueue((row[0], row[1])) while len(boundary): 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 """ visited = [[EMPTY for dummy_col in range(self.get_grid_width())] for dummy_row in range(self.get_grid_height())] 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())] boundary = poc_queue.Queue() if entity_type == ZOMBIE: for zombie in self.zombies(): boundary.enqueue(zombie) else: for human in self.humans(): boundary.enqueue(human) for cell in boundary: visited[cell[0]][cell[1]] = FULL distance_field[cell[0]][cell[1]] = 0 while len(boundary) != 0: current_cell = boundary.dequeue() neighbors = self.four_neighbors(current_cell[0], current_cell[1]) for neigh in neighbors: if visited[neigh[0]][neigh[1]] == EMPTY and self.is_empty( neigh[0], neigh[1]): visited[neigh[0]][neigh[1]] = FULL boundary.enqueue(neigh) distance_field[neigh[0]][neigh[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 """ # Create a new grid πππππππ of the same size as the original grid # and initialize its cells to be empty. visited = poc_grid.Grid(self._grid_height, self._grid_width) # Create a 2D list ππππππππ_πππππ of the same size as the original # grid and initialize each of its entries to be the product of the # height times the width of the grid. # (This value is larger than any possible distance.) distance_field = [[ self._grid_height * self._grid_width for dummy_column in range(self._grid_width) ] for dummy_row in range(self._grid_height)] # Create a queue ππππππππ’ that is a copy of either the zombie list or the human list. # For cells in the queue, initialize πππππππ to be π΅ππ»π» and ππππππππ_πππππ to be zero. # We recommend that you use our πππππ class. boundary = poc_queue.Queue() if entity_type == ZOMBIE: for zombie in self.zombies(): boundary.enqueue(zombie) else: for human in self.humans(): boundary.enqueue(human) for cell in boundary: # set visited grid to FULL and distance_field to 0 # for each cell in boundary queue visited.set_full(cell[0], cell[1]) distance_field[cell[0]][cell[1]] = 0 # while boundary is not empty: while len(boundary) != 0: # current_cell β dequeue boundary current_cell = boundary.dequeue() # for all neighbor_cell of current_cell: neighbors = visited.four_neighbors(current_cell[0], current_cell[1]) for neighbor_cell in neighbors: # if neighbor_cell is not in visited: if visited.is_empty(neighbor_cell[0], neighbor_cell[1]) and self.is_empty( neighbor_cell[0], neighbor_cell[1]): # add neighbor_cell to visited visited.set_full(neighbor_cell[0], neighbor_cell[1]) # enqueue neighbor_cell onto boundary boundary.enqueue(neighbor_cell) distance_field[neighbor_cell[0]][ neighbor_cell[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 Args: entity_type, string "zombie", "human", or "obstacle". """ # Initialize a grid the same size as our other. # The terminology used in the instructions for this was absolutely terrible # so be careful with this. If there are future problems look here. visited = poc_grid.Grid(self.get_grid_height(), self.get_grid_width()) # Set all of the cells in visited to empty. Maybe a little paranoid... # They start out as empty but we're going to do it anyhow just in case. visited.clear() # Setup distance_field as a list. Again the instructions for this were # absolutely terrible so watchout here. 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 an empty queue for tracking humans or zombies. boundary = poc_queue.Queue() # Add humans or zombies to the queue and initialize # the proper values in visited and distance_field for item in self.get_entity_list(entity_type): boundary.enqueue(item) # Mark the grid square as FULL visited.set_full(item[0], item[1]) # Set distance_field cells in boundary queue to 0 distance_field[item[0]][item[1]] = 0 ### End initialization ### # Use a breadth-first search to compute # the distance between humans and zombies. while len(boundary) > 0: neighbor_cells = [] current_cell = boundary.dequeue() neighbor_cells.extend( self.four_neighbors(current_cell[0], current_cell[1])) # Distance is the position of the current cell + 1 (incrementing is not necessary) distance = distance_field[current_cell[0]][current_cell[1]] + 1 for row, col in neighbor_cells: # If there isn't an obstacle # if self.is_empty(row, col): if (row, col) not in self.__get_obstacle_list(): # If the cell hasn't been visited if visited.is_empty(row, col): visited.set_full(row, col) distance_field[row][col] = distance boundary.enqueue((row, col)) return distance_field