| 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 | # To reduce the size of this file, the code of some dialogs has been moved | ||
| 12 | # to subordinate modules. These are loaded at their first usage, so this | ||
| 13 | # will hopefully speed up a bit the startup time of the application. | ||
| 14 | |||
| 15 | package | ||
| 16 | Games::Sudoku::Trainer::GUI; | ||
| 17 | |||
| 18 | use version; our $VERSION = qv('0.02'); # PBP | ||
| 19 | |||
| 20 | use Tk; | ||
| 21 | use Tk::ErrorDialog; | ||
| 22 | use File::Basename; | ||
| 23 | use Encode; | ||
| 24 | |||
| 25 | # globals of package GUI | ||
| 26 | |||
| 27 | # global widgets | ||
| 28 | my $mw; # main window | ||
| 29 | my $run_bt; # Run-button widget | ||
| 30 | my $details_bt; # the Show details Button | ||
| 31 | my $cv; # Canvas widget as sudoku board | ||
| 32 | my $restrict_lb; # Label to display pause mode restriction | ||
| 33 | my $file_en; # Entry widget for display of file name | ||
| 34 | my $menubar; # Menu widget for menu bar | ||
| 35 | |||
| 36 | # others | ||
| 37 | my $pause_mode; # current pause mode | ||
| 38 | my $pause_strat; # strategy that caused this pause | ||
| 39 | my $initfile; # input file of initial puzzle | ||
| 40 | my $values_count = 0; # count of found values | ||
| 41 | my $status; # text in status bar | ||
| 42 | my $preset_count; # preset counter | ||
| 43 | my $noop_sub = sub { }; | ||
| 44 | |||
| 45 | # global constants | ||
| 46 | my $candsize = 7; # size of a cand. square (pixels) | ||
| 47 | my $cellsize = 3 * ( $candsize + 3 ) - 1; # size of a sudoku cell | ||
| 48 | my @all_types = qw/units cands cells/; # all clue type names | ||
| 49 | |||
| 50 | $mw = MainWindow->new(-title => 'SudokuTrainer'); | ||
| 51 | $mw->withdraw; | ||
| 52 | _build_GUI(); | ||
| 53 | $details_bt->bind( '<Visibility>' | ||
| 54 | => sub { Games::Sudoku::Trainer::Run::initialize_and_start() } ); | ||
| 55 | $mw->deiconify; | ||
| 56 | |||
| 57 | set_pause_mode('default'); | ||
| 58 | |||
| 59 | sub _build_GUI { | ||
| 60 | _build_menubar(); | ||
| 61 | |||
| 62 | # pause bar | ||
| 63 | my $run_fr = $mw->Frame()->pack( -anchor => 'w' ); | ||
| 64 | $run_fr->Label( -text => 'Current pause mode: ' )->pack( -side => 'left' ); | ||
| 65 | $run_fr->Label( | ||
| 66 | -width => 20, | ||
| 67 | -background => 'white', | ||
| 68 | -textvariable => \$pause_mode, | ||
| 69 | -anchor => 'w', | ||
| 70 | )->pack( -side => 'left' ); | ||
| 71 | $run_fr->Label( -text => ' in' )->pack( -side => 'left' ); | ||
| 72 | $restrict_lb = $run_fr->Label( | ||
| 73 | -width => 4, | ||
| 74 | -background => 'white', | ||
| 75 | )->pack( -side => 'left', -padx => 4 ); | ||
| 76 | $run_bt = $run_fr->Button( | ||
| 77 | -text => 'Run', | ||
| 78 | -command => | ||
| 79 | sub { set_status(''); Games::Sudoku::Trainer::Check_pause::endpause() }, | ||
| 80 | -state => 'disabled', | ||
| 81 | )->pack( -side => 'right' ); | ||
| 82 | |||
| 83 | # current strategy bar | ||
| 84 | my $strat_fr = $mw->Frame()->pack( -anchor => 'w', -fill => 'x' ); | ||
| 85 | $strat_fr->Label( -text => 'Successful strategy: ' ) | ||
| 86 | ->pack( -side => 'left' ); | ||
| 87 | $strat_fr->Label( -textvariable => \$pause_strat )->pack( -side => 'left' ); | ||
| 88 | $details_bt = $strat_fr->Button( | ||
| 89 | -text => 'Show details', | ||
| 90 | -command => sub { | ||
| 91 | require Games::Sudoku::Trainer::GUIdetails; | ||
| 92 | # my $btnref = \$details_bt; | ||
| 93 | # Games::Sudoku::Trainer::GUIdetails::build_strat_details($btnref); | ||
| 94 | Games::Sudoku::Trainer::GUIdetails::build_strat_details(\$details_bt); | ||
| 95 | }, | ||
| 96 | -state => 'disabled', | ||
| 97 | )->pack( -side => 'right' ); | ||
| 98 | |||
| 99 | _build_sudoku_board(); | ||
| 100 | |||
| 101 | # file info bar | ||
| 102 | my $fileinfo_fr = $mw->Frame()->pack( -anchor => 'w', -fill => 'x' ); | ||
| 103 | $fileinfo_fr->Label( -text => 'File name' )->pack( -side => 'left' ); | ||
| 104 | $file_en = $fileinfo_fr->Entry( | ||
| 105 | -textvariable => \$initfile, | ||
| 106 | -bg => 'white', | ||
| 107 | -state => 'readonly' | ||
| 108 | )->pack( -side => 'left', -expand => 1, -fill => 'x' ); | ||
| 109 | $fileinfo_fr->Label( -text => 'preset values' )->pack( -side => 'right' ); | ||
| 110 | $fileinfo_fr->Label( | ||
| 111 | -textvariable => \$preset_count, | ||
| 112 | -width => 4, | ||
| 113 | -anchor => 'e' | ||
| 114 | )->pack( -side => 'right' ); | ||
| 115 | |||
| 116 | # status bar | ||
| 117 | my $status_fr = $mw->Frame()->pack( -anchor => 'w', -fill => 'x' ); | ||
| 118 | $status_fr->Label( -textvariable => \$status )->pack( -side => 'left' ); | ||
| 119 | $status_fr->Label( -text => 'values found' )->pack( -side => 'right' ); | ||
| 120 | $status_fr->Label( -textvariable => \$values_count ) | ||
| 121 | ->pack( -side => 'right' ); | ||
| 122 | |||
| 123 | $mw->resizable( 0, 0 ); # freeze window size | ||
| 124 | |||
| 125 | # Prevent the perl process from hanging when the user clicks on the | ||
| 126 | # File/Quit button or on the kill button near the right edge of the | ||
| 127 | # title bar: | ||
| 128 | # Change the noop sub to ref of sub quit, then terminate the pause. | ||
| 129 | # Note: Has been $mw->OnDestroy before. | ||
| 130 | $mw->protocol( 'WM_DELETE_WINDOW', | ||
| 131 | sub { $noop_sub = \&quit; Games::Sudoku::Trainer::Check_pause::endpause() } ); | ||
| 132 | return; | ||
| 133 | } ## end sub _build_GUI | ||
| 134 | |||
| 135 | #----------------------------------------------------------------------- | ||
| 136 | # Menubar | ||
| 137 | #----------------------------------------------------------------------- | ||
| 138 | |||
| 139 | sub _build_menubar { | ||
| 140 | $mw->configure( -menu => $menubar = $mw->Menu ); | ||
| 141 | $menubar->cascade( -label => '~File', -menuitems => _file_menuitems() ); | ||
| 142 | $menubar->cascade( | ||
| 143 | -label => '~Pause Mode', | ||
| 144 | -menuitems => _pausemode_menuitems() | ||
| 145 | ); | ||
| 146 | $menubar->cascade( | ||
| 147 | -label => '~Priorities', | ||
| 148 | -menuitems => [ | ||
| 149 | [ | ||
| 150 | qw/command ~Change -command/ => sub { | ||
| 151 | require Games::Sudoku::Trainer::GUIprio; | ||
| 152 | Games::Sudoku::Trainer::GUIprio::change_prios($mw); | ||
| 153 | } | ||
| 154 | ] | ||
| 155 | ], | ||
| 156 | ); | ||
| 157 | |||
| 158 | $menubar->cascade( | ||
| 159 | -label => '~History', | ||
| 160 | -menuitems => [ | ||
| 161 | [ | ||
| 162 | qw/command ~Summary -command/ => | ||
| 163 | sub { Games::Sudoku::Trainer::GUIhist::hist_summary($mw) }, | ||
| 164 | ], | ||
| 165 | [ | ||
| 166 | qw/command ~Overview -command/ => | ||
| 167 | sub { Games::Sudoku::Trainer::GUIhist::hist_overview($mw) }, | ||
| 168 | ], | ||
| 169 | [ | ||
| 170 | qw/command ~Details -command/ => | ||
| 171 | sub { Games::Sudoku::Trainer::GUIhist::hist_details($mw) }, | ||
| 172 | ], | ||
| 173 | ], | ||
| 174 | ); | ||
| 175 | $menubar->cascade( | ||
| 176 | -label => '~View', | ||
| 177 | -menuitems => [ | ||
| 178 | [ | ||
| 179 | 'checkbutton', | ||
| 180 | 'active candidates', | ||
| 181 | -command => [ \&_show_candsquares, 'active candidates' ] | ||
| 182 | ], | ||
| 183 | [ | ||
| 184 | 'checkbutton', | ||
| 185 | 'excluded candidates', | ||
| 186 | -command => [ \&_show_candsquares, 'excluded candidates' ] | ||
| 187 | ], | ||
| 188 | ], | ||
| 189 | ); | ||
| 190 | |||
| 191 | # set the init. state of the 'excluded' checkbutton to 'off' | ||
| 192 | # required for sub exclude_cand | ||
| 193 | my $view_menu = $menubar->entrycget( 'View', -menu ); | ||
| 194 | my $state_ref = $view_menu->entrycget( 'excluded candidates', -variable ); | ||
| 195 | $$state_ref = 0; | ||
| 196 | return; | ||
| 197 | } ## end sub _build_menubar | ||
| 198 | |||
| 199 | sub _file_menuitems { | ||
| 200 | return [ | ||
| 201 | [ qw/command ~Save -command/ => \&_result_save ], | ||
| 202 | [ | ||
| 203 | qw/command/, "S~ave as", | ||
| 204 | qw/-command/ => [ \&_sudoku_save_as, 'current' ] | ||
| 205 | ], | ||
| 206 | '', | ||
| 207 | [ | ||
| 208 | qw/command/, | ||
| 209 | "Save ~initial puzzle", | ||
| 210 | qw/-command/ => [ \&_sudoku_save_as, 'initial' ] | ||
| 211 | ], | ||
| 212 | '', | ||
| 213 | [ | ||
| 214 | qw/command/, "Save ~priority list", qw/-command/ => \&_prio_save_as | ||
| 215 | ], | ||
| 216 | [ qw/command/, "Load p~riority list", qw/-command/ => \&_prio_load ], | ||
| 217 | '', | ||
| 218 | [ qw/command ~Quit -command/ => \&quit ], | ||
| 219 | ]; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _pausemode_menuitems { | ||
| 223 | return [ | ||
| 224 | map ( [ | ||
| 225 | 'radiobutton', $_, | ||
| 226 | -variable => \$pause_mode, | ||
| 227 | -value => $_, | ||
| 228 | -command => [ \&set_pause_mode, $_ ] | ||
| 229 | ], | ||
| 230 | qw/single-step non-stop/ ), | ||
| 231 | '', | ||
| 232 | [ | ||
| 233 | qw/cascade Strategy -menuitems/ => [ | ||
| 234 | map ( [ | ||
| 235 | 'radiobutton', $_, | ||
| 236 | -variable => \$pause_mode, | ||
| 237 | -command => [ \&set_pause_mode, $_ ] | ||
| 238 | ], | ||
| 239 | ( | ||
| 240 | sort 'Full House', | ||
| 241 | Games::Sudoku::Trainer::Priorities->copy_strats() | ||
| 242 | ) ), | ||
| 243 | ], | ||
| 244 | ], | ||
| 245 | '', | ||
| 246 | [ | ||
| 247 | 'cascade', | ||
| 248 | 'Value found', | ||
| 249 | -menuitems => [ | ||
| 250 | # This group of radiobuttons is unusual (but still legal), since | ||
| 251 | # it doesn't have the '-variable' option. The internal variable | ||
| 252 | # '$Tk::selectedButton' is used instead. | ||
| 253 | # In addition, in '$Tk::selectedButton' the radiobutton label is | ||
| 254 | # used as the value, so we don't need the '-value' option. | ||
| 255 | [ | ||
| 256 | 'radiobutton', 'anywhere', | ||
| 257 | -command => sub { set_pause_mode('value found') } | ||
| 258 | ], | ||
| 259 | [ | ||
| 260 | 'radiobutton', | ||
| 261 | 'in a unit', | ||
| 262 | -command => sub { | ||
| 263 | set_pause_mode('value found'); | ||
| 264 | require Games::Sudoku::Trainer::GUIpause_restrict; | ||
| 265 | Games::Sudoku::Trainer::GUIpause_restrict::enter_unitname( | ||
| 266 | $restrict_lb); | ||
| 267 | } | ||
| 268 | ], | ||
| 269 | [ | ||
| 270 | 'radiobutton', | ||
| 271 | 'at a cell', | ||
| 272 | -command => sub { | ||
| 273 | set_pause_mode('value found'); | ||
| 274 | require Games::Sudoku::Trainer::GUIpause_restrict; | ||
| 275 | Games::Sudoku::Trainer::GUIpause_restrict::enter_cellname( | ||
| 276 | $restrict_lb); | ||
| 277 | } | ||
| 278 | ], | ||
| 279 | ], | ||
| 280 | ], | ||
| 281 | [ | ||
| 282 | 'radiobutton', | ||
| 283 | 'Trace a cell', | ||
| 284 | -variable => \$pause_mode, | ||
| 285 | -command => sub { | ||
| 286 | set_pause_mode('Trace a cell'); | ||
| 287 | require Games::Sudoku::Trainer::GUIpause_restrict; | ||
| 288 | Games::Sudoku::Trainer::GUIpause_restrict::enter_cellname( | ||
| 289 | $restrict_lb); | ||
| 290 | Games::Sudoku::Trainer::GUIpause_restrict::build_tracewindow( | ||
| 291 | $candsize, $cellsize ); | ||
| 292 | } | ||
| 293 | ], | ||
| 294 | ]; | ||
| 295 | } ## end sub _pausemode_menuitems | ||
| 296 | |||
| 297 | #----------------------------------------------------------------------- | ||
| 298 | # sudoku board | ||
| 299 | #----------------------------------------------------------------------- | ||
| 300 | |||
| 301 | sub _build_sudoku_board { | ||
| 302 | my $boardsize = 9 * ( $cellsize + 1 ) + 2; # size of the sudoku board | ||
| 303 | $cv = $mw->Canvas( | ||
| 304 | -scrollregion => [ 0, 0, $boardsize - 1, $boardsize - 1 ], | ||
| 305 | -height => $boardsize - 1, | ||
| 306 | -width => $boardsize - 1, | ||
| 307 | -bg => 'white', | ||
| 308 | )->pack(); | ||
| 309 | |||
| 310 | # draw cell separator lines | ||
| 311 | foreach my $pos ( 1 .. 8 ) { | ||
| 312 | next unless $pos % 3; | ||
| 313 | my $where = $pos * ( $cellsize + 1 ); | ||
| 314 | $cv->createLine( $where, 1, $where, $boardsize - 2, -fill => 'gray' ); | ||
| 315 | $cv->createLine( 1, $where, $boardsize - 2, $where, -fill => 'gray' ); | ||
| 316 | } | ||
| 317 | foreach my $pos ( 0 .. 3 ) { | ||
| 318 | my $where = 3 * $pos * ( $cellsize + 1 ); | ||
| 319 | $cv->createLine( $where, 0, $where, $boardsize - 1, -fill => 'black' ); | ||
| 320 | $cv->createLine( 0, $where, $boardsize - 1, $where, -fill => 'black' ); | ||
| 321 | } | ||
| 322 | |||
| 323 | # create the rectangle that frames the freshly found value | ||
| 324 | $cv->createRectangle( -999, -999, -888, -888, -tags => 'fresh' ); | ||
| 325 | |||
| 326 | # create the rectangle that frames the previously found value | ||
| 327 | $cv->createRectangle( | ||
| 328 | -999, -999, -888, -888, | ||
| 329 | -tags => 'previous', | ||
| 330 | -outline => 'gray' | ||
| 331 | ); | ||
| 332 | return; | ||
| 333 | } | ||
| 334 | |||
| 335 | # add a cell value to the Sudoku board | ||
| 336 | # display_cellvalue($cellrow, $cellcol, $cellvalue, $strategy) | ||
| 337 | # Called from the big loop (sub Run::_run_puzzle) | ||
| 338 | # Also called from sub Run::_insert_presets (displays value in blue) | ||
| 339 | # | ||
| 340 | sub display_cellvalue { | ||
| 341 | my ( $row, $col, $value, $strategy ) = @_; | ||
| 342 | |||
| 343 | my $tx = $cv->createText( | ||
| 344 | _cvpos($col) + $cellsize / 2, | ||
| 345 | _cvpos($row) + $cellsize / 2, | ||
| 346 | -text => $value | ||
| 347 | ); | ||
| 348 | |||
| 349 | # display preset values in blue and bold | ||
| 350 | if ( $strategy eq 'preset' ) { | ||
| 351 | |||
| 352 | if ( !defined $mw->fontNames ) | ||
| 353 | { # caveat: works if we use only one fontname | ||
| 354 | my $txfont = $cv->itemcget( $tx, -font ); | ||
| 355 | my $font_family = $mw->fontActual( $txfont, '-family' ); | ||
| 356 | $mw->fontCreate( | ||
| 357 | 'presetfontname', | ||
| 358 | -family => $font_family, | ||
| 359 | -weight => 'bold' | ||
| 360 | ); | ||
| 361 | } | ||
| 362 | $cv->itemconfigure( $tx, -font => 'presetfontname', -fill => 'blue' ); | ||
| 363 | $preset_count++; | ||
| 364 | } | ||
| 365 | |||
| 366 | _delete_cands("r${row}c$col"); # delete cand. squares of this cell | ||
| 367 | return if $strategy eq 'preset'; | ||
| 368 | $values_count++; | ||
| 369 | |||
| 370 | # move the "previous" frame to the old "fresh" cell | ||
| 371 | if ( $values_count > 1 ) { | ||
| 372 | $cv->coords( 'previous', $cv->coords('fresh') ); | ||
| 373 | } | ||
| 374 | |||
| 375 | # frame the new value | ||
| 376 | # the frame is created in sub _build_sudoku_board | ||
| 377 | # move the frame to the cell | ||
| 378 | my ( $basex, $basey ); | ||
| 379 | $basex = _cvpos($col); | ||
| 380 | $basey = _cvpos($row); | ||
| 381 | $cv->coords( | ||
| 382 | 'fresh', $basex + 4, $basey + 4, | ||
| 383 | $basex + $cellsize - 3, | ||
| 384 | $basey + $cellsize - 3 | ||
| 385 | ); | ||
| 386 | return; | ||
| 387 | } ## end sub display_cellvalue | ||
| 388 | |||
| 389 | sub get_initialpuzzle { | ||
| 390 | my @game; | ||
| 391 | |||
| 392 | require Tk::DialogBox; | ||
| 393 | my $db = $mw->DialogBox( | ||
| 394 | -title => 'Initial puzzle', | ||
| 395 | -buttons => [qw/Ok Cancel/], | ||
| 396 | -default_button => 'Cancel', | ||
| 397 | -cancel_button => 'Cancel' | ||
| 398 | ); | ||
| 399 | $db->add( 'Listbox', -selectmode => 'single', -height => 3, -width => 0 ) | ||
| 400 | ->pack(); | ||
| 401 | my $lb = $db->Subwidget('listbox'); | ||
| 402 | $lb->insert( | ||
| 403 | 'end', | ||
| 404 | 'Read from file', | ||
| 405 | 'Insert manually', | ||
| 406 | 'Read example file' | ||
| 407 | ); | ||
| 408 | $lb->bind( '<Double-Button-1>' => sub { $db->Subwidget('B_Ok')->invoke } ); | ||
| 409 | $db->configure( -popover => $mw ); | ||
| 410 | my $answer = $db->Show(); | ||
| 411 | exit if $answer eq 'Cancel'; | ||
| 412 | my ($sel_idx) = $lb->curselection; | ||
| 413 | |||
| 414 | if ( $sel_idx == 0) { # Read from file | ||
| 415 | my $initfile = name_in(); | ||
| 416 | return unless $initfile; | ||
| 417 | open( my $SUDO, '<', $initfile ) | ||
| 418 | or do { Games::Sudoku::Trainer::Run::user_err("can't open $initfile:\n$!"); return }; | ||
| 419 | @game = <$SUDO>; | ||
| 420 | close($SUDO); | ||
| 421 | show_filename($initfile); | ||
| 422 | } | ||
| 423 | elsif ( $sel_idx == 1 ) { # Insert manually | ||
| 424 | @game = do { require FindBin; qx"perl $FindBin::Bin/enter_presets.pl" }; | ||
| 425 | } | ||
| 426 | elsif ( $sel_idx == 2 ) { # Read example file | ||
| 427 | use File::Basename; | ||
| 428 | my $sampledir = dirname(__FILE__) . '/examples'; | ||
| 429 | $sampledir or die "0\nExamples not properly installed\n"; | ||
| 430 | my $options_ref = [-initialdir => $sampledir]; | ||
| 431 | $initfile = name_in($options_ref); | ||
| 432 | return unless $initfile; | ||
| 433 | open( my $SUDO, '<', $initfile ) | ||
| 434 | or do { Games::Sudoku::Trainer::Run::user_err("can't open $initfile:\n$!"); return }; | ||
| 435 | @game = <$SUDO>; | ||
| 436 | close($SUDO); | ||
| 437 | show_filename($initfile); | ||
| 438 | } | ||
| 439 | return @game; | ||
| 440 | } ## end sub get_initialpuzzle | ||
| 441 | |||
| 442 | sub show_filename { | ||
| 443 | $initfile = shift; | ||
| 444 | |||
| 445 | # if path in $initfile doesn't fit in Entry widget, show the end of it | ||
| 446 | my ( $left, $right ) = $file_en->xview(); | ||
| 447 | if ( $right < 1 ) { $file_en->xviewMoveto(1.0) } | ||
| 448 | return; | ||
| 449 | } | ||
| 450 | |||
| 451 | #======================================================================= | ||
| 452 | # Menubar callbacks | ||
| 453 | #======================================================================= | ||
| 454 | |||
| 455 | #----------------------------------------------------------------------- | ||
| 456 | # view candidate squares | ||
| 457 | #----------------------------------------------------------------------- | ||
| 458 | |||
| 459 | { | ||
| 460 | |||
| 461 | my $have_candsquares; # undef if never displayed until now | ||
| 462 | |||
| 463 | # change the visibility of candidate squares | ||
| 464 | # callback for the "view ... cand.s" checkbuttons | ||
| 465 | # labeltext: text of clicked checkbutton | ||
| 466 | # ('active candidates' or 'excluded candidates') | ||
| 467 | # | ||
| 468 | sub _show_candsquares { | ||
| 469 | my $checklabel = shift; # label of clicked checkbutton | ||
| 470 | |||
| 471 | if ( not $have_candsquares ) { | ||
| 472 | foreach my $cell ( @cells[ 1 .. 81 ] ) { | ||
| 473 | next if $cell->Value; | ||
| 474 | build_candsquares( $cv, $cell ); | ||
| 475 | _move_candsquares( $cv, $cell ); | ||
| 476 | } | ||
| 477 | $have_candsquares = 1; | ||
| 478 | } | ||
| 479 | |||
| 480 | my $view_menu = $menubar->entrycget( 'View', -menu ); | ||
| 481 | my $state_ref = $view_menu->entrycget( $checklabel, -variable ) | ||
| 482 | ; # ref to the current state of the checkbutton | ||
| 483 | my $tag = $checklabel =~ /^active /i ? 'black' : 'red'; | ||
| 484 | $cv->itemconfigure( $tag, | ||
| 485 | -state => ( $$state_ref ? 'normal' : 'hidden' ) ); | ||
| 486 | return; | ||
| 487 | } | ||
| 488 | |||
| 489 | # build the cand. squares of a cell | ||
| 490 | # called from sub _show_candsquares and sub build_tracewindow | ||
| 491 | # build_candsquares($canv, $cell); | ||
| 492 | # | ||
| 493 | sub build_candsquares { | ||
| 494 | my ( $canv, $cell ) = @_; | ||
| 495 | |||
| 496 | # key: tag name, value: color constant (gray resp. light red) | ||
| 497 | my %colors = ( 'black' => '#7f7f7f', 'red' => '#ff7f7f' ); | ||
| 498 | |||
| 499 | my $digit = 1; | ||
| 500 | foreach my $r ( 0 .. 2 ) { | ||
| 501 | my $y = $r * ( $candsize + 2 ) + 1; | ||
| 502 | foreach my $c ( 0 .. 2 ) { | ||
| 503 | my $x = $c * ( $candsize + 2 ) + 1; | ||
| 504 | my $coltag = $cell->has_candidate($digit) ? 'black' : 'red'; | ||
| 505 | $canv->createRectangle( | ||
| 506 | $x, $y, $x + $candsize - 1, $y + $candsize - 1, | ||
| 507 | -fill => $colors{$coltag}, | ||
| 508 | -outline => $colors{$coltag}, | ||
| 509 | -tags => [ $coltag, $cell->Name, "d$digit", 'new' ], | ||
| 510 | -state => 'hidden', | ||
| 511 | ); | ||
| 512 | $digit++; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | return; | ||
| 516 | } | ||
| 517 | |||
| 518 | # move the new candidate squares to their position | ||
| 519 | # _move_candsquares($canv, $cell); | ||
| 520 | # | ||
| 521 | sub _move_candsquares { | ||
| 522 | my ( $canv, $cell ) = @_; | ||
| 523 | my ( $basex, $basey ); | ||
| 524 | |||
| 525 | my $row = $cell->Row_num; | ||
| 526 | my $col = $cell->Col_num; | ||
| 527 | $basex = _cvpos($col) + 2; | ||
| 528 | $basey = _cvpos($row) + 2; | ||
| 529 | $canv->move( 'new', $basex, $basey ); | ||
| 530 | $canv->dtag('new'); | ||
| 531 | return; | ||
| 532 | } | ||
| 533 | |||
| 534 | # exclude a digit from the cand.s display of a cell | ||
| 535 | # exclude_cand($cell, $digit); | ||
| 536 | # | ||
| 537 | sub exclude_cand { | ||
| 538 | my ( $cell, $digit ) = @_; | ||
| 539 | |||
| 540 | $have_candsquares or return; | ||
| 541 | my ($square) = $cv->find( withtag => $cell->Name . "&&d$digit" ); | ||
| 542 | $square or return; | ||
| 543 | $cv->dtag( $square, 'black' ); | ||
| 544 | $cv->addtag( 'red', withtag => $cell->Name . "&&d$digit" ); | ||
| 545 | |||
| 546 | # change color to a light red | ||
| 547 | $cv->itemconfigure( | ||
| 548 | $square, | ||
| 549 | -fill => '#ff7f7f', | ||
| 550 | -outline => '#ff7f7f' | ||
| 551 | ); | ||
| 552 | |||
| 553 | # update the visibility of excluded cand.s | ||
| 554 | _show_candsquares('excluded candidates'); | ||
| 555 | return; | ||
| 556 | } | ||
| 557 | |||
| 558 | # delete all cand. squares of a cell | ||
| 559 | # called from sub display_cellvalue | ||
| 560 | # | ||
| 561 | sub _delete_cands { | ||
| 562 | my $cellname = shift; | ||
| 563 | |||
| 564 | $have_candsquares or return; | ||
| 565 | $cv->delete($cellname); | ||
| 566 | return; | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | #======================================================================= | ||
| 571 | # I/O dialogs | ||
| 572 | #======================================================================= | ||
| 573 | |||
| 574 | #----------------------------------------------------------------------- | ||
| 575 | # save sudoku puzzle | ||
| 576 | #----------------------------------------------------------------------- | ||
| 577 | |||
| 578 | { | ||
| 579 | |||
| 580 | use File::Basename; | ||
| 581 | |||
| 582 | my $lastfile; # last filename used to save the current puzzle | ||
| 583 | |||
| 584 | # ask for file name and write sudoku file | ||
| 585 | # callback for File menu items "Save as" and "Save initial puzzle" | ||
| 586 | # also called from sub _result_save | ||
| 587 | # _sudoku_save_as puzzle_state | ||
| 588 | # puzzle_state: 'current' or 'initial' | ||
| 589 | # | ||
| 590 | sub _sudoku_save_as { | ||
| 591 | my $puzzle_state = shift; | ||
| 592 | my $file; | ||
| 593 | |||
| 594 | $file = | ||
| 595 | $puzzle_state eq 'initial' ? undef | ||
| 596 | : defined $lastfile ? $lastfile | ||
| 597 | : defined($initfile) ? $initfile | ||
| 598 | : undef; | ||
| 599 | my ( $basename, $pathname ) = | ||
| 600 | defined $file ? fileparse($file) : ( '', '.' ); | ||
| 601 | $file = $mw->getSaveFile( | ||
| 602 | -title => 'Sudoku output file', | ||
| 603 | -initialdir => $pathname, | ||
| 604 | -initialfile => $basename, | ||
| 605 | -filetypes => [ | ||
| 606 | [ 'Sudoku Files', '.sudo' ], | ||
| 607 | [ 'Text Files', [ '.txt', '.text' ] ], | ||
| 608 | [ 'All Files', ['*'] ], | ||
| 609 | ], | ||
| 610 | -defaultextension => '.sudo', | ||
| 611 | ); | ||
| 612 | return unless defined $file; | ||
| 613 | use Encode; | ||
| 614 | $file = encode( 'iso-8859-1', $file ); | ||
| 615 | |||
| 616 | if ( $puzzle_state eq 'current' ) { | ||
| 617 | $lastfile = $file; | ||
| 618 | Games::Sudoku::Trainer::Write_puzzle::write_result($file); | ||
| 619 | $initfile or show_filename($file); | ||
| 620 | } | ||
| 621 | else { | ||
| 622 | Games::Sudoku::Trainer::Write_puzzle::write_initial($file); | ||
| 623 | show_filename($file); | ||
| 624 | } | ||
| 625 | return; | ||
| 626 | } | ||
| 627 | |||
| 628 | # write the current state of the sudoku puzzle to the current ouput file | ||
| 629 | # callback of the 'File / Save' menu item | ||
| 630 | # _result_save(); | ||
| 631 | # | ||
| 632 | sub _result_save { | ||
| 633 | if ( defined $lastfile ) { | ||
| 634 | Games::Sudoku::Trainer::Write_puzzle::write_result($lastfile); | ||
| 635 | } | ||
| 636 | else { | ||
| 637 | _sudoku_save_as('current'); | ||
| 638 | } | ||
| 639 | return; | ||
| 640 | } | ||
| 641 | } | ||
| 642 | |||
| 643 | #----------------------------------------------------------------------- | ||
| 644 | |||
| 645 | # Quit the trainer | ||
| 646 | # Remind if the initial puzzle hasn't been saved | ||
| 647 | # | ||
| 648 | sub quit { | ||
| 649 | $initfile and Tk::exit(); | ||
| 650 | my $answer = $mw->messageBox( | ||
| 651 | -title => 'Reminder', | ||
| 652 | -message => 'Save the initial puzzle?', | ||
| 653 | -type => 'YesNo', | ||
| 654 | -icon => 'question', | ||
| 655 | -default => 'Yes' | ||
| 656 | ); | ||
| 657 | $answer eq 'No' and Tk::exit(); | ||
| 658 | _sudoku_save_as('initial'); | ||
| 659 | Games::Sudoku::Trainer::Check_pause::endpause(); | ||
| 660 | # Tk::exit(); | ||
| 661 | exit(); | ||
| 662 | } | ||
| 663 | |||
| 664 | #----------------------------------------------------------------------- | ||
| 665 | # save / load priority list | ||
| 666 | #----------------------------------------------------------------------- | ||
| 667 | |||
| 668 | sub _prio_save_as { | ||
| 669 | my $types = [ | ||
| 670 | [ 'Priority Files', '.prio' ], | ||
| 671 | [ 'Text Files', [ '.txt', '.text' ] ], | ||
| 672 | [ 'All Files', ['*'] ], | ||
| 673 | ]; | ||
| 674 | |||
| 675 | my $outfile = $mw->getSaveFile( | ||
| 676 | -filetypes => $types, | ||
| 677 | -defaultextension => '.prio' | ||
| 678 | ); | ||
| 679 | return unless defined $outfile; | ||
| 680 | use Encode; | ||
| 681 | $outfile = encode( 'iso-8859-1', $outfile ); | ||
| 682 | open( my $PRIO, '>', $outfile ) | ||
| 683 | or do { Games::Sudoku::Trainer::Run::user_err("Cannot open $outfile:\n$!"); return }; | ||
| 684 | print $PRIO join( "\n", Games::Sudoku::Trainer::Priorities::copy_strats() ), "\n"; | ||
| 685 | close($PRIO) or die "Cannot close $outfile: $!\n"; | ||
| 686 | return; | ||
| 687 | } | ||
| 688 | |||
| 689 | sub _prio_load { | ||
| 690 | my $types = [ | ||
| 691 | [ 'Priority Files', '.prio' ], | ||
| 692 | [ 'Text Files', [ '.txt', '.text' ] ], | ||
| 693 | [ 'All Files', ['*'] ], | ||
| 694 | ]; | ||
| 695 | |||
| 696 | my $options_ref = [ | ||
| 697 | -filetypes => $types, | ||
| 698 | -defaultextension => '.prio', | ||
| 699 | ]; | ||
| 700 | my $infile = name_in($options_ref); | ||
| 701 | return unless $infile; | ||
| 702 | open( my $PRIO, '<', $infile ) | ||
| 703 | or do { Games::Sudoku::Trainer::Run::user_err("can't open $infile:\n$!"); return }; | ||
| 704 | my @strats = <$PRIO>; | ||
| 705 | close($PRIO); | ||
| 706 | chomp foreach @strats; | ||
| 707 | #TODO: chomp @strats; _or_ my @strats = chomp <$PRIO>; | ||
| 708 | Games::Sudoku::Trainer::Priorities::set_strats( \@strats ); | ||
| 709 | return; | ||
| 710 | } | ||
| 711 | |||
| 712 | #----------------------------------------------------------------------- | ||
| 713 | # GUI helpers | ||
| 714 | #----------------------------------------------------------------------- | ||
| 715 | |||
| 716 | # Set pause mode | ||
| 717 | # set_pause_mode(<new mode>); | ||
| 718 | # set_pause_mode('default'); # revert to default pause mode | ||
| 719 | # | ||
| 720 | sub set_pause_mode { | ||
| 721 | my $new_mode = shift; | ||
| 722 | $new_mode eq 'default' and $new_mode = 'single-step'; | ||
| 723 | |||
| 724 | # clear pause mode restriction | ||
| 725 | if ( Games::Sudoku::Trainer::Pause->Mode ne 'Trace a cell' ) { | ||
| 726 | require Games::Sudoku::Trainer::GUIpause_restrict; | ||
| 727 | Games::Sudoku::Trainer::GUIpause_restrict::norestrict_pause($menubar); | ||
| 728 | } | ||
| 729 | $pause_mode = $new_mode; | ||
| 730 | Games::Sudoku::Trainer::Pause->setMode($new_mode); | ||
| 731 | return; | ||
| 732 | } | ||
| 733 | |||
| 734 | # Insert the message in the status line | ||
| 735 | # GUI::set_status($message); | ||
| 736 | # | ||
| 737 | sub set_status { | ||
| 738 | $status = shift; | ||
| 739 | return; | ||
| 740 | } | ||
| 741 | |||
| 742 | # (de)activate a button | ||
| 743 | # GUI::button_state(button_name, 'enable' or 'disable'); | ||
| 744 | # currently supported button names: 'Run', 'Details' | ||
| 745 | # | ||
| 746 | sub button_state { | ||
| 747 | my ( $but, $new_state ) = @_; | ||
| 748 | my %buttons = ( Run => $run_bt, Details => $details_bt ); | ||
| 749 | |||
| 750 | $buttons{$but} | ||
| 751 | ->configure( -state => $new_state eq 'enable' ? 'normal' : 'disabled' ); | ||
| 752 | if ( $but eq 'Details' ) { | ||
| 753 | $pause_strat = | ||
| 754 | $new_state eq 'enable' ? Games::Sudoku::Trainer::Pause->Strat : ''; | ||
| 755 | } | ||
| 756 | return; | ||
| 757 | } | ||
| 758 | |||
| 759 | # wait for change of a variable | ||
| 760 | # GUI::wait($variable_ref); | ||
| 761 | # | ||
| 762 | sub wait { | ||
| 763 | my $var_ref = shift; | ||
| 764 | $mw->waitVariable($$var_ref); | ||
| 765 | |||
| 766 | # see the comment at $mw->protocol( 'WM_DELETE_WINDOW', ...) | ||
| 767 | # near the end of sub _build_GUI | ||
| 768 | $mw->raise(); | ||
| 769 | &$noop_sub(); | ||
| 770 | return; | ||
| 771 | } | ||
| 772 | |||
| 773 | # call Check_pause::endpause no longer on a click of the kill button | ||
| 774 | # reason: the game is over, we aren't in a pause | ||
| 775 | # | ||
| 776 | sub set_exit_on_delete { | ||
| 777 | $mw->protocol( 'WM_DELETE_WINDOW', \&quit ); | ||
| 778 | return; | ||
| 779 | } | ||
| 780 | |||
| 781 | # compute the upper or left canvas coordinate of a sudoku cell | ||
| 782 | # $upper = _cvpos($rownum) resp. $left = _cvpos($colnum) | ||
| 783 | # | ||
| 784 | sub _cvpos { | ||
| 785 | return ( $_[0] - 1 ) * ( $cellsize + 1 ); | ||
| 786 | } | ||
| 787 | |||
| 788 | # build clues hash from clues array | ||
| 789 | # %clues = recode_clues(ref_to_clues_array); | ||
| 790 | # | ||
| 791 | sub recode_clues { | ||
| 792 | my $clues_ref = shift; | ||
| 793 | my %clues_all; | ||
| 794 | my ( @clue_units, $clue_cands, @clue_cells ); | ||
| 795 | |||
| 796 | foreach my $clue (@$clues_ref) { | ||
| 797 | if ( ref $clue and $clue->isa('Games::Sudoku::Trainer::Unit') ) { | ||
| 798 | push( @clue_units, $clue ); | ||
| 799 | } | ||
| 800 | elsif ( !ref $clue ) { | ||
| 801 | defined $clue or $clue = '<undef>'; | ||
| 802 | if ($clue_cands) { | ||
| 803 | $mw->messageBox( | ||
| 804 | -title => 'Code error', | ||
| 805 | -icon => 'info', | ||
| 806 | -message => "Duplicate candidates clue $clue_cands" | ||
| 807 | . " and $clue ((hopefully) harmless)" | ||
| 808 | ); | ||
| 809 | } | ||
| 810 | $clue_cands .= $clue; # ?? | ||
| 811 | } | ||
| 812 | elsif ( ref $clue eq 'Games::Sudoku::Trainer::Cell' ) { | ||
| 813 | push( @clue_cells, $clue ); | ||
| 814 | } | ||
| 815 | else { | ||
| 816 | die "3\nUnknown clue $clue"; | ||
| 817 | } | ||
| 818 | } | ||
| 819 | |||
| 820 | @clue_units and $clues_all{units} = names(@clue_units); | ||
| 821 | |||
| 822 | # separate cand. digits by commas | ||
| 823 | $clue_cands and ( $clues_all{cands} = $clue_cands ) =~ s/(\d)(?=\d)/$1,/g; | ||
| 824 | @clue_cells and $clues_all{cells} = names(@clue_cells); | ||
| 825 | return \%clues_all; | ||
| 826 | } ## end sub recode_clues | ||
| 827 | |||
| 828 | # ask user for input filename | ||
| 829 | # $infile = name_in($opts_array_ref); | ||
| 830 | # | ||
| 831 | sub name_in { | ||
| 832 | my $opts_ref = shift; | ||
| 833 | |||
| 834 | my %opts = ( | ||
| 835 | -filetypes => [ | ||
| 836 | [ 'Sudoku Files', '.sudo' ], | ||
| 837 | [ 'Text Files', [ '.txt', '.text' ] ], | ||
| 838 | [ 'All Files', ['*'] ], | ||
| 839 | ], | ||
| 840 | -defaultextension => '.sudo', | ||
| 841 | # overwrite defaults (Modern Perl Chap. 3/Hash Idioms) | ||
| 842 | $opts_ref ? @$opts_ref : (), | ||
| 843 | ); | ||
| 844 | my $file = $mw->getOpenFile(%opts); | ||
| 845 | return unless $file; | ||
| 846 | use Encode; | ||
| 847 | $file = encode( 'iso-8859-1', $file ); | ||
| 848 | return $file; | ||
| 849 | } | ||
| 850 | |||
| 851 | # return the names of the given objects as a string | ||
| 852 | # This is a dev. tool to inspect the erroneous result | ||
| 853 | # of a strategy sub. Tries to help identify faulty | ||
| 854 | # parameters. May also be helpful for other lists. | ||
| 855 | # $namestring = names(list of cells and/or units); | ||
| 856 | # | ||
| 857 | sub names { | ||
| 858 | return join( | ||
| 859 | ',', | ||
| 860 | map { | ||
| 861 | !ref $_ ? "$_" | ||
| 862 | : ref $_ eq 'REF' ? 'REF' | ||
| 863 | : ref $_ eq 'ARRAY' ? 'ARRAY' | ||
| 864 | : $_->can('Name') ? $_->Name | ||
| 865 | : '???' | ||
| 866 | } @_ | ||
| 867 | ); | ||
| 868 | } | ||
| 869 | |||
| 870 | # Show message in messageBox widget | ||
| 871 | # showmessage(message_lines); | ||
| 872 | # | ||
| 873 | sub showmessage { | ||
| 874 | $mw->messageBox(@_); | ||
| 875 | return; | ||
| 876 | } | ||
| 877 | |||
| 878 | 1; |