patternphpMinor
Sudoku solving class in PHP
Viewed 0 times
phpsolvingsudokuclass
Problem
Here is my PHP class for solving Sudoku:
GitHub
Sudoku
Sudoku Solver
```
* @package Solver
* @subpackage Sudoku
* @version 0.1
*/
/**
* Sudoku Solver class
*
* This class solves the sudoku in my own logic.
*
* This solver takes time to execute according to the
* complexity of the sudoku.
*
* @author Anush Prem
* @package Solver
* @subpackage Sudoku
* @version 0.1
*/
Class SudokuSolver{
/**
* To store the input Sudoku
* @access private
* @var array $_input row == column mapping
*/
private $_input;
/**
* To store the currently solved sudoku at any moment of time
* @access private
* @var array $_currentSudoku row == column mapping
*/
private $_currentSudoku;
/**
* To store the probable values for each cell
* @access private
* @var array $_probable [row][col] == possible values array mapping
*/
private $_probable;
/**
* to store weather the sudoku h
GitHub
Sudoku
* @package Solver
* @subpackage Sudoku
* @version 0.1
*/
/**
* Required Class files
*/
include_once "SudokuSolver.class.php";
// The application could take longer than normal php execution
// time. So set the execution time limit to 0(unlimited).
set_time_limit(0);
// input sudoku array in the format row == col array mapping
$sudoku = array(
array(0,4,0,0,5,3,1,0,2),
array(2,0,8,1,0,0,7,0,0),
array(5,0,1,4,2,0,6,0,0),
array(8,1,4,0,3,0,2,0,7),
array(0,6,0,2,0,5,0,1,9),
array(0,5,0,7,4,0,0,6,3),
array(0,0,0,0,7,4,5,8,1),
array(1,8,5,9,0,2,0,0,0),
array(4,0,3,0,0,8,0,2,6)
);
// create an object of SudokuSolver.
$solver = new SudokuSolver();
// Pass the input sudoku to the $solver object.
$solver -> input ($sudoku);
// Solve the sudoku and return the solved sudoku.
$solved = $solver -> solve ();
// printing the formated input sudoku
print "Input Sudoku:";
foreach ($sudoku as $row){
foreach ($row as $col ){
print $col . " ";
}
print "";
}
print "";
// printing the formated solved sudoku.
print "Solved Sudoku:";
foreach ($solved as $row){
foreach ($row as $col ){
print $col . " ";
}
print "";
}
?>Sudoku Solver
```
* @package Solver
* @subpackage Sudoku
* @version 0.1
*/
/**
* Sudoku Solver class
*
* This class solves the sudoku in my own logic.
*
* This solver takes time to execute according to the
* complexity of the sudoku.
*
* @author Anush Prem
* @package Solver
* @subpackage Sudoku
* @version 0.1
*/
Class SudokuSolver{
/**
* To store the input Sudoku
* @access private
* @var array $_input row == column mapping
*/
private $_input;
/**
* To store the currently solved sudoku at any moment of time
* @access private
* @var array $_currentSudoku row == column mapping
*/
private $_currentSudoku;
/**
* To store the probable values for each cell
* @access private
* @var array $_probable [row][col] == possible values array mapping
*/
private $_probable;
/**
* to store weather the sudoku h
Solution
Here's a few comments on the code rather than the algorithm - I'll leave that to someone else :)
Commenting
-
Quite a few comments repeat what the code does. For example:
For loops
Input
How come you've separated input and solve? Is there any case where you'd want to call one without the other? What happens if you call solve when there isn't any input given? A better interface in my opinion would be:
_checkAllSolved
Would this be better returning a boolean? You could then get rid of the
Hope that helps.
Commenting
-
Quite a few comments repeat what the code does. For example:
// initially set the $probable as array 1 to 9.
$probable = range(1,9);- There are a few spelling/grammatical mistakes in your comments and function names
- Weather should be whether.
- completly should be completely
- confilicting should be conflicting
- _findPosibilites() should be _findPossibilities()
For loops
- for loops in _findAllProbables and _updateSolved don't have braces. I think it'd be more readable and less liable to introducing bugs if you put braces in; especially on nested for loops.
Input
How come you've separated input and solve? Is there any case where you'd want to call one without the other? What happens if you call solve when there isn't any input given? A better interface in my opinion would be:
solve($input)_checkAllSolved
Would this be better returning a boolean? You could then get rid of the
$this->_solved I think. You should certainly return earlier on ln. 268. In fact wouldn't it be better to just store the number you have solved?Hope that helps.
Code Snippets
// initially set the $probable as array 1 to 9.
$probable = range(1,9);solve($input)Context
StackExchange Code Review Q#1489, answer score: 4
Revisions (0)
No revisions yet.