#!/usr/bin/perl -w use strict; # # Read a board and a pattern, and emit a new pattern # with the board filled in. # my $board_name = ""; my $pattern_name = "blank-sudoku-2-X.svg"; ##my $pattern_name = "test-X.svg"; my $debug = 0; # **************************************************************** # Process arguments while ($_ = shift) { my $arg = $_; if ($arg =~ /^-debug/) { $debug = 1; } else { $board_name = $_; } } # **************************************************************** # Read the board open BD, "<$board_name" or die "Can't open input board $board_name"; my $line_no = 1; my @vals = (); while () { my $line = $_; if ($debug) { print "Read board line '${line}'\n"; } if ($line !~ /^\s*([0-9-])\s*([0-9-])\s*([0-9-])\s*([0-9-])\s*([0-9-])\s*([0-9-])\s*([0-9-])\s*([0-9-])\s*([0-9-])/) { next; } if ($debug) { print "Matched line; read values $1,$2,$3,$4,$5,$6,$7,$8,$9\n"; } push @vals, $1,$2,$3,$4,$5,$6,$7,$8,$9; $line_no++; if ($line_no > 9) { last; } } if (scalar(@vals) < 81) { my $vc = scalar(@vals); die "Didn't find enough values: found $vc\n"; } close BD or die "Error closing input board"; open BLANK, "<$pattern_name" or die "Can't open blank pattern"; $line_no = 0; while () { my $line = $_; if ($line_no < 81 && $line =~ /X/) { my $val = shift @vals; if ($debug) { print "Line ${line_no}, val = $val\n"; } if ($val eq "-" || $val eq "0") { $val = " "; } $line =~ s/X/$val/; $line_no ++; } print $line; } close BLANK or die "Error closing the blank pattern file";