check unknown grid
This commit is contained in:
parent
58c50f0e1d
commit
e1cd9ca1a2
65
classes.php
65
classes.php
@ -52,8 +52,8 @@
|
||||
$this->columns = array_fill(0, 9, array());
|
||||
$this->regions = array_fill(0, 9, array());
|
||||
for ($regionRowId = 0; $regionRowId < 3; $regionRowId++) {
|
||||
for($regionColumnId = 0; $regionColumnId < 3; $regionColumnId++) {
|
||||
for ($rowId = 3*$regionRowId; $rowId < 3*($regionRowId+1); $rowId++) {
|
||||
for ($rowId = 3*$regionRowId; $rowId < 3*($regionRowId+1); $rowId++) {
|
||||
for($regionColumnId = 0; $regionColumnId < 3; $regionColumnId++) {
|
||||
for ($columnId = 3*$regionColumnId; $columnId < 3*($regionColumnId+1); $columnId++) {
|
||||
$regionId = 3*$regionRowId + $regionColumnId;
|
||||
$box = new Box($rowId, $columnId, $regionId);
|
||||
@ -73,14 +73,37 @@
|
||||
$box->neighbourhood[] = $neighbour;
|
||||
}
|
||||
}
|
||||
|
||||
function import($gridStr) {
|
||||
foreach ($this->boxes as $i => $box) {
|
||||
$box->value = $gridStr[$i];
|
||||
}
|
||||
forEach($this->boxes as $box) {
|
||||
forEach($box->neighbourhood as $neighbour)
|
||||
array_unset_value($box->value, $neighbour->candidates);
|
||||
}
|
||||
}
|
||||
|
||||
function containsDuplicates() {
|
||||
foreach(array_merge($this->rows, $this->columns, $this->regions) as $area) {
|
||||
$unknownBoxes = array_filter($area, "isUnknown");
|
||||
foreach($unknownBoxes as $box1) {
|
||||
foreach($unknownBoxes as $box2) {
|
||||
if (($box1 !== $box2) && ($box1->value == $box2->value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function generate() {
|
||||
// Init with a shuffle row
|
||||
$values = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
|
||||
shuffle($values);
|
||||
forEach($values as $columnId => $value) {
|
||||
$box = $this->rows[0][$columnId];
|
||||
$box->value = $value;
|
||||
forEach($this->rows[0] as $columnId => $box) {
|
||||
$box->value = $values[$columnId];
|
||||
forEach($box->neighbourhood as $neighbour)
|
||||
array_unset_value($box->value, $neighbour->candidates);
|
||||
}
|
||||
@ -145,16 +168,9 @@
|
||||
if ($randomized) shuffle($testBox->candidates);
|
||||
$stop = null;
|
||||
foreach($testBox->candidates as $testBox->value) {
|
||||
$correctGrid = true;
|
||||
foreach(array_filter($testBox->neighbourhood, "isUnknown") as $neighbour)
|
||||
$neighbour->candidateRemoved[] = array_unset_value($testBox->value, $neighbour->candidates);
|
||||
foreach(array_filter($testBox->neighbourhood, "isUnknown") as $neighbour) {
|
||||
if (count($neighbour->candidates) == 0) {
|
||||
$correctGrid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($correctGrid) {
|
||||
if ($this->candidatesOnEachUnknownBoxeOf($testBox->neighbourhood)) {
|
||||
$solutions = $this->solutionsGenerator($randomized);
|
||||
foreach($solutions as $solution) {
|
||||
$stop = (yield $solution);
|
||||
@ -171,9 +187,32 @@
|
||||
}
|
||||
$testBox->value = UNKNOWN;
|
||||
} else {
|
||||
foreach(array($this->rows, $this->columns, $this->regions) as $areas) {
|
||||
foreach ($areas as $area) {
|
||||
foreach($area as $box1) {
|
||||
if (($box1->value == UNKNOWN) && (count($box1->candidates) == 0)) {
|
||||
return;
|
||||
}
|
||||
foreach($area as $box2) {
|
||||
if (($box1 !== $box2) && ($box1->value != UNKNOWN) && ($box1->value == $box2->value)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
yield $this->toString();
|
||||
}
|
||||
}
|
||||
|
||||
function candidatesOnEachUnknownBoxeOf($area) {
|
||||
foreach($area as $box) {
|
||||
if (($box->value == UNKNOWN) && (count($box->candidates) == 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function toString() {
|
||||
$str = "";
|
||||
|
@ -3,6 +3,8 @@
|
||||
session_start();
|
||||
$grid = new Grid();
|
||||
$grid->generate();
|
||||
header("Location: ".$_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].dirname($_SERVER["DOCUMENT_URI"])."/".$grid->toString());
|
||||
$currentGrid = $grid->toString();
|
||||
$_SESSION[$currentGrid] = "checked";
|
||||
header("Location: ".$_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"].dirname($_SERVER["DOCUMENT_URI"])."/".$currentGrid);
|
||||
exit();
|
||||
?>
|
||||
|
@ -1,6 +1,9 @@
|
||||
<?php
|
||||
session_start();
|
||||
$currentGrid = $_SESSION["currentGrid"];
|
||||
if ($_SESSION["currentGrid"])
|
||||
$currentGrid = $_SESSION["currentGrid"];
|
||||
else
|
||||
$currentGrid = ".";
|
||||
?>
|
||||
{
|
||||
"short_name": "Sudoku",
|
||||
|
@ -1,6 +1,9 @@
|
||||
<?php
|
||||
session_start();
|
||||
$currentGrid = $_SESSION["currentGrid"];
|
||||
if ($_SESSION["currentGrid"])
|
||||
$currentGrid = $_SESSION["currentGrid"];
|
||||
else
|
||||
$currentGrid = ".";
|
||||
header ("Content-type: application/javascript");
|
||||
?>
|
||||
/*
|
||||
|
25
sudoku.php
25
sudoku.php
@ -3,6 +3,24 @@
|
||||
session_start();
|
||||
$currentGrid = strip_tags($_GET['grid']);
|
||||
$_SESSION["currentGrid"] = $currentGrid;
|
||||
|
||||
if (!isset($_SESSION[$currentGrid])) {
|
||||
$grid = new Grid();
|
||||
$grid->import($currentGrid);
|
||||
if ($grid->containsDuplicates()) {
|
||||
$warning = "Cette grille contient des doublons.";
|
||||
} else {
|
||||
switch($grid->countSolutions(2)) {
|
||||
case 0:
|
||||
$warning = "Cette grille n'a pas de solution.";
|
||||
break;
|
||||
case 1:
|
||||
break;
|
||||
default:
|
||||
$warning = "Cette grille a plusieurs solutions.";
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang='fr' prefix="og: https://ogp.me/ns#">
|
||||
@ -95,7 +113,12 @@
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
Remplissez la grille de sorte que chaque ligne, colonne et région (carré de 3×3 cases) contienne tous les chiffres de 1 à 9.
|
||||
<?php
|
||||
if (isset($warning))
|
||||
echo(" <strong>⚠️ $warning</strong><br/>\n");
|
||||
else
|
||||
echo(" Remplissez la grille de sorte que chaque ligne, colonne et région (carré de 3×3 cases) contienne tous les chiffres de 1 à 9.\n")
|
||||
?>
|
||||
</section>
|
||||
<ul id="contextMenu" class="context-menu"></ul>
|
||||
<footer>
|
||||
|
@ -1,9 +1,13 @@
|
||||
<?php
|
||||
require("classes.php");
|
||||
session_start();
|
||||
$currentGrid = $_SESSION["currentGrid"];
|
||||
if ($_SESSION["currentGrid"])
|
||||
$currentGrid = $_SESSION["currentGrid"];
|
||||
else
|
||||
$currentGrid = ".528.3....4.9.1...39.562......73.129...1.64.7...42.3656.13.5...28.6.4...4.5287...w";
|
||||
header ("Content-type: image/png");
|
||||
$size = (int) $_GET['size'];
|
||||
|
||||
$thumbnail = imagecreate($size, $size);
|
||||
$transparent = imagecolorallocate($thumbnail, 1, 1, 1);
|
||||
imagecolortransparent($thumbnail, $transparent);
|
||||
|
Loading…
x
Reference in New Issue
Block a user