toujours trop stupid

This commit is contained in:
Gnieark 2016-11-15 18:55:03 +01:00
parent f7dd39228e
commit 284bbf7300
3 changed files with 35 additions and 18 deletions

View File

@ -1,5 +1,13 @@
<?php
/*
* 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
*/
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
@ -9,28 +17,34 @@ header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Ac
include ("incTron/Coords.php");
include ("incTron/Direction.php");
/*
* stupid IA for tron
*/
$in=file_get_contents('php://input');
$params=json_decode($in, TRUE);
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(
new Direction('x+'),
new Direction('x-'),
new Direction('y+'),
new Direction('y-')
Direction::make("x+"),
Direction::make("x-"),
Direction::make("y+"),
Direction::make("y-")
);
$availablesDirs = array();
foreach ($directions as $direction){
if(in_array($myCoords->addDirection($direction),$busyCells)){
$availablesDirs[] = $direction;
foreach ($directions as $dirObj){
if(!in_array_objet($myCoords->addDirection($dirObj),$busyCells, TRUE)){
$availablesDirs[] = $dirObj;
}
}
@ -73,11 +87,13 @@ switch($params['action']){
//get my head coords
$myCoords = new Coords($params['board'][$params['player-index']][0][0],$params['board'][$params['player-index']][0][1]);
$availablesDirs = get_available_dirs($busyCells,$myCoords);
//score them
$majoredAvailableDirs = array();
foreach($availablesDirs as $dir){
$score = scoreDirection($busyCells,$myCoords,$dir);
for($i = 0; $i < $score * 5; $i++){
for($i = 0; $i < $score * 10; $i++){
$majoredAvailableDirs[] = $dir;
}
}
@ -89,7 +105,8 @@ switch($params['action']){
}else{
shuffle($majoredAvailableDirs);
echo '{"play":"'.$majoredAvailableDirs[0].'"}';
error_log(json_encode($majoredAvailableDirs));
//error_log(json_encode($majoredAvailableDirs));
}
break;

View File

@ -70,7 +70,7 @@ class Direction
$dir->setValue(Direction::$bottom);
break;
default:
//error_log("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received.");
error_log("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received.");
return false;
//throw new InvalidDirectionException("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received.");
break;

View File

@ -61,7 +61,7 @@ switch($params['action']){
if(count($availablesDirs) == 0){
echo '{"play":"x+","comment":"I Loose"}';
error_log("i ll loose");
//error_log("i ll loose");
}else{
shuffle($availablesDirs);
echo '{"play":"'.$availablesDirs[0].'"}';