Merge pull request #47 from gnieark/dev

Dev
This commit is contained in:
Gnieark 2016-05-15 14:52:09 +02:00
commit d0bc030a48
9 changed files with 457 additions and 0 deletions

View File

@ -0,0 +1,18 @@
<?php
/*
* stupid IA for battle ship
* choose by random a free column
*/
$grid=json_decode($_POST['grid']);
$colAvailable=array();
for($i=0;$i<7;$i++){
if($grid[5][$i] == ""){
$colAvailable[]=$i;
}
}
shuffle($colAvailable);
echo $colAvailable[0];
die;

View File

@ -0,0 +1,243 @@
<?php
require_once(__DIR__."/functions.php");
$bots=get_Bots_Array('connectFou');
switch ($_POST['act']){
case "newFight":
//remove $_SESSION less xd_check
$xd=$_SESSION['xd_check'];
session_unset();
$_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";
//echo "plop".json_encode($_SESSION['map']);
case "fight":
if($_SESSION['game'] <> "connectFou"){
error(500,"game non found");
}
//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";
}
}
//make post datas to send
$postDatas=array(
'game' => 'connectFour',
'match_id' => $_SESSION['matchId']."-".$_SESSION['currentPlayer'],
'opponent' => $opponentName,
'you' => $you,
'grid' => json_encode( $_SESSION['map'])
);
//send query
$anwserPlayer=get_IA_Response($botUrl,$postDatas);
//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;
while(($x > 0) && ($y < 5) && ($_SESSION['map'][$y + 1][$x - 1] == $you)){
$x--;
$y++;
$count++;
}
$x=$strikeX;
$y=$strikeY;
while(($x < 6) && ($y > 0) && ($_SESSION['map'][$y - 1][$x + 1] == $you)){
$x++;
$y--;
$count++;
}
if($count>3){
$wins=true;
}
//diagonale /
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
while(($x < 6) && ($y < 5) && ($_SESSION['map'][$y + 1][$x + 1 ] == $you)){
$x++;
$y++;
$count++;
}
$x=$strikeX;
$y=$strikeY;
while(($x > 0) && ($y > 0) && ($_SESSION['map'][$y - 1][$x - 1 ] == $you)){
$x--;
$y--;
$count++;
}
if($count>3){
$wins=true;
}
}
//horizontale
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
while(($x < 6) && ($_SESSION['map'][$y][$x + 1 ] == $you)){
$x++;
$count++;
}
$x=$strikeX;
while(($x >0) && ($_SESSION['map'][$y][$x - 1 ] == $you)){
$count++;
$x--;
}
if($count>3){
$wins=true;
}
}
//verticale
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
while(($y < 5) && ($_SESSION['map'][$y + 1 ][$x] == $you)){
$y++;
$count++;
}
$y=$strikeY;
while(($y >0) && ($_SESSION['map'][$y - 1][$x] == $you)){
$count++;
$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é"
);
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{
$anwserToJS=array(
'continue' => 1,
'strikeX' => $strikeX,
'strikeY' => $strikeY,
'strikeSymbol'=> $you,
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer
);
}
}else{
//reponse non conforme
$anwserToJS=array(
'continue' =>0,
'strikeX' => -1,
'strikeY' => -1,
'log' => $you." ".$currentBotName." a fait une réponse non conforme, il perd"
);
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;
}

View File

@ -0,0 +1,46 @@
<h2>Fonctionnement des duels de puisance 4</h2>
<h3>La grille</h3>
<ul>
<li>largeur: 7 cases</li>
<li>hauteur: 6 cases</li>
</ul>
</p>
<h3>Requ&ecirc;te générée par l'arène (ce site) vers votre bot</h3>
L'arène fait une requ&ecirc;te http(s) avec les parametres POST suivants vers votre site:
<table class="tabledoc">
<tr><th>paramètre</th><th>valeur</th></tr>
<tr>
<td>game</td>
<td>
Chaîne de caractères, sera toujours "connectFour".<br/>
Peut servir si votre url sert à plusieurs jeux.
</td>
</tr>
<tr>
<td>match_id</td>
<td>
Chaîne de caractères. Répond à l'expression régulière suivante: ^[0-9]+-(1|2)$<br/>
Le premier nombre (digits avant le tiret) identifie le match.<br/>
Le numéro après le tiret vous indique si vous êtes le bot 1 ou le bot 2 dans l'ordre de jeu.<br/>
Ça vous servira si votre IA fait des statistiques sur les matchs.
</td>
</tr>
<tr><td>you</td><td>vous indique quel est votre symbole dans la grille</td></tr>
<tr><td>grid</td><td>tableau à deux dimmensions au format JSON vous indiquant l'état de la grille<br/>
Exemple:<br/>
<pre>
[["","","","X","0","",""],
["","","","X","","",""],
["","","","","","",""],
["","","","","","",""],
["","","","","","",""],
["","","","","","",""]]
</pre>
</td></tr>
</table>
<h3>Réponse de votre bot</h3>
<p>Il indique la colonne dans laquelle vous souhaitez jouer 0 à 7</p>

View File

@ -0,0 +1,27 @@
<?php
function get_IA_Response($iaUrl,$postParams){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $iaUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return htmlentities($output);
}
function get_Post_Params($botsCount){
$keysBots=array('bot1','bot2');
foreach($keysBots as $botKey){
if(!isset($_POST[$botKey])){
return false;
}
if(!is_numeric(($_POST[$botKey]))){
}
if(($_POST[$botKey] < 0) OR ($_POST[$botKey] > $botsCount)){
error(400,"wrong parameters");
die;
}
}
return array('bot1' => $_POST['bot1'],'bot2' => $_POST['bot2']);
}

View File

@ -0,0 +1,80 @@
function Ajx(){
var request = false;
try {request = new ActiveXObject('Msxml2.XMLHTTP');}
catch (err2) {
try {request = new ActiveXObject('Microsoft.XMLHTTP');}
catch (err3) {
try { request = new XMLHttpRequest();}
catch (err1) {
request = false;
}
}
}
return request;
}
function createElem(type,attributes){
var elem=document.createElement(type);
for (var i in attributes)
{elem.setAttribute(i,attributes[i]);}
return elem;
}
function connectFour(bot1,bot2,xd_check, newGame){
if (newGame === undefined){
newGame = true;
}
if (newGame){
//empty
while (document.getElementById('fightResult').firstChild) {
document.getElementById('fightResult').removeChild(document.getElementById('fightResult').firstChild);
}
//create grid
var table=createElem('table',{'class':'battleGrid'});
for (var i=6; i > -1; i--){
var tr=createElem('tr');
for (var j=0;j<7; j++){
var td=createElem('td',{'id': 'td' + j + '_' + i});
tr.appendChild (td);
}
table.appendChild(tr);
}
document.getElementById('fightResult').appendChild(table);
var divLogs=createElem("div",{"id":"logs"});
document.getElementById('fightResult').appendChild(divLogs);
}
//send request
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
try{
var reponse = JSON.parse(xhr.responseText);
}catch(e){
document.getElementById('logs').innerHTML += 'erreur' +xhr.responseText;
return;
}
//log
document.getElementById('logs').innerHTML += reponse['log'] + '<br/>';
//fill the grid
if( reponse['strikeX'] > -1){
document.getElementById('td' + reponse['strikeX'] + '_' + reponse['strikeY']).innerHTML = reponse['strikeSymbol'];
}
//if game isn't finished, continue
if(reponse['continue'] == 1){
connectFour(bot1,bot2,xd_check, false);
}
}else{
alert ('error ' + xhr.status);
return;
}
}};
xhr.open("POST", '/connectFour', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
if(newGame){
var act='newFight';
}else{
var act='fight';
}
xhr.send('act=' + act + '&bot1=' + bot1 + '&bot2=' + bot2 + '&xd_check=' + xd_check);
}

View File

@ -0,0 +1,27 @@
<?php
require_once(__DIR__."/functions.php");
$bots=get_Bots_Array('connectFou');
?>
<article>
<h2><?php echo $lang['MAKE_DUEL'];?></h2>
<p>
<select name="bot1" id="bot1">
<?php
foreach($bots as $bot){
echo '<option value="'.$bot['id'].'">'.$bot['name'].'</option>';
}
?>
</select>
&nbsp;VS&nbsp;
<select name="bot2" id="bot2">
<?php
foreach($bots as $bot){
echo '<option value="'.$bot['id'].'">'.$bot['name'].'</option>';
}
?>
</select>
</p>
<p><input type="button" value="<?php echo $lang['FIGHT']; ?>" onclick="connectFour(document.getElementById('bot1').value,document.getElementById('bot2').value,'<?php echo xd_check_input(2); ?>');"></p>
<div id="fightResult"></div>

View File

@ -0,0 +1,7 @@
.tabledoc{border-collapse:collapse;}
.tabledoc tr td,.tabledoc tr th{border: 1px solid green; padding-left: 5px;}
#fightResult{display:table;}
#logs{display:block;padding-left:10px; height: 200px; overflow-y: scroll;}
.battleGrid{display:table-cell; padding-left:10px; border-collapse:collapse; margin: 20px 20px 20px 20px;}
.battleGrid tr{}
.battleGrid tr td{border: 1px dashed green; text-align: center; font-weight: bold;min-width:20px; height:20px;}

View File

@ -15,6 +15,14 @@ $arenas=array(
'metaDescription' => 'Affrontements de bots à la battaille navale',
'jsFile'=> "js.js",
'cssFile'=> "style.css"
),
array(
'id' => "connectFour",
'url' => "/connectFour",
'title' => "Puissance 4",
'metaDescription' => 'Affrontements de bots puissance 4',
'jsFile'=> "js.js",
'cssFile'=> "style.css"
)
);

View File

@ -227,6 +227,7 @@ function get_unique_id(){
fclose($fp);
return $count;
}
function does_arena_exist($string,$arenasArr){
foreach($arenasArr as $arena){
if($string == $arena['id']){