示例#1
0
def main():
    x = ''
    while x != 'no':
        x = input(
            "\nplease enter a file name. If you wish to create a new file,\nenter the name of the file you would like to create. "
        )
        if not os.path.isfile(x + ".txt"):
            f = open(x + '.txt', 'w')
            board = init.setup_board()
            blank = init.visualise_board(board)
            f.write(blank)
            f.close()
            input(
                "I have created a file called " + x +
                " on the filesytem. \n Please enter your sudoku in this file and press enter."
            )

        f = open(x + ".txt")
        sudoku = f.read()
        print(sudoku)
        input("I am going to use this sudoku, hit enter to continue.")

        board = init.parse_board(sudoku)
        provided = init.provided_points(board)
        rules.run_event_loop(board, provided)
        print()
        print(init.visualise_board(board))
        solved = init.validate_board(board)
        print(init.get_message(solved))
        x = input("\nwould you like me to solve another sudoku?(yes/no) ")
    time.sleep(3)
示例#2
0
def test_find_multiples():
    board = initialise.setup_board()
    slice = initialise.get_col(board, (0, 0))
    slice[0, 4] = {1, 2}
    slice[0, 5] = {1, 2}
    multiples = rules.find_multiples(slice)
    assert len(multiples) == 1
    assert multiples[0] == ({(0, 4), (0, 5)}, {1, 2})
示例#3
0
def test_hidden_actual_numbers_rule():
    board = initialise.setup_board()
    for y in range(8):
        board[(0, y)] = set(range(1, 9))

    hidden = initialise.get_col(board, (0, 8))
    updated = rules.actual_numbers_rule(hidden)
    assert updated == {(0, 8)}
    assert board[(0, 8)] == {9}
示例#4
0
def test_naked_actual_numbers_rule():
    board = initialise.setup_board()
    board[(0, 8)] = {1}
    naked = initialise.get_col(board, (0, 8))
    updated = rules.actual_numbers_rule(naked)
    for y in range(8):
        assert 1 not in board[(0, y)]
        assert (0, y) in updated

    assert 1 in board[(0, 8)]
    assert (0, 8) not in updated
示例#5
0
def test_find_intersection_values():
    board = initialise.setup_board()
    slice = initialise.get_col(board, (0, 0))
    for y in range(3, 9):
        slice[(0, y)] = set(range(1,7))

    slice[(0, 0)] = {1, 2, 8}
    slice[(0, 1)] = {5, 6, 7}
    slice[(0, 2)] = {3, 6, 9}

    intersect = {(0, 0), (0, 1), (0, 2)}
    values = rules.find_intersection_values(intersect, slice)
    assert values == {7, 8, 9}
示例#6
0
def test_naked_multiples_rule():
    board = initialise.setup_board()
    slice = initialise.get_col(board, (0, 0))
    slice[0, 7] = {1, 2}
    slice[0, 8] = {1, 2}
    updated = rules.multiples_rule(slice)
    assert len(updated) == 7
    for y in range(7):
        assert (0, y) in updated
        assert 1 not in slice[(0, y)]
        assert 2 not in slice[(0, y)]
        assert 1 not in board[(0, y)]
        assert 2 not in board[(0, y)]
示例#7
0
def test_intersection_rule():
    board =  initialise.setup_board()
    s1 = initialise.get_sqr(board, (0, 0))
    s2 = initialise.get_col(board, (0, 0))
    intersection = {(0, 0), (0, 1), (0, 2)}
    for point in set(s1.keys()) - intersection:
        s1[point].remove(1)
    for point in set(s2.keys()) - intersection:
        s2[point].remove(2)
    updated = rules.intersection_rule(s1, s2)
    for point in set(s1.keys()) - intersection:
        assert 2 not in s1[point]
    for point in set(s2.keys()) - intersection:
        assert 1 not in s2[point]
    assert updated == (set(s1.keys()) | set(s2.keys())) - intersection
示例#8
0
def test_hidden_multiples_rule():
    board = initialise.setup_board()
    slice = initialise.get_col(board, (0, 0))
    for y in range(7):
        slice[0, y] = set(range(3, 10))
        board[(0, y)] = slice[(0, y)]

    slice[0, 7] = {1, 2, 3}
    slice[0, 8] = {1, 2, 4}

    for y in range(9):
        board[(0, y)] = slice[(0, y)]

    updated = rules.multiples_rule(slice)
    assert updated == {(0, 7), (0, 8)}
    assert slice[(0, 7)] == {1, 2}
    assert slice[(0, 8)] == {1, 2}
    assert board[(0, 7)] == {1, 2}
    assert board[(0, 8)] == {1, 2}
示例#9
0
def test_multiples_sort():
    board = initialise.setup_board()
    slice = initialise.get_col(board, (0, 0))
    slice[0, 4] = {1, 2, 3}
    sorted = rules.multiples_sort(slice)
    assert sorted[0] == (3, {1, 2, 3}, (0, 4))