.
This commit is contained in:
parent
b22dac43ef
commit
adf5d14c35
|
@ -1 +1 @@
|
||||||
1448
|
1456
|
|
@ -16,70 +16,19 @@ require_once(__DIR__."/functions.php");
|
||||||
switch ($_POST['act']){
|
switch ($_POST['act']){
|
||||||
case "initGame":
|
case "initGame":
|
||||||
|
|
||||||
//check if bots exists
|
|
||||||
$botsArrayTemp = json_decode($_POST['bots']);
|
$botsArrayTemp = json_decode($_POST['bots']);
|
||||||
|
$game = new TronGame($botsArrayTemp);
|
||||||
$bots = array();
|
$logs = $game->init_game();
|
||||||
$positions = array();
|
|
||||||
$botCount = 0;
|
|
||||||
foreach($botsArrayTemp as $botId){
|
|
||||||
do{
|
|
||||||
$x = rand(1,999);
|
|
||||||
$y = rand(1,999);
|
|
||||||
}while(in_array($x.",".$y,$positions));
|
|
||||||
|
|
||||||
$positions[] = $x.",".$y;
|
|
||||||
$bots[$botCount] = new TronPlayer($botId,$x,$y,'y+');
|
|
||||||
|
|
||||||
if ($bots[$botCount]->getStatus() === false){
|
|
||||||
unset($bots[$botCount]);
|
|
||||||
}else{
|
|
||||||
$botCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
$_SESSION['players'] = $botCount;
|
|
||||||
if ($botCount < 2){
|
|
||||||
error (500,"missing bots");
|
|
||||||
}
|
|
||||||
|
|
||||||
$logs="";
|
|
||||||
|
|
||||||
//send init message
|
|
||||||
$gameId = get_unique_id();
|
|
||||||
$responses = array();
|
|
||||||
|
|
||||||
for ($botCount = 0; $botCount < count($bots); $botCount ++){
|
|
||||||
$messageArr = array(
|
|
||||||
'game-id' => "".$gameId,
|
|
||||||
'action' => 'init',
|
|
||||||
'game' => 'tron',
|
|
||||||
'board' => '',
|
|
||||||
'players' => $_SESSION['players'],
|
|
||||||
'player-index' => $botCount
|
|
||||||
);
|
|
||||||
|
|
||||||
$resp = get_IA_Response($bots[$botCount]->getURL(),$messageArr);
|
|
||||||
if($_POST['fullLogs'] == "true"){
|
|
||||||
$logs.='Arena send to '.$bots[$botCount]->getName().'<em>'.htmlentities($resp['messageSend']).'</em><br/>
|
|
||||||
HTTP status: <em>'.htmlentities($resp['httpStatus']).'</em><br/>
|
|
||||||
Bot anwser: <em>'.htmlentities($resp['response']).'</em><br/>';
|
|
||||||
}else{
|
|
||||||
$logs.="Init message send to ".$bots[$botCount]->getName()."<br/>";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//save bots on session var
|
|
||||||
$_SESSION['bots'] = serialize($bots);
|
|
||||||
$_SESSION['gameId'] = $gameId;
|
|
||||||
|
|
||||||
echo json_encode(array(
|
echo json_encode(array(
|
||||||
'status' => 'OK',
|
'status' => $game->getStatus();,
|
||||||
'logs' => $logs,
|
'logs' => $logs
|
||||||
'gameId' => $gameId
|
'gameId' => $game->getGameId();
|
||||||
));
|
));
|
||||||
|
|
||||||
|
$_SESSION['game'] = serialize($game);
|
||||||
|
|
||||||
|
|
||||||
die;
|
die;
|
||||||
break;
|
break;
|
||||||
case "play":
|
case "play":
|
||||||
|
@ -114,19 +63,19 @@ switch ($_POST['act']){
|
||||||
|
|
||||||
$busyCells = array_merge($busyCells, $bots[$botCount]->getTail());
|
$busyCells = array_merge($busyCells, $bots[$botCount]->getTail());
|
||||||
$responses[$botCount] = get_IA_Response($bots[$botCount]->getURL(),$messageArr);
|
$responses[$botCount] = get_IA_Response($bots[$botCount]->getURL(),$messageArr);
|
||||||
|
print_r($responses[$botCount]);
|
||||||
if(in_array($responses[$botCount]['responseArr']['play'], $busyCells)){
|
if(in_array($bots[$botCount]->getTargetCell($responses[$botCount]['responseArr']['play']), $busyCells)){
|
||||||
//this bot plays on a non empty cell, it looses
|
//this bot plays on a non empty cell, it looses
|
||||||
$bots[$botCount]->loose();
|
$bots[$botCount]->loose();
|
||||||
$logs.= $bots[$botCount]->getName()." Played on a non empty cell, he loses.<br/>";
|
$logs.= $bots[$botCount]->getName()." Played on a non empty cell, he loses.<br/>";
|
||||||
$loosingBots[] = $bots[$botCount]->getName();
|
$loosingBots[] = $bots[$botCount]->getName();
|
||||||
}else{
|
}else{
|
||||||
$targets[] = $responses[$botCount]['responseArr']['play'];
|
$targets[] = $bots[$botCount]->getTargetCell($responses[$botCount]['responseArr']['play']);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//test if some bots plays at the same place
|
||||||
for ($botCount = 0; $botCount < count($bots); $botCount ++){
|
for ($botCount = 0; $botCount < count($bots); $botCount ++){
|
||||||
if($bots[$botCount]->getStatus()){
|
if($bots[$botCount]->getStatus()){
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,95 @@ function save_draw_bots($arr){
|
||||||
save_draw_bots($arr);
|
save_draw_bots($arr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
class TronGame{
|
||||||
|
private $bots;
|
||||||
|
private $gameId;
|
||||||
|
public function getGameId(){
|
||||||
|
return $this->gameId;
|
||||||
|
}
|
||||||
|
public function get_continue(){
|
||||||
|
//count bots alive. if less than 1, game is ended
|
||||||
|
$count = 0;
|
||||||
|
foreach($this->bots as $bot){
|
||||||
|
if( $bot->getStatus() == true){
|
||||||
|
$count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if($count > 1){
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function init_game(){
|
||||||
|
//send init messages to bots
|
||||||
|
|
||||||
|
$nbeBots = count($this->bots);
|
||||||
|
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
||||||
|
$messageArr = array(
|
||||||
|
'game-id' => "".$this->gameId,
|
||||||
|
'action' => 'init',
|
||||||
|
'game' => 'tron',
|
||||||
|
'board' => '',
|
||||||
|
'players' => $nbeBots,
|
||||||
|
'player-index' => $botCount
|
||||||
|
);
|
||||||
|
|
||||||
|
$resp = get_IA_Response($bots[$botCount]->getURL(),$messageArr);
|
||||||
|
|
||||||
|
if($_POST['fullLogs'] == "true"){
|
||||||
|
$logs.='Arena send to '.$bots[$botCount]->getName().'<em>'.htmlentities($resp['messageSend']).'</em><br/>
|
||||||
|
HTTP status: <em>'.htmlentities($resp['httpStatus']).'</em><br/>
|
||||||
|
Bot anwser: <em>'.htmlentities($resp['response']).'</em><br/>';
|
||||||
|
}else{
|
||||||
|
$logs.="Init message send to ".$bots[$botCount]->getName()."<br/>";
|
||||||
|
}
|
||||||
|
|
||||||
|
//check response
|
||||||
|
if(
|
||||||
|
($resp['httpStatus'] <> 200)
|
||||||
|
OR (!pregmatch('^[0-9]*,[0-9]*$', $resp['responseArr']))
|
||||||
|
){
|
||||||
|
$this->bots[$botCount]->loose();
|
||||||
|
$logs.= $this->bots[$botCount]->getName." Made a non conform response <br/>";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return $logs;
|
||||||
|
}
|
||||||
|
private function getBusyCells(){
|
||||||
|
$arr=array();
|
||||||
|
foreach($this->bots as $bot){
|
||||||
|
$arr = array_merge($arr,$bot->getTail);
|
||||||
|
}
|
||||||
|
return $arr;
|
||||||
|
}
|
||||||
|
public function __construct($botsIds){
|
||||||
|
|
||||||
|
$this->gameId = get_unique_id();
|
||||||
|
$this->bots = array();
|
||||||
|
$positions = array();
|
||||||
|
$botCount = 0;
|
||||||
|
$err = "";
|
||||||
|
foreach($botsIds as $botId){
|
||||||
|
//find a random start position
|
||||||
|
do{
|
||||||
|
$x = rand(1,999);
|
||||||
|
$y = rand(1,999);
|
||||||
|
}while(in_array($x.",".$y,$positions));
|
||||||
|
|
||||||
|
$positions[] = $x.",".$y;
|
||||||
|
$this->bots[$botCount] = new TronPlayer($botId,$x,$y,'y+');
|
||||||
|
|
||||||
|
if ($this->bots[$botCount]->getStatus() === false){
|
||||||
|
$err = "Something went wrong for ".$this->bots[$botCount]->getName()."<br/>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class TronPlayer{
|
class TronPlayer{
|
||||||
private $url;
|
private $url;
|
||||||
private $name;
|
private $name;
|
||||||
|
@ -45,39 +134,42 @@ class TronPlayer{
|
||||||
){
|
){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$this->direction = $newdir;
|
$this->direction = $newDir;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
public function getTargetCell($dir){
|
||||||
|
|
||||||
public function grow($dir=""){
|
|
||||||
if($dir == ""){
|
if($dir == ""){
|
||||||
$dir = $this->direction;
|
$dir = $this->direction;
|
||||||
}
|
}
|
||||||
if(!$this->set_direction()){
|
if(!$this->set_direction($dir)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$headCoords = end($this->tail);
|
$headCoords = end($this->tail);
|
||||||
|
|
||||||
switch ($dir){
|
switch ($dir){
|
||||||
case "y+":
|
case "y+":
|
||||||
$targetCoords = array($headCoords[0],$headCoords[1]++);
|
return array($headCoords[0],$headCoords[1]++);
|
||||||
break;
|
break;
|
||||||
case "y-":
|
case "y-":
|
||||||
$targetCoords = array($headCoords[0],$headCoords[1]--);
|
return array($headCoords[0],$headCoords[1]--);
|
||||||
break;
|
break;
|
||||||
case "x+":
|
case "x+":
|
||||||
$targetCoords = array($headCoords[0]++,$headCoords[1]);
|
return array($headCoords[0]++,$headCoords[1]);
|
||||||
break;
|
break;
|
||||||
case "x-":
|
case "x-":
|
||||||
$targetCoords = array($headCoords[0]--,$headCoords[1]);
|
return array($headCoords[0]--,$headCoords[1]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$this->tail[] = $targetCoords;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function grow($dir=""){
|
||||||
|
$this->tail[] = $this->getTargetCell($dir);
|
||||||
|
}
|
||||||
public function loose(){
|
public function loose(){
|
||||||
$this->state = false;
|
$this->state = false;
|
||||||
$this->tail = array();
|
$this->tail = array();
|
||||||
|
@ -97,8 +189,5 @@ class TronPlayer{
|
||||||
}else{
|
}else{
|
||||||
$this->state = false;
|
$this->state = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user