Ejemplo n.º 1
0
def solve(grid):
    """Find the solution to a Sudoku puzzle using search and constraint propagation

    Parameters
    ----------
    grid(string)
        a string representing a sudoku grid.
        
        Ex. '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'

    Returns
    -------
    dict or False
        The dictionary representation of the final sudoku grid or False if no solution exists.
    """
    values = grid2values(grid)
    values = search(values)
    return values
def grid_generate(fraction=0.2, solvable=True):
    """Generate a random sudoku grid

    Parameters
    ----------
    fraction(float) - percentage of squares that are originally filled
    solvable(bool)  - if the output must be a solvable sudoku puzzle

    Returns
    -------
        a string representing a sudoku grid.
        Ex. '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
    """
    output = ''
    for i in range(9 * 9):
        if random() > fraction: output += '.'
        else: output += str(int(random() * 10))

    if solvable and not solve(grid2values(output)):
        return grid_generate(fraction, solvable)
    else:
        return output
import utils
import solution
if __name__=='__main__':
    solution.search(
            utils.grid2values('..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..')
            )
Ejemplo n.º 4
0
        a string representing a sudoku grid.
        
        Ex. '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'

    Returns
    -------
    dict or False
        The dictionary representation of the final sudoku grid or False if no solution exists.
    """
    values = grid2values(grid)
    values = search(values)
    return values


if __name__ == "__main__":
    diag_sudoku_grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
    display(grid2values(diag_sudoku_grid))
    result = solve(diag_sudoku_grid)
    display(result)

    try:
        import PySudoku
        PySudoku.play(grid2values(diag_sudoku_grid), result, history)

    except SystemExit:
        pass
    except:
        print(
            'We could not visualize your board due to a pygame issue. Not a problem! It is not a requirement.'
        )
Ejemplo n.º 5
0
        The dictionary representation of the final sudoku grid or False if no solution exists.
    """
    values = grid2values(grid)
    values = search(values)
    return values


if __name__ == "__main__":
    diag_sudoku_grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
    #g2value=grid2values(diag_sudoku_grid)
    #print(g2value)
    #units = extract_units(square_units,boxes)
    #print(units['A1'])
    #peer=extract_peers(units,boxes)
    #print(peer['A1'])

    display(grid2values(diag_sudoku_grid))
    result = solve(diag_sudoku_grid)
    display(result)

    try:
        import PySudoku
        #PySudoku.play(grid2values(diag_sudoku_grid), result, history)

    except SystemExit:
        pass
    except:
        print(
            'We could not visualize your board due to a pygame issue. Not a problem! It is not a requirement.'
        )
Ejemplo n.º 6
0
        a string representing a sudoku grid.

        Ex. '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'

    Returns
    -------
    dict or False
        The dictionary representation of the final sudoku grid or False if no solution exists.

    """
    values = ut.grid2values(grid)
    values = search(values)
    return values


if __name__ == "__main__":
    diag_sudoku_grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3'
    ut.display(ut.grid2values(diag_sudoku_grid))
    result = solve(diag_sudoku_grid)
    ut.display(result)

    try:
        import PySudoku
        PySudoku.play(ut.grid2values(diag_sudoku_grid), result, ut.history)

    except SystemExit:
        pass

    except:
        print('We could not visualize your board due to a pygame issue. Not a problem! It is not a requirement.')