This commit is contained in:
Gnieark 2016-09-30 18:36:24 +02:00
parent f873d6233d
commit 0fb60e7cf4
6 changed files with 48 additions and 50 deletions

View File

@ -1 +1 @@
1622 1660

View File

@ -73,7 +73,10 @@ class Direction
$dir->setValue(Direction::$bottom); $dir->setValue(Direction::$bottom);
break; break;
default: default:
throw new InvalidDirectionException("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received."); //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; return $dir;
} }

View File

@ -25,9 +25,10 @@ class Trail {
} }
if($this->contains($value)) { if($this->contains($value)) {
throw new AlreadyBeenAddedException( //throw new AlreadyBeenAddedException(
'value has already been added to the trail' // 'value has already been added to the trail'.$value.'|'.$this->__toString()
); //);
return false;
} }
} }

View File

@ -92,6 +92,7 @@ class TronGame
$urls = array(); $urls = array();
$paramToSend = array(); $paramToSend = array();
$board = $this->get_trails(); $board = $this->get_trails();
//$board = $this->get_lasts_trails();
$loosers = array(); $loosers = array();
$lastsCells = array(); $lastsCells = array();
@ -108,18 +109,30 @@ class TronGame
'player-index' => $botCount, // To do: verifier que ça restera le même à chaque tour 'player-index' => $botCount, // To do: verifier que ça restera le même à chaque tour
'players' => $nbeBots 'players' => $nbeBots
); );
//!!!!!!!!!!!!! To debug
//error_log(json_encode($paramsToSend[$botCount]));
} }
} }
$responses = $this->get_multi_IAS_Responses($urls,$paramsToSend); $responses = $this->get_multi_IAS_Responses($urls,$paramsToSend);
//print_r($responses);
//grow bots'tails //grow bots'tails
for ($botCount = 0; $botCount < $nbeBots; $botCount++){ for ($botCount = 0; $botCount < $nbeBots; $botCount++){
if ($this->bots[$botCount]->isAlive){ if ($this->bots[$botCount]->isAlive){
$dir = Direction::make($responses[$botCount]['responseArr']['play']); if(!$dir = Direction::make($responses[$botCount]['responseArr']['play'])){
//he loses , non conform response
$loosers[] = $botCount;
$this->bots[$botCount]->loose();
}else{
$lastsCells[$botCount] = $this->bots[$botCount]->grow($dir); $lastsCells[$botCount] = $this->bots[$botCount]->grow($dir);
if($lastsCells[$botCount] === false){
$loosers[] = $botCount;
$this->bots[$botCount]->loose();
}
}
} }
} }
@ -127,8 +140,9 @@ class TronGame
//test if loose //test if loose
for ($botCount = 0; $botCount < $nbeBots; $botCount++){ for ($botCount = 0; $botCount < $nbeBots; $botCount++){
if ($this->bots[$botCount]->isAlive){ if ($this->bots[$botCount]->isAlive){
for( $botCount2 = 0; $botCount2 < $nbeBots; $botCount2++){
//tester si collusion avec les cases actuelles
for( $botCount2 = 0; $botCount2 < $nbeBots; $botCount2++){
if(($botCount <> $botCount2) if(($botCount <> $botCount2)
&& ($this->bots[$botCount2]->trail->contains($lastsCells[$botCount])) && ($this->bots[$botCount2]->trail->contains($lastsCells[$botCount]))
){ ){
@ -137,13 +151,13 @@ class TronGame
break; break;
} }
} }
} }
} }
return $this->get_lasts_trails(); return $this->get_lasts_trails();
} }
@ -218,11 +232,11 @@ class TronGame
$nbeBots = count($this->bots); $nbeBots = count($this->bots);
for ($botCount = 0; $botCount < $nbeBots; $botCount++){ for ($botCount = 0; $botCount < $nbeBots; $botCount++){
$messageArr = array( $messageArr = array(
'game-id' => "".$this->gameId, 'game-id' => "".$this->gameId,
'action' => 'init', 'action' => 'init',
'game' => 'tron', 'game' => 'tron',
'board' => '', 'board' => '',
'players' => $nbeBots, 'players' => $nbeBots,
'player-index' => $botCount 'player-index' => $botCount
); );

View File

@ -10,6 +10,8 @@
# #
# -- END LICENSE BLOCK ----------------------------------------- # -- END LICENSE BLOCK -----------------------------------------
//error_log(json_encode($_SESSION,true)."\n\n");
require_once ("TronGame.php"); require_once ("TronGame.php");
require_once ("TronPlayer.php"); require_once ("TronPlayer.php");
require_once ("Direction.php"); require_once ("Direction.php");

View File

@ -12,24 +12,7 @@ function addLog(message){
divLogs.scrollTop = divLogs.scrollHeight; divLogs.scrollTop = divLogs.scrollHeight;
} }
/*
function growTails(newPointsByPlayer){
var botsColor = ['cyan','darkmagenta','darkred','darkslategrey','deeppink','dodgerblue','goldenrod','grey','indigo','lightgreen','mediumslateblue','midnightblue'];
//document.getElementById('map');
for (var botId in newPointsByPlayer){
var tail = newPointsByPlayer[botId]['tail'];
for(var coordsIx in tail){
coords = tail[coordsIx];
//draw the point
var rect=createElemNS('rect',{'x':coords[0],'y':coords[1],'width':'2','height':'2','style':'fill:' + botsColor[botId] + ';'});
document.getElementById('map').appendChild(rect);
}
}
}
*/
function createElemNS(type,attributes){ function createElemNS(type,attributes){
//same as createElem but with ns for svg file //same as createElem but with ns for svg file
var elem=document.createElementNS("http://www.w3.org/2000/svg",type); var elem=document.createElementNS("http://www.w3.org/2000/svg",type);
@ -92,7 +75,7 @@ function applyInitMessage(req,xd_check){
} }
}else{ }else{
alert ('error ' + req.status); alert ('error ' + req.status + req.responseText );
document.getElementById('fightButton').disabled=false; document.getElementById('fightButton').disabled=false;
return; return;
} }
@ -108,15 +91,6 @@ function drawMap(map){
//draw the point //draw the point
var rect=createElemNS('rect',{'x':map[botId]['x'],'y':map[botId]['y'],'width':'2','height':'2','style':'fill:' + botsColor[botId] + ';'}); var rect=createElemNS('rect',{'x':map[botId]['x'],'y':map[botId]['y'],'width':'2','height':'2','style':'fill:' + botsColor[botId] + ';'});
document.getElementById('map').appendChild(rect); document.getElementById('map').appendChild(rect);
/*
for(var coordsI in map[botId]){
coords = map[botId][coordsI];
//draw the point
var rect=createElemNS('rect',{'x':coords[0],'y':coords[1],'width':'2','height':'2','style':'fill:' + botsColor[botId] + ';'});
document.getElementById('map').appendChild(rect);
}
*/
} }
} }
@ -128,12 +102,16 @@ function play(gameId,xd_check){
if(req.status == 200) { if(req.status == 200) {
addLog(req.responseText); addLog(req.responseText);
var reponse = JSON.parse(req.responseText); var reponse = JSON.parse(req.responseText);
drawMap(reponse['lap']); drawMap(reponse['lap']);
if(reponse['continue'] == '1'){ if(reponse['continue'] == '1'){
play(gameId,xd_check);
setTimeout(function(){
play(gameId,xd_check);
} ,500);
}else{ }else{
alert ('game ended'); alert ('game ended');
} }