docs
[mirrors/Programs.git] / php / skripty / sudoku_web.php
CommitLineData
8de51304
H
1<form action="?" method="POST">\r
2<textarea rows="9" cols="9" name="sudokupost">43 6 5 89\r
356 23\r
4 2 3 \r
57 5 6 9 4\r
6 9 1 \r
72 9 7 5 1\r
8 4 8 \r
932 56\r
1089 5 6 12</textarea>\r
11<br /><input type="submit" value="Vyresit">\r
12</form>\r
13\r
14<?php\r
15//Sudoku\r
16\r
17//\r
18function sudoku_print ($sudo) {\r
19 $sudo = str_replace(" ", ".", implode("\n", $sudo) );\r
20 $sudo = explode("\n", $sudo);\r
21 print_r($sudo);\r
22}\r
23\r
24//X line\r
25//Y row\r
26\r
27//\r
28function sudoku_pos ($sudo, $x, $y) {\r
29 return($sudo[$x-1][$y-1]);\r
30}\r
31//\r
32function sudoku_row ($sudo, $y) {\r
33 $row = "";\r
34 foreach ($sudo as $line) {\r
35 $row = ($row.$line[$y-1]);\r
36 }\r
37 return $row;\r
38}\r
39//\r
40function sudoku_line ($sudo, $x) {\r
41 return $sudo[$x-1];\r
42}\r
43//\r
44function sudoku_square ($sudo, $x, $y) {\r
45 if ( $x == 1 || $x == 2 || $x == 3 ) { $x = 1; }\r
46 if ( $x == 4 || $x == 5 || $x == 6 ) { $x = 4; }\r
47 if ( $x == 7 || $x == 8 || $x == 9 ) { $x = 7; }\r
48 if ( $y == 1 || $y == 2 || $y == 3 ) { $y = 1; }\r
49 if ( $y == 4 || $y == 5 || $y == 6 ) { $y = 4; }\r
50 if ( $y == 7 || $y == 8 || $y == 9 ) { $y = 7; }\r
51 //echo("\n$x\n$y\n");\r
52 $square = "";\r
53 $x--;\r
54 $y--;\r
55 $y0 = $y;\r
56 \r
57 $i2 = 0;\r
58 while( $i2 < 3 ){\r
59 $i1 = 0;\r
60 $y = $y0;\r
61 while ( $i1 < 3 ){\r
62 $square = $square.$sudo[$x][$y];\r
63 $y++;\r
64 $i1++;\r
65 //echo("\n$x-$y");\r
66 }\r
67 $i2++;\r
68 $x++;\r
69 }\r
70 //echo("\n".$square);\r
71 return ($square);\r
72}\r
73\r
74//\r
75function sudoku_candidates ($sudo, $x, $y) {\r
76 $cands = "123456789";\r
77 $donenums = ( sudoku_line($sudo, $x).sudoku_row($sudo, $y).sudoku_square($sudo, $x, $y) );\r
78 //echo $donenums;\r
79 //echo "\n\n";\r
80 //foreach ( $donenums as $del ) {\r
81 $indx = strlen($donenums)-1;\r
82 while ($indx >= 0) {\r
83 $del = $donenums[$indx];\r
84 //echo("!".$del);\r
85 $cands = str_replace($del, "", $cands);\r
86 $indx--;\r
87 }\r
88 //echo "\n\n".$cands;\r
89 return $cands;\r
90}\r
91\r
92//\r
93function sudoku_compute($sudoku) {\r
94\r
95 $sudoku_old = "";\r
96 while( $sudoku_old != $sudoku ) {\r
97 $sudoku_old = $sudoku;\r
98\r
99 $is = sizeof($sudoku)-1;\r
100 while ($is >= 0) {\r
101 $line = $sudoku[$is];\r
102 \r
103 $i = strlen($line)-1;\r
104 while ($i >= 0) {\r
105 //echo($line[$i]);\r
106 \r
107 $cands = sudoku_candidates ( $sudoku, ($is+1), ($i+1) );\r
108 if ( strlen($cands) == 1 && !is_numeric($sudoku[$is][$i]) ) {\r
109 $sudoku[$is][$i] = $cands;\r
110 }\r
111 \r
112 $i--;\r
113 }\r
114 //echo("\n");\r
115 \r
116 $is--;\r
117 }\r
118 }\r
119 return($sudoku);\r
120}\r
121\r
122///CODE///////////////////////////////////////////\r
123/*\r
124$sudoku = array(\r
125 "43 6 5 89",\r
126 "56 23",\r
127 " 2 3 ",\r
128 "7 5 6 9 4",\r
129 " 9 1 ",\r
130 "2 9 7 5 1",\r
131 " 4 8 ",\r
132 "32 56",\r
133 "89 5 6 12"\r
134 );\r
135*/\r
136\r
137sudoku = explode ("\n",$_POST["sudokupost"]);\r
138\r
139\r
140echo("Zadani:\n");\r
141sudoku_print($sudoku);\r
142echo("\nVysledek:\n");\r
143sudoku_print(sudoku_compute($sudoku));\r
144\r
145?>\r
This page took 1.107323 seconds and 5 git commands to generate.