NAME

Games::Sudoku::Component - provides APIs for Sudoku solver/generator

SYNOPSIS

use Games::Sudoku::Component;

# Let's create a Sudoku object first.

my $sudoku = Games::Sudoku::Component->new(
  size => 9,
);

# Then, generate a new Sudoku puzzle. This may take minutes.

$sudoku->generate(
  blanks => 50,
);

# Or, you can load puzzle data from text file.

$sudoku->load(
  filename => 'puzzle.txt',
);

# Let's see if the puzzle is created successfully.

print $sudoku->as_string(
  separator => ' ',
  linebreak => "\n",
);

# Then solve the puzzle. This may take minutes, too.
# Solver may fail sometimes, especially the puzzle is large,
# but it automatically tries another solution(s) if possible.

$sudoku->solve;

# Check the result.

print $sudoku->is_solved ? 'solved' : 'gave up';

# You can output the result as an HTML table, too.

print $sudoku->as_HTML;

DESCRIPTION

This is yet another Sudoku (Numberplace) solver/generator. Games::Sudoku::Component provides common (but rather limited) methods to make it easy to play Sudoku -- just for example.

Actually, this module set is written to provide 'controller' APIs to other applications. You can easily integrate this with CGI or Perl/Tk application. See appropriate PODs for details.

METHODS

new (hash or hashref)

Creates an object. Options are:

size

Specifies the size of a puzzle board (table). The default is 9. Actually this value is assumed to be a square of another integer.

block_width
block_height

Specify the width/height of internal blocks, respectively. (block_width x block_height = size)

generate (hash or hashref)

Generates a puzzle. Options are:

blanks

Specifies how many blanks are there in the puzzle. The default is (size x size x 0.75).

load (string or hash or hashref)

Loads and parses puzzle data from file or string. If there is only one argument, it is assumed to be raw puzzle data.

    $sudoku->load(<<'EOT');
    4 . . . . 1
    2 1 . . 5 .
    3 5 1 2 6 .
    1 . . . 3 .
    6 . . 5 1 2
    5 . . . 4 6
    EOT

If the argument seems to be a hash, data will be loaded from $hash{filename} (or $hash{file}, for short).

solve

Solves the puzzle that you generated or loaded. You can solve a 'blank' puzzle. In fact, that is how it generates a new puzzle.

is_solved

Returns true if the puzzle is solved.

clear

Clears the generated or loaded puzzle.

as_string (hash or hashref)

Returns the stringified puzzle. Options are:

separator

Specifies a separator between table columns. The default is a whitespace (' ').

linebreak

Specifies a separator between table rows. The default is a line break ("\n").

as_HTML

Almost same as above but returns an HTML table. Options are:

border

Specifies a size of the table borders. The default is 1.

linebreak

Specifies a separator between table tags. The default is a line break ("\n").

color_by_block
color_by_cell

If set true, each cell has an 'even' or 'odd' class attribute. If your prepare an appropriate CSS, the table will be two-toned.

SEE ALSO

There are many Sudoku implementations around there. I haven't seen them all yet, but the POD of Games::Sudoku::General is a good starting point.

As for the details of Games::Sudoku::Component modules, see:

Games::Sudoku::Component::Base
Games::Sudoku::Component::Controller
Games::Sudoku::Component::Controller::History
Games::Sudoku::Component::Controller::Loader
Games::Sudoku::Component::Controller::Status
Games::Sudoku::Component::Result
Games::Sudoku::Component::Table
Games::Sudoku::Component::Table::Cell
Games::Sudoku::Component::Table::Item
Games::Sudoku::Component::Table::Permission

AUTHOR

Kenichi Ishigaki, <ishigaki@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2006 by Kenichi Ishigaki

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