Location: PHPKode > scripts > Draw Sudoku > draw-sudoku/drawSudokuExSudoku.class.php
<?php
/**
 * @author Can Ince <hide@address.com>
 * @copyright copyright @ 2006 by Can Ince
 * @license GNU/LGPL
 * @version 0.0.1
 * @package drawSudoku
 * ! extended from Richard Munroe's DM-Sudoku class. Richard uses a modified NetBSD license
 * ! http://www.csworks.com/publications/ModifiedNetBSD.html
 * ! DM-Sudoku class can be found at http://www.phpclasses.org/browse/package/2674.html
 */

require_once("class.Sudoku.php");
class drawSudoku extends Sudoku {
	var $tableContent,
	    $script,
	    $diagramArray;
		
	/**
	Constructor function
	*/
	
	function drawSudoku() {
	$this->Sudoku();
	}

	/**
	Draw the sudoku table. The output is static; always the same.
	*/
	
	function sudokuTable() {
	$tableContent= "\n<table class=\"board\" cellspacing=\"0\" cellpadding=\"0\">";
	for ($z=1;$z<10;$z++) {
		$tableContent.= "\n<tr>";
		for ($n=1;$n<10;$n++) {
			$tableContent.= "\n<td id=td$z$n"; 
			if(($n/3)==intval($n/3) && $n!=9) {
				if(($z/3)==intval($z/3)){
					$tableContent.= " class=\"topRightLine\"";
					} else {
					$tableContent.= " class=\"rightLine\"";
					}
				}
			elseif(($z/3)==intval($z/3)) {
			$tableContent.= " class=\"topLine\"";
				}
			$tableContent.= "><input type=\"text\" class=\"cell\" id=\"cell$z$n\" size=\"1\" maxlength=\"1\" autocomplete=\"off\"></td>";
 			} 
		$tableContent.= "\n</tr>";
		}
 	$tableContent.= "\n</table>";
	$this->tableContent= $tableContent;
	}

	/**
	Print the sudoku table.
	*/

	function drawTable($diagramArray) {
	$this->sudokuTable();
	print $this->tableContent;
	$this->generateScript($diagramArray);
	print $this->script;
	}

	/**
	Generate the javascript used to fill the empty table.
	@param array $diagramArray : Array containing the diagram to be printed 
	*/

	function generateScript($diagramArray) {
		$script="\n<script language=\"javascript\">";
		foreach ($diagramArray as $numberPosition) {
		$y=$numberPosition[0];
		$x=$numberPosition[1];
		$value=$numberPosition[2];
		$script.= "\ndocument.getElementById('cell$y$x').value='$value';";
		}
	$script.="\n</script>";
	$this->script=$script;
	}

	/**
	Load the diagram from a textfile.
	@param resource $handle : Handle to the file containing the diagram notation.
	*/

	function loadDiagramArrayFromFile($handle) {
	$handle = @fopen($handle, "r");
	if ($handle) {
  		 	while (!feof($handle)) {
       			$buffer = str_replace("\n","",fgets($handle, 4096));
			$diagramArray[]=explode(" ",$buffer);
   			}
   	fclose($handle);
	$this->diagramArray=$diagramArray;
		}
	}
	
	/**
	Alias for loadDiagramArrayFromFile().
	@param resource $handle : Handle to the file containing the diagram notation.
	*/

	function loadDiagram($handle) {
	return $this->loadDiagramArrayFromFile($handle);
	}

}
?>
Return current item: Draw Sudoku