lib/Games/Sudoku/Trainer/Write_puzzle.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 65 CC 3 SCAR 13.9
CC
3
Cov
0%
write_result
6.0
_write_puzzle
6.0
write_initial
2.0
CRAP 4.0
1 use strict;
2 use warnings;
3
4 # basic Sudoku structures
5 # don't panic - all basic Sudoku structures are constant
6 package main;
7 our @cells; # cell objects (1 .. 81)
8
9 #-----------------------------------------------------------------------
10 # IO routines for sudoku puzzles
11 #-----------------------------------------------------------------------
12
13 package
14 Games::Sudoku::Trainer::Write_puzzle;
15
16 use version; our $VERSION = qv('0.01'); # PBP
17
18 # write the current state of the sudoku puzzle
19 # write_result($outfile);
20 #
21 sub write_result {
22 my $outfile = shift;
23
24 # placeholder for unknown digits in sudoku output files
25 my $unknown_digit = '-';
26 my @alldigits = map( {
27 my $value = $_->Value;
28 $value ? $value : $unknown_digit
29 } @cells[ 1 .. 81 ] );
30 _write_puzzle( $outfile, \@alldigits );
31 return;
32 }
33
34 # write the initial state of the sudoku puzzle
35 # write_initial($outfile);
36 #
37 sub write_initial {
38 my $outfile = shift;
39
40 my $gamestring = Games::Sudoku::Trainer::Run::initial_puzzle();
41 $gamestring =~ tr/[1-9]/-/c; # convert all placeholders to '-'
42 _write_puzzle( $outfile, [ split( '', $gamestring ) ] );
43 return;
44 }
45
46 # write a sudoku puzzle
47 # _write_puzzle($outfile, ref_to_chars_array);
48 #
49 sub _write_puzzle {
50 my ( $outfile, $puzzle_chars_ref ) = @_;
51
52 open( my $out, '>', $outfile ) or do {
53 Run::user_err("Cannot open $outfile:\n$!");
54 return;
55 };
56 for ( my $pos = 0 ; $pos < $#$puzzle_chars_ref ; $pos += 9 ) {
57
58 # for better human readability
59 if ( $pos > 0 and $pos % 27 == 0 ) { print $out "\n" }
60 printf $out "%s%s%s %s%s%s %s%s%s\n",
61 ( @$puzzle_chars_ref[ $pos .. $pos + 8 ] );
62 }
63 close($out) or die "9\nCannot close $outfile: $!\n";
64 return;
65 }
66
67 1;