From f7dd39228e8bda48958c7b8a40a8e3471d5bb114 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 10 Nov 2016 21:34:06 +0100 Subject: [PATCH] need less stupid IA to test tron --- aBitLessStupidIATron.php | 98 ++++++++++++++++++++++++++++++++++++++++ incTron/Coords.php | 30 ++++++++++++ incTron/Direction.php | 92 +++++++++++++++++++++++++++++++++++++ stupidIATron.php | 33 +++++++------- 4 files changed, 236 insertions(+), 17 deletions(-) create mode 100644 aBitLessStupidIATron.php create mode 100644 incTron/Coords.php create mode 100644 incTron/Direction.php diff --git a/aBitLessStupidIATron.php b/aBitLessStupidIATron.php new file mode 100644 index 0000000..bb55880 --- /dev/null +++ b/aBitLessStupidIATron.php @@ -0,0 +1,98 @@ +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; +} \ No newline at end of file diff --git a/incTron/Coords.php b/incTron/Coords.php new file mode 100644 index 0000000..2521bcc --- /dev/null +++ b/incTron/Coords.php @@ -0,0 +1,30 @@ + 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 + ); + } +} \ No newline at end of file diff --git a/incTron/Direction.php b/incTron/Direction.php new file mode 100644 index 0000000..3fe4a83 --- /dev/null +++ b/incTron/Direction.php @@ -0,0 +1,92 @@ +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; + } +} \ No newline at end of file diff --git a/stupidIATron.php b/stupidIATron.php index 28d2da2..9edd4ed 100644 --- a/stupidIATron.php +++ b/stupidIATron.php @@ -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-"; - } - if (!in_array(($x -1 ).",".$y, $busyCells)){ $availablesDirs[] = "x+"; } + if (!in_array(($x -1 ).",".$y, $busyCells)){ + $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].'"}';