76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
/*
|
|
* Tic Tac Toe gnieark's IA V2
|
|
* Gnieark 2016
|
|
* licensed under the Do What the Fuck You Want to Public License http://www.wtfpl.net/
|
|
*/
|
|
|
|
//get the query
|
|
$message=json_decode(file_get_contents('php://input'), TRUE);
|
|
|
|
if($message['action'] == "init"){
|
|
//juste le petit message d'init
|
|
echo '{"name":"gnieark"}';
|
|
die;
|
|
}
|
|
|
|
|
|
function score_case($map,$me,$him,$case,$depth=0){
|
|
//test si la case est gagnante
|
|
$newMap=$map;
|
|
$newMap[$case] = $me;
|
|
|
|
if(
|
|
(($newMap['0-0']==$newMap['0-1'])&&($newMap['0-1']==$newMap['0-2'])&&($newMap['0-2']!==""))
|
|
OR (($newMap['1-0']==$newMap['1-1'])&&($newMap['1-1']==$newMap['1-2'])&&($newMap['1-2']!==""))
|
|
OR (($newMap['2-0']==$newMap['2-1'])&&($newMap['2-1']==$newMap['2-2'])&&($newMap['2-2']!==""))
|
|
OR (($newMap['0-0']==$newMap['1-0'])&&($newMap['1-0']==$newMap['2-0'])&&($newMap['2-0']!==""))
|
|
OR (($newMap['0-1']==$newMap['1-1'])&&($newMap['1-1']==$newMap['2-1'])&&($newMap['2-1']!==""))
|
|
OR (($newMap['0-2']==$newMap['1-2'])&&($newMap['1-2']==$newMap['2-2'])&&($newMap['2-2']!==""))
|
|
OR (($newMap['0-0']==$newMap['1-1'])&&($newMap['1-1']==$newMap['2-2'])&&($newMap['2-2']!==""))
|
|
OR (($newMap['0-2']==$newMap['1-1'])&&($newMap['1-1']==$newMap['2-0'])&&($newMap['2-0']!==""))
|
|
){
|
|
return 10 - $depth;
|
|
}else{
|
|
$sc=choose_better_cell($newMap,$him,$me,$depth + 1);
|
|
return -$sc[1];
|
|
}
|
|
}
|
|
function choose_better_cell($map,$me,$him,$depth=0){
|
|
$betterCell="";
|
|
$betterScore=-100;
|
|
static $arrCells= array('0-0','0-1','0-2','1-0','1-1','1-2','2-0','2-1','2-2');
|
|
foreach($arrCells as $cellKey){
|
|
if($map[$cellKey] == ""){
|
|
$sc = score_case($map, $me,$him,$cellKey,$depth);
|
|
if( $sc > $betterScore ){
|
|
$betterCell = $cellKey;
|
|
$betterScore= $sc;
|
|
}
|
|
}
|
|
}
|
|
return array($betterCell,$betterScore);
|
|
}
|
|
|
|
//count free cases
|
|
$freeCells=0;
|
|
for($x = 0; $x < 3; $x++){
|
|
for($y = 0; $y < 3; $y++){
|
|
if($message['board'][$x."-".$y] == ""){
|
|
$freeCells ++;
|
|
}elseif($message['board'][$x."-".$y] !== $message['you']){
|
|
$hisSymbol=$message['board'][$x."-".$y];
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!isset($hisSymbol)){
|
|
if($message['you'] == 'X'){
|
|
$hisSymbol='O';
|
|
}else{
|
|
$hisSymbol="X";
|
|
}
|
|
}
|
|
|
|
|
|
echo choose_better_cell($message['board'],$message['you'], $hisSymbol,9 - $freeCells); |