it works, lets debug

This commit is contained in:
root 2016-10-03 19:12:07 +02:00
parent 9a2224f019
commit 1f1f6dcd47
5 changed files with 72 additions and 41 deletions

View File

@ -1 +1 @@
1671 1677

View File

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

View File

@ -9,6 +9,9 @@ class Trail {
} }
public function last() { public function last() {
if($this->trail->isEmpty()){
return false;
}
return $this->trail->top(); return $this->trail->top();
} }
@ -19,9 +22,12 @@ class Trail {
public function add($value) { public function add($value) {
if(!$this->trail->isEmpty()) { if(!$this->trail->isEmpty()) {
if(Trail::kind($this->trail->bottom()) !== Trail::kind($value)) { 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)) { if($this->contains($value)) {

View File

@ -123,6 +123,7 @@ class TronGame
}else{ }else{
$lastsCells[$botCount] = $this->bots[$botCount]->grow($dir); $lastsCells[$botCount] = $this->bots[$botCount]->grow($dir);
if($lastsCells[$botCount] === false){ if($lastsCells[$botCount] === false){
$loosers[] = $botCount; $loosers[] = $botCount;
$this->bots[$botCount]->loose(); $this->bots[$botCount]->loose();
@ -152,7 +153,6 @@ class TronGame
} }
} }
//loosers list (in order to pass their id) //loosers list (in order to pass their id)
$loosersList = array(); $loosersList = array();
foreach($loosers as $looser){ foreach($loosers as $looser){
@ -173,6 +173,7 @@ class TronGame
$cmh = curl_multi_init(); $cmh = curl_multi_init();
for ($i = 0; $i < count($iasUrls); $i++){ for ($i = 0; $i < count($iasUrls); $i++){
if(isset($postParams[$i])){ //dont use already deads bots
$data_string = json_encode($postParams[$i]); $data_string = json_encode($postParams[$i]);
$ch[$i] = curl_init($iasUrls[$i]); $ch[$i] = curl_init($iasUrls[$i]);
@ -187,6 +188,7 @@ class TronGame
); );
curl_multi_add_handle($cmh,$ch[$i]); curl_multi_add_handle($cmh,$ch[$i]);
} }
}
//send the requests //send the requests
do { do {
@ -195,7 +197,10 @@ class TronGame
//Get results //Get results
for ($i = 0; $i < count($iasUrls); $i++){ for ($i = 0; $i < count($iasUrls); $i++){
if(isset($postParams[$i])){
// Check for errors // Check for errors
$curlError = curl_error($ch[$i]); $curlError = curl_error($ch[$i]);
if($curlError == "") { if($curlError == "") {
@ -216,6 +221,9 @@ class TronGame
//close //close
curl_multi_remove_handle($cmh, $ch[$i]); curl_multi_remove_handle($cmh, $ch[$i]);
curl_close($ch[$i]); curl_close($ch[$i]);
}
} }
// Clean up the curl_multi handle // Clean up the curl_multi handle
curl_multi_close($cmh); curl_multi_close($cmh);

View File

@ -8,12 +8,19 @@ class TronPlayer{
public $isAlive = true; public $isAlive = true;
public function grow(Direction $dir){ 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)); $this->trail->add($this->trail->last()->addDirection($dir));
return $this->trail->last(); return $this->trail->last();
} }
public function loose(){ public function loose(){
$this->isAlive = false; $this->isAlive = false;
$this->trail->emptyTrail(); $this->trail->emptyTrail();
error_log($this->name." a perdu");
return false; return false;
} }
public function make($botId, Coords $initialsCoords,$name,$url){ public function make($botId, Coords $initialsCoords,$name,$url){