Algorithm-X-DLX

 view release on metacpan or  search on metacpan

examples/sudoku/sudoku.pl  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Std;

use SudokuGenerator;
use SudokuSolver;
use SudokuType;

sub HELP_MESSAGE {
  my $script = $0;
  $script =~ s|^.*/||;
  print<<HELP

Usage: $script -[options|f:format] <input>

Option flags are:
 -h print help
 -l input has 1 puzzle per line
 -v print solution underneath the puzzle, not side by side
 -s print solutions only, not the puzzle

Output format options (-f) are:
  'preserve' (default):  stick to the input format
  'oneline':             all cell values concatenated
  'compact':             cells only, omitting regions
  'default':             vanilla 9x9

HELP
;
  exit(1);
}

my %format_names = (
  'default'  => 'DEFAULT',
  'oneline'  => 'ONELINE',
  'compact'  => 'COMPACT',
  'preserve' => 'PRESERVE'
);

my $opt_format = 'PRESERVE';
my $opt_one_sudoku_per_line = 0;
my $opt_side_by_side = 1;
my $opt_print_solved_only = 0;

my %opts;
getopts('f:hlsv', \%opts)
  or HELP_MESSAGE();

if ($opts{'h'}) {
  HELP_MESSAGE();
}

if ($opts{'l'}) {
  $opt_one_sudoku_per_line = 1;
}

if ($opts{'s'}) {
  $opt_print_solved_only = 1;
}

if ($opts{'v'}) {
  $opt_side_by_side = 0;
}

if ($opts{'f'}) {
  if (exists $format_names{$opts{'f'}}) {
    $opt_format = $format_names{$opts{'f'}};
  } else {
    print "Invalid argument for -f '$opts{'f'}'";
    HELP_MESSAGE();
  }
}

my $generator = SudokuGenerator->new();
my $input = '';
my $first = 1;



( run in 0.723 second using v1.01-cache-2.11-cpan-df04353d9ac )