Exemplo n.º 1
0
    def setUp(self):
        self.red_car = Car('r', Direction.horizontal, 2)
        self.board = board_from_string("""\
....AA
..BBCC
rr..EF
GGHHEF
...IEF
...IJJ
""")
Exemplo n.º 2
0
def test_solve_solves_solved_board():
    board = board_from_string(
        """\
......
......
....rr
......
......
......
"""
    )

    eq_(solve(board), [])
Exemplo n.º 3
0
def test_load_empty_board():
    board_description = """\
......
......
......
......
......
......
"""

    board = board_from_string(board_description)
    
    eq_(len(board.cars), 0)
Exemplo n.º 4
0
def test_solve_simple_case():
    board = board_from_string(
        """\
......
......
rr....
......
......
......
"""
    )

    eq_(solve(board), [(Car("r", Direction.horizontal, 2), Direction.horizontal, 4)])
Exemplo n.º 5
0
def test_load_board_with_only_red_car():
    board_description = """\
......
......
rr....
......
......
......
"""

    board = board_from_string(board_description)
    
    eq_(len(board.cars), 1)
    assert_in(Car('r', Direction.horizontal, 2), board.cars)
Exemplo n.º 6
0
def test_get_car_at_position():
    board_description = """\
....AA
..BBCC
rr..EF
GGHHEF
...IEF
...IJJ
"""

    board = board_from_string(board_description)
    eq_(board.car_at_position(1, 2).color, 'r')
    eq_(board.car_at_position(4, 3).color, 'E')
    eq_(board.car_at_position(5, 5).color, 'J')
Exemplo n.º 7
0
def test_get_moves():
    board = board_from_string("""\
......
..A...
..A...
......
......
......
""")

    eq_(board.get_moves(), {
        (Car('A', Direction.vertical, 2), Direction.vertical, -1),
        (Car('A', Direction.vertical, 2), Direction.vertical, 1),
        (Car('A', Direction.vertical, 2), Direction.vertical, 2),
        (Car('A', Direction.vertical, 2), Direction.vertical, 3),
    })
Exemplo n.º 8
0
def test_get_moves():
    board = board_from_string("""\
......
......
rr....
......
......
......
""")

    eq_(board.get_moves(), {
        (Car('r', Direction.horizontal, 2), Direction.horizontal, 1),
        (Car('r', Direction.horizontal, 2), Direction.horizontal, 2),
        (Car('r', Direction.horizontal, 2), Direction.horizontal, 3),
        (Car('r', Direction.horizontal, 2), Direction.horizontal, 4),
    })
Exemplo n.º 9
0
def test_complex_test_case():
    board = board_from_string(
        """\
....AA
..BBCC
rr..EF
GGHHEF
...IEF
...IJJ
"""
    )

    moves = solve(board)
    for move in moves:
        board.move(*move)
    ok_(board.is_victory())
    eq_(len(moves), 13)
Exemplo n.º 10
0
def test_hardest_board():
    # According to http://www.ulb.ac.be/di/algo/secollet/papers/crs06.pdf,
    # this is actually the hardest composition.
    # It is supposed to take 93 steps, but the solver solves it in 49. This is
    # due to a counting-difference in how slides across multiple squares is
    # counted (red car, 4 to the left can be counted as either 1 or 4 steps).
    board = board_from_string(
        """\
AAABCD
EFFBCD
E.rrCD
HHI...
.JI.KK
.JLLMM
"""
    )

    moves = solve(board)
    ok_(_check_solves(board, moves))
    eq_(len(moves), 49)
Exemplo n.º 11
0
def test_first_level():
    board = board_from_string(
        """\
.....A
.....A
.rr..A
......
......
......
"""
    )
    moves = solve(board)

    ok_(_check_solves(board, moves))
    eq_(
        moves,
        [
            (Car("A", Direction.vertical, 3), Direction.vertical, 3),
            (Car("r", Direction.horizontal, 2), Direction.horizontal, 3),
        ],
    )
Exemplo n.º 12
0
def test_load_complex_board():
    board_description = """\
....AA
..BBCC
rr..EF
GGHHEF
...IEF
...IJJ
"""

    board = board_from_string(board_description)

    eq_(len(board.cars), 10)
    assert_in(Car('r', Direction.horizontal, 2), board.cars)
    assert_in(Car('A', Direction.horizontal, 2), board.cars)
    assert_in(Car('B', Direction.horizontal, 2), board.cars)
    assert_in(Car('C', Direction.horizontal, 2), board.cars)
    assert_in(Car('E', Direction.vertical, 3), board.cars)
    assert_in(Car('F', Direction.vertical, 3), board.cars)
    assert_in(Car('G', Direction.horizontal, 2), board.cars)
    assert_in(Car('H', Direction.horizontal, 2), board.cars)
    assert_in(Car('I', Direction.vertical, 2), board.cars)
    assert_in(Car('J', Direction.horizontal, 2), board.cars)
Exemplo n.º 13
0

def _sign(v):
    return v / abs(v)


if __name__ == "__main__":
    # Read the board
    if len(sys.argv) == 1:
        board_description = sys.stdin.read()
    else:
        with open(sys.argv[1]) as fp:
            board_description = fp.read()

    # Solve the board
    board = board_from_string(board_description)
    moves = solve(board)

    if moves is None:
        print("Board is not solvable. Sorry.")
    else:
        # Render the board
        for move in moves:
            direction_string = {
                (Direction.horizontal, 1): 'R',
                (Direction.horizontal, -1): 'L',
                (Direction.vertical, 1): 'D',
                (Direction.vertical, -1): 'U',
            }[move[1], _sign(move[2])]
            print("{}{}{}".format(
                move[0].color,
Exemplo n.º 14
0
 def _check(lvl, board_desc, num_moves):
     board = board_from_string(board_desc)
     moves = solve(board)
     ok_(_check_solves(board, moves))
     eq_(len(moves), num_moves)