Teka teki solver
January 5th, 2009
Ini dulu gue bikin waktu lagi isenk di kantor, adu cepet2an pecahin teka teki silang, akhirnya karena kalah melulu gue kesel, terus bikin programnya buat pecahin teka teki silang otomatis. Emang engga guna
tapi sapa tau suatu saat berguna.
<html>
<head><title>Crozz Puzzle - Problem Solver</title></head>
<body onLoad="frmSolver.txtFind.focus();frmSolver.txtFind.select()">
<form name="frmSolver" method="GET" action="crossSolverFixedMulti.php">
Soal : <br>
<textarea name="txtSoal" cols="30" rows="12"><?echo $txtSoal?></textarea><br>
Kata yang dicari :<br>
<input type="text" name="txtFind" value="<?echo $txtFind?>">
<input type="submit" value="Cari">
</form>
<br>
Hasil : <br>
<table cellpadding="10"><tr>
<?
// Pecahin soalnya jadi array of char
$soal = trim($_GET["txtSoal"]);
$verti = 0; $hori = 0; $totalchar = 0;
for($a = 0 ; $a < strlen($soal) ; $a++) {
if (ord($soal[$a]) != 13 && ord($soal[$a]) != 10) {
$chara[] = $soal[$a];
$warna[] = 0;
$verti++;
$totalchar++;
}
else {
$a++;
$verti = 0;
$hori++;
}
}
$hori++;
$find = $_GET["txtFind"];
$maxLevel = strlen($find);
// Proses $warna
function process($x, $y, $level, $arah) {
global $chara, $verti, $hori, $totalchar, $maxLevel, $find, $warna;
$pos = ($y * $verti) + $x;
if ($level == $maxLevel) {
return true; // Ketemu nich berarti
}
if ($x < 0 || $x >= $verti) {
return false; // Melebihi batas X
}
if ($y < 0 || $y >= $hori) {
return false; // Melebihi batas Y
}
if ($chara[$pos] != $find[$level]) { // Huruf di posisi ini salah :D
return false;
}
$found = 0;
if ($arah == 1 || $arah == 99) if (process($x + 1 , $y , $level + 1, $arah)) $found++; // Kanan
if ($arah == 2 || $arah == 99) if (process($x + 1 , $y + 1, $level + 1, $arah)) $found++; // Kanan Bawah
if ($arah == 3 || $arah == 99) if (process($x + 1 , $y - 1, $level + 1, $arah)) $found++; // Kanan Atas
if ($arah == 4 || $arah == 99) if (process($x , $y + 1, $level + 1, $arah)) $found++; // Bawah
if ($arah == 5 || $arah == 99) if (process($x , $y - 1, $level + 1, $arah)) $found++; // Atas
if ($arah == 6 || $arah == 99) if (process($x - 1 , $y , $level + 1, $arah)) $found++; // Kiri
if ($arah == 7 || $arah == 99) if (process($x - 1 , $y + 1, $level + 1, $arah)) $found++; // Kiri Bawah
if ($arah == 8 || $arah == 99) if (process($x - 1 , $y - 1, $level + 1, $arah)) $found++; // Kiri Atas
if ($found > 0) {
$warna[$pos] = 1;
return true;
}
else {
return false;
}
}
function cetakHasil() {
global $warna, $chara, $verti, $hori;
static $counter = 0;
if ($counter % 5 == 0 && $counter > 0) echo "</tr><tr>";
echo "<td style='border: 1px solid #FFCCFF;'><gantijadipre>";
for ($y = 0 ; $y < $hori ; $y++) {
for ($x = 0 ; $x < $verti ; $x++) {
$pos = ($y * $verti) + $x;
if ($warna[$pos] == 1) {
echo "<font color='red'>" . $chara[$pos] . "</font>";
}
else {
echo "<font color='black'>" . $chara[$pos] . "</font>";
}
}
echo "\n";
}
echo "</gantijadipre></td>";
$counter++;
flush();
}
function resetWarna() {
global $warna, $verti , $hori;
for ($y = 0 ; $y < $hori ; $y++) {
for ($x = 0 ; $x < $verti ; $x++) {
$pos = ($y * $verti) + $x;
$warna[$pos] = 0;
}
}
}
for ($a = 0 ; $a < $totalchar ; $a++) {
if (process($a % $verti , floor($a / $verti) , 0 , 1)) {cetakHasil();resetWarna();};
if (process($a % $verti , floor($a / $verti) , 0 , 2)) {cetakHasil();resetWarna();};
if (process($a % $verti , floor($a / $verti) , 0 , 3)) {cetakHasil();resetWarna();};
if (process($a % $verti , floor($a / $verti) , 0 , 4)) {cetakHasil();resetWarna();};
if (process($a % $verti , floor($a / $verti) , 0 , 5)) {cetakHasil();resetWarna();};
if (process($a % $verti , floor($a / $verti) , 0 , 6)) {cetakHasil();resetWarna();};
if (process($a % $verti , floor($a / $verti) , 0 , 7)) {cetakHasil();resetWarna();};
if (process($a % $verti , floor($a / $verti) , 0 , 8)) {cetakHasil();resetWarna();};
}
?>
</tr></table>
</body>
</html>