more logs on tictactoe
This commit is contained in:
parent
8eae918d36
commit
3f79c6bf6b
|
|
@ -4,333 +4,333 @@ require_once(__DIR__."/functions.php");
|
||||||
$bots=get_Bots_Array('connectFou');
|
$bots=get_Bots_Array('connectFou');
|
||||||
$new=false;
|
$new=false;
|
||||||
switch ($_POST['act']){
|
switch ($_POST['act']){
|
||||||
|
|
||||||
case "newFight":
|
case "newFight":
|
||||||
$new=true;
|
$new=true;
|
||||||
//remove $_SESSION less xd_check
|
//remove $_SESSION less xd_check
|
||||||
$xd=$_SESSION['xd_check'];
|
$xd=$_SESSION['xd_check'];
|
||||||
session_unset();
|
session_unset();
|
||||||
$_SESSION['xd_check']=$xd;
|
$_SESSION['xd_check']=$xd;
|
||||||
|
|
||||||
|
|
||||||
//init map
|
|
||||||
$_SESSION['map']=array(
|
|
||||||
array("","","","","","",""),
|
|
||||||
array("","","","","","",""),
|
|
||||||
array("","","","","","",""),
|
|
||||||
array("","","","","","",""),
|
|
||||||
array("","","","","","",""),
|
|
||||||
array("","","","","","",""),
|
|
||||||
);
|
|
||||||
|
|
||||||
//check if bots exists
|
|
||||||
$bot1Exists = false;
|
|
||||||
$bot2Exists = false;
|
|
||||||
foreach($bots as $bot){
|
|
||||||
if($bot['id'] == $_POST['bot1']){
|
|
||||||
$bot1 = $bot;
|
|
||||||
$_SESSION['bot1']=$bot;
|
|
||||||
$bot1Exists =true;
|
|
||||||
}
|
|
||||||
if($bot['id'] == $_POST['bot2']){
|
|
||||||
$bot2 = $bot;
|
|
||||||
$_SESSION['bot2']=$bot;
|
|
||||||
$bot2Exists =true;
|
|
||||||
}
|
|
||||||
if ($bot1Exists && $bot2Exists){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((!$bot1Exists) OR (!$bot2Exists)){
|
|
||||||
error (500,"missing parameter 2");
|
|
||||||
}
|
|
||||||
|
|
||||||
//get a match id
|
|
||||||
$_SESSION['matchId']=get_unique_id();
|
|
||||||
$_SESSION['game']="connectFou";
|
|
||||||
|
|
||||||
//send init message
|
|
||||||
|
|
||||||
for($player = 0; $player < 2; $player++){
|
|
||||||
$params[$player]=array(
|
|
||||||
'game-id' => $_SESSION['matchId'],
|
|
||||||
'action' => 'init',
|
|
||||||
'game' => 'connectfour',
|
|
||||||
'players' => 2,
|
|
||||||
'board' => '',
|
|
||||||
'player-index' => $player
|
|
||||||
);
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
'messageSend' => $data_string,
|
|
||||||
'httpStatus' => $curl_getinfo($ch)['http_code'],
|
|
||||||
'response' => $output,
|
|
||||||
'responseArr' => $arr
|
|
||||||
*/
|
|
||||||
get_IA_Response($_SESSION['bot1']['url'],$params[0]); //don't care about result
|
|
||||||
get_IA_Response($_SESSION['bot2']['url'],$params[1]); //don't care about result
|
|
||||||
|
|
||||||
|
|
||||||
case "fight":
|
|
||||||
|
|
||||||
if($_SESSION['game'] <> "connectFou"){
|
|
||||||
error(500,"game non found");
|
|
||||||
die;
|
|
||||||
}
|
|
||||||
if((!$new) && ($_POST['gameId'] <> $_SESSION['matchId'])){
|
|
||||||
error (512, "not correct gameId");
|
|
||||||
}
|
|
||||||
|
|
||||||
//What player has to play?
|
|
||||||
if(!isset($_SESSION['currentPlayer'])){
|
|
||||||
$_SESSION['currentPlayer']=1;
|
|
||||||
$you="X";
|
|
||||||
$currentBotName=$_SESSION['bot1']['name'];
|
|
||||||
$opponentName=$_SESSION['bot2']['name'];
|
|
||||||
$botUrl=$_SESSION['bot1']['url'];
|
|
||||||
}else{
|
|
||||||
if($_SESSION['currentPlayer']==1){
|
|
||||||
$_SESSION['currentPlayer']=2;
|
|
||||||
$you="O";
|
|
||||||
$opponentName=$_SESSION['bot1']['name'];
|
|
||||||
$currentBotName=$_SESSION['bot2']['name'];
|
|
||||||
$botUrl=$_SESSION['bot2']['url'];
|
|
||||||
}else{
|
|
||||||
$_SESSION['currentPlayer']=1;
|
|
||||||
$opponentName=$_SESSION['bot2']['name'];
|
|
||||||
$currentBotName=$_SESSION['bot1']['name'];
|
|
||||||
$botUrl=$_SESSION['bot1']['url'];
|
|
||||||
$you="X";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
|
|
||||||
|
|
||||||
game-id string identifiant la partie.
|
|
||||||
action string identifiant la phase, init tout de suite, sera play-turn dans le châpitre suivant.
|
|
||||||
game string identifiant le jeu. Ici, ce sera forcément tictactoe. ça peut servir si vous donnez une seule URL pour plusieurs bots.
|
|
||||||
players Int indiquant le nombre de joueurs dans la partie, toujours 2 au morpion.
|
|
||||||
board Vide à cette étape, voir chapitre suivant.
|
|
||||||
player-index int, L'ordre de votre bot dans les tours de jeu. Le premier joueur a la valeur 0, le deuxième 1.
|
|
||||||
*/
|
|
||||||
//make post datas to send
|
|
||||||
$postDatas=array(
|
|
||||||
'game-id' => "".$_SESSION['matchId'],
|
|
||||||
'action' => 'play-turn',
|
|
||||||
'game' => 'connectfour',
|
|
||||||
'players' => 2,
|
|
||||||
'opponent' => $opponentName,
|
|
||||||
'you' => $you,
|
|
||||||
'board' => $_SESSION['map'],
|
|
||||||
'player-index' => $_SESSION['currentPlayer'] - 1
|
|
||||||
);
|
|
||||||
|
|
||||||
//send query
|
|
||||||
$tempPlayer = get_IA_Response($botUrl,$postDatas);
|
|
||||||
if(isset($tempPlayer['responseArr']['play'])){
|
|
||||||
$anwserPlayer = $tempPlayer['responseArr']['play'];
|
|
||||||
}else{
|
|
||||||
$anwserPlayer = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//vérifier la validité de la réponse
|
|
||||||
if((isset($_SESSION['map'][5][$anwserPlayer])) && ($_SESSION['map'][5][$anwserPlayer] == "")){
|
|
||||||
//reponse conforme
|
|
||||||
|
|
||||||
for($y = 0; $_SESSION['map'][$y][$anwserPlayer] <> ""; $y++){
|
|
||||||
}
|
|
||||||
|
|
||||||
$_SESSION['map'][$y][$anwserPlayer]=$you;
|
|
||||||
$strikeX=$anwserPlayer;
|
|
||||||
$strikeY=$y;
|
|
||||||
|
|
||||||
//does he win?
|
|
||||||
$wins=false;
|
|
||||||
|
|
||||||
//diagonale \
|
|
||||||
$count=1;
|
|
||||||
$x=$strikeX;
|
|
||||||
$y=$strikeY;
|
|
||||||
$cellsWin=array();
|
|
||||||
$cellsWin[]=array($x,$y);
|
|
||||||
while(($x > 0) && ($y < 5) && ($_SESSION['map'][$y + 1][$x - 1] == $you)){
|
|
||||||
$x--;
|
|
||||||
$y++;
|
|
||||||
$count++;
|
|
||||||
$cellsWin[]=array($x,$y);
|
|
||||||
}
|
|
||||||
|
|
||||||
$x=$strikeX;
|
|
||||||
$y=$strikeY;
|
//init map
|
||||||
while(($x < 6) && ($y > 0) && ($_SESSION['map'][$y - 1][$x + 1] == $you)){
|
$_SESSION['map']=array(
|
||||||
$x++;
|
array("","","","","","",""),
|
||||||
$y--;
|
array("","","","","","",""),
|
||||||
$count++;
|
array("","","","","","",""),
|
||||||
$cellsWin[]=array($x,$y);
|
array("","","","","","",""),
|
||||||
}
|
array("","","","","","",""),
|
||||||
|
array("","","","","","",""),
|
||||||
if($count>3){
|
);
|
||||||
$wins=true;
|
|
||||||
}
|
//check if bots exists
|
||||||
|
$bot1Exists = false;
|
||||||
//diagonale /
|
$bot2Exists = false;
|
||||||
if(!$wins){
|
foreach($bots as $bot){
|
||||||
|
if($bot['id'] == $_POST['bot1']){
|
||||||
$count=1;
|
$bot1 = $bot;
|
||||||
$x=$strikeX;
|
$_SESSION['bot1']=$bot;
|
||||||
$y=$strikeY;
|
$bot1Exists =true;
|
||||||
$cellsWin =array();
|
}
|
||||||
$cellsWin[]=array($x ,$y);
|
if($bot['id'] == $_POST['bot2']){
|
||||||
while(($x < 6) && ($y < 5) && ($_SESSION['map'][$y + 1][$x + 1 ] == $you)){
|
$bot2 = $bot;
|
||||||
$x++;
|
$_SESSION['bot2']=$bot;
|
||||||
$y++;
|
$bot2Exists =true;
|
||||||
$count++;
|
}
|
||||||
$cellsWin[]=array($x,$y);
|
if ($bot1Exists && $bot2Exists){
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$x=$strikeX;
|
if ((!$bot1Exists) OR (!$bot2Exists)){
|
||||||
$y=$strikeY;
|
error (500,"missing parameter 2");
|
||||||
while(($x > 0) && ($y > 0) && ($_SESSION['map'][$y - 1][$x - 1 ] == $you)){
|
|
||||||
$x--;
|
|
||||||
$y--;
|
|
||||||
$count++;
|
|
||||||
$cellsWin[]=array($x,$y);
|
|
||||||
}
|
}
|
||||||
if($count>3){
|
|
||||||
$wins=true;
|
//get a match id
|
||||||
|
$_SESSION['matchId']=get_unique_id();
|
||||||
|
$_SESSION['game']="connectFou";
|
||||||
|
|
||||||
|
//send init message
|
||||||
|
|
||||||
|
for($player = 0; $player < 2; $player++){
|
||||||
|
$params[$player]=array(
|
||||||
|
'game-id' => $_SESSION['matchId'],
|
||||||
|
'action' => 'init',
|
||||||
|
'game' => 'connectfour',
|
||||||
|
'players' => 2,
|
||||||
|
'board' => '',
|
||||||
|
'player-index' => $player
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
/*
|
||||||
|
* 'messageSend' => $data_string,
|
||||||
|
* 'httpStatus' => $curl_getinfo($ch)['http_code'],
|
||||||
//horizontale
|
* 'response' => $output,
|
||||||
if(!$wins){
|
* 'responseArr' => $arr
|
||||||
$count=1;
|
*/
|
||||||
$x=$strikeX;
|
get_IA_Response($_SESSION['bot1']['url'],$params[0]); //don't care about result
|
||||||
$y=$strikeY;
|
get_IA_Response($_SESSION['bot2']['url'],$params[1]); //don't care about result
|
||||||
$cellsWin =array();
|
|
||||||
$cellsWin[]=array($x ,$y);
|
|
||||||
while(($x < 6) && ($_SESSION['map'][$y][$x + 1 ] == $you)){
|
case "fight":
|
||||||
$x++;
|
|
||||||
$count++;
|
|
||||||
$cellsWin[]=array($x,$y);
|
|
||||||
|
|
||||||
}
|
if($_SESSION['game'] <> "connectFou"){
|
||||||
$x=$strikeX;
|
error(500,"game non found");
|
||||||
while(($x >0) && ($_SESSION['map'][$y][$x - 1 ] == $you)){
|
die;
|
||||||
$count++;
|
}
|
||||||
$x--;
|
if((!$new) && ($_POST['gameId'] <> $_SESSION['matchId'])){
|
||||||
$cellsWin[]=array($x,$y);
|
error (512, "not correct gameId");
|
||||||
}
|
}
|
||||||
if($count>3){
|
|
||||||
$wins=true;
|
//What player has to play?
|
||||||
}
|
if(!isset($_SESSION['currentPlayer'])){
|
||||||
}
|
$_SESSION['currentPlayer']=1;
|
||||||
|
$you="X";
|
||||||
//verticale
|
$currentBotName=$_SESSION['bot1']['name'];
|
||||||
if(!$wins){
|
$opponentName=$_SESSION['bot2']['name'];
|
||||||
$count=1;
|
$botUrl=$_SESSION['bot1']['url'];
|
||||||
$x=$strikeX;
|
}else{
|
||||||
$y=$strikeY;
|
if($_SESSION['currentPlayer']==1){
|
||||||
|
$_SESSION['currentPlayer']=2;
|
||||||
$cellsWin =array();
|
$you="O";
|
||||||
$cellsWin[]=array($x ,$y);
|
$opponentName=$_SESSION['bot1']['name'];
|
||||||
|
$currentBotName=$_SESSION['bot2']['name'];
|
||||||
while(($y < 5) && ($_SESSION['map'][$y + 1 ][$x] == $you)){
|
$botUrl=$_SESSION['bot2']['url'];
|
||||||
$y++;
|
}else{
|
||||||
$count++;
|
$_SESSION['currentPlayer']=1;
|
||||||
$cellsWin[]=array($x,$y);
|
$opponentName=$_SESSION['bot2']['name'];
|
||||||
}
|
$currentBotName=$_SESSION['bot1']['name'];
|
||||||
|
$botUrl=$_SESSION['bot1']['url'];
|
||||||
$y=$strikeY;
|
$you="X";
|
||||||
while(($y >0) && ($_SESSION['map'][$y - 1][$x] == $you)){
|
}
|
||||||
$count++;
|
}
|
||||||
$y--;
|
/*
|
||||||
$cellsWin[]=array($x,$y);
|
*
|
||||||
}
|
*
|
||||||
if($count>3){
|
* game-id string identifiant la partie.
|
||||||
$wins=true;
|
* action string identifiant la phase, init tout de suite, sera play-turn dans le châpitre suivant.
|
||||||
}
|
* game string identifiant le jeu. Ici, ce sera forcément tictactoe. ça peut servir si vous donnez une seule URL pour plusieurs bots.
|
||||||
}
|
* players Int indiquant le nombre de joueurs dans la partie, toujours 2 au morpion.
|
||||||
|
* board Vide à cette étape, voir chapitre suivant.
|
||||||
|
* player-index int, L'ordre de votre bot dans les tours de jeu. Le premier joueur a la valeur 0, le deuxième 1.
|
||||||
if($wins){
|
*/
|
||||||
$anwserToJS=array(
|
//make post datas to send
|
||||||
'continue' => 0,
|
$postDatas=array(
|
||||||
'strikeX' => $strikeX,
|
'game-id' => "".$_SESSION['matchId'],
|
||||||
'strikeY' => $strikeY,
|
'action' => 'play-turn',
|
||||||
'strikeSymbol'=> $you,
|
'game' => 'connectfour',
|
||||||
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer." et a gagné",
|
'players' => 2,
|
||||||
'cellsWin' => json_encode($cellsWin),
|
'opponent' => $opponentName,
|
||||||
'gameId' => $_SESSION['matchId']
|
'you' => $you,
|
||||||
);
|
'board' => $_SESSION['map'],
|
||||||
if($_SESSION['currentPlayer']==1){
|
'player-index' => $_SESSION['currentPlayer'] - 1
|
||||||
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],1);
|
);
|
||||||
}else{
|
|
||||||
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],2);
|
//send query
|
||||||
}
|
$tempPlayer = get_IA_Response($botUrl,$postDatas);
|
||||||
|
if(isset($tempPlayer['responseArr']['play'])){
|
||||||
}else{
|
$anwserPlayer = $tempPlayer['responseArr']['play'];
|
||||||
|
}else{
|
||||||
//Was it the last cell?
|
$anwserPlayer = -1;
|
||||||
|
}
|
||||||
$full=true;
|
|
||||||
foreach ($_SESSION['map'][5] as $cell) {
|
//vérifier la validité de la réponse
|
||||||
if ($cell == ""){
|
if((isset($_SESSION['map'][5][$anwserPlayer])) && ($_SESSION['map'][5][$anwserPlayer] == "")){
|
||||||
$full=false;
|
//reponse conforme
|
||||||
break;
|
|
||||||
}
|
for($y = 0; $_SESSION['map'][$y][$anwserPlayer] <> ""; $y++){
|
||||||
}
|
}
|
||||||
|
|
||||||
if($full){
|
$_SESSION['map'][$y][$anwserPlayer]=$you;
|
||||||
|
$strikeX=$anwserPlayer;
|
||||||
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],0);
|
$strikeY=$y;
|
||||||
$anwserToJS=array(
|
|
||||||
'continue' => 0,
|
//does he win?
|
||||||
'strikeX' => $strikeX,
|
$wins=false;
|
||||||
'strikeY' => $strikeY,
|
|
||||||
'strikeSymbol'=> $you,
|
//diagonale \
|
||||||
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer." match nul",
|
$count=1;
|
||||||
'gameId' => $_SESSION['matchId']
|
$x=$strikeX;
|
||||||
);
|
$y=$strikeY;
|
||||||
|
$cellsWin=array();
|
||||||
}else{
|
$cellsWin[]=array($x,$y);
|
||||||
|
while(($x > 0) && ($y < 5) && ($_SESSION['map'][$y + 1][$x - 1] == $you)){
|
||||||
$anwserToJS=array(
|
$x--;
|
||||||
'continue' => 1,
|
$y++;
|
||||||
'strikeX' => $strikeX,
|
$count++;
|
||||||
'strikeY' => $strikeY,
|
$cellsWin[]=array($x,$y);
|
||||||
'strikeSymbol'=> $you,
|
}
|
||||||
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer,
|
|
||||||
'gameId' => $_SESSION['matchId']
|
$x=$strikeX;
|
||||||
);
|
$y=$strikeY;
|
||||||
}
|
while(($x < 6) && ($y > 0) && ($_SESSION['map'][$y - 1][$x + 1] == $you)){
|
||||||
}
|
$x++;
|
||||||
|
$y--;
|
||||||
}else{
|
$count++;
|
||||||
//reponse non conforme
|
$cellsWin[]=array($x,$y);
|
||||||
|
}
|
||||||
$anwserToJS=array(
|
|
||||||
'continue' =>0,
|
if($count>3){
|
||||||
'strikeX' => -1,
|
$wins=true;
|
||||||
'strikeY' => -1,
|
}
|
||||||
'log' => $you." ".$currentBotName." made a non conform anwser: <br/>
|
|
||||||
Bots Arena sent:<em>".$tempPlayer['messageSend']."</em><br/>
|
//diagonale /
|
||||||
".$currentBotName." HTTP STATUS:<em> ".$tempPlayer['httpStatus']."</em><br/>
|
if(!$wins){
|
||||||
His response: <em>".htmlentities($tempPlayer['response'])."</em>",
|
|
||||||
'gameId' => $_SESSION['matchId']
|
$count=1;
|
||||||
);
|
$x=$strikeX;
|
||||||
if($_SESSION['currentPlayer']==1){
|
$y=$strikeY;
|
||||||
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],2);
|
$cellsWin =array();
|
||||||
}else{
|
$cellsWin[]=array($x ,$y);
|
||||||
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],1);
|
while(($x < 6) && ($y < 5) && ($_SESSION['map'][$y + 1][$x + 1 ] == $you)){
|
||||||
}
|
$x++;
|
||||||
}
|
$y++;
|
||||||
|
$count++;
|
||||||
echo json_encode($anwserToJS);
|
$cellsWin[]=array($x,$y);
|
||||||
|
}
|
||||||
|
$x=$strikeX;
|
||||||
|
$y=$strikeY;
|
||||||
die;
|
while(($x > 0) && ($y > 0) && ($_SESSION['map'][$y - 1][$x - 1 ] == $you)){
|
||||||
break;
|
$x--;
|
||||||
default:
|
$y--;
|
||||||
break;
|
$count++;
|
||||||
|
$cellsWin[]=array($x,$y);
|
||||||
|
}
|
||||||
|
if($count>3){
|
||||||
|
$wins=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//horizontale
|
||||||
|
if(!$wins){
|
||||||
|
$count=1;
|
||||||
|
$x=$strikeX;
|
||||||
|
$y=$strikeY;
|
||||||
|
$cellsWin =array();
|
||||||
|
$cellsWin[]=array($x ,$y);
|
||||||
|
while(($x < 6) && ($_SESSION['map'][$y][$x + 1 ] == $you)){
|
||||||
|
$x++;
|
||||||
|
$count++;
|
||||||
|
$cellsWin[]=array($x,$y);
|
||||||
|
|
||||||
|
}
|
||||||
|
$x=$strikeX;
|
||||||
|
while(($x >0) && ($_SESSION['map'][$y][$x - 1 ] == $you)){
|
||||||
|
$count++;
|
||||||
|
$x--;
|
||||||
|
$cellsWin[]=array($x,$y);
|
||||||
|
}
|
||||||
|
if($count>3){
|
||||||
|
$wins=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//verticale
|
||||||
|
if(!$wins){
|
||||||
|
$count=1;
|
||||||
|
$x=$strikeX;
|
||||||
|
$y=$strikeY;
|
||||||
|
|
||||||
|
$cellsWin =array();
|
||||||
|
$cellsWin[]=array($x ,$y);
|
||||||
|
|
||||||
|
while(($y < 5) && ($_SESSION['map'][$y + 1 ][$x] == $you)){
|
||||||
|
$y++;
|
||||||
|
$count++;
|
||||||
|
$cellsWin[]=array($x,$y);
|
||||||
|
}
|
||||||
|
|
||||||
|
$y=$strikeY;
|
||||||
|
while(($y >0) && ($_SESSION['map'][$y - 1][$x] == $you)){
|
||||||
|
$count++;
|
||||||
|
$y--;
|
||||||
|
$cellsWin[]=array($x,$y);
|
||||||
|
}
|
||||||
|
if($count>3){
|
||||||
|
$wins=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if($wins){
|
||||||
|
$anwserToJS=array(
|
||||||
|
'continue' => 0,
|
||||||
|
'strikeX' => $strikeX,
|
||||||
|
'strikeY' => $strikeY,
|
||||||
|
'strikeSymbol'=> $you,
|
||||||
|
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer." et a gagné",
|
||||||
|
'cellsWin' => json_encode($cellsWin),
|
||||||
|
'gameId' => $_SESSION['matchId']
|
||||||
|
);
|
||||||
|
if($_SESSION['currentPlayer']==1){
|
||||||
|
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],1);
|
||||||
|
}else{
|
||||||
|
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],2);
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
|
||||||
|
//Was it the last cell?
|
||||||
|
|
||||||
|
$full=true;
|
||||||
|
foreach ($_SESSION['map'][5] as $cell) {
|
||||||
|
if ($cell == ""){
|
||||||
|
$full=false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($full){
|
||||||
|
|
||||||
|
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],0);
|
||||||
|
$anwserToJS=array(
|
||||||
|
'continue' => 0,
|
||||||
|
'strikeX' => $strikeX,
|
||||||
|
'strikeY' => $strikeY,
|
||||||
|
'strikeSymbol'=> $you,
|
||||||
|
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer." match nul",
|
||||||
|
'gameId' => $_SESSION['matchId']
|
||||||
|
);
|
||||||
|
|
||||||
|
}else{
|
||||||
|
|
||||||
|
$anwserToJS=array(
|
||||||
|
'continue' => 1,
|
||||||
|
'strikeX' => $strikeX,
|
||||||
|
'strikeY' => $strikeY,
|
||||||
|
'strikeSymbol'=> $you,
|
||||||
|
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer,
|
||||||
|
'gameId' => $_SESSION['matchId']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
//reponse non conforme
|
||||||
|
|
||||||
|
$anwserToJS=array(
|
||||||
|
'continue' =>0,
|
||||||
|
'strikeX' => -1,
|
||||||
|
'strikeY' => -1,
|
||||||
|
'log' => $you." ".$currentBotName." made a non conform anwser: <br/>
|
||||||
|
Bots Arena sent:<em>".$tempPlayer['messageSend']."</em><br/>
|
||||||
|
".$currentBotName." HTTP STATUS:<em> ".$tempPlayer['httpStatus']."</em><br/>
|
||||||
|
His response: <em>".htmlentities($tempPlayer['response'])."</em>",
|
||||||
|
'gameId' => $_SESSION['matchId']
|
||||||
|
);
|
||||||
|
if($_SESSION['currentPlayer']==1){
|
||||||
|
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],2);
|
||||||
|
}else{
|
||||||
|
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode($anwserToJS);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
die;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -2,29 +2,29 @@
|
||||||
|
|
||||||
require_once(__DIR__."/functions.php");
|
require_once(__DIR__."/functions.php");
|
||||||
switch ($_POST['act']){
|
switch ($_POST['act']){
|
||||||
|
|
||||||
case "fight":
|
|
||||||
$bots=get_Bots_Array('tictactoe');
|
|
||||||
//clean $_POST vars
|
|
||||||
$postParams=get_Post_Params(count($bots));
|
|
||||||
if(!$postParams){
|
|
||||||
error(400,"wrong parameters send");
|
|
||||||
die;
|
|
||||||
}else{
|
|
||||||
$bot1=$postParams['bot1'];
|
|
||||||
$bot2=$postParams['bot2'];
|
|
||||||
}
|
|
||||||
//init map
|
|
||||||
$map=array(
|
|
||||||
'0-0' => '','0-1' => '','0-2' => '',
|
|
||||||
'1-0' => '','1-1' => '','1-2' => '',
|
|
||||||
'2-0' => '','2-1' => '','2-2' => '');
|
|
||||||
|
|
||||||
$end=false;
|
|
||||||
|
|
||||||
//send init message to bots
|
case "fight":
|
||||||
$game_id = "".get_unique_id();
|
$bots=get_Bots_Array('tictactoe');
|
||||||
for($player = 0; $player < 2; $player++){
|
//clean $_POST vars
|
||||||
|
$postParams=get_Post_Params(count($bots));
|
||||||
|
if(!$postParams){
|
||||||
|
error(400,"wrong parameters send");
|
||||||
|
die;
|
||||||
|
}else{
|
||||||
|
$bot1=$postParams['bot1'];
|
||||||
|
$bot2=$postParams['bot2'];
|
||||||
|
}
|
||||||
|
//init map
|
||||||
|
$map=array(
|
||||||
|
'0-0' => '','0-1' => '','0-2' => '',
|
||||||
|
'1-0' => '','1-1' => '','1-2' => '',
|
||||||
|
'2-0' => '','2-1' => '','2-2' => '');
|
||||||
|
|
||||||
|
$end=false;
|
||||||
|
|
||||||
|
//send init message to bots
|
||||||
|
$game_id = "".get_unique_id();
|
||||||
|
for($player = 0; $player < 2; $player++){
|
||||||
$params[$player]=array(
|
$params[$player]=array(
|
||||||
'game-id' => $game_id,
|
'game-id' => $game_id,
|
||||||
'action' => 'init',
|
'action' => 'init',
|
||||||
|
|
@ -32,106 +32,117 @@ switch ($_POST['act']){
|
||||||
'players' => 2,
|
'players' => 2,
|
||||||
'board' => '',
|
'board' => '',
|
||||||
'player-index' => $player
|
'player-index' => $player
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
get_IA_Response($bots[$bot1]['url'],$params[0]); //don't care about result
|
get_IA_Response($bots[$bot1]['url'],$params[0]); //don't care about result
|
||||||
get_IA_Response($bots[$bot2]['url'],$params[1]); //don't care about result
|
get_IA_Response($bots[$bot2]['url'],$params[1]); //don't care about result
|
||||||
|
|
||||||
|
|
||||||
$playerPlayingNow=1;
|
$playerPlayingNow=1;
|
||||||
|
|
||||||
while($end==false){
|
while($end==false){
|
||||||
switch($playerPlayingNow){
|
switch($playerPlayingNow){
|
||||||
case 1:
|
case 1:
|
||||||
$playerURL=$bots[$bot1]['url'];
|
$playerURL=$bots[$bot1]['url'];
|
||||||
$playerCHAR='X';
|
$playerCHAR='X';
|
||||||
$playerName=$bots[$bot1]['name'];
|
$playerName=$bots[$bot1]['name'];
|
||||||
$playerIndex=0;
|
$playerIndex=0;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
$playerURL=$bots[$bot2]['url'];
|
$playerURL=$bots[$bot2]['url'];
|
||||||
$playerCHAR='O';
|
$playerCHAR='O';
|
||||||
$playerName=$bots[$bot2]['name'];
|
$playerName=$bots[$bot2]['name'];
|
||||||
$playerIndex=1;
|
$playerIndex=1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
error(500,"oups");
|
error(500,"oups");
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
|
|
||||||
$paramsToSend=array(
|
$paramsToSend=array(
|
||||||
'game-id' => $game_id,
|
'game-id' => $game_id,
|
||||||
'action' => 'play-turn',
|
'action' => 'play-turn',
|
||||||
'game' => 'tictactoe',
|
'game' => 'tictactoe',
|
||||||
'players' => 2,
|
'players' => 2,
|
||||||
'board' => $map,
|
'board' => $map,
|
||||||
'you' => $playerCHAR,
|
'you' => $playerCHAR,
|
||||||
'player-index' =>$playerIndex
|
'player-index' =>$playerIndex
|
||||||
);
|
);
|
||||||
|
|
||||||
$playerResponse=get_IA_Response($playerURL,$paramsToSend)['play'];
|
|
||||||
|
$tempPlayer = get_IA_Response($botUrl,$paramsToSend);
|
||||||
//tester la validité de la réponse
|
if(isset($tempPlayer['responseArr']['play'])){
|
||||||
if((isset($map[$playerResponse])) && ($map[$playerResponse]=="")){
|
$playerResponse = $tempPlayer['responseArr']['play'];
|
||||||
//reponse conforme
|
}else{
|
||||||
echo "<p>".$playerName." joue en ".$playerResponse." la nouvelle grille est <br/>";
|
$anwserPlayer = -1;
|
||||||
$map[$playerResponse]=$playerCHAR;
|
}
|
||||||
echo "<table>";
|
|
||||||
for($j=0;$j<3;$j++){
|
//tester la validité de la réponse
|
||||||
echo "<tr>";
|
if((isset($map[$playerResponse])) && ($map[$playerResponse]=="")){
|
||||||
for($i=0;$i<3;$i++){
|
//reponse conforme
|
||||||
echo '<td class="cellj'.$j.' celli'.$i.'">'.$map[$j.'-'.$i].'</td>';
|
echo "<p>".$playerName." joue en ".$playerResponse." la nouvelle grille est <br/>";
|
||||||
}
|
$map[$playerResponse]=$playerCHAR;
|
||||||
echo "</tr>";
|
echo "<table>";
|
||||||
}
|
for($j=0;$j<3;$j++){
|
||||||
echo "</table>";
|
echo "<tr>";
|
||||||
//tester si trois caracteres allignés
|
for($i=0;$i<3;$i++){
|
||||||
if(
|
echo '<td class="cellj'.$j.' celli'.$i.'">'.$map[$j.'-'.$i].'</td>';
|
||||||
(($map['0-0']==$map['0-1'])&&($map['0-1']==$map['0-2'])&&($map['0-2']!==""))
|
}
|
||||||
OR (($map['1-0']==$map['1-1'])&&($map['1-1']==$map['1-2'])&&($map['1-2']!==""))
|
echo "</tr>";
|
||||||
OR (($map['2-0']==$map['2-1'])&&($map['2-1']==$map['2-2'])&&($map['2-2']!==""))
|
}
|
||||||
OR (($map['0-0']==$map['1-0'])&&($map['1-0']==$map['2-0'])&&($map['2-0']!==""))
|
echo "</table>";
|
||||||
OR (($map['0-1']==$map['1-1'])&&($map['1-1']==$map['2-1'])&&($map['2-1']!==""))
|
//tester si trois caracteres allignés
|
||||||
OR (($map['0-2']==$map['1-2'])&&($map['1-2']==$map['2-2'])&&($map['2-2']!==""))
|
if(
|
||||||
OR (($map['0-0']==$map['1-1'])&&($map['1-1']==$map['2-2'])&&($map['2-2']!==""))
|
(($map['0-0']==$map['0-1'])&&($map['0-1']==$map['0-2'])&&($map['0-2']!==""))
|
||||||
OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!==""))
|
OR (($map['1-0']==$map['1-1'])&&($map['1-1']==$map['1-2'])&&($map['1-2']!==""))
|
||||||
){
|
OR (($map['2-0']==$map['2-1'])&&($map['2-1']==$map['2-2'])&&($map['2-2']!==""))
|
||||||
echo "<p>".$playerName." ".$playerCHAR." a gagné.</p>";
|
OR (($map['0-0']==$map['1-0'])&&($map['1-0']==$map['2-0'])&&($map['2-0']!==""))
|
||||||
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],$playerPlayingNow);
|
OR (($map['0-1']==$map['1-1'])&&($map['1-1']==$map['2-1'])&&($map['2-1']!==""))
|
||||||
$end=true;
|
OR (($map['0-2']==$map['1-2'])&&($map['1-2']==$map['2-2'])&&($map['2-2']!==""))
|
||||||
break;
|
OR (($map['0-0']==$map['1-1'])&&($map['1-1']==$map['2-2'])&&($map['2-2']!==""))
|
||||||
}
|
OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!==""))
|
||||||
//tester si toutes les cases ne seraient pas prises
|
){
|
||||||
$full=true;
|
echo "<p>".$playerName." ".$playerCHAR." a gagné.</p>";
|
||||||
foreach($map as $char){
|
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],$playerPlayingNow);
|
||||||
if($char==""){
|
$end=true;
|
||||||
$full=false;
|
break;
|
||||||
break;
|
}
|
||||||
}
|
//tester si toutes les cases ne seraient pas prises
|
||||||
}
|
$full=true;
|
||||||
if($full){
|
foreach($map as $char){
|
||||||
echo "<p>Match nul</p>";
|
if($char==""){
|
||||||
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],0);
|
$full=false;
|
||||||
$end=true;
|
break;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
|
if($full){
|
||||||
//on change de joueur
|
echo "<p>Match nul</p>";
|
||||||
if($playerPlayingNow==1){
|
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],0);
|
||||||
$playerPlayingNow=2;
|
$end=true;
|
||||||
}else{
|
break;
|
||||||
$playerPlayingNow=1;
|
}
|
||||||
}
|
|
||||||
}else{
|
//on change de joueur
|
||||||
echo "<p>".$playerName." a fait une réponse non conforme. Il perd.</p>";
|
if($playerPlayingNow==1){
|
||||||
break;
|
$playerPlayingNow=2;
|
||||||
|
}else{
|
||||||
}
|
$playerPlayingNow=1;
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
die;
|
echo "<p>".$playerName." made a non conform anwser, he lost: <br/>
|
||||||
break;
|
Bots Arena sent:<em>".$tempPlayer['messageSend']."</em><br/>
|
||||||
default:
|
".$currentBotName." HTTP STATUS:<em> ".$tempPlayer['httpStatus']."</em><br/>
|
||||||
break;
|
His response: <em>".htmlentities($tempPlayer['response'])."</em></p>",
|
||||||
|
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
die;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,5 @@ td {min-width: 10px; height: 10px;}
|
||||||
.arrenaMessage{color: green;}
|
.arrenaMessage{color: green;}
|
||||||
.arrenaMessage::before{content: "arena message:"; color: grey; font-size: 70%; test-transform:italic;}
|
.arrenaMessage::before{content: "arena message:"; color: grey; font-size: 70%; test-transform:italic;}
|
||||||
.botResponse{color: red;}
|
.botResponse{color: red;}
|
||||||
.botResponse::before{content: "bot response:";color: grey; font-size: 70%; test-transform:italic;}
|
.botResponse::before{content: "bot response:";color: grey; font-size: 70%; test-transform:italic;}
|
||||||
|
#fightResult p em {color: grey; font-size: 70%; test-transform:italic;}
|
||||||
Loading…
Reference in New Issue
Block a user