toujours trop stupid

master
Gnieark 8 years ago
parent f7dd39228e
commit 284bbf7300

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

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

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

Loading…
Cancel
Save