Example #1
0
<?php

$sudoku = Sudoku::createFromFile();
include 'view.php';
class Sudoku
{
    const N = 8;
    protected $inputSudoku = [];
    protected $outputSudoku = [];
    public $result = [];
    public $cells = [];
    public $points = [];
    public $debug = [];
    public function __construct($sudoku)
    {
        $this->inputSudoku = $this->outputSudoku = $sudoku;
        $this->init();
        $this->calculate();
    }
    public function getOutputSudoku()
    {
        return $this->outputSudoku;
    }
    public static function createFromFile()
    {
        $lines = file('sudoku.data');
        $sudoku = [];
        foreach ($lines as $line) {
            $sudoku[] = array_map('intval', explode('   ', $line));
        }
        return new static($sudoku);
Example #2
0
        return true;
    }
    /**
     * validate
     *
     * Checks all conditions for a valid sudoku
     *
     * @access public
     * @param none
     * @return 1 or 0
     */
    public function validate()
    {
        if (!$this->convert_and_validate_rows()) {
            return 0;
        }
        if (!$this->validate_columns()) {
            return 0;
        }
        if (!$this->validate_regions()) {
            return 0;
        }
        return 1;
    }
}
// Read file contents
$puzzle = file_get_contents('puzzle.txt');
// Create sudoku
$su = new Sudoku($puzzle);
// Check if its valid or not
print $su->validate();
function create(&$si, &$sp, $level = 1)
{
    for ($i = 1; $i <= 40; $i++) {
        //set_time_limit( 30);
        $sp = new Sudoku();
        $theInitialPosition = $sp->generatePuzzle(10, 50, $level);
        if (count($theInitialPosition)) {
            break;
        }
    }
    if ($i > 40) {
        return false;
    }
    $si = new Sudoku();
    $si->initializePuzzleFromArray($theInitialPosition);
    return true;
}
<?php

include dirname(__FILE__) . '/sudoku.php';
include dirname(__FILE__) . '/conf-sudoku.php';
$sudo = new Sudoku($tableHardHard1);
$sudo->show();
$sudo->explain();
$sudo->show();
Example #5
0
<?php

require_once '../Sudoku.php';
// $puzzle = "003020600900305001001806400008102900700000008006708200002609500800203009005010300";
// $puzzle = "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......";
// $puzzle = "3...8.......7....51..............36...2..4....7...........6.13..452...........8..";
// $puzzle = "6.....7.3.4.8.................5.4.8.7..2.....1.3.......2.....5.....7.9......1....";
// $puzzle = "963......1....8......2.5....4.8......1....7......3..257......3...9.2.4.7......9..";
// $puzzle = "..7..8.....6.2.3...3......9.1..5..6.....1.....7.9....2........4.83..4...26....51.";
$puzzle = "1.....3.8.6.4..............2.3.1...........958.........5.6...7.....8.2...4.......";
$sudoku = new Sudoku();
echo $sudoku->solve($puzzle);
Example #6
0
<?php

require_once 'sudoku.php';
$grid = array(array(0, 1, 9, 0, 6, 0, 0, 8, 5), array(0, 0, 0, 0, 3, 4, 0, 0, 0), array(0, 2, 0, 5, 0, 0, 1, 0, 0), array(0, 0, 8, 0, 5, 0, 0, 1, 3), array(5, 3, 7, 0, 9, 0, 4, 2, 6), array(2, 6, 0, 0, 4, 0, 9, 0, 0), array(0, 0, 4, 0, 0, 5, 0, 3, 0), array(0, 0, 0, 3, 7, 0, 0, 0, 0), array(8, 5, 0, 0, 1, 0, 2, 7, 0));
$action = filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
if (isset($action)) {
    $sudoku = new Sudoku($grid);
    switch ($action) {
        case 'display':
            echo json_encode($grid);
            break;
        case 'solve':
            echo json_encode($sudoku->solution());
            break;
        default:
            echo json_encode($grid);
    }
}
     * This function validate number of rows should be 9 and number of coloum should be 9
     * @author Pankaj Kumar
     */
    private function checkSuokuPattern()
    {
        $file = fopen($this->_fileName, "r");
        $noOfRows = 0;
        $noOfCols = 0;
        $colArr = array();
        while ($c = fgetc($file)) {
            if (in_array($c, array("\n"))) {
                // To check new line
                $noOfRows++;
                $colArr[] = $noOfCols;
                // Store all numbers of columns
                $noOfCols = 0;
            } else {
                $noOfCols++;
            }
        }
        if ($noOfRows == 9 && count(array_unique($colArr)) == 1 && $colArr[0] == 9) {
            // Possible condition to check  Suduko board is valid or not
            echo $this->_fileName . ': 1<br>';
        } else {
            echo $this->_fileName . ': 0<br>';
        }
        fclose($file);
    }
}
$objSudoku = new Sudoku('files');
$objSudoku->checkSudoku();
Example #8
0
$file = fopen($filename, 'r');
if (!$file) {
    throw new \Exception('Could not open file ' . $filename);
}
$sudokuStrs = array();
$i = -1;
while (($line = fgets($file)) !== false) {
    if (false !== strpos($line, 'Grid')) {
        $i++;
        continue;
    }
    $line = str_replace("\n", "", $line);
    if (!isset($sudokuStrs[$i])) {
        $sudokuStrs[$i] = '';
    }
    $sudokuStrs[$i] .= $line;
}
fclose($file);
$now = microtime(true);
$sum = 0;
foreach ($sudokuStrs as $sudokuStr) {
    $sudoku = new Sudoku($sudokuStr);
    // var_dump($sudokuStr);
    $answer = $sudoku->solve();
    // echo $solvedSudokuStr . '<br/>';
    $sum += intval(substr($answer, 0, 3));
    unset($sudoku);
}
$tookTime = microtime(true) - $now;
echo "\n\nSum: " . $sum . "\n";
echo "took time: " . $tookTime . " secs.\n";