2016-07-01 07:32:39 +02:00
|
|
|
<?php
|
2016-07-01 07:34:27 +02:00
|
|
|
|
2016-07-01 07:32:39 +02:00
|
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
header('Access-Control-Allow-Methods: GET, POST');
|
|
|
|
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stupid IA for tron
|
|
|
|
*/
|
|
|
|
$in=file_get_contents('php://input');
|
2016-11-10 21:34:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-01 07:32:39 +02:00
|
|
|
$params=json_decode($in, TRUE);
|
|
|
|
switch($params['action']){
|
|
|
|
case "init":
|
|
|
|
echo '{"name":"Stupid AI"}';
|
|
|
|
break;
|
|
|
|
case "play-turn":
|
2016-09-30 18:28:10 +02:00
|
|
|
|
|
|
|
//Input JSON exemple:
|
2016-11-10 21:34:06 +01:00
|
|
|
/*
|
|
|
|
{"game-id":"1784",
|
2016-09-30 18:28:10 +02:00
|
|
|
"action":"play-turn",
|
|
|
|
"game":"tron",
|
|
|
|
"board":[
|
2016-11-10 21:34:06 +01:00
|
|
|
[[490,937],[489,937],[489,938]],
|
|
|
|
[[349,806],[350,806],[350,805]]
|
|
|
|
],"player-index":0,"players":2}
|
2016-09-30 18:28:10 +02:00
|
|
|
*/
|
2016-07-04 07:56:25 +02:00
|
|
|
|
|
|
|
//put all non empty coords on array
|
|
|
|
$busyCells = array();
|
|
|
|
|
2016-09-30 18:28:10 +02:00
|
|
|
foreach($params['board'] as $tail){
|
|
|
|
foreach($tail as $coord){
|
|
|
|
$busyCells[] = $coord[0].",".$coord[1];
|
2016-07-04 07:56:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-30 18:28:10 +02:00
|
|
|
|
2016-11-10 21:34:06 +01:00
|
|
|
|
2016-07-04 07:56:25 +02:00
|
|
|
//get my head coords
|
2016-11-10 21:34:06 +01:00
|
|
|
$myCoords = $params['board'][$params['player-index']][0];
|
2016-07-04 22:45:04 +02:00
|
|
|
|
|
|
|
$x = $myCoords[0];
|
|
|
|
$y = $myCoords[1];
|
2016-07-04 07:56:25 +02:00
|
|
|
|
|
|
|
$availablesDirs = array();
|
2016-09-30 18:28:10 +02:00
|
|
|
if (!in_array(($x + 1).",".$y, $busyCells)){
|
2016-11-10 21:34:06 +01:00
|
|
|
$availablesDirs[] = "x+";
|
2016-07-04 07:56:25 +02:00
|
|
|
}
|
2016-09-30 18:28:10 +02:00
|
|
|
if (!in_array(($x -1 ).",".$y, $busyCells)){
|
2016-11-10 21:34:06 +01:00
|
|
|
$availablesDirs[] = "x-";
|
2016-07-04 07:56:25 +02:00
|
|
|
}
|
2016-09-30 18:28:10 +02:00
|
|
|
if (!in_array($x.",".($y + 1), $busyCells)){
|
2016-11-10 21:34:06 +01:00
|
|
|
$availablesDirs[] = "y+";
|
2016-07-04 07:56:25 +02:00
|
|
|
}
|
2016-09-30 18:28:10 +02:00
|
|
|
if (!in_array($x.",".($y - 1), $busyCells)){
|
2016-11-10 21:34:06 +01:00
|
|
|
$availablesDirs[] = "y-";
|
2016-09-30 18:28:10 +02:00
|
|
|
}
|
2016-07-04 07:56:25 +02:00
|
|
|
|
|
|
|
if(count($availablesDirs) == 0){
|
2016-07-04 22:46:34 +02:00
|
|
|
echo '{"play":"x+","comment":"I Loose"}';
|
2016-11-15 18:55:03 +01:00
|
|
|
//error_log("i ll loose");
|
2016-07-04 07:56:25 +02:00
|
|
|
}else{
|
|
|
|
shuffle($availablesDirs);
|
2016-07-04 22:45:04 +02:00
|
|
|
echo '{"play":"'.$availablesDirs[0].'"}';
|
2016-07-04 07:56:25 +02:00
|
|
|
}
|
2016-09-30 18:28:10 +02:00
|
|
|
//error_log(json_encode($availablesDirs));
|
2016-07-01 07:32:39 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-09-30 18:28:10 +02:00
|
|
|
}
|