botsArena/src/arenas/tictactoe/act.php

138 lines
3.9 KiB
PHP
Raw Normal View History

2015-11-28 15:10:35 +01:00
<?php
require_once(__DIR__."/functions.php");
switch ($_POST['act']){
case "fight":
2015-11-30 22:28:20 +01:00
$bots=get_Bots_Array('tictactoe');
2015-11-28 15:10:35 +01:00
//clean $_POST vars
2015-11-28 15:57:35 +01:00
$postParams=get_Post_Params(count($bots));
2015-11-28 15:10:35 +01:00
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;
2016-05-29 22:09:53 +02:00
//send init message to bots
2016-05-29 22:35:57 +02:00
$game_id = "".get_unique_id();
2016-05-29 22:12:23 +02:00
for($player = 0; $player < 2; $player++){
2016-05-29 22:09:53 +02:00
$params[$player]=array(
'game-id' => $game_id,
'action' => 'init',
'game' => 'tictactoe',
'players' => 2,
'board' => '',
'player-index' => $player
);
}
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
2016-06-06 13:12:01 +02:00
2016-05-29 22:09:53 +02:00
2015-11-28 15:10:35 +01:00
$playerPlayingNow=1;
2016-05-29 22:09:53 +02:00
2015-11-28 15:10:35 +01:00
while($end==false){
switch($playerPlayingNow){
case 1:
$playerURL=$bots[$bot1]['url'];
$playerCHAR='X';
$playerName=$bots[$bot1]['name'];
2016-05-29 22:09:53 +02:00
$playerIndex=0;
2015-11-28 15:10:35 +01:00
break;
case 2:
$playerURL=$bots[$bot2]['url'];
$playerCHAR='O';
$playerName=$bots[$bot2]['name'];
2016-05-29 22:09:53 +02:00
$playerIndex=1;
2015-11-28 15:10:35 +01:00
break;
default:
error(500,"oups");
die;
2016-06-06 21:59:23 +02:00
}
2016-05-29 22:09:53 +02:00
$paramsToSend=array(
'game-id' => $game_id,
'action' => 'play-turn',
'game' => 'tictactoe',
'players' => 2,
'board' => $map,
2016-06-06 11:46:28 +02:00
'you' => $playerCHAR,
2016-05-29 22:09:53 +02:00
'player-index' =>$playerIndex
);
2016-05-29 22:43:45 +02:00
$playerResponse=get_IA_Response($playerURL,$paramsToSend)['play'];
2015-11-28 15:10:35 +01:00
//tester la validité de la réponse
if((isset($map[$playerResponse])) && ($map[$playerResponse]=="")){
//reponse conforme
echo "<p>".$playerName." joue en ".$playerResponse." la nouvelle grille est <br/>";
$map[$playerResponse]=$playerCHAR;
echo "<table>";
for($j=0;$j<3;$j++){
echo "<tr>";
for($i=0;$i<3;$i++){
echo '<td class="cellj'.$j.' celli'.$i.'">'.$map[$j.'-'.$i].'</td>';
}
echo "</tr>";
}
echo "</table>";
//tester si trois caracteres allignés
if(
(($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']!==""))
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']!==""))
OR (($map['0-1']==$map['1-1'])&&($map['1-1']==$map['2-1'])&&($map['2-1']!==""))
OR (($map['0-2']==$map['1-2'])&&($map['1-2']==$map['2-2'])&&($map['2-2']!==""))
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']!==""))
){
echo "<p>".$playerName." ".$playerCHAR." a gagné.</p>";
2015-11-29 08:19:06 +01:00
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],$playerPlayingNow);
2015-11-28 15:10:35 +01:00
$end=true;
break;
}
//tester si toutes les cases ne seraient pas prises
$full=true;
foreach($map as $char){
if($char==""){
$full=false;
break;
}
}
if($full){
echo "<p>Match nul</p>";
2015-11-29 08:19:06 +01:00
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],0);
2015-11-28 15:10:35 +01:00
$end=true;
break;
}
//on change de joueur
if($playerPlayingNow==1){
$playerPlayingNow=2;
}else{
$playerPlayingNow=1;
}
}else{
2016-06-06 13:12:01 +02:00
echo "<p>".$playerName." a fait une réponse non conforme. Il perd.</p>";
2015-11-28 15:10:35 +01:00
break;
}
}
die;
break;
default:
break;
2015-11-29 08:19:06 +01:00
}