.
This commit is contained in:
parent
734e4d9a61
commit
3c0cba6fb3
|
@ -1 +1 @@
|
||||||
1512
|
1515
|
|
@ -33,7 +33,12 @@ class Trail {
|
||||||
|
|
||||||
$this->trail->push($value);
|
$this->trail->push($value);
|
||||||
}
|
}
|
||||||
|
public function __toString(){
|
||||||
|
$str = "";
|
||||||
|
return implode(
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
public function contains($searchedValue) {
|
public function contains($searchedValue) {
|
||||||
foreach($this->trail as $value) {
|
foreach($this->trail as $value) {
|
||||||
if($value == $searchedValue) return TRUE;
|
if($value == $searchedValue) return TRUE;
|
||||||
|
|
|
@ -4,6 +4,56 @@ class TronGame
|
||||||
private $bots; //array of bots
|
private $bots; //array of bots
|
||||||
private $gameId;
|
private $gameId;
|
||||||
private $status; //false => Game ended or not initialised
|
private $status; //false => Game ended or not initialised
|
||||||
|
|
||||||
|
private function apply_looses($loosersArr){
|
||||||
|
|
||||||
|
//save draws
|
||||||
|
if( count($loosersArr) > 1 ){
|
||||||
|
$loosersById = array();
|
||||||
|
foreach($loosersArr as $bot){
|
||||||
|
$loosersById[] = $this->bots[$bot]->id;
|
||||||
|
}
|
||||||
|
$this->save_draw_bots($loosersById);
|
||||||
|
}
|
||||||
|
|
||||||
|
//save victories
|
||||||
|
if( count($loosersArr) > 0 ){
|
||||||
|
//make victorous bots array
|
||||||
|
$vbots = array();
|
||||||
|
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
||||||
|
if($this->bots[$botCount]->isAlive){
|
||||||
|
$vbots[] = $this->bots[$botCount]->id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->save_losers_winers($loosersById,$vbots);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
private function save_draw_bots($arr){
|
||||||
|
/*
|
||||||
|
* Recursive function who save all combionaisons of draw matches
|
||||||
|
*/
|
||||||
|
|
||||||
|
if(count($arr) < 2){
|
||||||
|
return;
|
||||||
|
}else{
|
||||||
|
$a = $arr[0];
|
||||||
|
array_shift($arr);
|
||||||
|
foreach($arr as $bot){
|
||||||
|
save_battle('tron',$a,$bot,0,'id');
|
||||||
|
}
|
||||||
|
$this->save_draw_bots($arr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function save_losers_winers($arrLoosers,$arrWiners){
|
||||||
|
foreach($arrWiners as $winner){
|
||||||
|
foreach($arrLoosers as $loser){
|
||||||
|
save_battle('tron',$winer,$loser,1,'id');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function new_lap(){
|
public function new_lap(){
|
||||||
// for all alive bots
|
// for all alive bots
|
||||||
|
@ -13,10 +63,13 @@ class TronGame
|
||||||
$paramToSend = array();
|
$paramToSend = array();
|
||||||
$board = $this->getBoard();
|
$board = $this->getBoard();
|
||||||
$loosers = array();
|
$loosers = array();
|
||||||
|
$lastsCells = array();
|
||||||
|
|
||||||
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
||||||
if ($this->bots[$botCount]->getStatus()){
|
if ($this->bots[$botCount]->getStatus()){
|
||||||
$urls[$botCount] = $this->bots[$botCount]->getURL();
|
|
||||||
|
$urls[$botCount] = $this->bots[$botCount]->getURL();
|
||||||
|
|
||||||
$paramsToSend[$botCount] = array(
|
$paramsToSend[$botCount] = array(
|
||||||
'game-id' => "".$this->gameId,
|
'game-id' => "".$this->gameId,
|
||||||
'action' => 'play-turn',
|
'action' => 'play-turn',
|
||||||
|
@ -29,77 +82,29 @@ class TronGame
|
||||||
}
|
}
|
||||||
|
|
||||||
$responses = $this->get_multi_IAS_Responses($urls,$paramsToSend);
|
$responses = $this->get_multi_IAS_Responses($urls,$paramsToSend);
|
||||||
|
//$responses[$botCount]['responseArr']['play']
|
||||||
|
|
||||||
|
//grow bots'tails
|
||||||
//print_r($responses);
|
|
||||||
$targetsList = array();
|
|
||||||
$busyCells = $this->getBusyCells();
|
|
||||||
$busyCellsStr = array();
|
|
||||||
foreach ($busyCells as $bs){
|
|
||||||
$busyCellsStr[] = $bs[0].",".$bs[1]; //as string for use in in_array
|
|
||||||
}
|
|
||||||
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
|
||||||
if ($this->bots[$botCount]->getStatus()){
|
|
||||||
//tester si sa réponse n'est pas sur une case déjà occupée.
|
|
||||||
$target = $this->bots[$botCount]->grow($responses[$botCount]['responseArr']['play']);
|
|
||||||
$targetByBot[$botCount] = $target;
|
|
||||||
$x = $target[0];
|
|
||||||
$y = $target[1];
|
|
||||||
$hashTargetsList[$botCount] = $x * 1000 + $y; //wil be easyest to compare than if it was arrays
|
|
||||||
if(($target === false)
|
|
||||||
OR (in_array($target,$busyCellsStr))
|
|
||||||
OR ($x < 0) OR ($x > 999) OR ($y < 0) OR ($y > 999)
|
|
||||||
){
|
|
||||||
$this->bots[$botCount]->loose();
|
|
||||||
//he loses
|
|
||||||
$loosers[] = $botCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//did some bots have played on the same cell?
|
|
||||||
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
||||||
if ($this->bots[$botCount]->getStatus()){
|
if ($this->bots[$botCount]->getStatus()){
|
||||||
for ($botCount2 = 0; $botCount2 < $nbeBots; $botCount2++){
|
$lastsCells[$botCount] = $this->bots[$botCount]->grow($responses[$botCount]['responseArr']['play']);
|
||||||
if (($this->bots[$botCount2]->getStatus())
|
|
||||||
&& ($botCount <> $botCount2)
|
|
||||||
&& ($hashTargetsList[$botCount] == $hashTargetsList[$botCount2])
|
|
||||||
){
|
|
||||||
$this->bots[$botCount]->loose();
|
|
||||||
//they loose
|
|
||||||
$loosers[] = $botCount;
|
|
||||||
$loosers[] = $botCount2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//test if loose
|
||||||
if(count($loosers > 0)){
|
|
||||||
//save_draw_bots
|
|
||||||
$this->save_draw_bots($loosers);
|
|
||||||
$winners = array();
|
|
||||||
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
|
||||||
if ($this->bots[$botCount]->getStatus()){
|
|
||||||
$winners[] = $this->bots[$botCount]->getId();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//sauver les relations winers loosers
|
|
||||||
$this->save_losers_winers($loosers,$winners);
|
|
||||||
}
|
|
||||||
|
|
||||||
// generer un array en retour qui permettra de dessiner les modifications
|
|
||||||
// sur la map
|
|
||||||
$arrRapport = array();
|
|
||||||
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
for ($botCount = 0; $botCount < $nbeBots; $botCount++){
|
||||||
if ($this->bots[$botCount]->getStatus()){
|
if ($this->bots[$botCount]->getStatus()){
|
||||||
$arrRapport[$botCount] = $targetByBot[$botCount];
|
foreach($this->bots as $otherBot){
|
||||||
}else{
|
if($otherBot->trail->contains($lastsCells($botCount)))
|
||||||
$arrRapport[$botCount] = "die";
|
$loosers[] = $botCount;
|
||||||
|
$this->bots[$botCount]->loose();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $arrRapport;
|
//return all trails for draw svg
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +179,6 @@ class TronGame
|
||||||
$fullLogs = "";
|
$fullLogs = "";
|
||||||
$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',
|
||||||
|
@ -218,7 +222,7 @@ class TronGame
|
||||||
$positions = array();
|
$positions = array();
|
||||||
$botCount = 0;
|
$botCount = 0;
|
||||||
$err = "";
|
$err = "";
|
||||||
foreach($botsInfo as $bot){
|
foreach($botsInfos as $bot){
|
||||||
//find a random start position
|
//find a random start position
|
||||||
do{
|
do{
|
||||||
$x = rand(1,999);
|
$x = rand(1,999);
|
||||||
|
|
|
@ -11,6 +11,7 @@ class TronPlayer{
|
||||||
|
|
||||||
public function grow(Direction $dir){
|
public function grow(Direction $dir){
|
||||||
$this->trail->add($this->trail->last()->addDirection($dir));
|
$this->trail->add($this->trail->last()->addDirection($dir));
|
||||||
|
return $this->trail->last();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loose(){
|
public function loose(){
|
||||||
|
@ -23,8 +24,8 @@ class TronPlayer{
|
||||||
$this->trail = new Trail;
|
$this->trail = new Trail;
|
||||||
$this->trail->add($initialsCoords);
|
$this->trail->add($initialsCoords);
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
$this->state = true;
|
$this->state = true;
|
||||||
}
|
}
|
||||||
public function __construct(){
|
public function __construct(){
|
||||||
$this->state = false;
|
$this->state = false;
|
||||||
|
|
|
@ -20,15 +20,29 @@ require_once ("Coords.php");
|
||||||
switch ($_POST['act']){
|
switch ($_POST['act']){
|
||||||
case "initGame":
|
case "initGame":
|
||||||
|
|
||||||
|
$rs = mysqli_query($lnBdd,"SELECT id,name,url FROM bots WHERE game='tron';");
|
||||||
|
while($r = mysqli_fetch_row($rs)){
|
||||||
|
$botsFullArr[$r[0]] = array('name' => $r[1], 'url' => $r[2]);
|
||||||
|
}
|
||||||
|
|
||||||
$botsArrayTemp = json_decode($_POST['bots']);
|
$botsArrayTemp = json_decode($_POST['bots']);
|
||||||
$botsIds = array();
|
$botsIds = array();
|
||||||
//dont take non selected bots (value=0)
|
//dont take non selected bots (value=0)
|
||||||
|
$queries = "";
|
||||||
foreach($botsArrayTemp as $bot){
|
foreach($botsArrayTemp as $bot){
|
||||||
if($bot > 0){
|
if($bot > 0){
|
||||||
$botsIds[] = $bot;
|
$botsIds[] = $bot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$game = new TronGame($botsIds);
|
$game = new TronGame($botsIds);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$logs = $game->init_game();
|
$logs = $game->init_game();
|
||||||
|
|
||||||
echo json_encode(array(
|
echo json_encode(array(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user