예제 #1
0
def solve_tree(request):
    """Solve the current board using the Tree method."""
    with open("electric_slide/data/state_almanac_data.json") as f:
        state_almanac = json.load(f)
    b = Board()
    b.solve(json.loads(request.params["state"]), state_almanac)

    return {"solution": b.previous_states}
예제 #2
0
def test_record_output_types_tree(state, comp):
    """Test output types of record_solution_stats for tree are correct."""
    b = Board()
    state = json.loads(state)
    time, moves = record_solution_stats('test_data.json', b.solve, state, comp,
                                        state_almanac)
    assert type(time) == float
    assert type(moves) == int
예제 #3
0
def generate_unique_states_from_sets(complexity, almanac=set_almanac, size=3):
    """From a state of complexity n, generate all states of complexity n+1."""
    for state in almanac[complexity]:
        board = Board(size)
        board.previous_states = []
        board.state = json.loads(state)
        open_y = 1
        open_x = 1

        for row in board.state:
            if 9 in row:
                open_y += board.state.index(row)
                open_x += row.index(9)

        board.open_cell_coords = (open_x, open_y)

        board._determine_legal_moves(board.open_cell_coords)

        pboard = PracticeBoard(board.state)

        for move in board.legal_moves:
            if complexity > 0:
                if str(pboard.practice_slide(move)) in almanac[complexity - 1]:
                    continue
            almanac[complexity + 1].add(str(pboard.practice_slide(move)))

    return almanac
예제 #4
0
def diversify_comparator():  # pragma: no cover
    """Compare time and moves each algoithm takes to solve the same board."""
    with open("electric_slide/data/state_almanac_data.json") as f:
        state_almanac = json.load(f)

    b = Board()

    for c in range(32):
        print('\nComplexity: {}'.format(c))
        diversify_state_sampling('greedy_data.json', greedy_pure_search,
                                 c, state_almanac)

    for c in range(32):
        print('\nComplexity: {}'.format(c))
        diversify_state_sampling('tree_data.json', b.solve, c,
                                 state_almanac, True)

    for c in range(32):
        print('\nComplexity: {}'.format(c))
        diversify_state_sampling('a_star_data.json', a_star, c, state_almanac)
예제 #5
0
def solved_board():
    """Instantiate a solved board."""
    from electric_slide.scripts.board import Board
    return Board()
예제 #6
0
def test_determine_legal_moves_finds_all_moves(coords, result):
    """Test that the legal moves are correctly determined around any coords."""
    from electric_slide.scripts.board import Board
    b = Board()
    b._determine_legal_moves(coords)
    assert sorted(b.legal_moves) == sorted(result)
예제 #7
0
def test_empty_constructor_makes_board_in_solved_state():
    """Test that the Board constructor creates a solved board."""
    from electric_slide.scripts.board import Board
    b = Board()
    assert b.state == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
예제 #8
0
def sol_board():
    """Create a solved board."""
    from electric_slide.scripts.board import Board
    return Board()