Workaround mod_rewrite escaping bug.
[www.jankratochvil.net.git] / project / Nokia61 / Nokia61.php
1 <?php
2         if (!$HTTP_GET_VARS["nohtml"]) {
3         ?>
4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
5 <HTML>
6 <HEAD>
7 <TITLE>Nokia61</TITLE>
8 <LINK REV=MADE HREF="mailto:short@ucw.cz">
9 </HEAD><BODY>
10         <?php
11                 }
12
13 $tables_per_row=4;
14 $bgcolor_head="#D0D0D0";
15 $bgcolor_body="#F0F0F0";
16
17 function table($array,$rw=0)
18 {
19 global $tables,$bgcolor_head,$bgcolor_body,$HTTP_GET_VARS;
20
21         print "<FORM METHOD=GET ACTION=\"".$HTTP_GET_VARS["base"]."Nokia61.php\"><TABLE BORDER=1 RULES=NONE>\n";
22         print "<TR><TD COLSPAN=3 BGCOLOR=\"".$bgcolor_head."\">";
23         if ($rw)
24                 print "Input:";
25         else
26                 printf("Step #%d",$tables+1);
27         print "</TD></TR>\n";
28         for ($row=0;$row<3;$row++) {
29                 print "<TR>";
30                 for ($col=0;$col<3;$col++) {
31                         $idx=$row*3+$col;
32                         printf("<TD BGCOLOR=\"".$bgcolor_body."\"><INPUT NAME=\"%d\"",$idx);
33                         if (isset($array[$idx]))
34                                 printf(" VALUE=\"%d\"",$array[$idx]);
35                         if (!$rw)
36                                 print " READONLY";
37                         print " TYPE=STRING SIZE=2 MAXLENGTH=2></TD>";
38                         }
39                 print "</TR>\n";
40                 }
41         if ($rw)
42                 print "<TR><TD COLSPAN=3 ALIGN=CENTER><INPUT TYPE=SUBMIT VALUE=\"Solve!\"></TD></TR>\n";
43         print "</TABLE></FORM>\n";
44         $tables++;
45 }
46
47         $input=array();
48         $used=array();
49         $filled=0;
50         $tables=0;
51         
52         for ($idx=0;$idx<9;$idx++) {
53                 $s=$HTTP_GET_VARS[sprintf("%d",$idx)];
54                 if (!strlen($s))
55                         continue;
56                 $x=ord($s)-ord("0");
57                 if (strlen($s)!=1 || $x<1 || $x>9 || isset($used[$x])) {
58                         printf("<H1>WARNING: Digit at row %d, column %d was %s, erased!</H1>\n",1+$idx/3,1+$idx%3,
59                                         ($used[$x] ? "repeated" : "invalid"));
60                         continue;
61                         }
62                 $input[$idx]=$x;
63                 $used[$x]=1;
64                 $filled++;
65                 }
66         if ($filled==9) {
67                 print "<TABLE BORDER=0><TR>";
68                 $cmd="echo -n -e '";
69                 for ($row=0;$row<3;$row++) {
70                         for ($col=0;$col<3;$col++) {
71                                 if ($col)
72                                         $cmd.=" ";
73                                 $cmd.=sprintf("%d",$input[$row*3+$col]);
74                                 }
75                         $cmd.="\\n";
76                         }
77                 $cmd.="' | ./Nokia61";
78                 $cmd=popen($cmd,"r");
79                 $idx=0;
80                 $output=array();
81                 while (!feof($cmd)) {
82                         if (3!=fscanf($cmd,"\n  %d%d%d",&$output[$idx+0],&$output[$idx+1],&$output[$idx+2]))
83                                 continue;
84                         $idx+=3;
85                         if ($idx<9)
86                                 continue;
87                         print "<TD>";
88                         table($output);
89                         print "</TD><TD>&nbsp;</TD>";
90                         if (!($tables%$tables_per_row))
91                                 print "</TR><TR>";
92                         $idx=0;
93                         }
94                 pclose($cmd);
95                 print "</TR></TABLE>\n";
96                 print "<HR>\n";
97                 $input=array();
98                 }
99         table($input,1);
100
101         if (!$HTTP_GET_VARS["nohtml"]) {
102         ?>
103 </BODY></HTML>
104 <?php
105                 }
106 ?>