| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | #use feature qw( say ); | ||
| 4 | |||
| 5 | #----------------------------------------------------------------------- | ||
| 6 | # strategy details dialog | ||
| 7 | #----------------------------------------------------------------------- | ||
| 8 | |||
| 9 | package | ||
| 10 | Games::Sudoku::Trainer::GUIdetails; | ||
| 11 | |||
| 12 | use version; our $VERSION = qv('0.03'); # PBP | ||
| 13 | |||
| 14 | my $details_bt; # the Show details Button | ||
| 15 | my $details_db; # DialogBox to show strategy details | ||
| 16 | my @clues_objs; | ||
| 17 | |||
| 18 | # global constants | ||
| 19 | my @all_types = qw/units cands cells/; # all clue type names | ||
| 20 | |||
| 21 | # construct the strategy details dialog | ||
| 22 | # build_strat_details(\$details_bt); | ||
| 23 | # | ||
| 24 | sub build_strat_details { | ||
| 25 | $details_bt = ${ shift() }; # dereference par. | ||
| 26 | require Tk::DialogBox; | ||
| 27 | |||
| 28 | my %detail_typenames; | ||
| 29 | @detail_typenames{@all_types} = qw/Units Candidates Cells/; | ||
| 30 | $details_db = $details_bt->DialogBox( | ||
| 31 | -title => 'Strategy details', | ||
| 32 | -showcommand => \&_fill_details, | ||
| 33 | ); | ||
| 34 | $details_db->Label( -text => 'Success details of this strategy' )->pack(); | ||
| 35 | $details_db->Label( -text => "Uncover as few details as possible\n" | ||
| 36 | . "to detect this strategy by yourself\n" )->pack(); | ||
| 37 | my $fr = | ||
| 38 | $details_db->add( 'Frame', -borderwidth => 1, -relief => 'solid' ) | ||
| 39 | ->pack(); | ||
| 40 | |||
| 41 | foreach my $row ( 0 .. $#all_types ) { | ||
| 42 | my $type = $all_types[$row]; | ||
| 43 | my $show_bt; # the 'Show' Button for this type | ||
| 44 | my $count; # click count of this 'Show' Button | ||
| 45 | my $uncovered; # the textvar holding the names of uncovered clues | ||
| 46 | $show_bt = $fr->Button( | ||
| 47 | -text => 'Show', | ||
| 48 | -command => [ \&_show_more, \$clues_objs[$row] ] | ||
| 49 | ); | ||
| 50 | $fr->Label( -text => $detail_typenames{$type} )->grid( | ||
| 51 | $fr->Label( | ||
| 52 | -textvariable => \$uncovered, | ||
| 53 | -background => 'yellow' | ||
| 54 | ), | ||
| 55 | $show_bt, | ||
| 56 | -sticky => 'w', | ||
| 57 | ); | ||
| 58 | $clues_objs[$row] = Games::Sudoku::Trainer::Clues->new( | ||
| 59 | Show_btn => $show_bt, Uncov_clues => \$uncovered | ||
| 60 | ); | ||
| 61 | } | ||
| 62 | $details_db->resizable( 0, 0 ); # freeze window size | ||
| 63 | $details_db->Show(); | ||
| 64 | return; | ||
| 65 | } ## end sub build_strat_details | ||
| 66 | |||
| 67 | # prepare the details dialog for display of the clues of the currrent strategy | ||
| 68 | # Callback of the Details dialog | ||
| 69 | # | ||
| 70 | sub _fill_details { | ||
| 71 | my $clues_all_ref = | ||
| 72 | Games::Sudoku::Trainer::GUI::recode_clues( | ||
| 73 | Games::Sudoku::Trainer::Pause->Info_ref->[2] ); | ||
| 74 | my %clues_all = %$clues_all_ref; | ||
| 75 | foreach my $row ( 0 .. $#all_types ) { | ||
| 76 | my $type = $all_types[$row]; | ||
| 77 | my $clues_obj = $clues_objs[$row]; | ||
| 78 | my $show_btn = $clues_obj->Show_btn; | ||
| 79 | my $clues_all; # all avail. clues for this clue type | ||
| 80 | if ( exists( $clues_all{$type} ) ) { | ||
| 81 | $clues_all = $clues_all{$type}; | ||
| 82 | $show_btn->configure( -text => 'Show', -state => 'active' ); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | $clues_all = ''; | ||
| 86 | $show_btn->configure( -text => 'Show', -state => 'disabled' ); | ||
| 87 | } | ||
| 88 | $clues_obj->set_Clues_all($clues_all); | ||
| 89 | $clues_obj->set_Uncov_clues(''); # clear uncovered text in col 1 | ||
| 90 | $clues_obj->clear_Uncov_count; | ||
| 91 | } | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | # uncover one more clue of this type | ||
| 96 | # callback of the corresponding "Show more" button | ||
| 97 | # _show_more($clues_obj_ref); | ||
| 98 | # | ||
| 99 | sub _show_more { | ||
| 100 | my $clues_obj = ${ shift() }; # dereference par. | ||
| 101 | |||
| 102 | # my $clues_obj = ${shift}; # Ambiguous use of ${shift} resolved to $shift | ||
| 103 | # my $clues_obj = $&{shift}; # ($clues_obj is undef) | ||
| 104 | # my $clues_obj = ${&shift}; # Undefined subroutine &GUI::shift called | ||
| 105 | # my $clues_obj = ${&CORE::shift}; # Undefined sub &CORE::shift called | ||
| 106 | # my $clues_obj = ${&Run::shift}; # Undefined sub &Run::shift called | ||
| 107 | my $count = $clues_obj->incr_Uncov_count; | ||
| 108 | my $all = $clues_obj->Clues_all; | ||
| 109 | $all =~ /^((\w+,?){$count})/; | ||
| 110 | my $uncovered = $1 || $all; | ||
| 111 | my $btn = $clues_obj->Show_btn; | ||
| 112 | if ( $uncovered eq $all ) { | ||
| 113 | $btn->configure( -state => 'disabled' ); | ||
| 114 | } | ||
| 115 | elsif ( $btn->cget( -text ) eq 'Show' ) { | ||
| 116 | $btn->configure( -text => 'Show more' ); | ||
| 117 | } | ||
| 118 | $uncovered =~ s/,$//; # some cosmetics | ||
| 119 | $uncovered =~ s/,/, /g; | ||
| 120 | |||
| 121 | # insert uncovered text in col 1 | ||
| 122 | $clues_obj->set_Uncov_clues($uncovered); | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | # Private class for module Games::Sudoku::Trainer::GUIdetails. | ||
| 127 | # Each Clues object holds the info for 1 row in the strategy details dialog, | ||
| 128 | # i.e. for 1 of the 3 clues types: units, cands, cells | ||
| 129 | # | ||
| 130 | package | ||
| 131 | Games::Sudoku::Trainer::Clues; | ||
| 132 | |||
| 133 | sub new { # constructor for Clues objects | ||
| 134 | my $class = shift; | ||
| 135 | my $self = {@_}; | ||
| 136 | return bless $self, $class; | ||
| 137 | } | ||
| 138 | |||
| 139 | # Standard getters | ||
| 140 | sub Clues_all { return $_[0]->{Clues_all} } | ||
| 141 | sub Show_btn { return $_[0]->{Show_btn} } | ||
| 142 | |||
| 143 | # set the Clues_all property of this clues type | ||
| 144 | sub set_Clues_all { | ||
| 145 | my $self = shift; | ||
| 146 | my ($new_val) = @_; | ||
| 147 | $self->{Clues_all} = $new_val; | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | # set the uncovered clues of this clues type | ||
| 152 | sub set_Uncov_clues { | ||
| 153 | my $self = shift; | ||
| 154 | my ($new_text) = @_; | ||
| 155 | ${ $self->{Uncov_clues} } = $new_text; | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | # clear the count of uncovered clues of this clues type | ||
| 160 | sub clear_Uncov_count { | ||
| 161 | my $self = shift; | ||
| 162 | $self->{Uncov_count} = 0; | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | # increment the count of uncovered clues of this clues type | ||
| 167 | # returns the new value | ||
| 168 | sub incr_Uncov_count { | ||
| 169 | my $self = shift; | ||
| 170 | return ++$self->{Uncov_count}; | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; |