check unknown grid
This commit is contained in:
parent
58c50f0e1d
commit
e1cd9ca1a2
63
classes.php
63
classes.php
@ -52,8 +52,8 @@
|
|||||||
$this->columns = array_fill(0, 9, array());
|
$this->columns = array_fill(0, 9, array());
|
||||||
$this->regions = array_fill(0, 9, array());
|
$this->regions = array_fill(0, 9, array());
|
||||||
for ($regionRowId = 0; $regionRowId < 3; $regionRowId++) {
|
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++) {
|
for ($columnId = 3*$regionColumnId; $columnId < 3*($regionColumnId+1); $columnId++) {
|
||||||
$regionId = 3*$regionRowId + $regionColumnId;
|
$regionId = 3*$regionRowId + $regionColumnId;
|
||||||
$box = new Box($rowId, $columnId, $regionId);
|
$box = new Box($rowId, $columnId, $regionId);
|
||||||
@ -74,13 +74,36 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
function generate() {
|
||||||
// Init with a shuffle row
|
// Init with a shuffle row
|
||||||
$values = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
|
$values = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
|
||||||
shuffle($values);
|
shuffle($values);
|
||||||
forEach($values as $columnId => $value) {
|
forEach($this->rows[0] as $columnId => $box) {
|
||||||
$box = $this->rows[0][$columnId];
|
$box->value = $values[$columnId];
|
||||||
$box->value = $value;
|
|
||||||
forEach($box->neighbourhood as $neighbour)
|
forEach($box->neighbourhood as $neighbour)
|
||||||
array_unset_value($box->value, $neighbour->candidates);
|
array_unset_value($box->value, $neighbour->candidates);
|
||||||
}
|
}
|
||||||
@ -145,16 +168,9 @@
|
|||||||
if ($randomized) shuffle($testBox->candidates);
|
if ($randomized) shuffle($testBox->candidates);
|
||||||
$stop = null;
|
$stop = null;
|
||||||
foreach($testBox->candidates as $testBox->value) {
|
foreach($testBox->candidates as $testBox->value) {
|
||||||
$correctGrid = true;
|
|
||||||
foreach(array_filter($testBox->neighbourhood, "isUnknown") as $neighbour)
|
foreach(array_filter($testBox->neighbourhood, "isUnknown") as $neighbour)
|
||||||
$neighbour->candidateRemoved[] = array_unset_value($testBox->value, $neighbour->candidates);
|
$neighbour->candidateRemoved[] = array_unset_value($testBox->value, $neighbour->candidates);
|
||||||
foreach(array_filter($testBox->neighbourhood, "isUnknown") as $neighbour) {
|
if ($this->candidatesOnEachUnknownBoxeOf($testBox->neighbourhood)) {
|
||||||
if (count($neighbour->candidates) == 0) {
|
|
||||||
$correctGrid = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($correctGrid) {
|
|
||||||
$solutions = $this->solutionsGenerator($randomized);
|
$solutions = $this->solutionsGenerator($randomized);
|
||||||
foreach($solutions as $solution) {
|
foreach($solutions as $solution) {
|
||||||
$stop = (yield $solution);
|
$stop = (yield $solution);
|
||||||
@ -171,10 +187,33 @@
|
|||||||
}
|
}
|
||||||
$testBox->value = UNKNOWN;
|
$testBox->value = UNKNOWN;
|
||||||
} else {
|
} 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();
|
yield $this->toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function candidatesOnEachUnknownBoxeOf($area) {
|
||||||
|
foreach($area as $box) {
|
||||||
|
if (($box->value == UNKNOWN) && (count($box->candidates) == 0)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
function toString() {
|
function toString() {
|
||||||
$str = "";
|
$str = "";
|
||||||
foreach($this->rows as $row) {
|
foreach($this->rows as $row) {
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
session_start();
|
session_start();
|
||||||
$grid = new Grid();
|
$grid = new Grid();
|
||||||
$grid->generate();
|
$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();
|
exit();
|
||||||
?>
|
?>
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
|
if ($_SESSION["currentGrid"])
|
||||||
$currentGrid = $_SESSION["currentGrid"];
|
$currentGrid = $_SESSION["currentGrid"];
|
||||||
|
else
|
||||||
|
$currentGrid = ".";
|
||||||
?>
|
?>
|
||||||
{
|
{
|
||||||
"short_name": "Sudoku",
|
"short_name": "Sudoku",
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
|
if ($_SESSION["currentGrid"])
|
||||||
$currentGrid = $_SESSION["currentGrid"];
|
$currentGrid = $_SESSION["currentGrid"];
|
||||||
|
else
|
||||||
|
$currentGrid = ".";
|
||||||
header ("Content-type: application/javascript");
|
header ("Content-type: application/javascript");
|
||||||
?>
|
?>
|
||||||
/*
|
/*
|
||||||
|
25
sudoku.php
25
sudoku.php
@ -3,6 +3,24 @@
|
|||||||
session_start();
|
session_start();
|
||||||
$currentGrid = strip_tags($_GET['grid']);
|
$currentGrid = strip_tags($_GET['grid']);
|
||||||
$_SESSION["currentGrid"] = $currentGrid;
|
$_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>
|
<!DOCTYPE html>
|
||||||
<html lang='fr' prefix="og: https://ogp.me/ns#">
|
<html lang='fr' prefix="og: https://ogp.me/ns#">
|
||||||
@ -95,7 +113,12 @@
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<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>
|
</section>
|
||||||
<ul id="contextMenu" class="context-menu"></ul>
|
<ul id="contextMenu" class="context-menu"></ul>
|
||||||
<footer>
|
<footer>
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
require("classes.php");
|
require("classes.php");
|
||||||
session_start();
|
session_start();
|
||||||
|
if ($_SESSION["currentGrid"])
|
||||||
$currentGrid = $_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");
|
header ("Content-type: image/png");
|
||||||
$size = (int) $_GET['size'];
|
$size = (int) $_GET['size'];
|
||||||
|
|
||||||
$thumbnail = imagecreate($size, $size);
|
$thumbnail = imagecreate($size, $size);
|
||||||
$transparent = imagecolorallocate($thumbnail, 1, 1, 1);
|
$transparent = imagecolorallocate($thumbnail, 1, 1, 1);
|
||||||
imagecolortransparent($thumbnail, $transparent);
|
imagecolortransparent($thumbnail, $transparent);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user