NAME

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

VERSION

Version 0.13

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

$puzzle =~ s/\s//g;

my $sudoku = Games::Sudoku::CPSearch->new();
die "bad puzzle" unless defined $sudoku->set_puzzle($puzzle);
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

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

Initializes the sudoku solving framework.

$sudoku_object->solve()

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

$sudoku_object->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->new();
$o->set_puzzle($puzzle);
print $o->solve(), "\n";
$o->set_puzzle($another_puzzle);
print $o->solve(), "\n";
$sudoku_object->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.