toujours trop stupid

master
Gnieark 8 years ago
parent f7dd39228e
commit 284bbf7300

@ -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-')
$directions = array(
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;

@ -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;

@ -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].'"}';

Loading…
Cancel
Save