| 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::GUIpause_restrict; | ||
| 13 | |||
| 14 | use version; our $VERSION = qv('0.01'); # PBP | ||
| 15 | |||
| 16 | use Tk; | ||
| 17 | |||
| 18 | # globals of module Games::Sudoku::Trainer::GUIpause_restrict | ||
| 19 | my $mw; # main window | ||
| 20 | my $display_mode_restrict; # for display of mode restriction | ||
| 21 | |||
| 22 | sub _config_disp_mode_restrict { | ||
| 23 | my $restrict_lb = shift; | ||
| 24 | |||
| 25 | $mw = $restrict_lb->toplevel; | ||
| 26 | $restrict_lb->configure( -textvariable => \$display_mode_restrict ); | ||
| 27 | return; | ||
| 28 | } | ||
| 29 | |||
| 30 | #----------------------------------------------------------------------- | ||
| 31 | # pause mode restriction | ||
| 32 | #----------------------------------------------------------------------- | ||
| 33 | |||
| 34 | { | ||
| 35 | |||
| 36 | # Dialog to ask for a unit name | ||
| 37 | # Used when restricting pause mode 'Value found' to a unit | ||
| 38 | # | ||
| 39 | sub enter_unitname { | ||
| 40 | $mw or _config_disp_mode_restrict(shift); | ||
| 41 | |||
| 42 | my $pause_restrict; # temp. pause mode restriction | ||
| 43 | |||
| 44 | _kill_tracewindow(); | ||
| 45 | require Tk::DialogBox; | ||
| 46 | my $db = $mw->DialogBox( | ||
| 47 | -title => 'Restrict pause mode', | ||
| 48 | -buttons => [qw/Ok Cancel/], | ||
| 49 | -default_button => 'Ok', | ||
| 50 | -cancel_button => 'Cancel' | ||
| 51 | ); | ||
| 52 | $db->Label( -text => "Restrict pause mode 'Value found'" | ||
| 53 | . " to a unit\n(e. g. b5 for the center block)" )->pack(); | ||
| 54 | my $en = $db->add( | ||
| 55 | 'Entry', | ||
| 56 | -width => 2, | ||
| 57 | -textvariable => \$pause_restrict, | ||
| 58 | -validate => 'key' | ||
| 59 | )->pack(); | ||
| 60 | $en->configure( -validatecommand => [ \&_validate_unit, $en ] ); | ||
| 61 | $en->selectionRange( 0, 'end' ); | ||
| 62 | $db->configure( -focus => $en ); | ||
| 63 | my $answer = $db->Show(); | ||
| 64 | |||
| 65 | if ( $answer eq 'Cancel' or $pause_restrict !~ /^(r|c|b)[1-9]$/ ) { | ||
| 66 | Games::Sudoku::Trainer::GUI::set_pause_mode('default'); | ||
| 67 | return; | ||
| 68 | } | ||
| 69 | my $unit = Games::Sudoku::Trainer::Unit->by_name($pause_restrict); | ||
| 70 | my $count = $unit->active_Members; | ||
| 71 | if ( $unit->active_Members <= 1 ) { | ||
| 72 | Games::Sudoku::Trainer::Run::user_err( | ||
| 73 | "All values of $pause_restrict already found"); | ||
| 74 | Games::Sudoku::Trainer::GUI::set_pause_mode('default'); | ||
| 75 | } | ||
| 76 | $display_mode_restrict = $pause_restrict; | ||
| 77 | Games::Sudoku::Trainer::Pause->setMode_restriction($pause_restrict); | ||
| 78 | return; | ||
| 79 | } ## end sub enter_unitname | ||
| 80 | |||
| 81 | sub _validate_unit { | ||
| 82 | my $en = shift; | ||
| 83 | my ( $unit_name, undef, undef, undef, $action ) = @_; | ||
| 84 | |||
| 85 | if ( $action > 1 ) { $action -= 7 } | ||
| 86 | ; # repair a bug in ActivePerl | ||
| 87 | if ( $action != 1 ) { return 1 } | ||
| 88 | ; # wait for action 'insert' | ||
| 89 | if ( $unit_name =~ /^(r|c|b)$/ ) { | ||
| 90 | return 1; # wait for 2nd char | ||
| 91 | } | ||
| 92 | if ( $unit_name !~ /^(r|c|b)[1-9]$/ ) { | ||
| 93 | Games::Sudoku::Trainer::Run::user_err( | ||
| 94 | "Invalid unit name $unit_name," | ||
| 95 | . " format is 'unit type - unit number'", | ||
| 96 | "\n(unit type: 'r' for row, 'c' for column, 'b' for block)" | ||
| 97 | ); | ||
| 98 | $en->focusForce(); | ||
| 99 | return 0; | ||
| 100 | } | ||
| 101 | return 1; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | { | ||
| 106 | |||
| 107 | # Dialog to ask for a cell name | ||
| 108 | # Used for pause mode 'Trace a cell' | ||
| 109 | # and when restricting pause mode 'Value found' to a cell | ||
| 110 | # | ||
| 111 | sub enter_cellname { | ||
| 112 | $mw or _config_disp_mode_restrict(shift); | ||
| 113 | |||
| 114 | my $pause_restrict; # temp. pause mode restriction | ||
| 115 | |||
| 116 | require Tk::DialogBox; | ||
| 117 | my $db = $mw->DialogBox( | ||
| 118 | -title => 'Enter cell name', | ||
| 119 | -buttons => [qw/Ok Cancel/], | ||
| 120 | -default_button => 'Ok', | ||
| 121 | -cancel_button => 'Cancel' | ||
| 122 | ); | ||
| 123 | $db->Label( -text => | ||
| 124 | "Enter the cell name\n(e. g. r1c9 for the upper right cell)" ) | ||
| 125 | ->pack(); | ||
| 126 | my $en = $db->add( | ||
| 127 | 'Entry', | ||
| 128 | -width => 4, | ||
| 129 | -textvariable => \$pause_restrict, | ||
| 130 | -validate => 'key' | ||
| 131 | )->pack(); | ||
| 132 | $en->configure( -validatecommand => [ \&_validate_cell, $en ] ); | ||
| 133 | $en->selectionRange( 0, 'end' ); | ||
| 134 | $db->configure( -focus => $en ); | ||
| 135 | my $answer = $db->Show(); | ||
| 136 | |||
| 137 | $pause_restrict = lc($pause_restrict); | ||
| 138 | if ( $answer eq 'Cancel' or $pause_restrict !~ /^r[1-9]c[1-9]$/ ) { | ||
| 139 | Games::Sudoku::Trainer::GUI::set_pause_mode('default'); | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | my $cell = Games::Sudoku::Trainer::Cell->by_name($pause_restrict); | ||
| 143 | my $candcount = $cell->cands_count; | ||
| 144 | |||
| 145 | # check if we are just inserting this value | ||
| 146 | my $pause_info_ref = Games::Sudoku::Trainer::Pause->Info_ref; | ||
| 147 | if ( $candcount > 0 | ||
| 148 | and defined $pause_info_ref | ||
| 149 | and $pause_info_ref->[1] eq 'insert' | ||
| 150 | and $pause_info_ref->[3]->[1] == $cell ) | ||
| 151 | { | ||
| 152 | $candcount = 0; | ||
| 153 | } | ||
| 154 | if ( $candcount == 0 ) { | ||
| 155 | Games::Sudoku::Trainer::Run::user_err( | ||
| 156 | "Value of $pause_restrict already found"); | ||
| 157 | Games::Sudoku::Trainer::GUI::set_pause_mode('default'); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | $display_mode_restrict = $pause_restrict; | ||
| 161 | Games::Sudoku::Trainer::Pause->setMode_restriction($pause_restrict); | ||
| 162 | return; | ||
| 163 | } ## end sub enter_cellname | ||
| 164 | |||
| 165 | # validate cell name | ||
| 166 | # | ||
| 167 | sub _validate_cell { | ||
| 168 | my $en = shift; | ||
| 169 | my ( $cell_name, undef, undef, undef, $action ) = @_; | ||
| 170 | |||
| 171 | if ( $action > 1 ) { $action -= 7 }; # repair a bug in ActivePerl | ||
| 172 | if ( $action != 1 ) { return 1 }; # wait for action 'insert' | ||
| 173 | if ( $cell_name =~ /^r[1-9]?c?[1-9]?$/i ) { | ||
| 174 | return 1; # (still) ok | ||
| 175 | } else { | ||
| 176 | Games::Sudoku::Trainer::Run::user_err( | ||
| 177 | "Invalid cell name $cell_name, format is\n", | ||
| 178 | '"r" - row number - "c" - column number' | ||
| 179 | ); | ||
| 180 | $en->focusForce(); | ||
| 181 | return 0; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | # Clear current pause mode restriction | ||
| 187 | # | ||
| 188 | sub norestrict_pause { | ||
| 189 | my $menubar = shift; | ||
| 190 | |||
| 191 | my $pause_restriction = | ||
| 192 | $display_mode_restrict; # current pause mode restriction | ||
| 193 | |||
| 194 | $pause_restriction or return; | ||
| 195 | length($pause_restriction) == 4 and _kill_tracewindow(); | ||
| 196 | $pause_restriction or return; | ||
| 197 | |||
| 198 | # now must be a 'Value found' restriction | ||
| 199 | my $pausemode_menu = $menubar->entrycget( 'Pause Mode', -menu ); | ||
| 200 | my $valfound = $pausemode_menu->entrycget( 'Value found', -menu ); | ||
| 201 | my $anywhere = $valfound->entrycget( 'anywhere', -variable ); | ||
| 202 | $$anywhere = 'anywhere'; | ||
| 203 | $display_mode_restrict = ''; | ||
| 204 | Games::Sudoku::Trainer::Pause->setMode_restriction(''); | ||
| 205 | return; | ||
| 206 | } | ||
| 207 | |||
| 208 | #----------------------------------------------------------------------- | ||
| 209 | # view trace window | ||
| 210 | #----------------------------------------------------------------------- | ||
| 211 | |||
| 212 | { | ||
| 213 | |||
| 214 | my $pause_restriction; # current pause mode restriction | ||
| 215 | my $tracewindow; # Toplevel widget | ||
| 216 | my $canv; # Canvas widget of trace window | ||
| 217 | my $tracestatus; # status text | ||
| 218 | |||
| 219 | sub build_tracewindow { | ||
| 220 | my ( $candsize, $cellsize ) = @_; | ||
| 221 | |||
| 222 | $pause_restriction = | ||
| 223 | $display_mode_restrict; # current pause mode restriction | ||
| 224 | |||
| 225 | $pause_restriction or return; | ||
| 226 | |||
| 227 | # if the user selected 'trace a cell' while 'trace a cell' is | ||
| 228 | # already active, the old trace window must be destroyed | ||
| 229 | if ( Exists($tracewindow) ) { | ||
| 230 | $tracewindow->destroy; | ||
| 231 | } | ||
| 232 | $tracewindow = $mw->Toplevel(); | ||
| 233 | $tracewindow->overrideredirect(1); # no window frame | ||
| 234 | # separator | ||
| 235 | $tracewindow->Frame( -height => 1, -bg => 'black' ) | ||
| 236 | ->pack( -pady => 4, -expand => 1, -fill => 'x' ); | ||
| 237 | $tracewindow->Label( -text => "Trace of cell $pause_restriction" ) | ||
| 238 | ->pack( -pady => 4 ); | ||
| 239 | |||
| 240 | $canv = | ||
| 241 | $tracewindow->Canvas( -borderwidth => 2, -relief => 'groove' ) | ||
| 242 | ->pack( -anchor => 'w' ); | ||
| 243 | $tracewindow->Label( -textvariable => \$tracestatus )->pack(); | ||
| 244 | Games::Sudoku::Trainer::GUI::build_candsquares( $canv, | ||
| 245 | Games::Sudoku::Trainer::Cell->by_name($pause_restriction) ); | ||
| 246 | $canv->move( 'new', 2, 2 ); | ||
| 247 | $canv->itemconfigure( | ||
| 248 | 'black', | ||
| 249 | -fill => 'black', | ||
| 250 | -outline => 'black', | ||
| 251 | -state => 'normal' | ||
| 252 | ); | ||
| 253 | $canv->itemconfigure( | ||
| 254 | 'red', | ||
| 255 | -fill => 'red', | ||
| 256 | -outline => 'red', | ||
| 257 | -state => 'normal' | ||
| 258 | ); | ||
| 259 | |||
| 260 | # frame the cand.s squares collection | ||
| 261 | $canv->createRectangle( | ||
| 262 | 0, 0, | ||
| 263 | $cellsize + 1, | ||
| 264 | $cellsize + 1, | ||
| 265 | -tags => 'new' | ||
| 266 | ); | ||
| 267 | $canv->scale( 'new', 0, 0, 1.5, 1.5 ); | ||
| 268 | $canv->dtag('new'); | ||
| 269 | |||
| 270 | # legend | ||
| 271 | my $pointfac = 1 / $canv->fpixels('1p'); | ||
| 272 | my $legendx = $cellsize + 30; | ||
| 273 | my $legendy = 3; | ||
| 274 | my %legend_values = | ||
| 275 | ( qw/black Active red Excluded/, 'orange' => 'Just excluded' ); | ||
| 276 | foreach my $key ( keys %legend_values ) { | ||
| 277 | $canv->createRectangle( | ||
| 278 | $legendx, $legendy, $legendx + $candsize - 1, | ||
| 279 | $legendy + $candsize - 1, | ||
| 280 | -fill => $key, | ||
| 281 | -outline => $key, | ||
| 282 | ); | ||
| 283 | $canv->createText( | ||
| 284 | $legendx + 10, $legendy + $candsize / 2 - 1, | ||
| 285 | -anchor => 'w', | ||
| 286 | -text => $legend_values{$key} | ||
| 287 | ); | ||
| 288 | $legendy += 20 * $pointfac; # linespace 20 points | ||
| 289 | } | ||
| 290 | $canv->configure( -scrollregion => [ $canv->bbox('all') ] ); | ||
| 291 | my ( $x_ul, $y_ul, $x_lr, $y_lr ) = $canv->bbox('all'); | ||
| 292 | $canv->configure( | ||
| 293 | -height => $y_lr - $y_ul - 1, | ||
| 294 | -width => $x_lr - $x_ul - 1 | ||
| 295 | ); | ||
| 296 | $tracewindow->resizable( 0, 1 ); # freeze window width | ||
| 297 | |||
| 298 | # follow the main window when it moves | ||
| 299 | $mw->bind( | ||
| 300 | '<Configure>' => sub { | ||
| 301 | $mw->after( | ||
| 302 | 200 => sub { | ||
| 303 | $tracewindow or return; | ||
| 304 | $tracewindow->state ne 'normal' and return; | ||
| 305 | $tracewindow->withdraw; | ||
| 306 | $tracewindow->Popup( | ||
| 307 | -overanchor => 'sw', | ||
| 308 | -popanchor => 'nw' | ||
| 309 | ); | ||
| 310 | } | ||
| 311 | ); | ||
| 312 | } | ||
| 313 | ); | ||
| 314 | |||
| 315 | # nothing to update directly after presetting | ||
| 316 | # (the pause directly after presetting doesn't set | ||
| 317 | # Games::Sudoku::Trainer::Pause::Info_ref) | ||
| 318 | my $pause_info_ref = Games::Sudoku::Trainer::Pause->Info_ref; | ||
| 319 | $pause_info_ref and update_tracewindow($pause_info_ref); | ||
| 320 | $tracewindow->Popup( | ||
| 321 | -popover => $mw, | ||
| 322 | -overanchor => 'sw', | ||
| 323 | -popanchor => 'nw' | ||
| 324 | ); | ||
| 325 | return; | ||
| 326 | } ## end sub build_tracewindow | ||
| 327 | |||
| 328 | # update the colors of the candidate squares in the trace window, | ||
| 329 | # then pause until the user hits the Run button. | ||
| 330 | # called from Games::Sudoku::Trainer::Check_pause::check_pause | ||
| 331 | # also called from show_tracewindow to initialize the colors (doesn't pause) | ||
| 332 | # | ||
| 333 | sub update_tracewindow { | ||
| 334 | my $found_info_ref = shift; | ||
| 335 | |||
| 336 | $tracestatus = ''; | ||
| 337 | if ( $found_info_ref->[1] eq 'insert' ) { | ||
| 338 | my $cell = $found_info_ref->[3]->[1]; | ||
| 339 | if ( $cell->Name eq $pause_restriction ) { | ||
| 340 | $tracestatus = | ||
| 341 | "Value found for the trace cell\nby strategy " | ||
| 342 | . Games::Sudoku::Trainer::Pause->Strat | ||
| 343 | . ".\nEnd of trace mode"; | ||
| 344 | Games::Sudoku::Trainer::GUI::set_pause_mode('default'); | ||
| 345 | Games::Sudoku::Trainer::Check_pause::pause(); | ||
| 346 | |||
| 347 | # don't kill the tracewindow if the user created a new one | ||
| 348 | # during the pause | ||
| 349 | _kill_tracewindow() | ||
| 350 | if ( Games::Sudoku::Trainer::Pause->Mode ne 'Trace a cell' ); | ||
| 351 | return; | ||
| 352 | } | ||
| 353 | else { | ||
| 354 | my @trace_units = | ||
| 355 | Games::Sudoku::Trainer::Cell->by_name($pause_restriction) | ||
| 356 | ->get_Containers(); | ||
| 357 | my @cell_units = $cell->get_Containers(); | ||
| 358 | |||
| 359 | # the trace cell may be in the same block _and_ in a same line | ||
| 360 | # as the cell with the just found value. In this case a | ||
| 361 | # duplicate pause is avoided in sub _change_color | ||
| 362 | foreach ( 0 .. 2 ) { | ||
| 363 | next if ( $cell_units[$_] != $trace_units[$_] ); | ||
| 364 | _change_color( $found_info_ref->[3]->[0] ); | ||
| 365 | } | ||
| 366 | } | ||
| 367 | } | ||
| 368 | else { # exclude cand. | ||
| 369 | my $exclude_info_ref = $found_info_ref->[3]; | ||
| 370 | |||
| 371 | # get freshly excluded cand.s | ||
| 372 | my @trace_this = map { | ||
| 373 | $_ =~ /(\d+)-(\d)/; | ||
| 374 | my $cellname = $cells[$1]->Name; | ||
| 375 | my $cand = $2; | ||
| 376 | $cellname =~ /$pause_restriction/ ? ($cand) : () | ||
| 377 | } @$exclude_info_ref; | ||
| 378 | _change_color(@trace_this); | ||
| 379 | } | ||
| 380 | return; | ||
| 381 | } ## end sub update_tracewindow | ||
| 382 | |||
| 383 | # change color of freshly excluded cand.s | ||
| 384 | # color goes from black to orange, then after the pause to red | ||
| 385 | # _change_color(freshly-excluded_list); | ||
| 386 | # | ||
| 387 | sub _change_color { | ||
| 388 | |||
| 389 | # digits freshly excluded as cand.s for the trace cell | ||
| 390 | my @trace_this = @_; | ||
| 391 | my $fresh; # count of fresh squares | ||
| 392 | |||
| 393 | foreach my $digit (@trace_this) { | ||
| 394 | my ($square) = | ||
| 395 | $canv->find( withtag => $pause_restriction . "&&d$digit" ); | ||
| 396 | next unless $square; | ||
| 397 | |||
| 398 | # avoid to confuse the user by pausing twice on a square | ||
| 399 | grep ( $_ =~ 'black', $canv->gettags($square) ) or next; | ||
| 400 | $canv->dtag( $square, 'black' ); | ||
| 401 | $canv->addtag( 'fresh', | ||
| 402 | withtag => $pause_restriction . "&&d$digit" ); | ||
| 403 | $fresh++; | ||
| 404 | } | ||
| 405 | $fresh or return; | ||
| 406 | $canv->itemconfigure( | ||
| 407 | 'fresh', | ||
| 408 | -fill => 'orange', | ||
| 409 | -outline => 'orange' | ||
| 410 | ); | ||
| 411 | $tracestatus = | ||
| 412 | "$fresh candidates excluded by strategy " | ||
| 413 | . Games::Sudoku::Trainer::Pause->Strat . ".\n"; | ||
| 414 | |||
| 415 | # if the trace window isn't popped up yet, we are currently in a pause | ||
| 416 | $canv->ismapped or return; | ||
| 417 | Games::Sudoku::Trainer::Check_pause::pause(); | ||
| 418 | if ( !Tk::Exists($canv) ) { | ||
| 419 | return; # user terminated trace mode | ||
| 420 | } | ||
| 421 | $canv->itemconfigure( 'fresh', -fill => 'red', -outline => 'red' ); | ||
| 422 | my @fresh = $canv->find( withtag => 'fresh' ); | ||
| 423 | $canv->addtag( 'red', withtag => 'fresh' ); | ||
| 424 | $canv->dtag('fresh'); | ||
| 425 | $tracewindow->update(); | ||
| 426 | return; | ||
| 427 | } ## end sub _change_color | ||
| 428 | |||
| 429 | # kill the trace window | ||
| 430 | # | ||
| 431 | sub _kill_tracewindow { | ||
| 432 | $tracewindow or return; | ||
| 433 | $tracewindow->destroy; | ||
| 434 | $tracewindow = undef; | ||
| 435 | $display_mode_restrict = ''; | ||
| 436 | Games::Sudoku::Trainer::Pause->setMode_restriction(''); | ||
| 437 | return; | ||
| 438 | } | ||
| 439 | |||
| 440 | } | ||
| 441 | |||
| 442 | 1; |