From a5c908f930d57dbdce7cd94b1bf8e8f5f09d6bbb Mon Sep 17 00:00:00 2001 From: Gnieark Date: Mon, 4 Jul 2016 07:56:25 +0200 Subject: [PATCH] . --- stupidIATron.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/stupidIATron.php b/stupidIATron.php index 8957651..9ebd476 100644 --- a/stupidIATron.php +++ b/stupidIATron.php @@ -15,6 +15,41 @@ switch($params['action']){ break; case "play-turn": //to do + //{"game-id":"","action":"play-turn","game":"tron","board":[[[619,240]],[[329,353]]],"player-index":0,"players":2} + + //put all non empty coords on array + $busyCells = array(); + + foreach($params['board'] as $tails){ + foreach($tails as $coords){ + $busyCells[] = $coords; + } + } + //get my head coords + $myCoords = end($params['board'][$params['player-index']]); + list($x,$y) = explode(",",$myCoords); + + $availablesDirs = array(); + 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+"; + } + if in_array(($x.",".($y - 1)), $busyCells){ + $availablesDirs[] = "y-"; + } + + if(count($availablesDirs) == 0){ + echo '{"play":"x+","comment":"I Loose"}'; + }else{ + shuffle($availablesDirs); + echo '{"play":"'.$availablesDirs[0].'","comment":"I Loose"}'; + } + break; default: break;