def test_random_cell() -> None: grid = Grid(2, 2) for _ in range(100): assert grid.random_cell() in [ Cell(0, 0), Cell(0, 1), Cell(1, 0), Cell(1, 1) ]
def _rotate_cell_neighbors(self, new: Cell, old: Cell, grid: Grid) -> None: for link in old.links: row, col = self._rotated_coordinates(link, grid) neighbor = grid[row, col] if neighbor is None: raise IndexError('Cell not found at row {} column {}'.format( row, col)) new.link(neighbor)
def _rotate_cell_neighbors(new_cell: Cell, old_cell: Cell, grid: Grid) -> None: for link in old_cell.links: row, column = Rotator._rotated_coordinates(link, grid) neighbor = grid.cell_at(row, column) if neighbor is None: raise IndexError("Cell not found at row {} column {}".format( row, column)) new_cell.link(neighbor)
def test_index_access() -> None: a_cell = Cell(0, 0) distances = Distances(a_cell) assert distances[a_cell] == 0 another_cell = Cell(0, 1) distance = 13 distances[another_cell] = distance assert distances[another_cell] == distance assert distances[Cell(2, 2)] is None
def test_cell_access() -> None: grid = Grid(2, 2) assert grid.get_cell(0, 0) == Cell(0, 0) # type: ignore assert grid.get_cell(0, 1) == Cell(0, 1) # type: ignore assert grid.get_cell(1, 0) == Cell(1, 0) # type: ignore assert grid.get_cell(1, 1) == Cell(1, 1) # type: ignore assert grid.get_cell(-1, 0) is None assert grid.get_cell(0, -1) is None assert grid.get_cell(4, 0) is None assert grid.get_cell(0, 4) is None
def test_links_listing() -> None: a_cell = Cell(1, 1) another_cell = Cell(1, 2) yet_another_cell = Cell(2, 1) a_cell += another_cell a_cell += yet_another_cell assert set(a_cell.links).intersection([another_cell, yet_another_cell ]) == set(a_cell.links) assert another_cell.links == [a_cell] assert yet_another_cell.links == [a_cell]
def test_cell_access_using_operator_overloads() -> None: grid = Grid(2, 2) assert grid[0, 0] == Cell(0, 0) assert grid[0, 1] == Cell(0, 1) assert grid[1, 0] == Cell(1, 0) assert grid[1, 1] == Cell(1, 1) assert grid[-1, 0] is None assert grid[0, -1] is None assert grid[4, 0] is None assert grid[0, 4] is None
def test_cell_access() -> None: grid = Grid(2, 2) assert grid.cell_at(0, 0) == Cell(0, 0) assert grid.cell_at(0, 1) == Cell(0, 1) assert grid.cell_at(1, 0) == Cell(1, 0) assert grid.cell_at(1, 1) == Cell(1, 1) assert grid.cell_at(-1, 0) is None assert grid.cell_at(0, -1) is None assert grid.cell_at(4, 0) is None assert grid.cell_at(0, 4) is None
def test_linking_using_operator_overloads() -> None: a_cell = Cell(1, 1) another_cell = Cell(1, 2) yet_another_cell = Cell(2, 1) assert not a_cell & another_cell assert not another_cell & a_cell assert not a_cell & yet_another_cell assert not another_cell & yet_another_cell a_cell += another_cell assert a_cell & another_cell assert another_cell & a_cell assert not a_cell & yet_another_cell assert not another_cell & yet_another_cell
def test_has_neighbors() -> None: a_cell = Cell(1, 1) another_cell = Cell(1, 2) yet_another_cell = Cell(2, 1) a_cell.north = another_cell another_cell.south = yet_another_cell yet_another_cell.east = another_cell yet_another_cell.west = a_cell assert another_cell in a_cell.neighbors assert yet_another_cell not in a_cell.neighbors assert len(a_cell.neighbors) == 1 assert a_cell not in another_cell.neighbors assert yet_another_cell in another_cell.neighbors assert len(another_cell.neighbors) == 1 assert a_cell in yet_another_cell.neighbors assert another_cell in yet_another_cell.neighbors assert len(yet_another_cell.neighbors) == 2
def test_max_distance() -> None: cell_1 = Cell(0, 0) distances = Distances(cell_1) cell_2 = Cell(0, 1) distance_from_2_to_1 = 4 distances[cell_2] = distance_from_2_to_1 cell_3 = Cell(1, 0) distance_from_3_to_1 = 5 distances[cell_3] = distance_from_3_to_1 cell_4 = Cell(2, 0) distance_from_4_to_1 = 8 distances[cell_4] = distance_from_4_to_1 assert distances.max == ( cell_4, distance_from_4_to_1, )
def get_topmost_junction(cell: Cell) -> str: # special case for 1st row's junctions # # [ X ] [X-east] # junction = UnicodeExporter.EAST + UnicodeExporter.WEST if not cell.linked_to(cast(Cell, cell.east)): junction += UnicodeExporter.SOUTH return UnicodeExporter.JUNCTIONS[junction]
def get_south_east_junction(cell: Cell) -> str: # Taking advantage that we always go forward east and south, just need to calculate available posibilities # # [ X ] [X-east] # # [ X-south] [X-southeast] # junction = 0 if cell.east: if not cell.linked_to(cell.east): junction += UnicodeExporter.NORTH if not cell.east.south: junction += UnicodeExporter.EAST else: junction += UnicodeExporter.NORTH if cell.south: if not cell.linked_to(cell.south): junction += UnicodeExporter.WEST if not cell.south.east: junction += UnicodeExporter.SOUTH else: junction += UnicodeExporter.WEST if cell.east and cell.south: south_east_cell = cast(Cell, cell.south.east) if not cell.east.linked_to(south_east_cell): junction += UnicodeExporter.EAST if not cell.south.linked_to(south_east_cell): junction += UnicodeExporter.SOUTH try: return UnicodeExporter.JUNCTIONS[junction] except IndexError as ie: print("junction:{} error:{}".format(junction, ie)) return " "
def get_leftmost_junction(cell: Cell) -> str: # # [ X ] # # [ X-south] # junction = UnicodeExporter.NORTH if cell.south: junction += UnicodeExporter.SOUTH if not cell.linked_to(cell.south): junction += UnicodeExporter.EAST else: junction += UnicodeExporter.EAST return UnicodeExporter.JUNCTIONS[junction]
def test_distances() -> None: a_cell = Cell(0, 0) another_cell = Cell(0, 1) yet_another_cell = Cell(0, 2) a_cell.east = another_cell a_cell += another_cell another_cell.east = yet_another_cell another_cell += yet_another_cell distances = a_cell.distances assert set(distances.cells) == {yet_another_cell, another_cell, a_cell} assert distances[a_cell] == 0 assert distances[another_cell] == 1 assert distances[yet_another_cell] == 2
def test_neighbors_setup_when_grid_is_created() -> None: grid = Grid(2, 2) assert grid.get_cell(0, 0).north is None # type: ignore assert grid.get_cell(0, 0).south == Cell(1, 0) # type: ignore assert grid.get_cell(0, 0).east == Cell(0, 1) # type: ignore assert grid.get_cell(0, 0).west is None # type: ignore assert grid.get_cell(0, 1).north is None # type: ignore assert grid.get_cell(0, 1).south == Cell(1, 1) # type: ignore assert grid.get_cell(0, 1).east is None # type: ignore assert grid.get_cell(0, 1).west == Cell(0, 0) # type: ignore assert grid.get_cell(1, 0).north == Cell(0, 0) # type: ignore assert grid.get_cell(1, 0).south is None # type: ignore assert grid.get_cell(1, 0).east == Cell(1, 1) # type: ignore assert grid.get_cell(1, 0).west is None # type: ignore assert grid.get_cell(1, 1).north == Cell(0, 1) # type: ignore assert grid.get_cell(1, 1).south is None # type: ignore assert grid.get_cell(1, 1).east is None # type: ignore assert grid.get_cell(1, 1).west == Cell(1, 0) # type: ignore
def test_has_no_neighbors() -> None: assert Cell(1, 1).neighbors == []
def test_linking() -> None: a_cell = Cell(1, 1) another_cell = Cell(1, 2) yet_another_cell = Cell(2, 1) assert a_cell.linked_to(another_cell) is False assert another_cell.linked_to(a_cell) is False assert a_cell.linked_to(yet_another_cell) is False assert another_cell.linked_to(yet_another_cell) is False a_cell.link(another_cell) assert a_cell.linked_to(another_cell) is True assert another_cell.linked_to(a_cell) is True assert a_cell.linked_to(yet_another_cell) is False assert another_cell.linked_to(yet_another_cell) is False
def test_equality() -> None: a_cell = Cell(1, 1) another_cell = Cell(1, 1) assert a_cell == another_cell
def prepare_grid(self) -> List[List[Cell]]: return [[Cell(row, column) for column in range(self.columns)] for row in range(self.rows)]
def prepareGrid(self) -> List[List[Cell]]: ''' Create the grid ''' return [[Cell(row, column) for column in range(self.cols)] for row in range(self.rows)]