diff --git a/countBattles.txt b/countBattles.txt index 3641460..f350689 100755 --- a/countBattles.txt +++ b/countBattles.txt @@ -1 +1 @@ -1671 \ No newline at end of file +1677 \ No newline at end of file diff --git a/src/arenas/tron/Coords.php b/src/arenas/tron/Coords.php index 31a42b3..fe760e2 100755 --- a/src/arenas/tron/Coords.php +++ b/src/arenas/tron/Coords.php @@ -1,16 +1,26 @@ min) || ($x > $this->max) || ($y < $this->min) || ($y > $this->max)){ + //out of limits + return false; + } + $this->x = $x; $this->y = $y; + + } public function __toString(){ return $this->x.",".$this->y; + } public function addDirection(Direction $dir){ diff --git a/src/arenas/tron/Trail.php b/src/arenas/tron/Trail.php index dca329b..7361574 100755 --- a/src/arenas/tron/Trail.php +++ b/src/arenas/tron/Trail.php @@ -9,6 +9,9 @@ class Trail { } public function last() { + if($this->trail->isEmpty()){ + return false; + } return $this->trail->top(); } @@ -19,9 +22,12 @@ class Trail { public function add($value) { if(!$this->trail->isEmpty()) { if(Trail::kind($this->trail->bottom()) !== Trail::kind($value)) { - throw new TypeError( - 'items added to a trail must be of the same kind' - ); + + // + // throw new TypeError( + // 'items added to a trail must be of the same kind' + // ); + return false; } if($this->contains($value)) { diff --git a/src/arenas/tron/TronGame.php b/src/arenas/tron/TronGame.php index bb978d4..c002205 100755 --- a/src/arenas/tron/TronGame.php +++ b/src/arenas/tron/TronGame.php @@ -123,6 +123,7 @@ class TronGame }else{ $lastsCells[$botCount] = $this->bots[$botCount]->grow($dir); + if($lastsCells[$botCount] === false){ $loosers[] = $botCount; $this->bots[$botCount]->loose(); @@ -151,8 +152,7 @@ class TronGame } } - - + //loosers list (in order to pass their id) $loosersList = array(); foreach($loosers as $looser){ @@ -173,19 +173,21 @@ class TronGame $cmh = curl_multi_init(); for ($i = 0; $i < count($iasUrls); $i++){ - $data_string = json_encode($postParams[$i]); - - $ch[$i] = curl_init($iasUrls[$i]); - curl_setopt($ch[$i], CURLOPT_CUSTOMREQUEST, "POST"); - curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch[$i], CURLOPT_POSTFIELDS, $data_string); - curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch[$i], CURLOPT_HTTPHEADER, array( - 'Content-Type: application/json', - 'Content-Length: ' . strlen($data_string)) - ); - curl_multi_add_handle($cmh,$ch[$i]); + if(isset($postParams[$i])){ //dont use already deads bots + $data_string = json_encode($postParams[$i]); + + $ch[$i] = curl_init($iasUrls[$i]); + curl_setopt($ch[$i], CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch[$i], CURLOPT_POSTFIELDS, $data_string); + curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch[$i], CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + 'Content-Length: ' . strlen($data_string)) + ); + curl_multi_add_handle($cmh,$ch[$i]); + } } //send the requests @@ -195,34 +197,40 @@ class TronGame //Get results - for ($i = 0; $i < count($iasUrls); $i++){ - // Check for errors - $curlError = curl_error($ch[$i]); - if($curlError == "") { - $response = curl_multi_getcontent($ch[$i]); - if(! $arr = json_decode($response,TRUE)){ - $arr=array(); - } - $res[$i] = array( - 'messageSend' => json_encode($postParams[$i]), - 'response' => $response, - 'httpStatus' => curl_getinfo($ch[$i])['http_code'], - 'responseArr' => $arr - ); - - }else{ - $res[$i] = false; + + for ($i = 0; $i < count($iasUrls); $i++){ + + if(isset($postParams[$i])){ + // Check for errors + $curlError = curl_error($ch[$i]); + if($curlError == "") { + $response = curl_multi_getcontent($ch[$i]); + if(! $arr = json_decode($response,TRUE)){ + $arr=array(); + } + $res[$i] = array( + 'messageSend' => json_encode($postParams[$i]), + 'response' => $response, + 'httpStatus' => curl_getinfo($ch[$i])['http_code'], + 'responseArr' => $arr + ); + + }else{ + $res[$i] = false; + } + //close + curl_multi_remove_handle($cmh, $ch[$i]); + curl_close($ch[$i]); + } - //close - curl_multi_remove_handle($cmh, $ch[$i]); - curl_close($ch[$i]); + } // Clean up the curl_multi handle curl_multi_close($cmh); return $res; } - public function init_game(){ + public function init_game(){ //send init messages to bots $logs = ""; $fullLogs = ""; @@ -246,7 +254,7 @@ class TronGame return array($logs,$fullLogs); } - public function __construct($botsInfos){ + public function __construct($botsInfos){ $this->gameId = get_unique_id(); $this->bots = array(); $positions = array(); diff --git a/src/arenas/tron/TronPlayer.php b/src/arenas/tron/TronPlayer.php index 60115f8..e1ce1de 100755 --- a/src/arenas/tron/TronPlayer.php +++ b/src/arenas/tron/TronPlayer.php @@ -8,12 +8,19 @@ class TronPlayer{ public $isAlive = true; public function grow(Direction $dir){ + $dest = $this->trail->last()->addDirection($dir); + if($dest == false){ + $this->loose(); + return false; + } + $this->trail->add($this->trail->last()->addDirection($dir)); return $this->trail->last(); } public function loose(){ $this->isAlive = false; $this->trail->emptyTrail(); + error_log($this->name." a perdu"); return false; } public function make($botId, Coords $initialsCoords,$name,$url){