コード例 #1
0
ファイル: text.py プロジェクト: ternus/arcnet
def solve_sudoku(filename, filename_out=None):
    """Open a sudoku located in filename and solve it.

    If filename_out is not None, the solved sudoku is saved.

    Arguments:
    filename -- the file name

    Keyword arguments:
    filename_out -- the output file name (default None)

    """
    board = Board(filename=filename)
    draw_board(board)

    print _(u"Solving sudoku..."),
    sys.stdout.flush()

    # Use all the algos
    sudoku = Sudoku(board, difficulty="hard")
    success = False
    if sudoku.solve():
        print _(u"success!")
        success = True
    else:
        print _(u"can't be solved!")

    draw_board(sudoku.to_board())

    if filename_out:
        sudoku.to_board().save(filename_out)

    return success
コード例 #2
0
ファイル: text.py プロジェクト: ternus/arcnet
def create_sudoku(filename):
    """Create a sudoku with handicap and save it to filename.

    The handicap are the extra numbers given.

    Arguments:
    filename -- the file name

    """
    while True:
        print _(u"Creating sudoku..."),
        sys.stdout.flush()

        sudoku = Sudoku(Board((options.getint("sudoku", "region_width"),
                               options.getint("sudoku", "region_height"))),
                        difficulty=options.get("sudoku", "difficulty"))
        sudoku.create(options.getint("sudoku", "handicap"))

        if options.getboolean("sudoku", "force") and \
           (difficulty(sudoku.to_board()) != options.get("sudoku",
                                                       "difficulty")):
            print _(u"sudoku with wrong difficulty!")
        else:
            sudoku.to_board().save(filename)
            print _(u"success!")
            break

    draw_board(sudoku.to_board())

    return True