| 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 | |||
| 10 | package | ||
| 11 | Games::Sudoku::Trainer::Check_pause; | ||
| 12 | |||
| 13 | use version; our $VERSION = qv('0.02'); # PBP | ||
| 14 | |||
| 15 | # This package checks whether a pause is requested at the current state | ||
| 16 | # of the puzzle. | ||
| 17 | # It is called from the Sudoku main loop (sub Run::_run_puzzle) when a change | ||
| 18 | # in the puzzle state (new value or exclusion of candidates) has been found. | ||
| 19 | |||
| 20 | # Check whether a pause is requested here | ||
| 21 | # If yes, wait in the pause until the 'Run' button is pushed | ||
| 22 | # check_pause($found_info_ref); | ||
| 23 | # | ||
| 24 | sub check_pause { | ||
| 25 | my $found_info_ref = shift; | ||
| 26 | my $strategy = $found_info_ref->[0]; | ||
| 27 | |||
| 28 | if ( _check_pause1($found_info_ref) ) { | ||
| 29 | Games::Sudoku::Trainer::Pause->setInfo_ref($found_info_ref); | ||
| 30 | Games::Sudoku::Trainer::Pause->setStrat($strategy); | ||
| 31 | # enable the Details button | ||
| 32 | Games::Sudoku::Trainer::GUI::button_state( 'Details', 'enable' ); | ||
| 33 | if ( $found_info_ref->[1] eq 'insert' ) { | ||
| 34 | Games::Sudoku::Trainer::GUI::set_status('Found a further value'); | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | Games::Sudoku::Trainer::GUI::set_status('Excluded further candidates'); | ||
| 38 | } | ||
| 39 | if ( Games::Sudoku::Trainer::Pause->Mode eq 'Trace a cell' ) { | ||
| 40 | Games::Sudoku::Trainer::GUIpause_restrict::update_tracewindow( | ||
| 41 | $found_info_ref); | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | pause(); | ||
| 45 | } | ||
| 46 | Games::Sudoku::Trainer::GUI::set_status(''); | ||
| 47 | Games::Sudoku::Trainer::Pause->setStrat(''); | ||
| 48 | # disable the Details button | ||
| 49 | Games::Sudoku::Trainer::GUI::button_state( 'Details', 'disable' ); | ||
| 50 | } | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | # Check the individual pause modes | ||
| 55 | # returns true if a pause is needed | ||
| 56 | # $bool = _check_pause1($found_info_ref); | ||
| 57 | # | ||
| 58 | sub _check_pause1 { | ||
| 59 | my $found_info_ref = shift; | ||
| 60 | my $strat = $found_info_ref->[0]; | ||
| 61 | my $pause_mode = Games::Sudoku::Trainer::Pause->Mode; | ||
| 62 | $pause_mode or die "3\nNo pause mode"; | ||
| 63 | |||
| 64 | if ( $pause_mode eq 'non-stop' ) { return 0 } | ||
| 65 | if ( $pause_mode eq 'single-step' ) { return 1 } | ||
| 66 | if ( $pause_mode eq 'BLI and LBI' ) { | ||
| 67 | return ( | ||
| 68 | $strat eq 'Block-Line Interaction' | ||
| 69 | or $strat eq 'Line-Block Interaction' | ||
| 70 | ); | ||
| 71 | } | ||
| 72 | if ( $pause_mode eq $strat ) { return 1 } | ||
| 73 | |||
| 74 | if ( $pause_mode eq 'value found' ) { | ||
| 75 | return _pause_value_found($found_info_ref); | ||
| 76 | } | ||
| 77 | |||
| 78 | if ( $pause_mode eq 'Trace a cell' ) { | ||
| 79 | return _pause_trace_cell($found_info_ref); | ||
| 80 | } | ||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | # check for pause mode "value found" | ||
| 85 | # $bool = _pause_value_found($found_info_ref); | ||
| 86 | # placed into a separate sub for better readability | ||
| 87 | # | ||
| 88 | sub _pause_value_found { | ||
| 89 | my $found_info_ref = shift; | ||
| 90 | |||
| 91 | $found_info_ref->[1] eq 'insert' or return 0; | ||
| 92 | my $pause_restrict = Games::Sudoku::Trainer::Pause->Mode_restriction; | ||
| 93 | $pause_restrict or return 1; # anywhere | ||
| 94 | my $cell = $found_info_ref->[3]->[1]; | ||
| 95 | if ( $pause_restrict eq $cell->Name() ) { # at requested cell | ||
| 96 | Games::Sudoku::Trainer::GUI::showmessage( | ||
| 97 | -title => 'Information', | ||
| 98 | -message => 'Value found at cell ' . $pause_restrict | ||
| 99 | ); | ||
| 100 | # revert to default pause mode | ||
| 101 | Games::Sudoku::Trainer::GUI::set_pause_mode('default'); | ||
| 102 | return 1; | ||
| 103 | } | ||
| 104 | else { # in requested unit | ||
| 105 | my $unit; | ||
| 106 | foreach my $container ( $cell->get_Containers ) { | ||
| 107 | $unit = $container; | ||
| 108 | last if $container->Name eq $pause_restrict; | ||
| 109 | } | ||
| 110 | return 0 if $unit->Name ne $pause_restrict; | ||
| 111 | if ( $unit->active_Members == 1 ) { | ||
| 112 | Games::Sudoku::Trainer::GUI::showmessage( | ||
| 113 | -title => 'Information', | ||
| 114 | -message => 'All values found in unit ' | ||
| 115 | . $unit->Name | ||
| 116 | . ".\nEnd of 'value found' mode" | ||
| 117 | ); | ||
| 118 | # revert to default pause mode | ||
| 119 | Games::Sudoku::Trainer::GUI::set_pause_mode('default'); | ||
| 120 | } | ||
| 121 | return 1; | ||
| 122 | } | ||
| 123 | } ## end sub _pause_value_found | ||
| 124 | |||
| 125 | # check for pause mode "trace a cell" | ||
| 126 | # $bool = _pause_trace_cell($found_info_ref); | ||
| 127 | # placed into a separate sub for better readability | ||
| 128 | # | ||
| 129 | sub _pause_trace_cell { | ||
| 130 | my $found_info_ref = shift; | ||
| 131 | |||
| 132 | my $pause_restrict = Games::Sudoku::Trainer::Pause->Mode_restriction; | ||
| 133 | if ( $found_info_ref->[1] eq 'insert' ) { | ||
| 134 | my $cell = $found_info_ref->[3]->[1]; | ||
| 135 | $cell->Name() eq $pause_restrict | ||
| 136 | and return 1; # value found for trace cell | ||
| 137 | my @trace_units = | ||
| 138 | Games::Sudoku::Trainer::Cell->by_name($pause_restrict)->get_Containers(); | ||
| 139 | my @cell_units = $cell->get_Containers(); | ||
| 140 | foreach ( 0 .. 2 ) { | ||
| 141 | $cell_units[$_] == $trace_units[$_] | ||
| 142 | and return 1; # value found in same unit | ||
| 143 | } | ||
| 144 | return 0; # trace cell not affected | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | # exclude cand.s: check whether there are exclusions in the trace cell | ||
| 148 | my $exclude_info_ref = $found_info_ref->[3]; | ||
| 149 | my $tracename = $pause_restrict; | ||
| 150 | return map ( { | ||
| 151 | $_ =~ /(\d+)-/; | ||
| 152 | my $cellname = $cells[$1]->Name; | ||
| 153 | $cellname =~ /$tracename/ ? ($cellname) : () | ||
| 154 | } @$exclude_info_ref ); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | # enter resp. terminate the pause | ||
| 159 | |||
| 160 | { # block for pause starter / terminater subs | ||
| 161 | |||
| 162 | my $inpause = 0; | ||
| 163 | |||
| 164 | sub pause { | ||
| 165 | $inpause = 1; | ||
| 166 | Games::Sudoku::Trainer::GUI::wait( \$inpause ); | ||
| 167 | return; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub endpause { | ||
| 171 | $inpause = 0; | ||
| 172 | return; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; | ||
| 177 |