from scripted import fixture_board from dojo.board import Board, Pos, edge from dojo.movement import walks BOARD = fixture_board() def test_walks_have_exact_length_and_never_repeat_a_square() -> None: start = Pos(4, 4) paths = walks(BOARD, start, 4, {start}, can_enter_dojo=False) assert paths for path in paths: assert len(path) == 4 assert len({start, *path}) == 5 for a, b in zip((start, *path), path, strict=False): assert b in set(BOARD.neighbours(a)) def test_visited_squares_are_never_entered() -> None: start = Pos(4, 4) visited = {start, Pos(4, 5), Pos(3, 4)} paths = walks(BOARD, start, 3, visited, can_enter_dojo=False) assert all(not visited & set(path) for path in paths) def test_dojo_is_a_wall_unless_it_may_be_entered() -> None: start = Pos(4, 7) closed = walks(BOARD, start, 5, {start}, can_enter_dojo=False) opened = walks(BOARD, start, 5, {start}, can_enter_dojo=True) assert not any(BOARD.dojo & set(path) for path in closed) into_dojo = [path for path in opened if path[-1] in BOARD.dojo] assert (Pos(5, 7), Pos(6, 7)) in into_dojo assert all(cell not in BOARD.dojo for path in into_dojo for cell in path[:-1]) assert set(opened) - set(into_dojo) == set(closed) def test_boxed_in_pawn_moves_as_far_as_possible() -> None: walls = frozenset(edge(Pos(0, c), Pos(1, c)) for c in range(3)) corridor = Board(cells=((".",) * 3,) * 3, walls=walls, tunnels={}) paths = walks(corridor, Pos(0, 0), 5, {Pos(0, 0)}, can_enter_dojo=False) assert paths == [(Pos(0, 1), Pos(0, 2))]