text.py :  » Game-2D-3D » Python-Sudoku » pythonsudoku-0.13 » pythonsudoku » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » Python Sudoku 
Python Sudoku » pythonsudoku 0.13 » pythonsudoku » text.py
# -*- coding: utf-8 -*-

"""Module for command line interface.

This exports the functions:
  - draw_board -- Draw the board and numbers.
  - create_sudoku -- Create a sudoku.
  - solve_sudoku -- Solve a sudoku.
  - test_difficulty -- Get the difficulty of a sudoku.

Copyright (C) 2005-2008  Xos Otero <xoseotero@users.sourceforge.net>

Modification history:
2005/10/12  Antti Kuntsi
  Better output.

"""

__all__ = ["draw_board", "create_sudoku", "solve_sudoku"]


import sys
import math

from sudoku import Sudoku,difficulty
from board import Board,Value
from config import options


def back(times):
    while times > 0:
        print "\b\b",
        times -= 1

def draw_board(board):
    """Draw the board and numbers.

    Arguments:
    board -- the board

    """
    for j in xrange(board.boardsize):
        for i in xrange(board.boardsize):
            if board.numbers[j][i] != 0:
                if options.getboolean("sudoku", "use_letters"):
                    format = " %1s"
                    text = str(Value(board.numbers[j][i]))
                else:
                    lenght = int(math.log10(board.boardsize)) + 1
                    format = " %%%ds" % lenght
                    text = str(Value(board.numbers[j][i]).integer())
                print format % text,
            else:
                if options.getboolean("sudoku", "use_letters"):
                    text = " _"
                else:
                    lenght = int(math.log10(board.boardsize)) + 1
                    text = " " + "".join(["_" for x in xrange(lenght)])
                print text,
            if (i + 1) % board.cellsize[0] == 0:
                print " ",
        print
        if (j + 1) % board.cellsize[1] == 0 and j != (board.boardsize - 1):
            print

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

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

def test_difficulty(filename):
    """Open a sudoku located in filename and get its the difficulty.

    Arguments:
    filename -- the file name

    """
    print _(u"The difficulty of the sudoku is..."),
    sys.stdout.flush()

    d = difficulty(Board(filename=filename))
    if d:
        print d
    else:
        print "unknown"
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.