From 3c0cba6fb3b69c750ffcd8fcbc1c14515ca122b3 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Mon, 18 Jul 2016 21:23:29 +0200 Subject: [PATCH] . --- countBattles.txt | 2 +- src/arenas/tron/Trail.php | 7 +- src/arenas/tron/TronGame.php | 130 +++++++++++++++++---------------- src/arenas/tron/TronPlayer.php | 5 +- src/arenas/tron/act.php | 14 ++++ 5 files changed, 91 insertions(+), 67 deletions(-) diff --git a/countBattles.txt b/countBattles.txt index 9bf7b0b..fc0a244 100644 --- a/countBattles.txt +++ b/countBattles.txt @@ -1 +1 @@ -1512 \ No newline at end of file +1515 \ No newline at end of file diff --git a/src/arenas/tron/Trail.php b/src/arenas/tron/Trail.php index 888366f..de6dd7d 100644 --- a/src/arenas/tron/Trail.php +++ b/src/arenas/tron/Trail.php @@ -33,7 +33,12 @@ class Trail { $this->trail->push($value); } - + public function __toString(){ + $str = ""; + return implode( + + + } public function contains($searchedValue) { foreach($this->trail as $value) { if($value == $searchedValue) return TRUE; diff --git a/src/arenas/tron/TronGame.php b/src/arenas/tron/TronGame.php index 58ed29f..e71d3eb 100644 --- a/src/arenas/tron/TronGame.php +++ b/src/arenas/tron/TronGame.php @@ -4,6 +4,56 @@ class TronGame private $bots; //array of bots private $gameId; private $status; //false => Game ended or not initialised + + private function apply_looses($loosersArr){ + + //save draws + if( count($loosersArr) > 1 ){ + $loosersById = array(); + foreach($loosersArr as $bot){ + $loosersById[] = $this->bots[$bot]->id; + } + $this->save_draw_bots($loosersById); + } + + //save victories + if( count($loosersArr) > 0 ){ + //make victorous bots array + $vbots = array(); + for ($botCount = 0; $botCount < $nbeBots; $botCount++){ + if($this->bots[$botCount]->isAlive){ + $vbots[] = $this->bots[$botCount]->id; + } + } + $this->save_losers_winers($loosersById,$vbots); + } + + } + private function save_draw_bots($arr){ + /* + * Recursive function who save all combionaisons of draw matches + */ + + if(count($arr) < 2){ + return; + }else{ + $a = $arr[0]; + array_shift($arr); + foreach($arr as $bot){ + save_battle('tron',$a,$bot,0,'id'); + } + $this->save_draw_bots($arr); + } + } + + private function save_losers_winers($arrLoosers,$arrWiners){ + foreach($arrWiners as $winner){ + foreach($arrLoosers as $loser){ + save_battle('tron',$winer,$loser,1,'id'); + } + } + + } public function new_lap(){ // for all alive bots @@ -13,10 +63,13 @@ class TronGame $paramToSend = array(); $board = $this->getBoard(); $loosers = array(); + $lastsCells = array(); for ($botCount = 0; $botCount < $nbeBots; $botCount++){ if ($this->bots[$botCount]->getStatus()){ - $urls[$botCount] = $this->bots[$botCount]->getURL(); + + $urls[$botCount] = $this->bots[$botCount]->getURL(); + $paramsToSend[$botCount] = array( 'game-id' => "".$this->gameId, 'action' => 'play-turn', @@ -29,77 +82,29 @@ class TronGame } $responses = $this->get_multi_IAS_Responses($urls,$paramsToSend); + //$responses[$botCount]['responseArr']['play'] - - //print_r($responses); - $targetsList = array(); - $busyCells = $this->getBusyCells(); - $busyCellsStr = array(); - foreach ($busyCells as $bs){ - $busyCellsStr[] = $bs[0].",".$bs[1]; //as string for use in in_array - } - for ($botCount = 0; $botCount < $nbeBots; $botCount++){ + //grow bots'tails + for ($botCount = 0; $botCount < $nbeBots; $botCount++){ if ($this->bots[$botCount]->getStatus()){ - //tester si sa réponse n'est pas sur une case déjà occupée. - $target = $this->bots[$botCount]->grow($responses[$botCount]['responseArr']['play']); - $targetByBot[$botCount] = $target; - $x = $target[0]; - $y = $target[1]; - $hashTargetsList[$botCount] = $x * 1000 + $y; //wil be easyest to compare than if it was arrays - if(($target === false) - OR (in_array($target,$busyCellsStr)) - OR ($x < 0) OR ($x > 999) OR ($y < 0) OR ($y > 999) - ){ - $this->bots[$botCount]->loose(); - //he loses - $loosers[] = $botCount; - } + $lastsCells[$botCount] = $this->bots[$botCount]->grow($responses[$botCount]['responseArr']['play']); } } - //did some bots have played on the same cell? + //test if loose for ($botCount = 0; $botCount < $nbeBots; $botCount++){ if ($this->bots[$botCount]->getStatus()){ - for ($botCount2 = 0; $botCount2 < $nbeBots; $botCount2++){ - if (($this->bots[$botCount2]->getStatus()) - && ($botCount <> $botCount2) - && ($hashTargetsList[$botCount] == $hashTargetsList[$botCount2]) - ){ - $this->bots[$botCount]->loose(); - //they loose - $loosers[] = $botCount; - $loosers[] = $botCount2; - } - } + foreach($this->bots as $otherBot){ + if($otherBot->trail->contains($lastsCells($botCount))) + $loosers[] = $botCount; + $this->bots[$botCount]->loose(); + break; + } } } + //return all trails for draw svg - if(count($loosers > 0)){ - //save_draw_bots - $this->save_draw_bots($loosers); - $winners = array(); - for ($botCount = 0; $botCount < $nbeBots; $botCount++){ - if ($this->bots[$botCount]->getStatus()){ - $winners[] = $this->bots[$botCount]->getId(); - } - } - //sauver les relations winers loosers - $this->save_losers_winers($loosers,$winners); - } - - // generer un array en retour qui permettra de dessiner les modifications - // sur la map - $arrRapport = array(); - for ($botCount = 0; $botCount < $nbeBots; $botCount++){ - if ($this->bots[$botCount]->getStatus()){ - $arrRapport[$botCount] = $targetByBot[$botCount]; - }else{ - $arrRapport[$botCount] = "die"; - } - } - - return $arrRapport; } @@ -174,7 +179,6 @@ class TronGame $fullLogs = ""; $nbeBots = count($this->bots); for ($botCount = 0; $botCount < $nbeBots; $botCount++){ - $messageArr = array( 'game-id' => "".$this->gameId, 'action' => 'init', @@ -218,7 +222,7 @@ class TronGame $positions = array(); $botCount = 0; $err = ""; - foreach($botsInfo as $bot){ + foreach($botsInfos as $bot){ //find a random start position do{ $x = rand(1,999); diff --git a/src/arenas/tron/TronPlayer.php b/src/arenas/tron/TronPlayer.php index 22cdab4..2a2fed6 100644 --- a/src/arenas/tron/TronPlayer.php +++ b/src/arenas/tron/TronPlayer.php @@ -11,6 +11,7 @@ class TronPlayer{ public function grow(Direction $dir){ $this->trail->add($this->trail->last()->addDirection($dir)); + return $this->trail->last(); } public function loose(){ @@ -23,8 +24,8 @@ class TronPlayer{ $this->trail = new Trail; $this->trail->add($initialsCoords); $this->name = $name; - $this->url = $url; - $this->state = true; + $this->url = $url; + $this->state = true; } public function __construct(){ $this->state = false; diff --git a/src/arenas/tron/act.php b/src/arenas/tron/act.php index 0a32d87..6adbdeb 100644 --- a/src/arenas/tron/act.php +++ b/src/arenas/tron/act.php @@ -20,15 +20,29 @@ require_once ("Coords.php"); switch ($_POST['act']){ case "initGame": + $rs = mysqli_query($lnBdd,"SELECT id,name,url FROM bots WHERE game='tron';"); + while($r = mysqli_fetch_row($rs)){ + $botsFullArr[$r[0]] = array('name' => $r[1], 'url' => $r[2]); + } + $botsArrayTemp = json_decode($_POST['bots']); $botsIds = array(); //dont take non selected bots (value=0) + $queries = ""; foreach($botsArrayTemp as $bot){ if($bot > 0){ $botsIds[] = $bot; } } + + + $game = new TronGame($botsIds); + + + + + $logs = $game->init_game(); echo json_encode(array(