| 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 (1 .. 81) | ||
| 9 | our @rows; # row objects (1 .. 9) | ||
| 10 | |||
| 11 | #package | ||
| 12 | # Games::Sudoku::Trainer::Cell; | ||
| 13 | package Games::Sudoku::Trainer::Cell; | ||
| 14 | |||
| 15 | use version; our $VERSION = qv('0.02'); # PBP | ||
| 16 | |||
| 17 | # constructor for cell objects | ||
| 18 | # | ||
| 19 | sub new { | ||
| 20 | my $class = shift; | ||
| 21 | my $cell_idx = shift; # cell index (1 .. 81) | ||
| 22 | |||
| 23 | my $row = int( ( $cell_idx - 1 ) / 9 ) + 1; | ||
| 24 | my $col = ( $cell_idx - 1 ) % 9 + 1; | ||
| 25 | my $block = int( ( $col - 1 ) / 3 ) + 3 * int( ( $row - 1 ) / 3 ) + 1; | ||
| 26 | |||
| 27 | # Property Siblings is not in this list. Its construction must be | ||
| 28 | # delayed (in module Const_structs) until all cells are created | ||
| 29 | my @props = ( # cell properties | ||
| 30 | 'Name', "r${row}c$col", | ||
| 31 | 'Cell_num', $cell_idx, | ||
| 32 | 'Row_num', $row, | ||
| 33 | 'Col_num', $col, | ||
| 34 | 'Block_num', $block, | ||
| 35 | 'Value', 0, # init. Value | ||
| 36 | 'Candidates', '123456789', # init. cand.s string (all digits) | ||
| 37 | # placeholders for the unit objects containing this cell | ||
| 38 | # The objects are inserted by sub set_Containers | ||
| 39 | # after all Unit objects are created | ||
| 40 | 'Containers', [ $row, $col, $block ] | ||
| 41 | ); | ||
| 42 | my $self = {@props}; | ||
| 43 | return bless $self, $class; | ||
| 44 | } | ||
| 45 | |||
| 46 | # Standard getters | ||
| 47 | # | ||
| 48 | sub Name { return $_[0]->{Name} } | ||
| 49 | sub Cell_num { return $_[0]->{Cell_num} } | ||
| 50 | sub Row_num { return $_[0]->{Row_num} } | ||
| 51 | sub Col_num { return $_[0]->{Col_num} } | ||
| 52 | sub Block_num { return $_[0]->{Block_num} } | ||
| 53 | sub Value { return $_[0]->{Value} } | ||
| 54 | sub Candidates { return $_[0]->{Candidates} } | ||
| 55 | |||
| 56 | # Getter for arrays (return a copy of the array) | ||
| 57 | sub get_Containers { return @{ $_[0]->{Containers} } } | ||
| 58 | |||
| 59 | # setter for Containers (called from Const_structs::define_objects) | ||
| 60 | # | ||
| 61 | sub set_Containers { | ||
| 62 | my $self = shift; | ||
| 63 | my ($new_val) = @_; | ||
| 64 | |||
| 65 | $self->{Containers} = $new_val; | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | # setter for Siblings property | ||
| 70 | # called from Const_structs::define_siblings | ||
| 71 | # | ||
| 72 | sub set_Siblings { | ||
| 73 | my $class = shift; | ||
| 74 | my $sibl_ref = shift; | ||
| 75 | |||
| 76 | foreach my $i ( 1 .. 81 ) { | ||
| 77 | $cells[$i]->{Siblings} = $sibl_ref->[$i]; | ||
| 78 | } | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | # return ref to the active siblings (siblings with candidates | ||
| 83 | # - value not yet found) of this cell | ||
| 84 | # $sibl_ref = $cell->get_Siblings_ref; | ||
| 85 | # | ||
| 86 | sub get_Siblings_ref { # getter for Siblings | ||
| 87 | my $self = shift; | ||
| 88 | |||
| 89 | return [ grep { $_->Candidates } @{ $self->{Siblings} } ]; | ||
| 90 | } | ||
| 91 | |||
| 92 | # return all common active siblings of 2 cells | ||
| 93 | # my @com_sibs = common_sibs->($cell1, $cell2); | ||
| 94 | # | ||
| 95 | sub common_sibs { | ||
| 96 | my $class = shift; | ||
| 97 | my ( $cell1, $cell2 ) = @_; | ||
| 98 | |||
| 99 | return map( { | ||
| 100 | my $sib2 = $_; | ||
| 101 | grep ( { $_ == $sib2 } @{ $cell1->get_Siblings_ref } ) | ||
| 102 | ? $_ | ||
| 103 | : () | ||
| 104 | } @{ $cell2->get_Siblings_ref } ); | ||
| 105 | } | ||
| 106 | |||
| 107 | # Insert passed digit as value for this cell | ||
| 108 | # Exclude all cands of this cell | ||
| 109 | # Exclude this digit as cand in all siblings | ||
| 110 | # $cell->insert_digit($digit); | ||
| 111 | # | ||
| 112 | sub insert_digit { | ||
| 113 | my $self = shift; | ||
| 114 | my $digit = shift; | ||
| 115 | |||
| 116 | $digit | ||
| 117 | or die "3\nInvalid digit ", defined $digit ? $digit : '', " passed\n "; | ||
| 118 | my $val = $self->Value; | ||
| 119 | return if ( $val == $digit ); # found already | ||
| 120 | # '\n ' at end of die message starts the location info on a new line | ||
| 121 | $val > 0 | ||
| 122 | and die "3\nDigit $digit not allowed in cell ", $self->Name, | ||
| 123 | ",\n has already value $val\n "; | ||
| 124 | $self->has_candidate($digit) or Games::Sudoku::Trainer::Pause->Mode eq 'in_preset' | ||
| 125 | ## error in preset values will be caught during verify | ||
| 126 | or | ||
| 127 | die( "3\nDigit $digit not allowed in empty cell ", $self->Name, "\n " ); | ||
| 128 | $self->_set_Value($digit); | ||
| 129 | foreach my $digt ( 1 .. 9 ) { | ||
| 130 | my $ok = $self->exclude_candidate($digt); | ||
| 131 | } | ||
| 132 | $self->_exclude_candidates($digit); | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | # exclude a candidate for a cell | ||
| 137 | # $cell->exclude_candidate($cand_digit); | ||
| 138 | # | ||
| 139 | sub exclude_candidate { | ||
| 140 | my $self = shift; | ||
| 141 | my ($digt) = @_; | ||
| 142 | my $ok = $self->{Candidates} =~ s/$digt//; | ||
| 143 | $ok or return 0; # already done (no error) | ||
| 144 | |||
| 145 | # adjust the cand. count in all containers | ||
| 146 | foreach my $sibl_unit ( $self->get_Containers ) { | ||
| 147 | $sibl_unit->remove_Cand_cell( $digt, $self ); | ||
| 148 | } | ||
| 149 | # change color on the board | ||
| 150 | Games::Sudoku::Trainer::GUI::exclude_cand( $self, $digt ); | ||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 154 | # Count the candidates of this cell | ||
| 155 | # $cands_count = $cell->cands_count; | ||
| 156 | # | ||
| 157 | sub cands_count { | ||
| 158 | my $self = shift; | ||
| 159 | |||
| 160 | return length( $self->Candidates ); | ||
| 161 | } | ||
| 162 | |||
| 163 | # check whether this cell has this digit as cand. | ||
| 164 | # bool = $cell->has_candidate($cand_digit); | ||
| 165 | # | ||
| 166 | sub has_candidate { | ||
| 167 | my $self = shift; | ||
| 168 | my $cand_digit = shift; | ||
| 169 | |||
| 170 | return $self->Candidates =~ /$cand_digit/; | ||
| 171 | } | ||
| 172 | |||
| 173 | # return the cell object for a given name | ||
| 174 | # $cell = Games::Sudoku::Trainer::Cell->by_name($cell_name); | ||
| 175 | # | ||
| 176 | sub by_name { | ||
| 177 | my $self = shift; | ||
| 178 | my $name = shift; | ||
| 179 | |||
| 180 | unless ( defined $name ) { | ||
| 181 | my @trace = caller(0); | ||
| 182 | Games::Sudoku::Trainer::Run::code_err( | ||
| 183 | "Undefined cell name provided\nby file $trace[1], line $trace[2]\n" | ||
| 184 | ); | ||
| 185 | Games::Sudoku::Trainer::GUI::quit(); | ||
| 186 | } | ||
| 187 | my ( $rownum, $colnum ) = ( $name =~ /^r(\d)c(\d)$/ ); | ||
| 188 | unless ($colnum) { | ||
| 189 | my @trace = caller(0); | ||
| 190 | Games::Sudoku::Trainer::Run::code_err( | ||
| 191 | "Invalid cell row or column number provided" | ||
| 192 | . "\nby file $trace[1], line $trace[2]\n" | ||
| 193 | ); | ||
| 194 | Games::Sudoku::Trainer::GUI::quit(); | ||
| 195 | } | ||
| 196 | return Games::Sudoku::Trainer::Cell->by_pos( $rownum, $colnum ); | ||
| 197 | } | ||
| 198 | |||
| 199 | # return the cell object for a given row/column position | ||
| 200 | # $cell = Games::Sudoku::Trainer::Cell->by_pos($rownum, $colnum); | ||
| 201 | # | ||
| 202 | sub by_pos { | ||
| 203 | ( undef, my $rownum, my $colnum ) = @_; | ||
| 204 | |||
| 205 | $rownum or $colnum or do { | ||
| 206 | |||
| 207 | # direct call of code_err avoids the silly error window of Perl/Tk | ||
| 208 | my @trace = caller(0); | ||
| 209 | Games::Sudoku::Trainer::Run::code_err( | ||
| 210 | "Invalid cell row or column number provided" | ||
| 211 | . "\nby file $trace[1], line $trace[2]\n" | ||
| 212 | ); | ||
| 213 | Games::Sudoku::Trainer::GUI::quit(); | ||
| 214 | }; | ||
| 215 | return $cells[ 9 * ( $rownum - 1 ) + $colnum ]; | ||
| 216 | } | ||
| 217 | |||
| 218 | #-------------------------------------- | ||
| 219 | # Private subs below this line - do not call from outside | ||
| 220 | #-------------------------------------- | ||
| 221 | |||
| 222 | sub _set_Value { # setter for Value | ||
| 223 | my $self = shift; | ||
| 224 | my ($new_val) = @_; | ||
| 225 | |||
| 226 | $self->{Value} = $new_val; | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | |||
| 230 | # Exclude all candidates in this cell (it just got a value) | ||
| 231 | # Exclude the new value from the candidate list in all sibling cells | ||
| 232 | # $cell->_exclude_candidates($digit); | ||
| 233 | # | ||
| 234 | sub _exclude_candidates { | ||
| 235 | my $self = shift; | ||
| 236 | my $digit = shift; | ||
| 237 | |||
| 238 | foreach my $unit ( $self->get_Containers ) { | ||
| 239 | |||
| 240 | # all digits are no longer candidates in this unit | ||
| 241 | foreach my $digt ( 1 .. 9 ) { | ||
| 242 | $unit->remove_Cand_cell( $digt, $self ) | ||
| 243 | if $self->has_candidate($digt); | ||
| 244 | ##?? ohne if?? | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | my $sibl_ref = $self->get_Siblings_ref; | ||
| 249 | foreach my $sibl_cell (@$sibl_ref) { | ||
| 250 | next unless $sibl_cell->has_candidate($digit); # is no cand. cell | ||
| 251 | $sibl_cell->exclude_candidate($digit); | ||
| 252 | $sibl_cell->Candidates | ||
| 253 | |||
| 254 | # error in preset values will be caught during verify | ||
| 255 | or Games::Sudoku::Trainer::Pause->Mode eq 'in_preset' | ||
| 256 | or die( | ||
| 257 | "1\nInsertion of digit $digit in cell ", | ||
| 258 | $self->Name, "\ncaused exclusion of the last candidate in cell ", | ||
| 259 | $sibl_cell->Name, "\n" | ||
| 260 | ); | ||
| 261 | } | ||
| 262 | return; | ||
| 263 | } | ||
| 264 | |||
| 265 | 1; |