| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | #use feature qw( say ); | ||
| 4 | |||
| 5 | # Define constant Sudoku structures | ||
| 6 | |||
| 7 | # basic Sudoku structures | ||
| 8 | # don't panic - all basic Sudoku structures are constant | ||
| 9 | package main; | ||
| 10 | our @cells; # cell objects (1 .. 81) | ||
| 11 | our @rows; # row objects (1 .. 9) | ||
| 12 | our @cols; # col objects (1 .. 9) | ||
| 13 | our @blocks; # block objects (1 .. 9) | ||
| 14 | our @units; # all unit objects (0 .. 26) rows, columns, and blocks | ||
| 15 | our @lines; # all line objects (0 .. 17) rows and columns | ||
| 16 | |||
| 17 | package | ||
| 18 | Games::Sudoku::Trainer::Const_structs; | ||
| 19 | |||
| 20 | use version; our $VERSION = qv('0.02'); # PBP | ||
| 21 | |||
| 22 | use Games::Sudoku::Trainer::Cell; | ||
| 23 | use Games::Sudoku::Trainer::Unit; | ||
| 24 | |||
| 25 | # globals of package Const_structs | ||
| 26 | # intersection of units | ||
| 27 | my ( $crossRowCol_ref, $crossRowBlock_ref, $crossColBlock_ref ); | ||
| 28 | |||
| 29 | define_objects(); | ||
| 30 | |||
| 31 | push( @units, @rows[ 1 .. 9 ], @cols[ 1 .. 9 ], @blocks[ 1 .. 9 ] ); | ||
| 32 | @lines = @units[ 0 .. 17 ]; | ||
| 33 | |||
| 34 | define_crossRowCol(); | ||
| 35 | define_crossLineBlock(); | ||
| 36 | |||
| 37 | sub define_objects { | ||
| 38 | |||
| 39 | # build cell objects | ||
| 40 | $#cells = 81; #pre-allocate | ||
| 41 | foreach my $idx ( 1 .. $#cells ) { | ||
| 42 | $cells[$idx] = Games::Sudoku::Trainer::Cell->new($idx); | ||
| 43 | } | ||
| 44 | |||
| 45 | # build row objects | ||
| 46 | foreach my $idx ( 1 .. 9 ) { | ||
| 47 | $rows[$idx] = Games::Sudoku::Trainer::Row->new($idx); | ||
| 48 | } | ||
| 49 | |||
| 50 | # build col objects | ||
| 51 | foreach my $idx ( 1 .. 9 ) { | ||
| 52 | $cols[$idx] = Games::Sudoku::Trainer::Col->new($idx); | ||
| 53 | } | ||
| 54 | |||
| 55 | # build block objects | ||
| 56 | foreach my $idx ( 1 .. 9 ) { | ||
| 57 | $blocks[$idx] = Games::Sudoku::Trainer::Block->new($idx); | ||
| 58 | } | ||
| 59 | |||
| 60 | # reset all containers - replace indices by unit objects | ||
| 61 | foreach my $cell (@cells) { | ||
| 62 | next unless defined $cell; # skip index 0 | ||
| 63 | my ( $row, $col, $block ) = $cell->get_Containers; | ||
| 64 | $cell->set_Containers( [ $rows[$row], $cols[$col], $blocks[$block] ] ); | ||
| 65 | } | ||
| 66 | |||
| 67 | # add Siblings property to cell objects | ||
| 68 | define_siblings(); | ||
| 69 | return; | ||
| 70 | } ## end sub define_objects | ||
| 71 | |||
| 72 | sub define_crossRowCol { | ||
| 73 | my @crosscells; | ||
| 74 | |||
| 75 | foreach my $i ( 1 .. 9 ) { # row index | ||
| 76 | foreach my $j ( 1 .. 9 ) { # col index | ||
| 77 | $crosscells[$i][$j] = $rows[$i]->get_Member( $j - 1 ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | $crossRowCol_ref = \@crosscells; | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub define_crossLineBlock { | ||
| 85 | my ( @crossRows, @crossCols ); | ||
| 86 | |||
| 87 | foreach my $j ( 1 .. 9 ) { # block index | ||
| 88 | foreach my $i ( 1 .. 9 ) { # line index | ||
| 89 | $crossRows[$i][$j] = [ () ]; | ||
| 90 | $crossCols[$i][$j] = [ () ]; | ||
| 91 | } | ||
| 92 | my @bmembers = $blocks[$j]->get_Members; | ||
| 93 | my $cell1 = $bmembers[0]; | ||
| 94 | my $row1 = $cell1->Row_num; # row index | ||
| 95 | my $col1 = $cell1->Col_num; # col index | ||
| 96 | foreach my $i ( 0 .. 2 ) { | ||
| 97 | my $row = $row1 + $i; # row index | ||
| 98 | my @rmembers = $rows[$row]->get_Members; | ||
| 99 | $crossRows[$row][$j] = [ @rmembers[ $col1 - 1 .. $col1 - 1 + 2 ] ]; | ||
| 100 | } | ||
| 101 | foreach my $i ( 0 .. 2 ) { | ||
| 102 | my $col = $col1 + $i; # col index | ||
| 103 | my @cmembers = $cols[$col]->get_Members; | ||
| 104 | $crossCols[$col][$j] = [ @cmembers[ $row1 - 1 .. $row1 - 1 + 2 ] ]; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | $crossRowBlock_ref = \@crossRows; | ||
| 108 | $crossColBlock_ref = \@crossCols; | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | #====================================================== | ||
| 113 | ## Getters for access to the 3 constant 2-dim. arrays | ||
| 114 | ## that hold the common cells in the crosssection of 2 units | ||
| 115 | # | ||
| 116 | # return the common cell of a row and a column (as an array) | ||
| 117 | # my ($crosscell) = crossRowCol($row, $col); | ||
| 118 | # | ||
| 119 | sub crossRowCol { | ||
| 120 | return _crossSect( @_, $crossRowCol_ref ); | ||
| 121 | } | ||
| 122 | |||
| 123 | # return the common cells of a row and a block | ||
| 124 | # my @crosscells = crossRowBlock($row, $block); | ||
| 125 | # | ||
| 126 | sub crossRowBlock { | ||
| 127 | return _crossSect( @_, $crossRowBlock_ref ); | ||
| 128 | } | ||
| 129 | |||
| 130 | # return the common cells of a column and a block | ||
| 131 | # my @crosscells = crossColBlock($col, $block); | ||
| 132 | # | ||
| 133 | sub crossColBlock { | ||
| 134 | return _crossSect( @_, $crossColBlock_ref ); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub _crossSect { | ||
| 138 | my ( $unit1, $unit2, $crossSect_ref ) = @_; | ||
| 139 | |||
| 140 | my ( $idx1, $idx2 ); | ||
| 141 | $idx1 = substr( $unit1->Name, 1 ); | ||
| 142 | $idx2 = substr( $unit2->Name, 1 ); | ||
| 143 | return ( $$crossSect_ref[$idx1][$idx2] ); | ||
| 144 | } | ||
| 145 | |||
| 146 | # | ||
| 147 | #====================================================== | ||
| 148 | |||
| 149 | sub define_siblings { | ||
| 150 | my @siblings; | ||
| 151 | use List::MoreUtils qw/uniq/; | ||
| 152 | |||
| 153 | push @siblings, undef; | ||
| 154 | foreach my $cell (@cells) { | ||
| 155 | next unless $cell; | ||
| 156 | my @tmp; | ||
| 157 | foreach my $unit ( $cell->get_Containers ) { | ||
| 158 | push @tmp, $unit->get_Members; | ||
| 159 | } | ||
| 160 | @tmp = uniq @tmp; | ||
| 161 | push @siblings, [ grep $_ != $cell, @tmp ]; # remove $cell | ||
| 162 | } | ||
| 163 | Games::Sudoku::Trainer::Cell->set_Siblings( \@siblings ); | ||
| 164 | return; | ||
| 165 | } | ||
| 166 | |||
| 167 | 1; |