2015-11-14 01:22:01 +01:00
|
|
|
<?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/
|
|
|
|
*/
|
2015-11-19 11:39:35 +01:00
|
|
|
function scoreCases($grid,$myChar,$hisChar,$depth=0){
|
|
|
|
//list empty cells
|
|
|
|
foreach($grid as $key => $cell){
|
2015-11-14 01:22:01 +01:00
|
|
|
$tempGrid=$grid;
|
|
|
|
$tempGrid[$key]=$myChar;
|
2015-11-19 11:39:35 +01:00
|
|
|
if($cell==""){
|
|
|
|
//do i win if i play there?
|
2015-11-14 01:22:01 +01:00
|
|
|
if(isGridWin($tempGrid)){
|
2015-11-19 11:39:35 +01:00
|
|
|
if(fmod($depth,2)==0){
|
|
|
|
$scores[$key]=10-$depth;
|
2015-11-14 01:22:01 +01:00
|
|
|
}else{
|
2015-11-19 11:39:35 +01:00
|
|
|
$scores[$key]=$depth-10;
|
2015-11-14 01:22:01 +01:00
|
|
|
}
|
|
|
|
}elseif(nbFreeCases($tempGrid)==0){
|
|
|
|
$scores[$key]=0;
|
|
|
|
}else{
|
2015-11-19 11:39:35 +01:00
|
|
|
//find the beast score
|
2015-11-14 01:22:01 +01:00
|
|
|
$scores[$key]=0;
|
2015-11-19 11:39:35 +01:00
|
|
|
$scoresDeeper=scoreCases($tempGrid,$hisChar,$myChar,$depth+1);
|
2015-11-14 01:22:01 +01:00
|
|
|
foreach($scoresDeeper as $scD){
|
|
|
|
if(abs($scD)>abs($scores[$key])){
|
|
|
|
$scores[$key]=$scD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $scores;
|
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
function nbFreeCases($map){
|
2015-11-14 01:22:01 +01:00
|
|
|
$nb=0;
|
2015-11-19 11:39:35 +01:00
|
|
|
foreach($map as $cell){
|
|
|
|
if ($cell==""){
|
2015-11-14 01:22:01 +01:00
|
|
|
$nb++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $nb;
|
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
function isGridWin($map){
|
2015-11-14 01:22:01 +01:00
|
|
|
if(
|
2015-11-19 11:39:35 +01:00
|
|
|
(($map['0-0']==$map['0-1'])&&($map['0-1']==$map['0-2'])&&($map['0-2']!==""))
|
|
|
|
OR (($map['1-0']==$map['1-1'])&&($map['1-1']==$map['1-2'])&&($map['1-2']!==""))
|
|
|
|
OR (($map['2-0']==$map['2-1'])&&($map['2-1']==$map['2-2'])&&($map['2-2']!==""))
|
|
|
|
OR (($map['0-0']==$map['1-0'])&&($map['1-0']==$map['2-0'])&&($map['2-0']!==""))
|
|
|
|
OR (($map['0-1']==$map['1-1'])&&($map['1-1']==$map['2-1'])&&($map['2-1']!==""))
|
|
|
|
OR (($map['0-2']==$map['1-2'])&&($map['1-2']==$map['2-2'])&&($map['2-2']!==""))
|
|
|
|
OR (($map['0-0']==$map['1-1'])&&($map['1-1']==$map['2-2'])&&($map['2-2']!==""))
|
|
|
|
OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!==""))
|
2015-11-14 01:22:01 +01:00
|
|
|
){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
$cells=array("0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2");
|
2015-11-14 01:22:01 +01:00
|
|
|
//filling array
|
2015-11-19 11:39:35 +01:00
|
|
|
$emptyCells=array();
|
|
|
|
foreach($cells as $cell){
|
|
|
|
if (!isset($_GET[$cell])){
|
|
|
|
echo "wrong parameters ".$cell; die;
|
2015-11-14 01:22:01 +01:00
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
if($_GET[$cell]==""){
|
|
|
|
$emptyCells[]=$cell;
|
2015-11-14 01:22:01 +01:00
|
|
|
}else{
|
2015-11-19 11:39:35 +01:00
|
|
|
if ($_GET[$cell]!==$_GET['you']){
|
|
|
|
$hisChar=$_GET[$cell];
|
2015-11-14 01:22:01 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
$map[$cell]=$_GET[$cell];
|
2015-11-14 01:22:01 +01:00
|
|
|
}
|
|
|
|
if(!isset($_GET['you'])){
|
|
|
|
echo "wrong parameters 2"; die;
|
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
if (count($emptyCells)==0){
|
2015-11-14 01:22:01 +01:00
|
|
|
echo "error. Grid is full, beach!";
|
|
|
|
die;
|
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
if (isGridWin($map)){
|
|
|
|
echo ("erreur, i have allready win this tictactoe"); die;
|
2015-11-14 01:22:01 +01:00
|
|
|
}
|
|
|
|
if(!isset($hisChar)){
|
2015-11-19 11:39:35 +01:00
|
|
|
// if opponent char does not exist. (first move) choose one for him.
|
2015-11-14 01:22:01 +01:00
|
|
|
if($_GET['you']=="!"){
|
|
|
|
$hisChar="?";
|
|
|
|
}else{
|
|
|
|
$hisChar="!";
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
$scoresDesCases=scoreCases($map,htmlentities($_GET['you']),$hisChar);
|
2015-11-14 01:22:01 +01:00
|
|
|
$sc=-10000;
|
2015-11-19 11:39:35 +01:00
|
|
|
foreach($scoresDesCases as $key=>$cellValue){
|
|
|
|
if($cellValue>$sc){
|
|
|
|
$sc=$cellValue;
|
2015-11-14 01:22:01 +01:00
|
|
|
$beastCase=$key;
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 11:39:35 +01:00
|
|
|
echo $beastCase;
|