botsArena/src/arenas/tron/functions.php

198 lines
4.7 KiB
PHP
Raw Normal View History

2016-06-29 16:30:39 +02:00
<?php
2016-07-04 21:54:14 +02:00
function save_draw_bots($arr){
/*
* Recursive function who save all combionaisons of draw matches
*/
if(count($arr) < 2){
return;
}else{
$a = $arr[0];
2016-07-04 22:01:11 +02:00
array_shift($arr);
2016-07-04 21:54:14 +02:00
foreach($arr as $bot){
save_battle('tron',$a,$bot,0);
}
save_draw_bots($arr);
}
}
2016-07-05 19:54:40 +02:00
class TronGame{
private $bots;
private $gameId;
2016-07-05 23:44:42 +02:00
public function getBotsPositions(){
$nbeBots = count($this->bots);
$arr = array();
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
$arr[$botCount] = array(
"name" => $this->bots[$botCount]->getName(),
"tail" => $this->bots[$botCount]->getTail()
);
}
return $arr;
}
2016-07-05 19:54:40 +02:00
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
2016-07-05 23:44:42 +02:00
$logs = "";
2016-07-05 19:54:40 +02:00
$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
);
2016-07-05 23:44:42 +02:00
$resp = get_IA_Response($this->bots[$botCount]->getURL(),$messageArr);
2016-07-05 19:54:40 +02:00
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{
2016-07-05 23:44:42 +02:00
$logs.="Init message send to ".$this->bots[$botCount]->getName()."<br/>";
}
2016-07-05 19:54:40 +02:00
}
return $logs;
}
private function getBusyCells(){
$arr=array();
foreach($this->bots as $bot){
$arr = array_merge($arr,$bot->getTail);
}
return $arr;
}
public function __construct($botsIds){
2016-07-06 08:01:18 +02:00
2016-07-05 19:54:40 +02:00
$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){
2016-07-06 08:01:18 +02:00
2016-07-05 19:54:40 +02:00
$err = "Something went wrong for ".$this->bots[$botCount]->getName()."<br/>";
2016-07-06 08:01:18 +02:00
}else{
$botCount++;
2016-07-05 19:54:40 +02:00
}
}
return $err;
}
}
2016-06-29 16:30:39 +02:00
class TronPlayer{
private $url;
private $name;
private $tail = array();
private $direction;
2016-06-29 16:42:21 +02:00
private $state;
2016-07-03 14:50:50 +02:00
public function getTail(){
return $this->tail;
}
2016-06-30 14:53:38 +02:00
public function getStatus(){
return $this->state;
}
2016-07-01 07:25:28 +02:00
public function getURL(){
return $this->url;
}
2016-07-01 07:43:20 +02:00
public function getName(){
return $this->name;
2016-07-01 07:51:16 +02:00
}
2016-06-29 16:30:39 +02:00
private function set_direction($newDir){
//can't be the opposite of the previous direction
if(
(($newDir == "x+") && ($this->direction == "x-"))
|| (($newDir == "x-") && ($this->direction == "x+"))
|| (($newDir == "y+") && ($this->direction == "y-"))
|| (($newDir == "y-") && ($this->direction == "y+"))
){
return false;
}
2016-07-05 19:54:40 +02:00
$this->direction = $newDir;
2016-06-29 16:30:39 +02:00
return true;
}
2016-07-05 19:54:40 +02:00
public function getTargetCell($dir){
2016-06-29 16:30:39 +02:00
if($dir == ""){
$dir = $this->direction;
}
2016-07-05 19:54:40 +02:00
if(!$this->set_direction($dir)){
2016-06-29 16:42:21 +02:00
return false;
}
2016-06-29 16:30:39 +02:00
$headCoords = end($this->tail);
2016-06-29 16:57:45 +02:00
switch ($dir){
2016-06-29 16:30:39 +02:00
case "y+":
2016-07-05 19:54:40 +02:00
return array($headCoords[0],$headCoords[1]++);
2016-06-29 16:30:39 +02:00
break;
case "y-":
2016-07-05 19:54:40 +02:00
return array($headCoords[0],$headCoords[1]--);
2016-06-29 16:30:39 +02:00
break;
case "x+":
2016-07-05 19:54:40 +02:00
return array($headCoords[0]++,$headCoords[1]);
2016-06-29 16:30:39 +02:00
break;
case "x-":
2016-07-05 19:54:40 +02:00
return array($headCoords[0]--,$headCoords[1]);
2016-06-29 16:30:39 +02:00
break;
default:
return false;
}
2016-07-05 19:54:40 +02:00
2016-06-29 16:30:39 +02:00
}
2016-07-05 19:54:40 +02:00
public function grow($dir=""){
$this->tail[] = $this->getTargetCell($dir);
}
2016-07-04 21:54:14 +02:00
public function loose(){
$this->state = false;
$this->tail = array();
return false;
}
2016-06-29 17:03:06 +02:00
public function __construct($id,$initialX,$initialY,$initialDirection){
2016-06-29 16:30:39 +02:00
$lnBdd = conn_bdd();
$rs = mysqli_query($lnBdd,
"SELECT name,url FROM bots WHERE game='tron' AND id='".mysqli_real_escape_string($lnBdd, $id)."';"
);
if(($r=mysqli_fetch_row($rs)) && in_array($initialDirection,array('x-','x+','y-','y+'))){
$this->name = $r[0];
$this->url = $r[1];
$this->tail = array(array($initialX,$initialY));
$this->direction = $initialDirection;
$this->state= true;
}else{
$this->state = false;
}
}
}