def push(self, g_n, h_n, cell: Cell, parent: Optional[Cell]): co_ordinates = cell.get_co_ordinates() if (co_ordinates not in self.closed) and (co_ordinates not in self.heap_co_ords): self.heap.append(Element(g_n, h_n, cell, parent)) self.heap_co_ords.add(cell.get_co_ordinates()) self.heapsort()
def testAddOpposite(self): cell = Cell() cell.addExit(1, 0) with self.assertRaises(InvalidExitError) as result: cell.addExit(-1, 0) self.assertEqual('Invalid exit (-1, 0) when (1, 0) is present.', result.exception.message)
def print_path(path, mazevis, algo_name, start: Cell, end: Cell): count = 0 for cell in path[:-1]: if cell.get_co_ordinates() not in [ start.get_co_ordinates(), end.get_co_ordinates() ]: mazevis.fill_cell(cell.row, cell.col) count += 1 print("Total {} moves are: {} \n".format(algo_name, count))
def setMaze(self, size): self.size = size # the dimension of one side of the maze self.cells = [Cell.Cell(i, size) for i in range(size**2)] self.startingCell = self.cells[0] self.endingCell = self.cells[size**2 - 1] self.mazeWalls = [] self.allRect = [] self.genMaze()
def BFS(maze): visited = [[False] * maze.cols for _ in range(maze.rows)] start = Cell(maze.get_start_coord()) end = Cell(maze.get_end_coord()) queue = [start] visited[start.coord[0]][start.coord[1]] = True while queue and queue[0].coord != end.coord: cur_cell = queue.pop(0) for d in directions: new_coord = (cur_cell.coord[0] + d[0], cur_cell.coord[1] + d[1]) if maze.is_valid_point(new_coord, visited): new = Cell(new_coord) new.prev = cur_cell queue.append(new) visited[new_coord[0]][new_coord[1]] = True if queue: return queue[0] else: raise IndexError
def testRemoveExit(self): cell = Cell() cell.addExit(1, 0) cell.removeExit(1, 0) self.assertFalse(cell.hasExit(1, 0))
def testAddExit(self): cell = Cell() cell.addExit(1, 0) self.assertTrue(cell.hasExit(1, 0)) self.assertFalse(cell.hasExit(-1, 0))
def testFromArrows(self): cell = Cell('^>') self.assertTrue(cell.hasExit(1, 0)) self.assertFalse(cell.hasExit(-1, 0))
def testHasExit(self): cell = Cell((1, 0)) self.assertTrue(cell.hasExit(1, 0)) self.assertFalse(cell.hasExit(-1, 0))