sudokuarrayonedim = split(";",$string); $this->sudokuarray = 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)); // initialise array for($row = 0; $row < 9; $row++) { for($column = 0; $column < 9; $column++) { $this->sudokuarray[$row][$column] = $this->sudokuarrayonedim[((9*$row)+$column)]; } } // check if array has right length $this->valid = (count($this->sudokuarrayonedim) == 81); // echo("Count:" . count($this->sudokuarrayonedim) . "
"); // check if all numbers are in the range 1-9 if($this->valid) { for($row = 0; $row < 9; $row++) { for($column = 0; $column < 9; $column++) { if ($this->sudokuarray[$row][$column] < 0 || $this->sudokuarray[$row][$column] > 9) { $this->valid = false; } } } } else { throw new SudokuException('Can\'t create sudoku from string.'); } } // Constructor with Array function sudokuObjectFromArray($array) { // initialise array for($row = 0; $row < 9; $row++) { for($column = 0; $column < 9; $column++) { $this->sudokuarray[$row][$column] = $array[$row][$column]; } } $this->valid = true; } // Create a sudokuObject from Database function sudokuObjectFromDatabase($level) { try { // Connecting to database $db=new Database("mysql","mysql.foo.com","databasename","user","password"); // Switching to debug mode //$db->debug_mode(); // Making query $db->query("SELECT * FROM sudoku WHERE level = '".$level."' ORDER BY RAND()"); // Counting results and showing it //echo $db->count_rows(); // Printing results while($my_row=$db->get_row()){ $this->sudokuObjectFromString($my_row['string']); } } catch(Exception $e) { throw new SudokuException('Can\'t create sudoku from database.'); } } // returns a value of the array function getValue($row, $column) { return $this->sudokuarray[$row][$column]; } function autoFill() { $str = ""; $getal = "0"; for ($i=0; $i<9; $i++) { for ($j=0; $j<9; $j++) { $thiscount = 0; $suggestions = $this->calculateSuggestions($i,$j); for($getalc=0; $getalc<9; $getalc++) { if($suggestions[$getalc]) { $thiscount++; } } if($thiscount == 1) { $str = "".$j.",".$i; } } } $suggestions2 = $this->calculateSuggestions($str{2}, $str{0}); $counter = 0; for($counter=0; $counter<9; $counter++) { if($suggestions2[$counter]) { $getal = (1+$counter); } } $str2 = $str{0} . $str{2} . ";" . $getal; return $str2; } // returns a string of coordinates that only have one option left // eg: "(1,5);(1,8)" function nextSteps() { $str = ""; for ($i=0; $i<9; $i++) { for ($j=0; $j<9; $j++) { $thiscount = 0; $suggestions = $this->calculateSuggestions($i,$j); for($getalc=0; $getalc<9; $getalc++) { if($suggestions[$getalc]) { $thiscount++; } } if($thiscount == 1) { $str .= $j.$i.";"; } } } return $str; } // returns a string of suggested number for ($row, $column) // eg: "1, 2, 5" function suggestionString($row, $column) { $suggestions = $this->calculateSuggestions($row,$column); $numbers = array(); $str = ""; $count = 0; for($i=0; $i<9; $i++) { if($suggestions[$i] == true) { $numbers[$count] = (1+$i); $count++; } } return implode(", ", $numbers); } // returns a one dimensional array with 9 elements with // each element representing wheter that index-number // would be a valid entry for ($row, $column) function calculateSuggestions($row,$column) { $boxrow = $row-($row%3); $boxcolumn = $column-($column%3); $suggestions = array(true, true, true, true, true, true, true, true, true); if($this->sudokuarray[$row][$column] != 0) { // there is already a number at this place // no suggestions for($getal = 0; $getal < 9; $getal++) { $suggestions[$getal] = false; } } else { for($getal = 1; $getal <= 9; $getal++) { if($this->isInRow($row, $getal)) { $suggestions[$getal-1] = false; continue; } if($this->isInColumn($column, $getal)) { $suggestions[$getal-1] = false; continue; } if($this->isInBox($boxrow, $boxcolumn, $getal)) { $suggestions[$getal-1] = false; continue; } } } return $suggestions; } // checks if $number is in row number $row function isInRow($row, $number) { for($counter = 0; $counter < 9; $counter++) { if($this->sudokuarray[$row][$counter] == $number) { return true; } } return false; } // checks if $number is in column number $column function isInColumn($column, $number) { for($counter = 0; $counter < 9; $counter++) { if($this->sudokuarray[$counter][$column] == $number) { return true; } } return false; } // checks if $number is in the 3*3 box with top-left coordinates $boxrow and $boxcolumn function isInBox($boxrow, $boxcolumn, $number) { for($counter1 = $boxrow; $counter1 <= ($boxrow+2); $counter1++) { for($counter2 = $boxcolumn; $counter2 <= ($boxcolumn+2); $counter2++) { if($this->sudokuarray[$counter1][$counter2] == $number) { return true; } } } return false; } public function __get ($var) { switch ($var) { case 'sudokuarray': { $Value = $this->sudokuarray; break; } } return $Value; } public function __toString () { $answer = ""; $onedim = array(); for($row = 0; $row < 9; $row++) { $onedim[$row] = implode(";", $this->sudokuarray[$row]); } $answer = implode(";", $onedim); return $answer; } } ?>