Modification du script arbitre

Suppression des champs libres dans le formulaires. l'ajour d'une IA dans la liste déroulante se fait en éditant un fichier sur le dépot.
master
Gnieark 9 years ago
parent a2f583cd0d
commit a48c31e3f7

@ -0,0 +1,32 @@
<?php
function getBotsArray(){
//Recupérer la liste des Bots
$bots=array();
$botsList=explode("\n",file_get_contents("listOfBots.txt"));
foreach($botsList as $botLigne){
if(preg_match("/\ (http|https):\/\//", $botLigne)){
list($name,$url)=explode(" ",$botLigne);
$bots[]=array("name" => $name, "url" =>$url);
}
}
return $bots;
}
function getIAResponse($youChar,$iaBaseUrl,$grille){
/*transforme la grille en parametres http GET
* et fait la requete vers $iaBaseUrl
* Retourne la réponse de l'IA
*/
$paramsGrille="";
foreach($grille as $key => $case){
$paramsGrille.="&".$key."=".$case;
}
$url=$iaBaseUrl."?you=".$youChar.$paramsGrille;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return htmlentities($output);
}

@ -1,149 +1,226 @@
<?php <?php
if((isset($_POST['ia1URL'])) OR (isset($_POST['ia1URL']))){ require_once("_functions.php");
if( $bots=getBotsArray();
((filter_var($_POST['ia1URL'], FILTER_VALIDATE_URL))===false)
OR ((filter_var($_POST['ia2URL'], FILTER_VALIDATE_URL))===false)
OR (!preg_match("/^(http|https):\/\//", $_POST['ia1URL']))
OR (!preg_match("/^(http|https):\/\//", $_POST['ia2URL']))
)
{echo "erreur"; die;}
}
?><!DOCTYPE html><html lang="fr"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Gnieark"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Arbitre Morpion</title>
<style type="text/css">
<!-- @font-face {font-family: 'Special Elite';font-style: normal; font-weight: 400; src: local('Special Elite'), local('SpecialElite-Regular'), url(http://themes.googleusercontent.com/static/fonts/specialelite/v4/9-wW4zu3WNoD5Fjka35Jm_n8qdNnd5eCmWXua5W-n7c.woff) format('woff');}
body{width:100%;font-size:100%; top: 0px;line-height:140%;word-wrap:break-word;text-rendering:optimizelegibility;margin:0 auto;font-family : "lucida grande", "gill sans", arial, sans-serif;}
header{background-color:#502FD2; width: 100%; overflow: hidden;height: auto;}
header h1{display: block; font-size:300%; color:#FFF;float: left; width: 45%;font-family: 'Special Elite', cursive;margin-left: 5%;}
header p{display: block; width: 45%;color:#FFF; float: left;}
section {border-bottom: 1px solid rgb(204, 204, 204); margin: 0 auto;overflow: hidden;width: 90%;}
-->
</style>
</head>
<body>
<header><h1>Arène à morpions</h1></header>
<section>
<form method="POST" action="#">
<p>
<input type="text" placeholder="URL de la premiere IA" name="ia1URL"<?php if(isset($_POST['ia1URL'])){echo "value=\"".$_POST['ia1URL']."\"";} ?>/>
<input type="text" placeholder="X ou O" name="youIA1" <?php if(isset($_POST['youIA1'])){echo "value=\"".$_POST['youIA1']."\"";} ?>/>
</p>
<p>
<input type="text" placeholder="URL de la deuxième IA" name="ia2URL" <?php if(isset($_POST['ia2URL'])){echo "value=\"".$_POST['ia2URL']."\"";} ?>/>
<input type="text" placeholder="X ou O" name="youIA2" <?php if(isset($_POST['youIA2'])){echo "value=\"".$_POST['youIA2']."\"";} ?>/>
</p> if(isset($_POST['act'])){
<p><input type="submit" value="Fight!" name="submit"> //un formulaire a été soumis
</form> switch ($_POST['act']){
<pre> case "reDownloadBotsList":
<?php file_put_contents("listOfBots.txt", file_get_contents("https://raw.githubusercontent.com/jeannedhack/programmingChallenges/master/morpionsFights/listOfBots.txt"));
if(isset($_POST['submit'])){ $bots=getBotsArray();
//initialiser la grille break;
$grille=array( case "fight":
//nettoyer les variables en entrée
/*
* $_POST['bot1'] et $_POST['bot2']
* doivent exister
* etres numériques
* etre compris entre 0 et le nombre de bots
*/
$keysBots=array('bot1','bot2');
foreach($keysBots as $botKey){
if(!isset($_POST[$botKey])){
echo "une erreur est survenue"; die;
}
if(!is_numeric(($_POST[$botKey]))){
echo '<script>window.location = "http://rickrolled.fr/";</script>'; die;
}
if(($_POST[$botKey] < 0) OR ($_POST[$botKey] > count($bots))){
echo "une erreur est survenue"; die;
}
}
$bot1=$_POST['bot1'];
$bot2=$_POST['bot2'];
//initialiser la grille
$grille=array(
'0-0' => '','0-1' => '','0-2' => '', '0-0' => '','0-1' => '','0-2' => '',
'1-0' => '','1-1' => '','1-2' => '', '1-0' => '','1-1' => '','1-2' => '',
'2-0' => '','2-1' => '','2-2' => ''); '2-0' => '','2-1' => '','2-2' => '');
$end=false; $end=false;
$playerEnCours=1; $playerEnCours=1;
while($end==false){ while($end==false){
switch($playerEnCours){ switch($playerEnCours){
case 1: case 1:
$playerURL=$_POST['ia1URL']; $playerURL=$bots[$bot1]['url'];
$playerCHAR=$_POST['youIA1']; $playerCHAR='X';
$playerName=$bots[$bot1]['name'];
break; break;
case 2: case 2:
$playerURL=$_POST['ia2URL']; $playerURL=$bots[$bot2]['url'];
$playerCHAR=$_POST['youIA2']; $playerCHAR='O';
$playerName=$bots[$bot2]['name'];
break; break;
default: default:
echo "une erreur est survenue"; echo "une erreur est survenue";
die; die;
} }
$playerRep=getIAResponse($playerCHAR,$playerURL,$grille); $playerRep=getIAResponse($playerCHAR,$playerURL,$grille);
echo "Reponse: ".$playerRep."\n";
//tester la validité de la réponse //tester la validité de la réponse
if((isset($grille[$playerRep])) && ($grille[$playerRep]=="")){ if((isset($grille[$playerRep])) && ($grille[$playerRep]=="")){
//reponse conforme //reponse conforme
echo $playerCHAR." joue en ".$playerRep." la nouvelle grille est \n"; echo "<p>".$playerName." joue en ".$playerRep." la nouvelle grille est <br/>";
$grille[$playerRep]=$playerCHAR; $grille[$playerRep]=$playerCHAR;
for($j=0;$j<3;$j++){ echo "<table>";
for($i=0;$i<3;$i++){ for($j=0;$j<3;$j++){
echo $grille[$j."-".$i]; echo "<tr>";
if ($grille[$j."-".$i]==""){ for($i=0;$i<3;$i++){
echo " "; echo "<td>".$grille[$j."-".$i]."</td>";
} }
} echo "</tr>";
echo "\n"; }
} echo "</table>";
//tester si trois caracteres allignés //tester si trois caracteres allignés
if( if(
(($grille['0-0']==$grille['0-1'])&&($grille['0-1']==$grille['0-2'])&&($grille['0-2']!=="")) (($grille['0-0']==$grille['0-1'])&&($grille['0-1']==$grille['0-2'])&&($grille['0-2']!==""))
OR (($grille['1-0']==$grille['1-1'])&&($grille['1-1']==$grille['1-2'])&&($grille['1-2']!=="")) OR (($grille['1-0']==$grille['1-1'])&&($grille['1-1']==$grille['1-2'])&&($grille['1-2']!==""))
OR (($grille['2-0']==$grille['2-1'])&&($grille['2-1']==$grille['2-2'])&&($grille['2-2']!=="")) OR (($grille['2-0']==$grille['2-1'])&&($grille['2-1']==$grille['2-2'])&&($grille['2-2']!==""))
OR (($grille['0-0']==$grille['1-0'])&&($grille['1-0']==$grille['2-0'])&&($grille['2-0']!=="")) OR (($grille['0-0']==$grille['1-0'])&&($grille['1-0']==$grille['2-0'])&&($grille['2-0']!==""))
OR (($grille['0-1']==$grille['1-1'])&&($grille['1-1']==$grille['2-1'])&&($grille['2-1']!=="")) OR (($grille['0-1']==$grille['1-1'])&&($grille['1-1']==$grille['2-1'])&&($grille['2-1']!==""))
OR (($grille['0-2']==$grille['1-2'])&&($grille['1-2']==$grille['2-2'])&&($grille['2-2']!=="")) OR (($grille['0-2']==$grille['1-2'])&&($grille['1-2']==$grille['2-2'])&&($grille['2-2']!==""))
OR (($grille['0-0']==$grille['1-1'])&&($grille['1-1']==$grille['2-2'])&&($grille['2-2']!=="")) OR (($grille['0-0']==$grille['1-1'])&&($grille['1-1']==$grille['2-2'])&&($grille['2-2']!==""))
OR (($grille['0-2']==$grille['1-1'])&&($grille['1-1']==$grille['2-0'])&&($grille['2-0']!=="")) OR (($grille['0-2']==$grille['1-1'])&&($grille['1-1']==$grille['2-0'])&&($grille['2-0']!==""))
){ ){
echo "le joueur ".$playerCHAR." a gagné."; echo "<p>".$playerName." ".$playerCHAR." a gagné.</p>";
$end=true; $end=true;
break; break;
} }
//tester si toutes les cases ne seraient pas prises //tester si toutes les cases ne seraient pas prises
$full=true; $full=true;
foreach($grille as $char){
foreach($grille as $char){ if($char==""){
if($char==""){ $full=false;
$full=false; break;
break; }
} }
} if($full){
if($full){ echo "<p>Match nul</p>";
echo "Match nul"; $end=true;
$end=true; break;
break; }
}
//on change de joueur
//on change de joueur if($playerEnCours==1){
if($playerEnCours==1){ $playerEnCours=2;
$playerEnCours=2; }else{
}else{ $playerEnCours=1;
$playerEnCours=1; }
} }else{
}else{ echo "<p>".$playerName." a fait une réponse non conforme. Il perd</p>";
echo "le joueur ".$playerCHAR." a fait une réponse non conforme. Il perd"; break;
break;
}
}
}
}
function getIAResponse($youChar,$iaBaseUrl,$grille){
$paramsGrille="";
foreach($grille as $key => $case){
$paramsGrille.="&".$key."=".$case;
} }
$url=$iaBaseUrl."?you=".$youChar.$paramsGrille;
echo "\n".$url."\n"; die;
//return http_get($url); break;
$ch = curl_init(); default:
curl_setopt($ch, CURLOPT_URL, $url); break;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); }
// $output contains the output string }else{
$output = curl_exec($ch); $bot1=0;
// close curl resource to free up system resources $bot2=0;
curl_close($ch);
return htmlentities($output);
} }
?><!DOCTYPE html><html lang="fr"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Gnieark"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Arbitre Morpion</title>
<style type="text/css">
<!-- @font-face {font-family: 'Special Elite';font-style: normal; font-weight: 400; src: local('Special Elite'), local('SpecialElite-Regular'), url(http://themes.googleusercontent.com/static/fonts/specialelite/v4/9-wW4zu3WNoD5Fjka35Jm_n8qdNnd5eCmWXua5W-n7c.woff) format('woff');}
body{width:100%;font-size:100%; top: 0px;line-height:140%;word-wrap:break-word;text-rendering:optimizelegibility;margin:0 auto;font-family : "lucida grande", "gill sans", arial, sans-serif;}
header{background-color:#502FD2; width: 100%; overflow: hidden;height: auto;}
header h1{display: block; font-size:300%; color:#FFF;float: left; width: 45%;font-family: 'Special Elite', cursive;margin-left: 5%;}
header p{display: block; width: 45%;color:#FFF; float: left;}
section {border-bottom: 1px solid rgb(204, 204, 204); margin: 0 auto;overflow: hidden;width: 90%;}
-->
</style>
<script type="text/javascript">
<!--
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 tictactoe(bot1,bot2){
document.getElementById('fightResult').innerHTML = '<p>Please wait...</p>';
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
document.getElementById('fightResult').innerHTML = xhr.responseText;
}
}};
xhr.open("POST", 'index.php', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send('act=fight&bot1=' + bot1 + '&bot2=' + bot2);
}
?> function refreshBots(){
</pre> var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
window.location.reload();
}
}};
xhr.open("POST", 'index.php', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send('act=reDownloadBotsList');
}
-->
</script>
</head>
<body>
<header><h1>Arène à morpions</h1></header>
<section>
<article>
<p>Bienvenue sur notre cette Arène pour pseudos intelligences artificielles de morpions.</p>
<p>L'explication sur quoi que c'est ce site est sur la page <a href="https://github.com/jeannedhack/programmingChallenges/tree/master/morpionsFights">challenge de programmation Morpion</a>.</p>
<p>Pour ajouter un boot, ajoutez une ligne contenant votre pseudo et l'url dans le fichier
<a href="https://github.com/jeannedhack/programmingChallenges/blob/master/morpionsFights/Master/listOfBots.txt">listOfBots.txt</a> et cliquez sur <a href="#" onclick="refreshBots();"> Raffraichir la liste des bots</a>.</p>
</article>
<article>
<h2>Lancer un combat</h2>
<p>
<select name="bot1" id="bot1">
<?php
for($i=0;$i<count($bots);$i++){
if($i==$bot1)
$selected='selected="selected"';
else
$selected='';
echo '<option value="'.$i.'" '.$selected.'>'.$bots[$i]['name'].'</option>';
}
?>
</select>
&nbsp;VS&nbsp;
<select name="bot2" id="bot2">
<?php
for($i=0;$i<count($bots);$i++){
if($i==$bot2)
$selected='selected="selected"';
else
$selected='';
echo '<option value="'.$i.'" '.$selected.'>'.$bots[$i]['name'].'</option>';
}
?>
</select>
</p>
<p><input type="button" value="Fight!" onclick="tictactoe(document.getElementById('bot1').value,document.getElementById('bot2').value);"></p>
</article>
<article id="fightResult"></article>
</section> </section>
</body> </body>
</html> </html>

@ -0,0 +1,3 @@
stupidAI http://morpionmaster.tinad.fr/stupidIa.php
Moul http://tictactoe.m.42.am/
Gnieark http://morpionmaster.tinad.fr/gnieark.php
Loading…
Cancel
Save