예제 #1
0
def steps(sudoku: Sudoku) -> Iterator[Step]:
    _sudoku = Sudoku(*sudoku.cells(), box_size=sudoku.box_size)

    all_techniques: Tuple[Type[Technique], ...] = (
        techniques.LoneSingle,
        techniques.HiddenSingle,
        techniques.NakedPair,
        techniques.NakedTriplet,
        techniques.LockedCandidate,
        techniques.XYWing,
        techniques.UniqueRectangle,
    )

    for step in techniques.BulkPencilMarking(_sudoku):
        _sudoku.update(step.changes)
        yield step

    while not _sudoku.is_solved():
        for technique in all_techniques:
            try:
                step = technique(_sudoku).first()
            except techniques.NotFound:
                continue
            else:
                _sudoku.update(step.changes)
                yield step
                break
        else:
            raise exceptions.Unsolvable
예제 #2
0
def test_random_initial_cells():
    box_size = BoxSize(3, 3)
    cells = generators._random_initial_cells(box_size)
    assert len(cells) == 15

    sudoku = Sudoku(*cells, box_size=box_size)
    assert sudoku.is_valid() is True
    assert sudoku.is_solved() is False
예제 #3
0
def step(sudoku: Sudoku, with_pencil_marking: bool = False) -> Step:
    all_techniques: Tuple[Type[Technique], ...] = (
        techniques.LoneSingle,
        techniques.HiddenSingle,
        techniques.NakedPair,
        techniques.NakedTriplet,
        techniques.LockedCandidate,
        techniques.XYWing,
        techniques.UniqueRectangle,
    )
    if with_pencil_marking:
        all_techniques = (BulkPencilMarking, PencilMarking) + all_techniques

    if sudoku.is_solved():
        raise Solved

    for technique in all_techniques:
        try:
            return technique(sudoku).first()
        except techniques.NotFound:
            continue
    raise exceptions.Unsolvable