remove french and useless comments. rename vars
This commit is contained in:
parent
d4525cece3
commit
8b52f7cb0f
85
plop.php
85
plop.php
|
@ -5,25 +5,25 @@
|
||||||
* Gnieark 2015
|
* Gnieark 2015
|
||||||
* licensed under the Do What the Fuck You Want to Public License http://www.wtfpl.net/
|
* licensed under the Do What the Fuck You Want to Public License http://www.wtfpl.net/
|
||||||
*/
|
*/
|
||||||
function scoreCases($grid,$myChar,$hisChar,$profondeur=0){
|
function scoreCases($grid,$myChar,$hisChar,$depth=0){
|
||||||
//lister les cases vides de la grille
|
//list empty cells
|
||||||
foreach($grid as $key => $case){
|
foreach($grid as $key => $cell){
|
||||||
$tempGrid=$grid;
|
$tempGrid=$grid;
|
||||||
$tempGrid[$key]=$myChar;
|
$tempGrid[$key]=$myChar;
|
||||||
if($case==""){ //on ne "score que les cases qui sont libres
|
if($cell==""){
|
||||||
//tester si je gagne en jouant là
|
//do i win if i play there?
|
||||||
if(isGridWin($tempGrid)){
|
if(isGridWin($tempGrid)){
|
||||||
if(fmod($profondeur,2)==0){
|
if(fmod($depth,2)==0){
|
||||||
$scores[$key]=10-$profondeur;
|
$scores[$key]=10-$depth;
|
||||||
}else{
|
}else{
|
||||||
$scores[$key]=$profondeur-10;
|
$scores[$key]=$depth-10;
|
||||||
}
|
}
|
||||||
}elseif(nbFreeCases($tempGrid)==0){
|
}elseif(nbFreeCases($tempGrid)==0){
|
||||||
$scores[$key]=0;
|
$scores[$key]=0;
|
||||||
}else{
|
}else{
|
||||||
//trouver le plus grand score dans l'array plus profond en valeur absolue
|
//find the beast score
|
||||||
$scores[$key]=0;
|
$scores[$key]=0;
|
||||||
$scoresDeeper=scoreCases($tempGrid,$hisChar,$myChar,$profondeur+1);
|
$scoresDeeper=scoreCases($tempGrid,$hisChar,$myChar,$depth+1);
|
||||||
foreach($scoresDeeper as $scD){
|
foreach($scoresDeeper as $scD){
|
||||||
if(abs($scD)>abs($scores[$key])){
|
if(abs($scD)>abs($scores[$key])){
|
||||||
$scores[$key]=$scD;
|
$scores[$key]=$scD;
|
||||||
|
@ -32,76 +32,73 @@ function scoreCases($grid,$myChar,$hisChar,$profondeur=0){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//print_r($scores);print_r($grid);echo "------------";
|
|
||||||
return $scores;
|
return $scores;
|
||||||
}
|
}
|
||||||
function nbFreeCases($grille){
|
function nbFreeCases($map){
|
||||||
$nb=0;
|
$nb=0;
|
||||||
foreach($grille as $case){
|
foreach($map as $cell){
|
||||||
if ($case==""){
|
if ($cell==""){
|
||||||
$nb++;
|
$nb++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $nb;
|
return $nb;
|
||||||
}
|
}
|
||||||
function isGridWin($grille){
|
function isGridWin($map){
|
||||||
if(
|
if(
|
||||||
(($grille['0-0']==$grille['0-1'])&&($grille['0-1']==$grille['0-2'])&&($grille['0-2']!==""))
|
(($map['0-0']==$map['0-1'])&&($map['0-1']==$map['0-2'])&&($map['0-2']!==""))
|
||||||
OR (($grille['1-0']==$grille['1-1'])&&($grille['1-1']==$grille['1-2'])&&($grille['1-2']!==""))
|
OR (($map['1-0']==$map['1-1'])&&($map['1-1']==$map['1-2'])&&($map['1-2']!==""))
|
||||||
OR (($grille['2-0']==$grille['2-1'])&&($grille['2-1']==$grille['2-2'])&&($grille['2-2']!==""))
|
OR (($map['2-0']==$map['2-1'])&&($map['2-1']==$map['2-2'])&&($map['2-2']!==""))
|
||||||
OR (($grille['0-0']==$grille['1-0'])&&($grille['1-0']==$grille['2-0'])&&($grille['2-0']!==""))
|
OR (($map['0-0']==$map['1-0'])&&($map['1-0']==$map['2-0'])&&($map['2-0']!==""))
|
||||||
OR (($grille['0-1']==$grille['1-1'])&&($grille['1-1']==$grille['2-1'])&&($grille['2-1']!==""))
|
OR (($map['0-1']==$map['1-1'])&&($map['1-1']==$map['2-1'])&&($map['2-1']!==""))
|
||||||
OR (($grille['0-2']==$grille['1-2'])&&($grille['1-2']==$grille['2-2'])&&($grille['2-2']!==""))
|
OR (($map['0-2']==$map['1-2'])&&($map['1-2']==$map['2-2'])&&($map['2-2']!==""))
|
||||||
OR (($grille['0-0']==$grille['1-1'])&&($grille['1-1']==$grille['2-2'])&&($grille['2-2']!==""))
|
OR (($map['0-0']==$map['1-1'])&&($map['1-1']==$map['2-2'])&&($map['2-2']!==""))
|
||||||
OR (($grille['0-2']==$grille['1-1'])&&($grille['1-1']==$grille['2-0'])&&($grille['2-0']!==""))
|
OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!==""))
|
||||||
){
|
){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
$cells=array("0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2");
|
||||||
|
|
||||||
$cases=array("0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2");
|
|
||||||
//filling array
|
//filling array
|
||||||
$freeCases=array();
|
$emptyCells=array();
|
||||||
foreach($cases as $case){
|
foreach($cells as $cell){
|
||||||
if (!isset($_GET[$case])){
|
if (!isset($_GET[$cell])){
|
||||||
echo "wrong parameters ".$case; die;
|
echo "wrong parameters ".$cell; die;
|
||||||
}
|
}
|
||||||
if($_GET[$case]==""){
|
if($_GET[$cell]==""){
|
||||||
$freeCases[]=$case;
|
$emptyCells[]=$cell;
|
||||||
}else{
|
}else{
|
||||||
if ($_GET[$case]!==$_GET['you']){
|
if ($_GET[$cell]!==$_GET['you']){
|
||||||
$hisChar=$_GET[$case];
|
$hisChar=$_GET[$cell];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$grille[$case]=$_GET[$case];
|
$map[$cell]=$_GET[$cell];
|
||||||
}
|
}
|
||||||
if(!isset($_GET['you'])){
|
if(!isset($_GET['you'])){
|
||||||
echo "wrong parameters 2"; die;
|
echo "wrong parameters 2"; die;
|
||||||
}
|
}
|
||||||
if (count($freeCases)==0){
|
if (count($emptyCells)==0){
|
||||||
echo "error. Grid is full, beach!";
|
echo "error. Grid is full, beach!";
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
if (isGridWin($grille)){
|
if (isGridWin($map)){
|
||||||
echo ("erreur, la grille a déjà été gagnée"); die;
|
echo ("erreur, i have allready win this tictactoe"); die;
|
||||||
}
|
}
|
||||||
if(!isset($hisChar)){
|
if(!isset($hisChar)){
|
||||||
// si le caractere ennemi n'a pas été trouvé (il n'a pas déjà joué), on le définit arbitrairement.
|
// if opponent char does not exist. (first move) choose one for him.
|
||||||
if($_GET['you']=="!"){
|
if($_GET['you']=="!"){
|
||||||
$hisChar="?";
|
$hisChar="?";
|
||||||
}else{
|
}else{
|
||||||
$hisChar="!";
|
$hisChar="!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$scoresDesCases=scoreCases($grille,htmlentities($_GET['you']),$hisChar);
|
$scoresDesCases=scoreCases($map,htmlentities($_GET['you']),$hisChar);
|
||||||
$sc=-10000;
|
$sc=-10000;
|
||||||
foreach($scoresDesCases as $key=>$caseValue){
|
foreach($scoresDesCases as $key=>$cellValue){
|
||||||
if($caseValue>$sc){
|
if($cellValue>$sc){
|
||||||
$sc=$caseValue;
|
$sc=$cellValue;
|
||||||
$beastCase=$key;
|
$beastCase=$key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
echo $beastCase;
|
echo $beastCase;
|
Loading…
Reference in New Issue
Block a user