Merge pull request #2 from gnieark/dev

resolve all
This commit is contained in:
Gnieark 2015-11-18 23:40:26 +01:00
commit 990bea9167
4 changed files with 37 additions and 40 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
//separer les map //separer les map
$grids=explode("\n",str_replace(array("+-----------------+\n","|"),"",file_get_contents("easy.txt"))); $grids=explode("\n",str_replace(array("+-----------------+\n","|"),"",file_get_contents("easy.txt")."\n".file_get_contents("medium.txt")."\n".file_get_contents("hard.txt")));
$grid=array(); $grid=array();
foreach($grids as $gridLine){ foreach($grids as $gridLine){
$lineArray=array(); $lineArray=array();
@ -9,54 +9,50 @@ foreach($grids as $gridLine){
} }
$grid[]=$lineArray; $grid[]=$lineArray;
if(count($grid)==9){ if(count($grid)==9){
//viewGrid($grid);
viewGrid(resolveSudoku($grid)); viewGrid(resolveSudoku($grid));
$grid=array(); $grid=array();
} }
} }
function resolveSudokuByBruteforce($grille){ function resolveSudokuByBruteforce($grille){
$possib=array();
//se placer sur la première case non remplie //se placer sur la première case non remplie
for ($y=0;$y<9;$y++){ $x=0;
for($x=0;$x<9;$x++){ $y=0;
if($grille[$y][$x]==" "){ for($i=0; (($grille[$y][$x]<>" ")&&($i<81));$i++){
//$possib[$x."-".$y]=array(); $y=(int)($i/9);
//enregistrer toutes les possibilités sur cette case $x=fmod($i,9);
}
if(($i==81)&&($grille[8][8]<>" ")){
//la grille est déjà pleine
return $grille;
}
//enregistrer toutes les possibilités sur cette case
$canPlay=false;
for($k=1;$k<10;$k++){
if(canBePlaced($k,$x,$y,$grille)){
//$possib[$x."-".$y][]=$k;
$canPlay=true;
$grilleTemp=$grille;
$grilleTemp[$y][$x]=$k;
if($gr=resolveSudokuByBruteforce($grilleTemp)){
return $gr;
}else{
//en fait, non , on ne peut pas jouer là
$canPlay=false; $canPlay=false;
for($k=1;$k<10;$k++){
if(canBePlaced($k,$x,$y,$grille)){
//$possib[$x."-".$y][]=$k;
$canPlay=true;
$grilleTemp=$grille;
$grilleTemp[$y][$x]=$k;
if($gr=resolveSudokuByBruteforce($grilleTemp)){
return $gr;
}else{
//en fait, non , on ne peut pas jouer là
$canPlay=false;
}
}
}
if(!$canPlay){
return false;
}
} }
} }
} }
/*
if(count($possib==1)){ if(!$canPlay){
//y a qu'une case à jouer // echo "CAN'T:".$x." ".$y."\n";
list($x,$y)=explode("-",array_keys($possib)[0]); //viewGrid($grille);
$grilleTemp[$y][$x]=$possib[$x."-".$y][0]; return false;
return $grilleTemp;
} }
*/
} }
function resolveSudoku($grille){ function resolveSudoku($grille){
@ -78,6 +74,7 @@ function resolveSudoku($grille){
if($grid=resolveSudokuByBruteforce($grille)){ if($grid=resolveSudokuByBruteforce($grille)){
return $grid; return $grid;
}else{ }else{
return $grille; return $grille;
} }
} }