This commit is contained in:
gnieark 2015-11-14 01:22:01 +01:00
parent 941ee9a28f
commit d4525cece3
3 changed files with 107 additions and 275119 deletions

View File

@ -1,69 +0,0 @@
<?php
$cases=array("0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2");
$grille=array(0,0,0,0,0,0,0,0,0); //0 nonjoué ; 1 joué par player 1; 2 idem player 2
play($grille,1,1);
play($grille,1,2);
function play($grid,$joueur,$statForPlayer,$profondeur=1){
//trouver toutes les cases libres
$count=0;
$sommeScores=0;
foreach($grid as $key => $case){
if($case==0){
//on joue là
$gridTemp=$grid;
$gridTemp[$key]=$joueur;
//echo $key." ".$profondeur;
//si c'est gagnant
if(isWinGrid($grid)){
$score=1;
}
//si la grille est pleine
elseif(isGridFull($gridTemp)){
$score=0;
}else{
//sinon, on part en profondeur, next player
$score= - play($gridTemp,2,$statForPlayer,$profondeur+1)/$profondeur;
}
if($joueur==$statForPlayer){
echo implode(",",$grid)." ".$key.":".$score." \n";
}
$count++;
$sommeScores=$sommeScores+$score;
}
}
return ($sommeScores/$count);
}
function isGridFull($grille){
foreach($grille as $case){
if ($case==0){
return false;
}
}
return true;
}
function isWinGrid($grille){
$linesAndDiags=array(
array(0,1,2),
array(3,4,5),
array(6,7,8),
array(0,3,6),
array(1,4,7),
array(2,5,8),
array(0,4,8),
array(2,4,6)
);
foreach($linesAndDiags as $winComb){
if(
($grille[$winComb[0]]>0)
&& ($grille[$winComb[0]]==$grille[$winComb[1]])
&& ($grille[$winComb[0]]==$grille[$winComb[2]])
){return true;}
}
return false;
}

107
plop.php Normal file
View File

@ -0,0 +1,107 @@
<?php
/*
* Tic Tac Toe gnieark's IA
* For programming challenge https://github.com/jeannedhack/programmingChallenges
* Gnieark 2015
* licensed under the Do What the Fuck You Want to Public License http://www.wtfpl.net/
*/
function scoreCases($grid,$myChar,$hisChar,$profondeur=0){
//lister les cases vides de la grille
foreach($grid as $key => $case){
$tempGrid=$grid;
$tempGrid[$key]=$myChar;
if($case==""){ //on ne "score que les cases qui sont libres
//tester si je gagne en jouant là
if(isGridWin($tempGrid)){
if(fmod($profondeur,2)==0){
$scores[$key]=10-$profondeur;
}else{
$scores[$key]=$profondeur-10;
}
}elseif(nbFreeCases($tempGrid)==0){
$scores[$key]=0;
}else{
//trouver le plus grand score dans l'array plus profond en valeur absolue
$scores[$key]=0;
$scoresDeeper=scoreCases($tempGrid,$hisChar,$myChar,$profondeur+1);
foreach($scoresDeeper as $scD){
if(abs($scD)>abs($scores[$key])){
$scores[$key]=$scD;
}
}
}
}
}
//print_r($scores);print_r($grid);echo "------------";
return $scores;
}
function nbFreeCases($grille){
$nb=0;
foreach($grille as $case){
if ($case==""){
$nb++;
}
}
return $nb;
}
function isGridWin($grille){
if(
(($grille['0-0']==$grille['0-1'])&&($grille['0-1']==$grille['0-2'])&&($grille['0-2']!==""))
OR (($grille['1-0']==$grille['1-1'])&&($grille['1-1']==$grille['1-2'])&&($grille['1-2']!==""))
OR (($grille['2-0']==$grille['2-1'])&&($grille['2-1']==$grille['2-2'])&&($grille['2-2']!==""))
OR (($grille['0-0']==$grille['1-0'])&&($grille['1-0']==$grille['2-0'])&&($grille['2-0']!==""))
OR (($grille['0-1']==$grille['1-1'])&&($grille['1-1']==$grille['2-1'])&&($grille['2-1']!==""))
OR (($grille['0-2']==$grille['1-2'])&&($grille['1-2']==$grille['2-2'])&&($grille['2-2']!==""))
OR (($grille['0-0']==$grille['1-1'])&&($grille['1-1']==$grille['2-2'])&&($grille['2-2']!==""))
OR (($grille['0-2']==$grille['1-1'])&&($grille['1-1']==$grille['2-0'])&&($grille['2-0']!==""))
){
return true;
}
return false;
}
$cases=array("0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2");
//filling array
$freeCases=array();
foreach($cases as $case){
if (!isset($_GET[$case])){
echo "wrong parameters ".$case; die;
}
if($_GET[$case]==""){
$freeCases[]=$case;
}else{
if ($_GET[$case]!==$_GET['you']){
$hisChar=$_GET[$case];
}
}
$grille[$case]=$_GET[$case];
}
if(!isset($_GET['you'])){
echo "wrong parameters 2"; die;
}
if (count($freeCases)==0){
echo "error. Grid is full, beach!";
die;
}
if (isGridWin($grille)){
echo ("erreur, la grille a déjà été gagnée"); die;
}
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($_GET['you']=="!"){
$hisChar="?";
}else{
$hisChar="!";
}
}
$scoresDesCases=scoreCases($grille,htmlentities($_GET['you']),$hisChar);
$sc=-10000;
foreach($scoresDesCases as $key=>$caseValue){
if($caseValue>$sc){
$sc=$caseValue;
$beastCase=$key;
}
}
echo $beastCase;

275050
result.txt

File diff suppressed because it is too large Load Diff