need less stupid IA to test tron

master
Gnieark 8 years ago
parent 3b4c14d782
commit f7dd39228e

@ -0,0 +1,98 @@
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
//load classes
include ("incTron/Coords.php");
include ("incTron/Direction.php");
/*
* stupid IA for tron
*/
$in=file_get_contents('php://input');
$params=json_decode($in, TRUE);
function get_available_dirs($busyCells,$myCoords){
$directions = array(
new Direction('x+'),
new Direction('x-'),
new Direction('y+'),
new Direction('y-')
);
$availablesDirs = array();
foreach ($directions as $direction){
if(in_array($myCoords->addDirection($direction),$busyCells)){
$availablesDirs[] = $direction;
}
}
return $availablesDirs;
}
function scoreDirection($busyCells,$headPOS,$dir){
$newBusyCells = $busyCells;
$newBusyCells[] = $headPOS->addDirection($dir);
return count(get_available_dirs($newBusyCells,$headPOS->addDirection($dir)));
}
switch($params['action']){
case "init":
echo '{"name":"Stupid AI"}';
break;
case "play-turn":
//Input JSON exemple:
/*
{"game-id":"1784",
"action":"play-turn",
"game":"tron",
"board":[
[[490,937],[489,937],[489,938]],
[[349,806],[350,806],[350,805]]
],"player-index":0,"players":2}
*/
//put all non empty coords on array
$busyCells = array();
foreach($params['board'] as $tail){
foreach($tail as $coord){
$busyCells[] = new Coords($coord[0],$coord[1]);
}
}
//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++){
$majoredAvailableDirs[] = $dir;
}
}
if(count($majoredAvailableDirs) == 0){
echo '{"play":"x+","comment":"I Loose"}';
error_log("i ll loose");
}else{
shuffle($majoredAvailableDirs);
echo '{"play":"'.$majoredAvailableDirs[0].'"}';
error_log(json_encode($majoredAvailableDirs));
}
break;
default:
break;
}

@ -0,0 +1,30 @@
<?php
class Coords{
private static $min = 0;
private static $max = 999;
public $x;
public $y;
public function __construct(int $x = 0, int $y = 0) {
if (($x < Coords::$min) || ($x > Coords::$max) || ($y < Coords::$min) || ($y > Coords::$max)){
//out of limits
error_log("a bot out of limits");
return false;
}
$this->x = $x;
$this->y = $y;
}
public function __toString(){
return $this->x.",".$this->y;
}
public function addDirection(Direction $dir){
return new Coords(
$this->x + $dir->deltaX,
$this->y + $dir->deltaY
);
}
}

@ -0,0 +1,92 @@
<?php
class Direction
{
private static $top = 0;
private static $bottom = 1;
private static $left = 2;
private static $right = 3;
private $value;
public $deltaX;
public $deltaY;
public function __construct(){
$this->value = 0;
}
private function setValue($value){
$this->value = $value;
switch ($value){
case Direction::$bottom:
$this->deltaY = -1;
$this->deltaX = 0;
break;
case Direction::$top:
$this->deltaY = 1;
$this->deltaX = 0;
break;
case Direction::$left:
$this->deltaY = 0;
$this->deltaX = -1;
break;
case Direction::$right:
$this->deltaY = 0;
$this->deltaX = 1;
break;
}
}
public function __toString(){
switch ($this->value){
case Direction::$top:
return "y+";
break;
case Direction::$bottom:
return "y-";
break;
case Direction::$left:
return "x-";
break;
case Direction::$right:
return "x+";
break;
}
}
public static function make($str){
$dir = new Direction();
switch((string)$str){
case "x+":
$dir->setValue(Direction::$right);
break;
case "x-":
$dir->setValue(Direction::$left);
break;
case "y+":
$dir->setValue(Direction::$top);
break;
case "y-":
$dir->setValue(Direction::$bottom);
break;
default:
//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;
}
return $dir;
}
public function opposite(){
$opposites = array(
Direction::$top => Direction::$bottom,
Direction::$bottom => Direction::$top,
Direction::$left => Direction::$right,
Direction::$right => Direction::$left
);
$opposite = new Direction();
$opposite->setValue($opposites[$this->value]);
return $opposite;
}
}

@ -8,6 +8,9 @@ header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Ac
* stupid IA for tron
*/
$in=file_get_contents('php://input');
$params=json_decode($in, TRUE);
switch($params['action']){
case "init":
@ -16,20 +19,14 @@ switch($params['action']){
case "play-turn":
//Input JSON exemple:
/*{
"game-id":"1647",
/*
{"game-id":"1784",
"action":"play-turn",
"game":"tron",
"board":[
[
[425,763],[424,763],[423,763],[422,763],[421,763],[420,763],[419,763]
],
[
[858,501],[857,501],[856,501],[855,501],[854,501],[853,501],[852,501]
]
],
"player-index":0,
"players":2}
[[490,937],[489,937],[489,938]],
[[349,806],[350,806],[350,805]]
],"player-index":0,"players":2}
*/
//put all non empty coords on array
@ -41,28 +38,30 @@ switch($params['action']){
}
}
//get my head coords
$myCoords = end($params['board'][$params['player-index']]);
$myCoords = $params['board'][$params['player-index']][0];
$x = $myCoords[0];
$y = $myCoords[1];
$availablesDirs = array();
if (!in_array(($x + 1).",".$y, $busyCells)){
$availablesDirs[] = "x-";
$availablesDirs[] = "x+";
}
if (!in_array(($x -1 ).",".$y, $busyCells)){
$availablesDirs[] = "x+";
$availablesDirs[] = "x-";
}
if (!in_array($x.",".($y + 1), $busyCells)){
$availablesDirs[] = "y-";
$availablesDirs[] = "y+";
}
if (!in_array($x.",".($y - 1), $busyCells)){
$availablesDirs[] = "y+";
$availablesDirs[] = "y-";
}
if(count($availablesDirs) == 0){
echo '{"play":"x+","comment":"I Loose"}';
error_log("i ll loose");
}else{
shuffle($availablesDirs);
echo '{"play":"'.$availablesDirs[0].'"}';

Loading…
Cancel
Save