| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | #use feature qw( say ); | ||
| 4 | |||
| 5 | package | ||
| 6 | Games::Sudoku::Trainer::GUIhist; | ||
| 7 | |||
| 8 | use version; our $VERSION = qv('0.01'); # PBP | ||
| 9 | |||
| 10 | my @history_raw; # raw history of successful strategies | ||
| 11 | |||
| 12 | # add info about latest found to history | ||
| 13 | # called from the main loop of the Sudoku trainer | ||
| 14 | # | ||
| 15 | sub add_history { | ||
| 16 | push( @history_raw, shift ); | ||
| 17 | } | ||
| 18 | |||
| 19 | #----------------------------------------------------------------------- | ||
| 20 | # history dialog | ||
| 21 | #----------------------------------------------------------------------- | ||
| 22 | |||
| 23 | my $mw; # main window | ||
| 24 | |||
| 25 | my $history_db; # DialogBox to show strategy history | ||
| 26 | |||
| 27 | sub _build_history { | ||
| 28 | $mw = shift; | ||
| 29 | |||
| 30 | require Tk::ROText; | ||
| 31 | |||
| 32 | $history_db = $mw->DialogBox(); | ||
| 33 | my $tx = $history_db->add( | ||
| 34 | 'ROText', | ||
| 35 | -width => 46, | ||
| 36 | -wrap => 'none', | ||
| 37 | -relief => 'flat' | ||
| 38 | )->pack( -expand => 1, -fill => 'x' ); | ||
| 39 | |||
| 40 | my $txfont = $tx->cget( -font ); | ||
| 41 | my $font_family = $mw->fontActual( $txfont, '-family' ); | ||
| 42 | $mw->fontCreate( 'histfontname', -family => $font_family, -size => 10 ); | ||
| 43 | $tx->configure( -font => 'histfontname' ); | ||
| 44 | $mw->fontDelete('histfontname'); | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub hist_summary { | ||
| 49 | $history_db or _build_history(shift); | ||
| 50 | |||
| 51 | my %found_counts; # counts of successful strategies | ||
| 52 | my $tx = $history_db->Subwidget('rotext'); | ||
| 53 | $tx->delete( '1.0', 'end' ); | ||
| 54 | |||
| 55 | foreach my $found_info_ref (@history_raw) { | ||
| 56 | my $strategy = $found_info_ref->[0]; | ||
| 57 | $found_counts{$strategy}++; | ||
| 58 | } | ||
| 59 | |||
| 60 | # sort by priority | ||
| 61 | my $prios_ref = _build_stratprios(); | ||
| 62 | foreach my $strat ( | ||
| 63 | sort { $prios_ref->{$a} <=> $prios_ref->{$b} } | ||
| 64 | keys %found_counts | ||
| 65 | ) | ||
| 66 | { | ||
| 67 | my $count = $found_counts{$strat}; | ||
| 68 | $tx->insert( 'end', | ||
| 69 | 'found ' . ( $count < 10 ? ' ' : '' ) . "$count '$strat'\n" ); | ||
| 70 | } | ||
| 71 | |||
| 72 | $history_db->configure( -title => 'History summary' ); | ||
| 73 | $tx->configure( -height => scalar keys %found_counts ); | ||
| 74 | $history_db->Show(); | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | # build the strategies priority hash | ||
| 79 | # | ||
| 80 | sub _build_stratprios { | ||
| 81 | my %stratprios; # key: strategy name, value: strategy priority | ||
| 82 | |||
| 83 | my @strats = Games::Sudoku::Trainer::Priorities->copy_strats(); | ||
| 84 | @stratprios{ 'Full House', @strats } = ( 0 .. @strats ); | ||
| 85 | return \%stratprios; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub hist_overview { | ||
| 89 | my $curr_strat; | ||
| 90 | my $count; | ||
| 91 | my $valcount; | ||
| 92 | my $strategy; | ||
| 93 | |||
| 94 | @history_raw or return; | ||
| 95 | $history_db or _build_history(shift); | ||
| 96 | |||
| 97 | my $tx = $history_db->Subwidget('rotext'); | ||
| 98 | $tx->delete( '1.0', 'end' ); | ||
| 99 | $curr_strat = $history_raw[0]->[0]; | ||
| 100 | my $action = $history_raw[0]->[1]; # 'insert' or 'exclude' | ||
| 101 | foreach my $found_info_ref (@history_raw) { | ||
| 102 | $strategy = $found_info_ref->[0]; | ||
| 103 | if ( $strategy eq $curr_strat ) { | ||
| 104 | $count++; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | if ( $action eq 'insert' ) { | ||
| 108 | $valcount += $count; | ||
| 109 | $tx->insert( 'end', sprintf "Value %2u found %2u '%s'\n", | ||
| 110 | $valcount, $count, $curr_strat ); | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | $tx->insert( 'end', sprintf " found %2u '%s'\n", | ||
| 114 | $count, $curr_strat ); | ||
| 115 | } | ||
| 116 | $curr_strat = $strategy; | ||
| 117 | $count = 1; | ||
| 118 | } | ||
| 119 | $action = $found_info_ref->[1]; # 'insert' or 'exclude' | ||
| 120 | } | ||
| 121 | if ( $action eq 'insert' ) { | ||
| 122 | $valcount += $count; | ||
| 123 | $tx->insert( 'end', sprintf "Value %2u found %2u '%s'\n", | ||
| 124 | $valcount, $count, $curr_strat ); | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | $tx->insert( 'end', sprintf " found %2u '%s'\n", | ||
| 128 | $count, $curr_strat ); | ||
| 129 | } | ||
| 130 | $history_db->configure( -title => 'History overview' ); | ||
| 131 | my $last = $tx->index('end'); | ||
| 132 | $last =~ s/\..*//; # remove character part of position | ||
| 133 | $tx->configure( -height => $last ); | ||
| 134 | $history_db->Show(); | ||
| 135 | return; | ||
| 136 | } ## end sub _hist_overview | ||
| 137 | |||
| 138 | sub hist_details { | ||
| 139 | $mw = shift; | ||
| 140 | require Tk::DialogBox; | ||
| 141 | require Tk::HList; | ||
| 142 | |||
| 143 | my $db = $mw->DialogBox( -title => 'History details' ); | ||
| 144 | my $hl = $db->Scrolled( | ||
| 145 | 'HList', | ||
| 146 | -scrollbars => 'oe', | ||
| 147 | -columns => 5, | ||
| 148 | -header => 1, | ||
| 149 | -takefocus => 0, | ||
| 150 | -height => 30, | ||
| 151 | -width => 60 | ||
| 152 | )->pack( -expand => 1, -fill => 'both' ); | ||
| 153 | my @headers = ( | ||
| 154 | "Value\nnumber", "Strategy", | ||
| 155 | "Clue\nunits", "Clue\ncandidates", | ||
| 156 | "Clue\ncells" | ||
| 157 | ); | ||
| 158 | require Tk::ItemStyle; | ||
| 159 | my $headstyle = $hl->ItemStyle( 'text', -anchor => 'center' ); | ||
| 160 | |||
| 161 | foreach my $col ( 0 .. 4 ) { | ||
| 162 | $hl->columnWidth( $col, '' ); | ||
| 163 | $hl->headerCreate( | ||
| 164 | $col, | ||
| 165 | -text => $headers[$col], | ||
| 166 | -style => $headstyle | ||
| 167 | ); | ||
| 168 | } | ||
| 169 | my $value_count; | ||
| 170 | foreach my $found_info_ref (@history_raw) { | ||
| 171 | my $strategy = $found_info_ref->[0]; | ||
| 172 | my $clues_all_ref = | ||
| 173 | Games::Sudoku::Trainer::GUI::recode_clues( $found_info_ref->[2] ); | ||
| 174 | $hl->add($found_info_ref); | ||
| 175 | $hl->itemCreate( $found_info_ref, 0, | ||
| 176 | -text => $found_info_ref->[1] eq 'insert' ? ++$value_count : '' ); | ||
| 177 | $hl->itemCreate( $found_info_ref, 1, -text => "$strategy " ); | ||
| 178 | $hl->itemCreate( $found_info_ref, 2, -text => $clues_all_ref->{units} ); | ||
| 179 | $hl->itemCreate( $found_info_ref, 3, -text => $clues_all_ref->{cands} ); | ||
| 180 | $hl->itemCreate( $found_info_ref, 4, -text => $clues_all_ref->{cells} ); | ||
| 181 | } | ||
| 182 | $db->Show(); | ||
| 183 | $db->destroy; | ||
| 184 | return; | ||
| 185 | } ## end sub _hist_details | ||
| 186 | |||
| 187 | 1; |