diff --git a/html/StupidIAconnectFour.php b/html/StupidIAconnectFour.php new file mode 100644 index 0000000..fda1d04 --- /dev/null +++ b/html/StupidIAconnectFour.php @@ -0,0 +1,18 @@ + "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; +} \ No newline at end of file diff --git a/src/arenas/connectFour/doc-fr.html b/src/arenas/connectFour/doc-fr.html new file mode 100644 index 0000000..a9ef362 --- /dev/null +++ b/src/arenas/connectFour/doc-fr.html @@ -0,0 +1,46 @@ +

Fonctionnement des duels de puisance 4

+

La grille

+ +

+ +

Requête générée par l'arène (ce site) vers votre bot

+L'arène fait une requête http(s) avec les parametres POST suivants vers votre site: + + + + + + + + + + + + + +
paramètrevaleur
game + Chaîne de caractères, sera toujours "connectFour".
+ Peut servir si votre url sert à plusieurs jeux. +
match_id + Chaîne de caractères. Répond à l'expression régulière suivante: ^[0-9]+-(1|2)$
+ Le premier nombre (digits avant le tiret) identifie le match.
+ Le numéro après le tiret vous indique si vous êtes le bot 1 ou le bot 2 dans l'ordre de jeu.
+ Ça vous servira si votre IA fait des statistiques sur les matchs. +
youvous indique quel est votre symbole dans la grille
gridtableau à deux dimmensions au format JSON vous indiquant l'état de la grille
+ Exemple:
+
+  [["","","","X","0","",""],
+   ["","","","X","","",""],
+   ["","","","","","",""],
+   ["","","","","","",""],
+   ["","","","","","",""],
+   ["","","","","","",""]]
+  
+ +
+ +

Réponse de votre bot

+

Il indique la colonne dans laquelle vous souhaitez jouer 0 à 7

diff --git a/src/arenas/connectFour/functions.php b/src/arenas/connectFour/functions.php new file mode 100644 index 0000000..e2633b4 --- /dev/null +++ b/src/arenas/connectFour/functions.php @@ -0,0 +1,27 @@ + $botsCount)){ + error(400,"wrong parameters"); + die; + } + } + return array('bot1' => $_POST['bot1'],'bot2' => $_POST['bot2']); +} \ No newline at end of file diff --git a/src/arenas/connectFour/js.js b/src/arenas/connectFour/js.js new file mode 100644 index 0000000..58e6939 --- /dev/null +++ b/src/arenas/connectFour/js.js @@ -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'] + '
'; + //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); +} diff --git a/src/arenas/connectFour/public.php b/src/arenas/connectFour/public.php new file mode 100644 index 0000000..9cfbe7e --- /dev/null +++ b/src/arenas/connectFour/public.php @@ -0,0 +1,27 @@ + +
+

+

+ +  VS  + +

+

+
diff --git a/src/arenas/connectFour/style.css b/src/arenas/connectFour/style.css new file mode 100644 index 0000000..7017dda --- /dev/null +++ b/src/arenas/connectFour/style.css @@ -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;} \ No newline at end of file diff --git a/src/arenas_lists.php b/src/arenas_lists.php index 2770c6f..709e4d9 100644 --- a/src/arenas_lists.php +++ b/src/arenas_lists.php @@ -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" ) ); \ No newline at end of file diff --git a/src/functions.php b/src/functions.php index e1af02b..2745efb 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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']){