script/enter_presets.pl untested

filter: Statementstmt 0.0% 0 / 180 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 257 CC 11 SCAR 22.3
CC
11
Cov
0%
write_result
20.0
read_edit
20.0
move_clickfield
12.0
CRAP 9.3
1 #!/usr/local/bin/perl
2 use strict;
3 use warnings;
4 #use feature 'say';
5
6 use Tk;
7 use Tk::Balloon;
8
9 use version; our $VERSION = qv('0.01.2'); # PBP
10
11 # Usage:
12 # perl enter_presets.pl
13
14 my $tinysize = 10; # size of a tiny square (pixels)
15 my $mw; # the MainWindow
16 my @sudoku_fields; # the fields (button widgets) of the sudoku game
17 my @sudoku_digits; # the digits displayed on the button widgets
18 my $fieldsize; # size of a sudoku field
19 my $activefield_index = -1; # the currently active sudoku field
20 my $clickfield; # the toplevel which covers the active sudoku field
21 # for clicking
22 my @tiny_fields = (undef); # the tiny squares of the
23 # clickfield (indexed 1 .. 9)
24 my $valuecount = 0; # count of entered values
25 my $edit_b; # the edit button
26
27 {
28 $#sudoku_digits = 80; # speedup: preallocate space
29 $fieldsize = 3 * ( $tinysize + 1 ) - 1; # size of sudoku field
30 my $totalsize = 9 * ( $fieldsize + 1 ) - 1;
31 $mw = MainWindow->new();
32 create_sudoku($totalsize);
33
34 # create bottom row
35
36 my $but_fr = $mw->Frame()->pack( -side => 'bottom', -fill => 'x' );
37
38 # make clickfield invisible while mouse is over the bottom frame
39 $but_fr->bind( '<Enter>', sub { $clickfield->withdraw } );
40
41 # create edit button
42 $edit_b = $but_fr->Button( -text => 'Edit puzzle', -command => \&read_edit )
43 ->pack( -side => 'left', -pady => 3 );
44 my $edit_tip = $mw->Balloon();
45 $edit_tip->attach( $edit_b, -balloonmsg => 'Edit an existing puzzle' );
46
47 # create Done button
48 $but_fr->Button(
49 -text => 'Done',
50 -command => sub { write_result(); Tk::exit() }
51 )->pack( -side => 'left', -padx => 10, -pady => 3 );
52
53 # create Cancel button
54 $but_fr->Button( -text => 'Cancel', -command => sub { Tk::exit() } )
55 ->pack( -side => 'left' );
56
57 # create value count labels
58 $but_fr->Label( -text => 'values' )->pack( -side => 'right' );
59 $but_fr->Label(
60 -textvariable => \$valuecount,
61 -width => 2,
62 -anchor => 'e'
63 )->pack( -side => 'right' );
64
65 # set window size
66 my $size_y = $totalsize + $edit_b->reqheight + 6;
67 $mw->geometry("${totalsize}x$size_y");
68 $mw->resizable( 0, 0 ); # freeze window size
69 create_clickfield();
70
71 # On Windows XP, the sudoku window likes to hide itself behind the "DOS"
72 # shell window at the moment where the clickfield pops up for the 2nd time.
73 # This can be avoided by
74 # $mw->raise() or $mw->focus() or $sudoku_fields[any]->focus().
75 # Set the initial focus to the 1st field
76 $sudoku_fields[0]->focus();
77
78 MainLoop();
79 exit 1;
80 }
81
82 sub create_sudoku {
83 my $totalsize = shift;
84
85 foreach my $i ( 0 .. 8 ) {
86 foreach my $j ( 0 .. 8 ) {
87 create_field( $j, $i );
88 }
89 }
90
91 # draw block separator lines
92 foreach my $pos ( 3, 6 ) {
93 my $where = $pos * ( $fieldsize + 1 ) - 1;
94 $mw->Frame(
95 -width => 1,
96 -height => $totalsize,
97 -background => 'black'
98 )->place( -x => $where, -y => 0 );
99 $mw->Frame(
100 -width => $totalsize,
101 -height => 1,
102 -background => 'black'
103 )->place( -x => 0, -y => $where );
104 }
105 return;
106 }
107
108 # create a sudoku field
109 #
110 sub create_field {
111 my ( $w, $h ) = my ( $w_num, $h_num ) = @_; # pos. num.s of sudoku field
112 my $field_index = $w + 9 * $h; # index of sudoku field
113 $w *= $fieldsize + 1; # pos. of sudoku field
114 $h *= $fieldsize + 1; #
115
116 my $space = $mw->Frame( -width => $fieldsize, -height => $fieldsize )
117 ->place( -x => $w, -y => $h );
118 $space->packPropagate(0); # prevent resizing the frame
119 my $fieldID = $space->Button()->pack( -fill => 'both', -expand => 1 );
120 push( @sudoku_fields, $fieldID );
121
122 # mouse and keyboard bindings
123
124 $fieldID->bind( '<Enter>', [ \&move_clickfield, $field_index ] );
125 foreach my $digit ( 1 .. 9 ) {
126 #alpha keypad
127 $fieldID->bind(
128 "<Key-$digit>" => [ \&change_digit, $field_index, $digit ] );
129
130 #numeric keypad
131 $fieldID->bind(
132 "<KP_$digit>" => [ \&change_digit, $field_index, $digit ] );
133 }
134 # delete digit
135 foreach my $key (qw/0 KP_0 space Delete/) {
136 $fieldID->bind( "<$key>" => [ \&change_digit, $field_index ] );
137 }
138 $fieldID->bind( "<Key-Up>" => [ \&move_focus, $w_num, $h_num - 1 ] );
139 $fieldID->bind( "<Key-Down>" => [ \&move_focus, $w_num, $h_num + 1 ] );
140 $fieldID->bind( "<Key-Left>" => [ \&move_focus, $w_num - 1, $h_num ] );
141 $fieldID->bind( "<Key-Right>" => [ \&move_focus, $w_num + 1, $h_num ] );
142 return;
143 }
144
145 # move focus to neighbouring sudoku field
146 # callback of the arrow keys
147 #
148 sub move_focus {
149 my ( $fieldID, $w_new, $h_new ) = @_;
150
151 $clickfield->withdraw; # make clickfield invisible
152 $w_new %= 9; # end-around
153 $h_new %= 9;
154 $sudoku_fields[ $w_new + 9 * $h_new ]->focus();
155 return;
156 }
157
158 # delete resp. replace sudoku digit
159 # callback of some keys (alpha or numeric keypad)
160 # also called from change_my_digit and read_edit
161 #
162 sub change_digit {
163 my ( $fieldID, $field_index, $digit_num ) = @_;
164
165 $clickfield->withdraw; # make clickfield invisible
166 if ($digit_num) {
167 # replace old digit
168 $sudoku_digits[$field_index] or $valuecount++;
169 $fieldID->configure( -text => $digit_num );
170 $sudoku_digits[$field_index] = $digit_num;
171 $valuecount == 1 and $edit_b->configure( -state => 'disabled' );
172 }
173 else {
174 # delete old digit
175 $fieldID->configure( -text => '' );
176 $sudoku_digits[$field_index] = undef;
177 $valuecount--;
178 $valuecount == 0 and $edit_b->configure( -state => 'normal' );
179 }
180 return;
181 }
182
183 sub create_clickfield {
184 $clickfield = $mw->Toplevel( -width => $fieldsize, -height => $fieldsize );
185 $clickfield->overrideredirect(1); # suppress window frame
186 foreach my $i ( 0 .. 2 ) {
187 foreach my $j ( 0 .. 2 ) {
188 create_tinysquare( $j, $i );
189 }
190 }
191 $clickfield->withdraw; # make clickfield invisible
192 return;
193 }
194
195 sub create_tinysquare {
196 my ( $w, $h ) = my ( $w_num, $h_num ) = @_; # pos. num.s of tiny square
197 $w *= $tinysize + 1; # pos. of tiny square
198 $h *= $tinysize + 1; #
199
200 my $space = $clickfield->Frame( -width => $tinysize, -height => $tinysize )
201 ->place( -x => $w, -y => $h );
202 $space->packPropagate(0); # prevent resizing the frame
203 my $tiny = $space->Button(
204 -relief => 'flat',
205 -background => 'black',
206 -command => [ \&change_my_digit, $w_num + 3 * $h_num + 1 ],
207 )->pack( -fill => 'both', -expand => 1 );
208 push( @tiny_fields, $tiny );
209 return;
210 }
211
212 # position the clickfield over the entered sudoku field
213 # callback of the <Enter> event
214 #
215 sub move_clickfield {
216 my ( $fieldID, $field_index ) = @_; # ID and index of button to be covered
217
218 # ignore re-entering the active field
219 # (this happens when withdrawing the clickfield)
220
221 # Color change and popup required when returning from the bottom row,
222 # so no return in this case
223 return
224 if ( $field_index == $activefield_index
225 and $clickfield->state eq 'normal' );
226
227 $activefield_index = $field_index;
228 $clickfield->withdraw; # make clickfield invisible
229
230 # mark the tiny square of the current digit by a different color
231 foreach my $tiny ( @tiny_fields[ 1 .. 9 ] ) {
232 $tiny->configure(
233 -background => 'black',
234 -activebackground => 'black'
235 );
236 }
237 if ( ( my $digit = $sudoku_digits[$activefield_index] ) ) {
238 $tiny_fields[$digit]->configure(
239 -background => 'red',
240 -activebackground => 'orange'
241 );
242 }
243 $clickfield->configure( -popover => $fieldID );
244 $clickfield->Popup(); # make clickfield visible
245 return;
246 }
247
248 # delete resp. replace old digit of the active sudoku field
249 # callback of the tiny squares
250 #
251 sub change_my_digit {
252 my $digit_num = shift; # digit of the clicked tiny square
253
254 my $fieldID = $sudoku_fields[$activefield_index];
255 my $olddigit = $sudoku_digits[$activefield_index] || '';
256 if ( $olddigit eq $digit_num ) { $digit_num = undef }
257 change_digit( $fieldID, $activefield_index, $digit_num );
258 return;
259 }
260
261 # write the entered sudoku puzzle to STDOUT
262 # callback of the 'Done' button
263 #
264 sub write_result {
265 # placeholder for unknown digits in sudoku output files
266 my $unknown_digit = '-';
267
268 my @alldigits = map( { defined $_ ? $_ : $unknown_digit } @sudoku_digits );
269 unless (-t) {
270 print @alldigits, "\n"; # output for sudoku trainer
271 return;
272 }
273 for ( my $pos = 0 ; $pos < $#sudoku_digits ; $pos += 9 ) {
274
275 # for better human readability
276 if ( $pos > 0 and $pos % 27 == 0 ) { print "\n" }
277 printf "%s%s%s %s%s%s %s%s%s\n", ( @alldigits[ $pos .. $pos + 8 ] );
278 }
279 return;
280 }
281
282 # read a sudoku puzzle for editing
283 # callback of the 'Edit puzzle' button
284 #
285 sub read_edit {
286 my $editname = $mw->getOpenFile;
287 use Encode;
288 $editname = encode( 'iso-8859-1', $editname );
289 return unless $editname;
290 open( my $edit, '<', $editname ) or die("can't open $editname: $!");
291 my @game = <$edit>;
292 close($edit);
293 while ( $game[0] =~ /^#/ ) { shift @game }; # ignore preceeding comment lines
294 my $gamestring = join( '', @game );
295 $gamestring =~ s/\n//g; # ignore newlines
296 # ignore whitespace
297 if ( length($gamestring) > 81 ) { $gamestring =~ s/\s//g }
298 $gamestring =~ tr/[1-9]/-/c; # convert all placeholders to '-'
299 @game = split( '', $gamestring );
300
301 # insert the puzzle values into the board
302 foreach my $field_index ( 0 .. 80 ) {
303 my $digit_num = $game[$field_index];
304 next if $digit_num eq '-';
305 change_digit( $sudoku_fields[$field_index], $field_index, $digit_num );
306 }
307 return;
308 }
309
310 __END__