SuDoKu
Source
<?php
function tausche_zeilen($arr, $zeile1, $zeile2){
$temp = $arr[$zeile1];
$arr[$zeile1] = $arr[$zeile2];
$arr[$zeile2] = $temp;
return $arr;
}
function tausche_spalten($arr, $spalte1, $spalte2){
for($z=0; $z<9; $z++){
$temp = $arr[$z][$spalte1];
$arr[$z][$spalte1] = $arr[$z][$spalte2];
$arr[$z][$spalte2] = $temp;
}
return $arr;
}
function rotation($arr){
/*
[0][0] -> [0][8]
[0][1] -> [1][8]
[0][2] -> [2][8]
[0][3] -> [3][8]
[0][4] -> [4][8]
[0][5] -> [5][8]
[0][6] -> [6][8]
[0][7] -> [7][8]
[0][8] -> [8][8]
[1][0] -> [0][7]
[1][1] -> [1][7]
[1][2] -> [2][7]
[1][3] -> [3][7]
[1][4] -> [4][7]
[1][5] -> [5][7]
[1][6] -> [6][7]
[1][7] -> [7][7]
[1][8] -> [8][7]
*/
$res=array(
array(0,0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0,0),
array(0,0,0,0,0,0,0,0,0)
);
for($z=0; $z < 9; $z++){
for($s=0; $s < 9; $s++){
$res[$s][8-$z] = $arr[$z][$s];
}
}
return $res;
}
function stanzmuster(){
$muster[0] = array(1,0,0,0,0,0,0,0,0);
$muster[1] = array(1,1,0,0,0,0,0,0,0);
$muster[2] = array(1,1,1,0,0,0,0,0,0);
$muster[3] = array(1,1,1,1,0,0,0,0,0);
$muster[4] = array(1,1,1,1,1,0,0,0,0);
$muster[5] = array(1,1,1,1,1,1,0,0,0);
$muster[6] = array(1,1,1,1,1,1,1,0,0);
$muster[7] = array(1,1,1,1,1,1,1,1,0);
foreach($muster as &$arr){
srand((float) microtime() * 10000000);
shuffle($arr);
}
return $muster;
}
function stanze($arr, $lv){
$lv+=3;
for($a=0;$a<9;$a+=3){
for($b=0;$b<9;$b+=3){
$sm = stanzmuster(); $e=0; $k=rand($lv, $lv-3);
for($c=0+$a;$c<3+$a;$c++){
for($d=0+$b;$d<3+$b;$d++){
$arr[$c][$d] = ($sm[$k][$e++])?$arr[$c][$d]:0;/**/
}
}
}
}
return $arr;
}
//Grund-Sudoku-Raster festlegen
$sudoku_raster=array(
array(1,2,3,4,5,6,7,8,9),
array(7,8,9,1,2,3,4,5,6),
array(4,5,6,7,8,9,1,2,3),
array(9,1,2,3,4,5,6,7,8),
array(6,7,8,9,1,2,3,4,5),
array(3,4,5,6,7,8,9,1,2),
array(8,9,1,2,3,4,5,6,7),
array(5,6,7,8,9,1,2,3,4),
array(2,3,4,5,6,7,8,9,1));
//for($z=0; $z<9; $z++){
// for($s=0; $s<9; $s++){
// echo $sudoku_raster[$z][$s]." ";
// }
// echo "<br />";
//}
// Zeile 0 mit Zeile 2 tauschen
//$sudoku_raster[0][...] mit $sudoku_raster[2][...] tauschen
// 5 mal im 1. block tauschen
for($i=0; $i<5; $i++){
$z1 = rand(0, 2);
$z2 = rand(0, 2);
$sudoku_raster = tausche_zeilen($sudoku_raster, $z1, $z2);
}
// 5 mal im 2. block tauschen
for($i=0; $i<5; $i++){
$z1 = rand(3, 5);
$z2 = rand(3, 5);
$sudoku_raster = tausche_zeilen($sudoku_raster, $z1, $z2);
}
// 5 mal im 3. block tauschen
for($i=0; $i<5; $i++){
$z1 = rand(6, 8);
$z2 = rand(6, 8);
$sudoku_raster = tausche_zeilen($sudoku_raster, $z1, $z2);
}
/* echo "<hr />";
for($z=0; $z<9; $z++){
for($s=0; $s<9; $s++){
echo $sudoku_raster[$z][$s]." ";
}
echo "<br />";
}
*/
// Spiegeln an 5. Zeile
if( rand(0, 1) == 1){
$sudoku_raster = tausche_zeilen($sudoku_raster, 0, 8);
$sudoku_raster = tausche_zeilen($sudoku_raster, 1, 7);
$sudoku_raster = tausche_zeilen($sudoku_raster, 2, 6);
$sudoku_raster = tausche_zeilen($sudoku_raster, 3, 5);
}
/*
echo "<hr />";
for($z=0; $z<9; $z++){
for($s=0; $s<9; $s++){
echo $sudoku_raster[$z][$s]." ";
}
echo "<br />";
}
*/
// 5 mal Tausch Spalte im 1. Block
for($i=0; $i<5; $i++){
$s1 = rand(0, 2);
$s2 = rand(0, 2);
$sudoku_raster = tausche_spalten($sudoku_raster, $s1, $s2);
}
// 5 mal Tausch Spalte im 2. Block
for($i=0; $i<5; $i++){
$s1 = rand(3, 5);
$s2 = rand(3, 5);
$sudoku_raster = tausche_spalten($sudoku_raster, $s1, $s2);
}
// 5 mal Tausch Spalte im 3. Block
for($i=0; $i<5; $i++){
$s1 = rand(6, 8);
$s2 = rand(6, 8);
$sudoku_raster = tausche_spalten($sudoku_raster, $s1, $s2);
}
/*
echo "<hr />";
for($z=0; $z<9; $z++){
for($s=0; $s<9; $s++){
echo $sudoku_raster[$z][$s]." ";
}
echo "<br />";
}
*/
// Spiegelung an 5. Spalte
if( rand(0, 1) ){
for($i=0; $i<4; $i++){
$sudoku_raster = tausche_spalten($sudoku_raster, $i, 8-$i);
}
}
//echo "<hr />";
//for($z=0; $z<9; $z++){
// for($s=0; $s<9; $s++){
// echo $sudoku_raster[$z][$s]." ";
// }
// echo "<br />";
//}
$anz = rand(0, 3);
for($i=0; $i <= $anz;$i++){
$sudoku_raster = rotation($sudoku_raster);
}
//echo "<hr />";
//for($z=0; $z<9; $z++){
// for($s=0; $s<9; $s++){
// echo $sudoku_raster[$z][$s]." ";
// }
// echo "<br />";
//}
// in jedem Block Zahlen löschen (1 - 8)
$lv = 4;
$sudoku_raster = stanze($sudoku_raster, $lv);
/*
echo "<hr />";
for($z=0; $z<9; $z++){
for($s=0; $s<9; $s++){
if($sudoku_raster[$z][$s] == 0){
echo " ";
}
else{
echo $sudoku_raster[$z][$s]." ";
}
}
echo "<br />";
}
*/
// ab hier dann die Ausgabe! für html
?>
<!DOCTYPE html>
<html>
<head>
<title>2711 internes SuDoKu</title>
<style>
input{
width:46px;
height:46px;
border:1px solid #000;
background-color:#994BFF;
text-align:center;
}
#sudoku td{
width:50px;
height:50px;
text-align:center;
}
</style>
</head>
<body>
<h1>SuDoKu<a name=SuDoKu> </a></h1>
<table id=sudoku border="2" cellspacing="3" cellpadding="2">
<?php for($z=0; $z<9; $z++): ?><tr>
<?php for($s=0; $s<9; $s++): ?><td>
<?php echo ($sudoku_raster[$z][$s])?$sudoku_raster[$z][$s]:'<input type="text" value="">'; ?>
</td><?php endfor; ?>
</tr><?php endfor; ?>
</table>
<hr>
<h2>Source</h2>
<div>
<?php echo highlight_file(__file__, true); ?>
</div>
</body>
</html>