<?php

/*
 * Author: Francisco José García Rico
 * Created on 07-sep-2005
 * Filename: sudokusolver.php
 * 
 */

//Check the possible values for the blank cells, seeing only the values for its square
function check_square($row, $column, $minirow, $minicolumn) {
	//echo "Function check_square: ";
	$array_square = array();
	for ($i = 1; $i <= 3; $i ++)
		for ($j = 1; $j <= 3; $j ++){
			$value = $_POST["pSudoku_".$row."_".$column."_".$i."_".$j]; 
			if ($value != "")
				$array_square[$value] = -1;
		}

	return $array_square;
}

//Check the possible values for the blank cells, seeing only the values for its row
function check_row($row, $column, $minirow, $minicolumn){
	//echo "Function check row: ";
	$array_row = array();
	for ($i=1;$i<=3;$i++)
		for ($j = 1; $j <= 3; $j ++){
			$value = $_POST["pSudoku_".$row."_".$i."_".$minirow."_".$j]; 
			if ($value != "")
				$array_row[$value] = -1;
		}
	return $array_row;
} 

//Check the possible values for the blank cells, seeing only the values for its column
function check_column($row, $column, $minirow, $minicolumn){
	//echo "Function check column: ";
	$array_column = array();
	for ($i=1;$i<=3;$i++)
		for ($j = 1; $j <= 3; $j ++){
			$value = $_POST["pSudoku_".$i."_".$column."_".$j."_".$minicolumn]; 
			if ($value != "")
				$array_column[$value] = -1;
		}
	return $array_column;
}

//Check the values from 1 to 9 which aren't in the passed array
function other_values($array_values){
	$str_return;
	for ($i=1;$i<=9;$i++){
		if ($array_values[$i]!=-1)
			$str_return.= $i;
	}
	return $str_return;
}

//This function obtain the different values that can put in a blank cell
function blank_values($row,$column,$minirow,$minicolumn){
	$array_square = check_square($row,$column,$minirow,$minicolumn);
	$array_row = check_row($row,$column,$minirow,$minicolumn);
	$array_column = check_column($row,$column,$minirow,$minicolumn);
	$array_total = $array_square + $array_row + $array_column;
	return other_values($array_total);
}

echo "<html>";
echo "<head>";

echo "</head>";
echo "<body>";
echo "<form name=\"formi\" method=\"POST\" action=\"sudokusolver.php\">";
echo "<table width=\"300\" align=\"center\">";
for ($i = 1; $i <= 3; $i ++) {
	echo "<tr>";
	for ($j = 1; $j <= 3; $j ++) {
		echo "<td>";
		echo "<table align=\"center\" border=\"3\" bordercolor=\"#000000\">";
		for ($k = 1; $k <= 3; $k ++) {
			echo "<tr>";
			for ($l = 1; $l <= 3; $l ++) {
				echo "<td>";
				if (sizeof($_POST)>0){
					$value_cell = $_POST["pSudoku_".$i."_".$j."_".$k."_".$l];
					if ($value_cell=="" || strlen($value_cell)>1)
						$value_cell = blank_values($i,$j,$k,$l);
				}
				else
					$value_cell = "";
				echo "<input type=\"text\" name=\"pSudoku_".$i."_".$j."_".$k."_".$l."\" size=\"4\" value=\"$value_cell\">";
				echo "</td>";
			}
			echo "</tr>";
		}
		echo "</table>";
		echo "</td>";
	}
}
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"3\" align=\"center\"><input type=\"submit\" name=\"pCalculateSudoku\" value=\"SOLVE SUDOKU\"></td>";
echo "</tr>";
echo "</table>";
/*if (sizeof($_POST)>0){
	$array_square = check_square(1,1,1,2);
	//print_r($array_square);
	//echo "<br>";
	$array_row = check_row(1,1,1,2);
	//print_r($array_row);
	//echo "<br>";
	$array_column = check_column(1,1,1,2);
	//print_r($array_column);
	//echo "<br>";
	$array_total = $array_square + $array_row + $array_column;
	//print_r($array_total);
	echo "Possible values for the cell (1,1,1,2): ";
	other_values($array_total);
	echo "<br>";
}*/
echo "</form>";
echo "</body>";
echo "</html>";
?>

