IAS/stupidIATron.php

115 lines
2.6 KiB
PHP
Raw Normal View History

2016-07-01 07:32:39 +02:00
<?php
2016-07-01 07:34:27 +02:00
2016-11-17 19:02:58 +01:00
/*
* stupid IA for tron
* but less stupid in order to test the arena code
*
* Copy left Gnieark https://blog-du-grouik.tinad.fr 2016
* GNU GPL V3 license
*/
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');
2016-11-17 19:02:58 +01:00
//load classes
include ("incTron/Coords.php");
include ("incTron/Direction.php");
2016-11-10 21:34:06 +01:00
2016-11-17 19:02:58 +01:00
$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);
2016-11-17 19:02:58 +01:00
function in_array_objet($searched,$array){
/*
*Because( in_array php function doesn't works if array contains objects)
*/
foreach($array as $obj){
if ($searched == $obj) return true;
}
return false;
}
function get_available_dirs($busyCells,$myCoords){
$directions = array(
Direction::make("x+"),
Direction::make("x-"),
Direction::make("y+"),
Direction::make("y-")
);
$availablesDirs = array();
foreach ($directions as $dirObj){
if(!in_array_objet($myCoords->addDirection($dirObj),$busyCells, TRUE)){
$availablesDirs[] = $dirObj;
}
}
return $availablesDirs;
}
function scoreDirection($busyCells,$headPOS,$dir){
$newBusyCells = $busyCells;
$newBusyCells[] = $headPOS->addDirection($dir);
return count(get_available_dirs($newBusyCells,$headPOS->addDirection($dir)));
}
2016-07-01 07:32:39 +02:00
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){
2016-11-17 19:02:58 +01:00
$busyCells[] = new Coords($coord[0],$coord[1]);
2016-07-04 07:56:25 +02:00
}
}
2016-11-17 19:02:58 +01:00
2016-07-04 07:56:25 +02:00
//get my head coords
2016-11-17 19:02:58 +01:00
$myCoords = new Coords($params['board'][$params['player-index']][0][0],$params['board'][$params['player-index']][0][1]);
$availablesDirs = get_available_dirs($busyCells,$myCoords);
2016-07-04 22:45:04 +02:00
2016-07-04 07:56:25 +02:00
2016-11-17 19:02:58 +01:00
//score them
$majoredAvailableDirs = array();
foreach($availablesDirs as $dir){
$score = scoreDirection($busyCells,$myCoords,$dir);
for($i = 0; $i < $score * 10; $i++){
$majoredAvailableDirs[] = $dir;
}
2016-09-30 18:28:10 +02:00
}
2016-07-04 07:56:25 +02:00
2016-11-17 19:02:58 +01:00
if(count($majoredAvailableDirs) == 0){
2016-07-04 22:46:34 +02:00
echo '{"play":"x+","comment":"I Loose"}';
2016-11-17 19:02:58 +01:00
error_log("i ll loose");
2016-07-04 07:56:25 +02:00
}else{
2016-11-17 19:02:58 +01:00
shuffle($majoredAvailableDirs);
echo '{"play":"'.$majoredAvailableDirs[0].'"}';
//error_log(json_encode($majoredAvailableDirs));
2016-07-04 07:56:25 +02:00
}
2016-11-17 19:02:58 +01:00
2016-07-01 07:32:39 +02:00
break;
default:
break;
2016-09-30 18:28:10 +02:00
}