blib/lib/Games/Sudoku/Html.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Games::Sudoku::Html;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '0.07';
7  
8 0 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 our @ISA = qw( Exporter );
11 our @EXPORT_OK = qw( sudoku_html_page );
12  
13 ###########################################################
14  
15 sub sudoku_html_page {
16 my ($puzzles) = @_;
17  
18 my $template = HTML::Template->new(
19 scalarref => page(),
20 loop_context_vars => 1,
21 );
22  
23 my @puzzle_list = map {
24 my $puzzle_string = $_->[0];
25 $puzzle_string =~ s/[^\.1-9]/\./g;
26 my $puzzle_parameter = $puzzle_string;
27 $puzzle_parameter =~ s/\./0/g;
28 {
29 strPuzzle => $puzzle_string,
30 properties => $_->[1],
31 paramBoard => $puzzle_parameter,
32 }
33 } @$puzzles;
34  
35 $template->param(
36 puzzleCount => ($#puzzle_list + 1),
37 puzzles => \@puzzle_list,
38 grid => [({sudokuBand => [({sudokuRow => [(+{}) x 9]}) x 3]}) x 3]
39 );
40  
41 return $template->output()
42 }
43  
44 sub page {
45 return \<<'PAGE_TEMPLATE'
46 <!DOCTYPE html>
47 <html lang="en">
48 <head>
49 <meta charset="UTF-8">
50 <meta name="viewport" content="width=device-width, initial-scale=1.0">
51 <meta name="description" content="Created with Perl-written sudoku generator Games::Sudoku::PatternSolver, available from https://metacpan.org/pod/Games::Sudoku::PatternSolver">
52  
53 <title><TMPL_VAR puzzleCount> Sudoku generated with PatternSolver</title>
54 <style>
55 #puzzlelist {
56 font-family: arial, sans-serif;
57 border-collapse: collapse;
58 }
59  
60 #puzzlelist td, th {
61 border: 1px solid #dddddd;
62 text-align: left;
63 padding: 8px;
64 }
65  
66 #puzzlelist tr:nth-child(even) {
67 background-color: #dddddd;
68 }
69  
70 .gridstring {
71 user-select: all;
72 }
73 </style>
74 </head>
75 <body>
76  
77 <script>
78 $ = function(id) {
79 return document.getElementById(id);
80 }
81  
82 /* timer display */
83 var timerVar;
84 var min = '00';
85 var sec = 0;
86  
87 function myTimer() {
88 sec++;
89 if (sec == 60) {
90 sec = 0;
91 min++;
92 if (min < 10) {
93 min = '0'+parseInt(min);
94 }
95 }
96 if (sec < 10) {
97 sec = '0'+sec;
98 }
99 $('timer').innerHTML = min+':'+sec;
100 }
101  
102 var hide = function(e, id) {
103 e.stopPropagation();
104 e.preventDefault();
105 $(id).style.display = 'none';
106 if (timerVar) clearInterval(timerVar);
107 }
108 </script>
109
110 <h2><TMPL_VAR puzzleCount> Sudoku generated with PatternSolver</h2>
111  
112 <p>Playing it on <a href="https://metacpan.org/author/GRANTM" target="_blank">Grant McLean&apos;s</a> web site offers thorough step-by-step help and a well-founded,
113 much better difficulty rating (after a click on the light bulb).<br/>
114 *However, playing puzzles with less than 8 different givens on sudokuexchenge, produces a warning and may even bring the javascript hint feature to crash.</p>
115 <p>The cursor can be moved around with the arrow keys, a stepwise undo is achieved with either escape or the backspace key.</p>
116  
117 <table id="puzzlelist">
118 <tr>
119 <th>#</th>
120 <th>Puzzle string</th>
121 <th>Properties</th>
122 <th>Play it</th>
123 </tr>
124 <TMPL_LOOP puzzles><tr id="row-<TMPL_VAR __counter__>">
125 <td><TMPL_VAR __counter__></td>
126 <td class="gridstring"><TMPL_VAR strPuzzle></td>
127 <td><TMPL_VAR properties ESCAPE="html"></td>
128 <td><a href="#" onclick="play(event, 'row-<TMPL_VAR __counter__>')">open here</a>, <a href="https://www.sudokuexchange.com/play/?s=<TMPL_VAR paramBoard>" target="_blank">play online</a></td>
129 </tr></TMPL_LOOP>
130 </table>
131  
132 <div class="popup" id="popup1">
133 <div id="popup1header" style="text-align:center;"><span id="timer" style="float:left;"></span><span id="puzzleid">Click here to move</span>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="continueOnline(event)">continue online</a><a href="#" onclick="hide(event, 'popup1')" style="float: right;">close</a></div>
134  
135 <script type="text/javascript">
136 /* partially based on Spreadsheet::HTML::Presets::Sudoku, Copyright 2017 Jeff Anderson */
137  
138 var MATRIX;
139 var STEPS;
140 var fillingivens = function(rowid, grid, playable) {
141 var board = $(rowid).querySelector('.gridstring').textContent;
142 var cells = grid.getElementsByTagName('td');
143 if (playable) {
144 MATRIX = new Array();
145 STEPS = new Array();
146 $('puzzleid').textContent = '# ' + rowid.match( /row-(\d+)/ )[1];
147 }
148 var i = 0;
149 for (var r = 0; r < 9; r++) {
150 var row = new Array();
151 for (var c = 0; c < 9; c++) {
152 var char = board.charAt(i);
153 var cell = cells[i];
154 cell.innerHTML = '';
155  
156 if (char == '.') {
157 char = '';
158 if (playable) {
159 const input = document.createElement('input');
160 input.setAttribute('id', 'input-' + r + '-' + c);
161 input.setAttribute('class', 'sudokuinput');
162 input.setAttribute('type', 'text');
163 input.setAttribute('size', '1');
164 input.setAttribute('maxLength', '1');
165 cell.appendChild(input);
166 }
167 } else {
168 cell.textContent = char;
169 }
170 row.push( char );
171 i++;
172 }
173 if (playable) {
174 MATRIX.push( row );
175 }
176 }
177 }
178  
179 function inputByCoords(row, col) {
180 var element = document.getElementById('input-'+row+'-'+col);
181 if (typeof(element) != 'undefined' && element != null) return element;
182  
183 return false;
184 }
185  
186 function puzzlecomplete() {
187 for (var r = 0; r < 9; r++) {
188 for (var c = 0; c < 9; c++) {
189 if (! MATRIX[r][c]) return false;
190 }
191 }
192 return true;
193 }
194  
195 // reverse input stepwise if ESC gets pressed
196 document.getElementById('popup1').addEventListener('keydown', function (event) {
197 var key = event.key;
198 if (key == 'Escape' || key == 'Backspace') {
199 var target = STEPS.pop();
200 if (! target) return 0;
201  
202 var targetid = target.id;
203 var result = targetid.match(/input-(\d)-(\d)/);
204  
205 target.value = '';
206 MATRIX[result[1]][result[2]] = '';
207 }
208 });
209 var play = function(e, rowid) {
210 e = e || window.event;
211 e.preventDefault();
212 if (timerVar) clearInterval(timerVar);
213  
214 fillingivens(rowid, $('sudoku'), true);
215 $('popup1').style.display ='block';
216  
217 var inputs = $('sudoku').getElementsByClassName('sudokuinput');
218 for (var i=0; i<inputs.length; i++) {
219 var input = inputs[i];
220  
221 input.addEventListener('keydown', function (event) {
222 var matches = this.id.match( /(\d+)-(\d+)/ );
223 var id_r = matches[1];
224 var id_c = matches[2];
225  
226 var key = event.key;
227 var target;
228  
229 if (key == 'ArrowRight') {
230 var c = parseInt(id_c) + 1;
231 while ((! target) && (c != id_c)) {
232 if (c > 8) c = 0;
233 target = inputByCoords(id_r, c);
234 c++;
235 }
236 } else if (key == 'ArrowDown') {
237 var r = parseInt(id_r) + 1;
238 while ((! target) && (r != id_r)) {
239 if (r > 8) r = 0;
240 target = inputByCoords(r, id_c);
241 r++;
242 }
243 } else if (key == 'ArrowLeft') {
244 var c = parseInt(id_c) - 1;
245 while ((! target) && (c != id_c)) {
246 if (c < 0) c = 8;
247 target = inputByCoords(id_r, c);
248 c--;
249 }
250 } else if (key == 'ArrowUp') {
251 var r = parseInt(id_r) - 1;
252 while ((! target) && (r != id_r)) {
253 if (r < 0) r = 8;
254 target = inputByCoords(r, id_c);
255 r--;
256 }
257 } else {
258 return;
259 }
260  
261 if (target) {
262 target.focus();
263 target.setSelectionRange(1, 1);
264 }
265 });
266  
267 input.addEventListener('keyup', function (event) {
268 this.value = this.value.replace( /[^1-9]/g, '' ).slice(0, 1);
269 var matches = this.id.match( /(\d+)-(\d+)/ );
270 var id_r = matches[1];
271 var id_c = matches[2];
272  
273 if (MATRIX[id_r][id_c] == this.value) return;
274  
275 if (this.value.length == 1) {
276 var boxstart_r = parseInt(id_r / 3) * 3;
277 var boxstart_c = parseInt(id_c / 3) * 3;
278  
279 var seen= {};
280  
281 for (var r = boxstart_r; r < boxstart_r + 3; r++) {
282 for (var c = boxstart_c; c < boxstart_c + 3; c++) {
283 seen[ MATRIX[r][c] ] = true;
284 }
285 }
286  
287 for (var i = 0; i < 9; i++) {
288 seen[ MATRIX[id_r][i] ] = true;
289 seen[ MATRIX[i][id_c] ] = true;
290 }
291  
292 if (seen[this.value]) {
293 this.value = '';
294 }
295  
296 if (MATRIX[id_r][id_c] != this.value) {
297 MATRIX[id_r][id_c] = this.value;
298 if (this.value) {
299 STEPS.push( this );
300 }
301 }
302  
303 if (puzzlecomplete()) {
304 $('timer').style.color = 'green';
305 if (timerVar) {
306 clearInterval(timerVar);
307 timerVar = null;
308 }
309 }
310 }
311 });
312 }
313  
314 min = '00';
315 sec = 0;
316 $('timer').style.color = '';
317 $('timer').innerHTML = '00:00';
318 timerVar = setInterval(myTimer, 1000);
319 }
320  
321 /* draggable code from w3schools.com */
322 dragElement(document.getElementById('popup1'));
323  
324 function dragElement(elmnt) {
325 var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
326  
327 if (document.getElementById(elmnt.id + 'header')) {
328 document.getElementById(elmnt.id + 'header').onmousedown = dragMouseDown;
329 } else {
330 elmnt.onmousedown = dragMouseDown;
331 }
332  
333 function dragMouseDown(e) {
334 e = e || window.event;
335 if (e.target !== e.currentTarget) {
336 return;
337 }
338 e.preventDefault();
339 // get the mouse cursor position at start
340 pos3 = e.clientX;
341 pos4 = e.clientY;
342 document.onmouseup = closeDragElement;
343 document.onmousemove = elementDrag;
344 }
345  
346 function elementDrag(e) {
347 e = e || window.event;
348 e.preventDefault();
349 pos1 = pos3 - e.clientX;
350 pos2 = pos4 - e.clientY;
351 pos3 = e.clientX;
352 pos4 = e.clientY;
353 elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
354 elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
355 }
356  
357 function closeDragElement() {
358 // stop moving when button is released
359 document.onmouseup = null;
360 document.onmousemove = null;
361 }
362 }
363  
364 function continueOnline(e) {
365 e = e || window.event;
366 e.preventDefault();
367 var url = 'https://www.sudokuexchange.com/play/?s=';
368 for (var r = 0; r < 9; r++) {
369 var row = new Array();
370 for (var c = 0; c < 9; c++) {
371 url = url + (MATRIX[r][c] || '0');
372 }
373 }
374 window.open(url);
375 }
376 </script>
377  
378 <style>
379 .popup {
380 padding: 10px;
381 background: #fff;
382 z-index: 20;
383 }
384  
385 #popup1 {
386 -webkit-box-shadow: 0px 0px 0px 9999px rgba(0, 0, 0, 0.5);
387 box-shadow: 0px 0px 0px 9999px rgba(0, 0, 0, 0.5);
388 display: none;
389 position: fixed;
390 left: 65%;
391 top: 10%;
392 }
393 #popup1header {
394 padding: 10px;
395 cursor: move;
396 z-index: 10;
397 background-color: #aaa;
398 color: #fff;
399 }
400 #popup1 table {
401 border-collapse: collapse;
402 font-family: arial, sans-serif;
403 font-weight: bold;
404 font-size: xx-large;
405 text-align: center;
406 }
407 #popup1 colgroup {
408 border: solid medium;
409 }
410 #popup1 tbody {
411 border: solid medium;
412 }
413 #popup1 td {
414 border: solid thin;
415 width: 50px;
416 height: 50px;
417 color: #aaa;
418 }
419 #popup1 input {
420 border: 0px;
421 max-width: 35px;
422 font-family: arial, sans-serif;
423 font-weight: bold;
424 font-size: xx-large;
425 text-align: center;
426 }
427 </style>
428  
429 <table id="sudoku">
430 <colgroup><col/><col/><col/></colgroup>
431 <colgroup><col/><col/><col/></colgroup>
432 <colgroup><col/><col/><col/></colgroup>
433 <TMPL_LOOP grid><tbody>
434 <TMPL_LOOP sudokuBand><tr><TMPL_LOOP sudokuRow><td/></TMPL_LOOP></tr>
435 </TMPL_LOOP></tbody></TMPL_LOOP>
436 </table>
437 </div>
438  
439 <div class="popup" id="popup2">
440 <script type="text/javascript">
441 const puzzlecells = document.querySelectorAll('.gridstring');
442 puzzlecells.forEach(cell => {
443 cell.addEventListener('mouseenter', function (e) {showgrid(e, cell.parentElement.id)});
444 cell.addEventListener('mouseleave', function (e) {$('popup2').style.display = 'none';});
445 });
446  
447 function showgrid(e, rowid) {
448 e = e || window.event;
449 if (e.target !== e.currentTarget) {
450 return;
451 }
452 e.preventDefault();
453 fillingivens(rowid, $('grid'), false);
454 $('popup2').style.display = 'block';
455 }
456 </script>
457 <style>
458 #popup2 {
459 padding: 3px;
460 background: #fff;
461 /* -webkit-box-shadow: 0px 0px 0px 9999px rgba(0, 0, 0, 0.5);
462 box-shadow: 0px 0px 0px 9999px rgba(0, 0, 0, 0.5); */
463 display: none;
464 position: fixed;
465 left: 35%;
466 top: 10%;
467 z-index: 15;
468 }
469 #popup2 table {
470 border-collapse: collapse;
471 font-family: arial, sans-serif;
472 font-weight: bold;
473 font-size: large;
474 text-align: center;
475 }
476 #popup2 colgroup {
477 border: solid 2px;
478 }
479 #popup2 tbody {
480 border: solid 2px;
481 }
482 #popup2 td {
483 border: solid thin;
484 width: 20px;
485 height: 20px;
486 color: #aaa;
487 }
488 </style>
489  
490 <table id="grid">
491 <colgroup><col/><col/><col/></colgroup>
492 <colgroup><col/><col/><col/></colgroup>
493 <colgroup><col/><col/><col/></colgroup>
494 <TMPL_LOOP grid><tbody>
495 <TMPL_LOOP sudokuBand><tr><TMPL_LOOP sudokuRow><td/></TMPL_LOOP></tr>
496 </TMPL_LOOP></tbody></TMPL_LOOP>
497 </table>
498 </div>
499 </body>
500 </html>
501 PAGE_TEMPLATE
502 }
503  
504 1;
505  
506 =encoding UTF-8
507  
508 =head1 NAME
509  
510 Games::Sudoku::Html - Visualize and play collections of standard 9x9 Sudoku in your browser.
511  
512 =head1 DESCRIPTION
513  
514 A very simple Module which converts an array with sudoku puzzles into a static html page.
515 Thus, long lists of your digital (text format) sudoku can be revised and played in a browser.
516  
517 Currently only the standard sudoku are supported, no variants.
518  
519 =head1 SYNOPSIS
520  
521 use Games::Sudoku::PatternSolver::Html qw( sudoku_html_page );
522  
523 @list = ( [$sudokuHash->{strPuzzle}, "<descriptive text with properties, rating, source, etc.>"], ... )
524 $htmlpage = sudoku_html_page( \@list )
525  
526 =head2 sudoku_html_page( arrayref )
527  
528 Returns a scalar with a static html page, playable in a browser
529  
530 Its only parameter is an array of arrays, each entry consisting of 2 items: an 81 character puzzle string and any descriptive text for the puzzle.
531 You may put as many puzzles as you like into the array, they can all be played from the one, javascript-driven static page.
532 Can also be used to play lists with numerous 9x9 Sudoku from various sources.
533  
534 =head1 SCRIPTS
535  
536 =head2 sudoku2html
537  
538 After installation of Games::Sudoku::Html this command line script should be in your path.
539 Flexible input options are available. Invoke C<E<gt>sudoku2html -h> for details.
540  
541 =head1 DEPENDENCIES
542  
543 =over 4
544  
545 =item * L<HTML::Template>
546  
547 =back
548  
549 =head1 SEE ALSO
550  
551 L<Games::Sudoku::PatternSolver::Generator>, L<Games::Sudoku::Pdf>
552  
553 =head1 COPYRIGHT AND LICENSE
554  
555 The following copyright notice applies to all the files provided in
556 this distribution, including binary files, unless explicitly noted
557 otherwise.
558  
559 This software is copyright (c) 2024 by Steffen Heinrich
560  
561 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
562  
563 =cut
↑ Summary