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

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 175 CC 11 SCAR 18.1
CC
11
Cov
0%
by_name
12.0
crosssection
12.0
crosssection
12.0
CRAP 6.1
1 use strict;
2 use warnings;
3 #use feature qw( say );
4
5 # basic Sudoku structures
6 # don't panic - all basic Sudoku structures are constant
7 package main;
8 our @cells; # cell objects
9 our @rows; # row objects (1 .. 9)
10 our @cols; # col objects (1 .. 9)
11 our @blocks; # block objects (1 .. 9)
12
13 package
14 Games::Sudoku::Trainer::Unit;
15
16 use version; our $VERSION = qv('0.01'); # PBP
17
18 sub new { # constructor for unit objects
19 my $class = shift;
20
21 my $self = {@_};
22
23 # initialize property Cand_cells
24 # index: cand. digit
25 # value: array of active members with this cand.
26 foreach my $cand ( 1 .. 9 ) {
27 $self->{Cand_cells}[$cand] = [ @{ $self->{Members} } ];
28 }
29 return bless( $self, $class );
30 }
31
32 # Standard getters
33 sub get_Index { return $_[0]->{Index} } # getter for Index
34 sub Name { return $_[0]->{Name} } # getter for Name
35
36 # getter for all Members of a unit (returns a copy of the array)
37 #
38 sub get_Members { return @{ $_[0]->{Members} } }
39
40 sub get_Member { # getter for a member
41 my $self = shift;
42 my $idx = shift;
43
44 return $self->{Members}->[$idx];
45 }
46
47 sub active_Members { # return the active members of a unit
48 my $self = shift;
49
50 return grep { not $_->Value } $self->get_Members;
51 }
52
53 # getter for a candidate count
54 # my $candcount = $unit->get_Cand_count($cand_digit);
55 #
56 sub get_Cand_count {
57 my $self = shift;
58 my ($cand_idx) = @_;
59
60 return scalar @{ $self->{Cand_cells}[$cand_idx] };
61 }
62
63 # remove a cell from property Cand_cells of a unit
64 # my $bool = $unit->remove_Cand_cell($cand_digit, $cell);
65 # $bool: false if value is already found (no error)
66 #
67 sub remove_Cand_cell {
68 my $self = shift;
69 my ( $cand_idx, $cell ) = @_;
70
71 my $cells_ref = $self->{Cand_cells}[$cand_idx];
72 return 0 unless @$cells_ref; # already empty (no error)
73
74 use List::MoreUtils qw(firstidx);
75 my $pos_idx = firstidx { $_ == $cell } @$cells_ref;
76 splice( @$cells_ref, $pos_idx, 1 );
77 return 1;
78 }
79
80 # return the unit object for a given name
81 # $unit = Games::Sudoku::Trainer::Unit->by_name($unit_name);
82 #
83 sub by_name {
84 my $self = shift;
85 my $name = shift;
86
87 my ( $unitchar, $unitnum ) = ( $name =~ /^(\w)(\d)$/ );
88 unless ($unitnum) {
89 my @trace = caller(0);
90 die "3\nInvalid unit name '$name' provided"
91 . " by file $trace[1], line $trace[2]\n";
92 }
93
94 =for ignore
95 my ($unitarray_ref) =
96 grep( { lc( substr( ref $_->[$unitnum], 0, 1 ) ) eq $unitchar }
97 ( \@rows, \@cols, \@blocks ) );
98 return $unitarray_ref->[$unitnum];
99
100 =cut
101
102 foreach my $units_refs ( \@rows, \@cols, \@blocks ) {
103 my $unitname = $units_refs->[$unitnum]->Name;
104 #test
105 #$unitname = 'abc';
106 return $units_refs->[$unitnum] if $unitname eq $name;
107 }
108
109 # die "3\nCannot find unit with name '$name'";
110 Games::Sudoku::Trainer::Run->code_err
111 ("Cannot find unit with name '$name'");
112 die;
113 }
114
115 # return the line crossing the given line and passing through the given cell
116 # the cell needs not belong to the line
117 # $cross = $line->crossline($cell);
118 #
119 sub crossline {
120 my $self = shift;
121 my $cell = shift;
122
123 my $unittype = ref $self;
124 $unittype =~ s/.*::(\w+)$/$1/;
125 return $unittype eq 'Row'
126 ? $cols[ $cell->Col_num ]
127 : $rows[ $cell->Row_num ];
128 }
129
130 #====================================================================
131 #package Row;
132 package
133 Games::Sudoku::Trainer::Row;
134
135 use base qw/Games::Sudoku::Trainer::Unit/;
136
137 sub new { # constructor for row objects
138 my $class = shift;
139 my $idx = shift; # row index (1 .. 9)
140 my $cell1 = 9 * $idx - 8;
141 my @args = (
142 'Index', $idx,
143 'Name', "r$idx",
144 'Members',
145 [ @cells[ $cell1 .. $cell1 + 8 ] ], # members are cell objects
146 );
147 my $self = $class->SUPER::new(@args);
148 return $self; # blessed in SUPER::new
149 }
150
151 # Return the cell objects that lie in the cross section of 2 units
152 # @cross_cells = $unit1->crosssection($unit2);
153 #
154 sub crosssection {
155 my ( $self, $unit2 ) = @_;
156
157 my $unittype = ref $unit2;
158 $unittype =~ s/.*::(\w+)$/$1/;
159 return
160 $unittype eq 'Row' ? ()
161 : $unittype eq 'Col'
162 ? ( Games::Sudoku::Trainer::Const_structs::crossRowCol( $self, $unit2 ) )
163 : @{ Games::Sudoku::Trainer::Const_structs::crossRowBlock( $self, $unit2 ) };
164 }
165
166 #====================================================================
167 #package Col;
168 package
169 Games::Sudoku::Trainer::Col;
170
171 use base qw/Games::Sudoku::Trainer::Unit/;
172
173 sub new { # constructor for col objects
174 my $class = shift;
175 my $idx = shift; # col index (1 .. 9)
176
177 my $cell1 = $idx;
178 my $members = [];
179 foreach my $i ( 0 .. 8 ) {
180 push @$members,
181 $cells[ $cell1 + $i * 9 ]; # members are cell objects
182 }
183 my @args = ( 'Index', $idx, 'Name', "c$idx", 'Members', $members, );
184 my $self = $class->SUPER::new(@args);
185 return $self; # blessed in SUPER::new
186 }
187
188 # Return the cell objects that lie in the cross section of 2 units
189 # @cross_cells = $unit1->crosssection($unit2);
190 #
191 sub crosssection {
192 my ( $self, $unit2 ) = @_;
193
194 my $unittype = ref $unit2;
195 $unittype =~ s/.*::(\w+)$/$1/;
196 return
197 $unittype eq 'Row'
198 ? ( Games::Sudoku::Trainer::Const_structs::crossRowCol( $unit2, $self ) )
199 : $unittype eq 'Col' ? ()
200 : @{ Games::Sudoku::Trainer::Const_structs::crossColBlock( $self, $unit2 ) };
201 }
202
203 #====================================================================
204 #package Block;
205 package
206 Games::Sudoku::Trainer::Block;
207
208 use base qw/Games::Sudoku::Trainer::Unit/;
209
210 sub new { # constructor for block objects
211 my $class = shift;
212 my $idx = shift; # block index (1 .. 9)
213
214 my @indx = ( 1 .. 3, 10 .. 12, 19 .. 21 );
215 my $offset;
216 $offset = 3 * ( ( $idx - 1 ) % 3 ) + 27 * int( ( $idx - 1 ) / 3 );
217 $_ += $offset foreach @indx;
218 my @args =
219 ( 'Index', $idx, 'Name', "b$idx", 'Members', [ @cells[@indx] ], );
220 my $self = $class->SUPER::new(@args);
221 return $self; # blessed in SUPER::new
222 }
223
224 # Return the cell objects that lie in the cross section of 2 units
225 # @cross_cells = $unit1->crosssection($unit2);
226 #
227 sub crosssection {
228 my ( $self, $unit2 ) = @_;
229
230 my $unittype = ref $unit2;
231 $unittype =~ s/.*::(\w+)$/$1/;
232 return
233 $unittype eq 'Row'
234 ? @{ Games::Sudoku::Trainer::Const_structs::crossRowBlock( $unit2, $self ) }
235 : $unittype eq 'Col'
236 ? @{ Games::Sudoku::Trainer::Const_structs::crossColBlock( $unit2, $self ) }
237 : ();
238 }
239
240 1;