lib/Games/Sudoku/Component/TkPlayer/Controller.pm

filter: Statementstmt 5.5% 11 / 197 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 13.6% 3 / 22 Podpod 100% 16 / 16 totaltotal 9.7% 30 / 309 CC 44 SCAR 35.7
CC
44
Cov
4%
_do_next
132.0
load_game
30.0
find_tmpvalue
30.0
CRAP 35.4
↑ Summary blib/script/tksudoku.pl »
1 package Games::Sudoku::Component::TkPlayer::Controller;
2 {
3 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 0 ▶ sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
8 0 my $class = shift;
9 0 ▶ my $this = bless {}, (ref $class || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
10
11 0 $this;
12 }
13
14 0 ▶ sub new_game {
Subroutine new_game: not called CC=4 SCAR=30.0
Pod documented
15 0 my ($this, $mw, $sdk, $wgt) = @_;
16
17 0 $mw->Busy(-recurse => 1);
18
19 0 $sdk->clear;
20
21 0 my $max = $sdk->table->size ** 2;
22 0 my $blank = int($max * .65);
23
24 0 my $splash = $wgt->{splash};
25
26 0 $splash->Show(-max => $max);
27 0 ▶ until($sdk->status->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sdk->status->is_finished) 0 0 0
28 0 $sdk->next;
29 0 $splash->progress($sdk->table->num_of_finished);
30 0 $splash->update;
31 }
32 0 $splash->progress($max);
33 0 $sdk->make_blank($blank);
34 0 $splash->Hide;
35
36 0 my $size = $sdk->table->size;
37
38 0 my $check = $this->{check_tmp}->();
39 0 $this->{check_tmp}->(0);
40 0 foreach my $row (1..$size) {
41 0 foreach my $col (1..$size) {
42 0 $this->_set_button($sdk, $wgt, $row, $col);
43 }
44 }
45 0 $this->{check_tmp}->($check);
46 0 $this->{message}->(q{Solve this!});
47
48 0 $mw->Unbusy;
49 }
50
51 0 ▶ sub load_game {
Subroutine load_game: not called CC=5 SCAR=34.0
Pod documented
52 0 my ($this, $mw, $sdk, $wgt) = @_;
53
54 0 $mw->Busy(-recurse => 1);
55
56 0 my $file = $mw->getOpenFile;
57
58 0 ▶ if (defined $file && -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -f $file) 0 0 0
Condition0%0/3
defined $file and -f $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and -f $file
defined $file -f $file
Truth table0%0/3
A and B
A= defined $fileB= -f $file
ABresult
0X0
100
111
59 0 $sdk->clear;
60
61 0 $sdk->load(file => $file);
62
63 0 my $size = $sdk->table->size;
64
65 0 my $check = $this->{check_tmp}->();
66 0 $this->{check_tmp}->(0);
67 0 foreach my $row (1..$size) {
68 0 foreach my $col (1..$size) {
69 0 $this->_set_button($sdk, $wgt, $row, $col);
70 }
71 }
72 0 $this->{check_tmp}->($check);
73 0 $this->{message}->(q{Loaded!});
74 }
75 0 $mw->Unbusy;
76 }
77
78 0 ▶ sub save_game {
Subroutine save_game: not called CC=3 SCAR=24.8
Pod documented
79 0 my ($this, $mw, $sdk, $wgt) = @_;
80
81 0 $mw->Busy(-recurse => 1);
82
83 0 my $file = $mw->getSaveFile;
84
85 0 ▶ if (defined $file and open my $fh, '>', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and open my $fh, ">", $file) 0 0 0
Condition0%0/3
defined $file and open my $fh, ">", $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and open my $fh, ">", $file
defined $file open my $fh, ">", $file
Truth table0%0/3
A and B
A= defined $fileB= open my $fh, ">", $file
ABresult
0X0
100
111
86 0 print $fh $sdk->table->as_string;
87 0 close $fh;
88
89 0 $this->{message}->(q{Saved!});
90 }
91
92 0 $mw->Unbusy;
93 }
94
95 0 ▶ sub lock {
Subroutine lock: not called CC=1 SCAR=6.9
Pod documented
96 0 my ($this, $mw, $sdk, $wgt) = @_;
97
98 0 $mw->Busy(-recurse => 1);
99
100 0 $sdk->table->lock_all;
101 0 $this->_update_lock_status($mw, $sdk, $wgt);
102 0 $this->{message}->(q{Locked});
103
104 0 $mw->Unbusy;
105 }
106
107 0 ▶ sub clear {
Subroutine clear: not called CC=3 SCAR=24.8
Pod documented
108 0 my ($this, $mw, $sdk, $wgt) = @_;
109
110 0 $mw->Busy(-recurse => 1);
111
112 0 $sdk->clear;
113 0 $this->unlock($mw, $sdk, $wgt);
114
115 0 my $size = $sdk->table->size;
116
117 0 my $check = $this->{check_tmp}->();
118 0 $this->{check_tmp}->(0);
119 0 foreach my $row (1..$size) {
120 0 foreach my $col (1..$size) {
121 0 $this->_set_button($sdk, $wgt, $row, $col);
122 }
123 }
124 0 $this->{check_tmp}->($check);
125 0 $this->{message}->(q{Cleared});
126
127 0 $mw->Unbusy;
128 }
129
130 0 ▶ sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
Pod documented
131 0 my ($this, $mw, $sdk, $wgt) = @_;
132
133 0 $mw->Busy(-recurse => 1);
134
135 0 $sdk->table->unlock_all;
136 0 $this->_update_lock_status($mw, $sdk, $wgt);
137 0 $this->{message}->(q{Unlocked});
138
139 0 $mw->Unbusy;
140 }
141
142 0 ▶ sub rewind_all {
Subroutine rewind_all: not called CC=3 SCAR=24.8
Pod documented
143 0 my ($this, $mw, $sdk, $wgt) = @_;
144
145 0 $mw->Busy(-recurse => 1);
146
147 0 $sdk->rewind_all;
148 0 $sdk->status->turn_to_ok;
149
150 0 my $size = $sdk->table->size;
151
152 0 my $check = $this->{check_tmp}->();
153 0 $this->{check_tmp}->(0);
154 0 foreach my $row (1..$size) {
155 0 foreach my $col (1..$size) {
156 0 $this->_set_button($sdk, $wgt, $row, $col);
157 }
158 }
159 0 $this->{check_tmp}->($check);
160 0 $this->{message}->(q{Rewinded});
161
162 0 $mw->Unbusy;
163 }
164
165 0 ▶ sub _update_lock_status {
Subroutine _update_lock_status: not called CC=3 SCAR=24.8
166 0 my ($this, $mw, $sdk, $wgt) = @_;
167
168 0 $mw->Busy(-recurse => 1);
169
170 0 my $table = $sdk->table;
171 0 my $size = $table->size;
172
173 0 my $check = $this->{check_tmp}->();
174 0 $this->{check_tmp}->(0);
175 0 foreach my $row (1..$size) {
176 0 foreach my $col (1..$size) {
177 0 my $id = ($row - 1) * $size + ($col - 1);
178 0 $this->configure_button($wgt, $id,
179 $table->cell($row, $col)->is_locked
180 );
181 }
182 }
183 0 $this->{check_tmp}->($check);
184
185 0 $mw->Unbusy;
186 }
187
188 0 ▶ sub _set_button {
Subroutine _set_button: not called CC=4 SCAR=30.0
189 0 my ($this, $sdk, $wgt, $row, $col) = @_;
190
191 0 my $value = $sdk->table->cell($row,$col)->value;
192 0 my $locked = $sdk->table->cell($row,$col)->is_locked;
193 0 my $size = $sdk->table->size;
194 0 my $id = ($row - 1) * $size + ($col - 1);
195
196 0 ▶ $wgt->{buttons}->[$id]->configure(
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
197 -text => $value ? $value : ' ',
198 );
199 0 $this->configure_button($wgt, $id, $locked);
200
201 0 ▶ if ( $this->{check_tmp}->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'check_tmp'}->()) { } 0 0 0
202 0 ▶ if ( $sdk->table->cell($row,$col)->tmpvalue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sdk->table->cell($row, $col)->tmpvalue) { } 0 0 0
203 0 $this->configure_button_color($wgt, $id, 'red');
204 0 $this->{message}->(q{might be wrong...});
205 }
206 else {
207 0 $this->configure_button_color($wgt, $id, 'gray');
208 0 $this->{message}->(q{hmm...});
209 0 $this->find_tmpvalue($sdk, $wgt);
210 }
211 }
212 else {
213 0 $this->configure_button_color($wgt, $id, 'gray');
214 0 $this->{message}->(q{hm...});
215 }
216 }
217
218 0 ▶ sub find_tmpvalue {
Subroutine find_tmpvalue: not called CC=5 SCAR=34.0
Pod documented
219 0 my ($this, $mw, $sdk, $wgt) = @_;
220
221 0 $mw->Busy(-recurse => 1);
222
223 0 my $size = $sdk->table->size;
224
225 0 $sdk->table->check_tmpvalue;
226
227 0 my $check_on = $this->{check_tmp}->();
228 0 foreach my $row (1..$size) {
229 0 foreach my $col (1..$size) {
230 0 my $id = ($row - 1) * $size + ($col - 1);
231 0 ▶ $this->configure_button_color($wgt, $id,
Branch: 1 branch
BranchTrueFalseTotal
$check_on && $sdk->table->cell($row, $col)->tmpvalue ? : 0 0 0
Condition0%0/3
$check_on and $sdk->table->cell($row, $col)->tmpvalue
!ll&&!rl&&r
000
MC/DC0%0/2
$check_on and $sdk->table->cell($row, $col)->tmpvalue
$check_on $sdk->table->cell($row, $col)->tmpvalue
Truth table0%0/3
A and B
A= $check_onB= $sdk->table->cell($row, $col)->tmpvalue
ABresult
0X0
100
111
232 $check_on && $sdk->table->cell($row,$col)->tmpvalue ?
233 'red' : 'gray',
234 );
235 }
236 }
237 0 $mw->Unbusy;
238 }
239
240 0 ▶ sub find_next {
Subroutine find_next: not called CC=2 SCAR=17.9
Pod documented
241 0 my ($this, $mw, $sdk, $wgt) = @_;
242
243 0 $mw->Busy(-recurse => 1);
244
245 0 my $next = $sdk->table->find_next;
246
247 0 ▶ if ($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) { } 0 0 0
248 0 my $size = $sdk->table->size;
249 0 my $row = $next->row;
250 0 my $col = $next->col;
251 0 my $id = ($row - 1) * $size + ($col - 1);
252 0 $this->configure_button_color($wgt, $id, 'yellow');
253 0 $this->{message}->(q{Try this.})
254 }
255 else {
256 0 $this->{message}->(q{I have no idea. Try some.})
257 }
258 0 $mw->Unbusy;
259 }
260
261 0 ▶ sub configure_button {
Subroutine configure_button: not called CC=2 SCAR=17.9
Pod documented
262 0 my ($this, $wgt, $id, $locked) = @_;
263
264 0 ▶ $wgt->{buttons}->[$id]->configure(
Branch: 1 branch
BranchTrueFalseTotal
$locked ? : 0 0 0
265 -font => [
266 -weight => $locked ? 'bold' : 'normal',
267 -family => 'courier',
268 -size => 25,
269 ],
270 -width => 2,
271 -height => 1,
272 );
273 }
274
275 0 ▶ sub configure_button_color {
Subroutine configure_button_color: not called CC=4 SCAR=30.0
Pod documented
276 0 my ($this, $wgt, $id, $color) = @_;
277
278 0 ▶ if ($color eq 'yellow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($color eq "yellow") 0 0 0
279 0 $wgt->{buttons}->[$id]->configure(
280 -activebackground => 'lightyellow',
281 -background => 'yellow',
282 );
283 }
284 0 ▶ if ($color eq 'red') {
Branch: 1 branch
BranchTrueFalseTotal
if ($color eq "red") 0 0 0
285 0 $wgt->{buttons}->[$id]->configure(
286 -activebackground => 'orange',
287 -background => 'red',
288 );
289 }
290 0 ▶ if ($color eq 'gray') {
Branch: 1 branch
BranchTrueFalseTotal
if ($color eq "gray") 0 0 0
291 0 $wgt->{buttons}->[$id]->configure(
292 -activebackground => 'gray',
293 -background => 'darkgray',
294 );
295 }
296 }
297
298 0 ▶ sub solve {
Subroutine solve: not called CC=4 SCAR=30.0
Pod documented
299 0 my ($this, $mw, $sdk, $wgt) = @_;
300
301 0 $mw->Busy(-recurse => 1);
302
303 0 my $status = $sdk->status;
304 0 $status->clear;
305
306 0 ▶ until($status->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status->is_finished) 0 0 0
307 0 $this->_do_next($mw, $sdk, $wgt, 1);
308 0 $mw->update;
309 }
310 0 ▶ if ($status->is_solved) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->is_solved) 0 0 0
311 0 $this->{message}->(q{OK, here you are!});
312 }
313 0 ▶ if ($status->is_giveup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->is_giveup) 0 0 0
314 0 $this->{message}->(q{Sorry I can't solve!});
315 }
316 0 $mw->Unbusy;
317 }
318
319 0 ▶ sub do_next {
Subroutine do_next: not called CC=1 SCAR=6.9
Pod documented
320 0 my ($this, $mw, $sdk, $wgt) = @_;
321
322 0 $mw->Busy(-recurse => 1);
323
324 0 $this->_do_next($mw, $sdk, $wgt, 0);
325
326 0 $mw->Unbusy;
327 }
328
329 0 ▶ sub _do_next {
Subroutine _do_next: not called CC=11 SCAR=48.8
330 0 my ($this, $mw, $sdk, $wgt, $silent) = @_;
331
332 0 my $item = $sdk->next;
333 0 my $status = $sdk->status;
334
335 0 ▶ if ($status->is_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->is_ok) 0 0 0
336 0 ▶ $this->_set_button($sdk, $wgt, $item->row, $item->col) if $item;
Branch: 1 branch
BranchTrueFalseTotal
if $item 0 0 0
337 0 ▶ $this->{message}->(q{Your turn...}) unless $silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 0 0 0
338 }
339 0 ▶ if ($status->is_rewind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->is_rewind) 0 0 0
340 0 ▶ $this->_set_button($sdk, $wgt, $item->row, $item->col) if $item;
Branch: 1 branch
BranchTrueFalseTotal
if $item 0 0 0
341 0 ▶ $this->{message}->(q{mmm, we should rewind some...}) unless $silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 0 0 0
342 }
343 0 ▶ if ($status->is_giveup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->is_giveup) 0 0 0
344 0 ▶ $this->{message}->(q{Sorry I can't solve!}) unless $silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 0 0 0
345 }
346 0 ▶ if ($sdk->table->is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sdk->table->is_finished) 0 0 0
347 0 ▶ $this->{message}->(q{OK, here you are!}) unless $silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 0 0 0
348 }
349 }
350
351 0 ▶ sub push_button {
Subroutine push_button: not called CC=3 SCAR=24.8
Pod documented
352 0 my ($this, $sdk, $wgt, $row, $col) = @_;
353
354 0 ▶ return if $sdk->table->cell($row,$col)->is_locked;
Branch: 1 branch
BranchTrueFalseTotal
if $sdk->table->cell($row, $col)->is_locked 0 0 0
355
356 0 my @allowed = $sdk->table->cell($row,$col)->allowed;
357
358 0 my $allowed_only = $this->{allowed_only}->();
359
360 0 $wgt->{selector}->set_allowed($allowed_only, @allowed);
361
362 0 my $value = $wgt->{selector}->Show;
363
364 0 $sdk->table->cell($row,$col)->value($value);
365
366 0 $this->_set_button($sdk, $wgt, $row, $col);
367
368 0 ▶ $this->{message}->('Conguratulations!') if $sdk->table->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $sdk->table->is_finished 0 0 0
369 }
370
371 0 ▶ sub quit {
Subroutine quit: not called CC=1 SCAR=6.9
Pod documented
372 0 my ($this, $mw) = @_;
373
374 0 $mw->destroy;
375 }
376 }
377
378 1 1;
379
380 __END__
↑ Summary blib/script/tksudoku.pl »