The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Games::Sudoku::CPSearch - Solve Sudoku problems quickly.

VERSION

Version 1.00

SYNOPSIS

    use Games::Sudoku::CPSearch;

    my $puzzle = <<PUZZLE;
    4.....8.5
    .3.......
    ...7.....
    .2.....6.
    ....8.4..
    ....1....
    ...6.3.7.
    5..2.....
    1.4......
    PUZZLE

    open FH, ">example.txt";
    print FH $puzzle;
    close FH;

    my $sudoku = Games::Sudoku::CPSearch->new("example.txt");
    print $sudoku->solve(), "\n";

DESCRIPTION

This module solves a Sudoku puzzle using the same constraint propagation technique/algorithm explained on Peter Norvig's website (http://norvig.com/sudoku.html), and implemented there in Python.

METHODS

$o = Games::Sudoku::CPSearch->new()

Initializes the sudoku solving framework.

$o->solve()

Solves the puzzle. Returns the solution as a flat 81 character string.

$o->set_puzzle($puzzle)

Sets the puzzle to be solved. The only parameter is the 81 character string representing the puzzle. The only characters allowed are [0-9\.\-]. Sets the puzzle to be solved. You can then reuse the object:

    my $o = Games::Sudoku::CPSearch->set_puzzle($puzzle);
    print $o->solve(), "\n";
    $o->set_puzzle($another_puzzle);
    print $o->solve(), "\n";
$o->solution()

Returns the solution string, or the empty string if there is no solution.

INTERNAL METHODS

These methods are exposed but are not intended to be used.

$o->_fullgrid()

Returns a hash with squares as keys and "123456789" as each value.

$o->_puzzle()

Returns the object's puzzle as an 81 character string.

$o->_unitlist($square)

Returns an list of sudoku "units": rows, columns, boxes for a given square.

$o->_propagate()

Perform the constraint propagation on the Sudoku grid.

$o->_eliminate($grid, $square, $digit)

Eliminate digit from the square in the grid.

$o->_assign($grid, $square, $digit)

Assign digit to square in grid. Mutually recursive with eliminate().

$o->_rows()

Returns array of row values: A-I

$o->_cols()

Returns array of column values: 1-9

$o->_squares()

Return list of all the squares in a Sudoku grid: A1, A2, ..., A9, B1, ..., I1, ..., I9

$o->_units($square)

Return list of all the units for a given square.

$o->_peers($square)

Return list of all the peers for a given square.

$o->_search()

Perform search for a given grid after constraint propagation.

$o->_cross()

Return "cross product" of 2 arrays.

$o->_verify($solution)

Returns undef if the sudoku solution is not valid. Returns 1 if it is.

AUTHOR

Martin-Louis Bright, <mlbright at gmail.com>

BUGS

Please report any bugs or feature requests to bug-games-sudoku-cpsearch at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Sudoku-CPSearch. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Games::Sudoku::CPSearch

You can also look for information at:

ACKNOWLEDGEMENTS

Peter Norvig, for the explanation/tutorial and python code at http://www.norvig.com/sudoku.html.

COPYRIGHT & LICENSE

Copyright 2008 Martin-Louis Bright, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.