botsArena/html/testBot.html

230 lines
7.4 KiB
HTML
Raw Normal View History

2016-05-23 21:18:31 +02:00
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="author" content="Gnieark" />
<title>Test ton bot</title>
<style type="text/css">
2016-05-24 20:48:31 +02:00
body{width:100%; font-size:100%; line-height:140%; word-wrap:break-word; text-rendering:optimizelegibility;
margin:0 auto; font-family : "lucida grande", "gill sans", arial, sans-serif; left:auto;}
header{ background-color:#A60800; width: 100%; overflow: hidden; height: auto;}
header h1{display: block; font-size:300%; color:#FFF;float: left; margin-left: 5%;}
header nav{display: block; width: 45%; color:#FFF; float: right;}
#menus{ margin-left: 50px; width:100%; display: table;}
#menus a{color: #fff; display: table-cell; text-decoration: none;text-align: center;border-radius: 15px 15px 0px 0px;}
#menus a.selected{color:#202020; background-color:#fff;}
footer{height: 70px; display: block; color: #343638; font-size: 11pt; line-height: 15pt; margin: 0; padding-top: 15pt;
overflow-x: hidden; box-sizing: border-box; background-image: -webkit-linear-gradient(top, #f5f5f5,#e9e9e9);
border-top: 1px solid #bebebe; color: #999; font-size: 12px; line-height: 1.5em; text-align: center;width: 100%;}
footer a {margin:0px 15px 0px 15px; color: #666;text-decoration: none; font-weight: normal;}
#languages{float: right; text-align: right;}
section{margin: 0 auto; width: 90%;}
article{float: right; width:70%;}
aside{float:left; width: 28%; border-right: 1px dashed green;}
aside table {width: 90%;}
aside table tr td{width: 33%;}
aside table tr td input{width: 100%;}
2016-05-23 21:18:31 +02:00
.center{text-align: center;}
aside p img{ width: 100%; max-width:342px;}
form textarea, form input, form select {width:40%;}
form input[type=checkbox], form input[type=radio] { width:15px; }
2016-05-24 20:48:31 +02:00
form label {float:left; width:40%; margin-right:0.5em;
padding-top:0.2em; text-align:right;}
pre{ font-style: normal;font-size: 16px; margin-left: 32px;font-family: Consolas, "Times New Roman", Verdana;
border-left: 4px solid #CCC; padding-left: 8px;}
.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;}
.winCase{background-color:red;}
2016-05-27 22:38:24 +02:00
.hidden{display: none;}
2016-05-24 20:48:31 +02:00
</style>
<script>
2016-05-26 00:00:54 +02:00
var grid=[["","","","","","",""],
["","","","","","",""],
["","","","","","",""],
["","","","","","",""],
["","","","","","",""],
["","","","","","",""]];
var currentPlayer=1;
2016-05-27 22:38:24 +02:00
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;
}
2016-05-24 20:48:31 +02:00
function createElem(type,attributes){
var elem=document.createElement(type);
for (var i in attributes)
{elem.setAttribute(i,attributes[i]);}
return elem;
}
function changePlayerType(player,newValue){
if(newValue == "bot"){
document.getElementById('url' + player).disabled="";
}else{
document.getElementById('url' + player).disabled="disabled";
2016-05-23 21:18:31 +02:00
}
}
2016-05-26 00:00:54 +02:00
function playingAT(col){
2016-05-27 22:38:24 +02:00
//hide buttons
document.getElementById("playerButtons").setAttribute("class", "hidden");
2016-05-26 00:00:54 +02:00
if(currentPlayer == 1){
2016-05-27 22:38:24 +02:00
var symbol= "X";
2016-05-26 00:00:54 +02:00
}else{
2016-05-27 22:38:24 +02:00
var symbol="O";
2016-05-26 00:00:54 +02:00
}
2016-05-27 22:38:24 +02:00
//find the first line empty
2016-05-26 00:00:54 +02:00
var i=0;
for(i = 0; i < 6 , grid[i][col] !== ""; i++){
2016-05-27 22:38:24 +02:00
//nothing juste a counter
2016-05-26 00:00:54 +02:00
}
grid[i][col]=symbol;
document.getElementById('td' + col + '_' + i).innerHTML = symbol;
2016-05-27 22:38:24 +02:00
//change player
if(currentPlayer == 1){
play(2);
}else{
play(1);
}
2016-05-26 00:00:54 +02:00
}
function play(player){
2016-05-27 22:38:24 +02:00
currentPlayer=player;
2016-05-26 00:00:54 +02:00
if(document.getElementById("player" + player + "Type").value == "bot"){
//call bot url
2016-05-27 22:38:24 +02:00
if(currentPlayer == 1){
var symbol= "X";
}else{
var symbol="O";
2016-05-26 00:00:54 +02:00
}
2016-05-27 22:38:24 +02:00
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
var response=xhr.responseText;
2016-05-27 23:00:42 +02:00
var t = new RegExp('^[0-6]$');
2016-05-27 22:56:26 +02:00
if (t.test(response)){
2016-05-27 22:38:24 +02:00
playingAT(response);
}else{
2016-05-27 22:47:32 +02:00
alert ('la reponse du bot doit etre un digit compris entre 0 et 7, correspondant à la colonne. Voici sa réponse ' + response);
2016-05-27 22:38:24 +02:00
}
}else{
2016-05-27 22:43:39 +02:00
2016-05-27 22:38:24 +02:00
alert (xhr.status);
die;
}
}};
2016-05-27 22:46:00 +02:00
xhr.open("POST", document.getElementById('url' + player).value, true);
2016-05-27 22:38:24 +02:00
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send('game=connectFour&match_id=666-' + player + '&you=' + symbol + '&grid=' + JSON.stringify(grid) );
}else{
//wait for human response, show buttons
document.getElementById("playerButtons").setAttribute("class", "");
2016-05-26 00:00:54 +02:00
}
2016-05-23 21:18:31 +02:00
2016-05-24 20:48:31 +02:00
}
function startGame(){
//empty div
document.getElementById("fightResult").innerHTML="";
//create grid
2016-05-26 00:00:54 +02:00
var table=createElem('table',{'class':'battleGrid', 'id': 'grid'});
2016-05-24 20:48:31 +02:00
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"});
2016-05-27 22:38:24 +02:00
document.getElementById('fightResult').appendChild(divLogs);
var newTr=createElem('tr',{'id':'playerButtons','class':'hidden'});
for(var i = 0; i < 7; i++){
var newTd=createElem('td');
var button=createElem('input',{'type':'button','value': i, 'onclick': "playingAT('" + i + "');"});
newTd.appendChild(button);
newTr.appendChild(newTd);
}
document.getElementById('grid').appendChild(newTr);
grid=[["","","","","","",""],
["","","","","","",""],
["","","","","","",""],
["","","","","","",""],
["","","","","","",""],
["","","","","","",""]];
play(1);
2016-05-24 20:48:31 +02:00
}
</script>
2016-05-23 21:18:31 +02:00
</head>
<body>
<header>
<h1>Debug and test your connectFour AI</h1>
</header>
<section>
<aside>
<h2>Configure the test</h2>
2016-05-24 20:48:31 +02:00
<table>
<tr>
<td>Player 1</td>
2016-05-26 00:00:54 +02:00
<td><select id="player1Type" name="player1Type" onchange="changePlayerType(1,this.value);">
2016-05-24 20:48:31 +02:00
<option value="bot">bot</option>
<option value="human">human</option>
</select>
</td>
<td>
2016-05-27 22:38:24 +02:00
<input id="url1" type="text" name="player1URL" placeholder="url du bot http://localhost" value="https://botsarena.tinad.fr/StupidIAconnectFour.php"/>
2016-05-24 20:48:31 +02:00
</td>
</tr>
<tr>
<td>Player 2</td>
<td>
2016-05-26 00:00:54 +02:00
<select id="player2Type" name="player2Type" onchange="changePlayerType(2,this.value);">
2016-05-24 20:48:31 +02:00
<option value="human">human</option>
<option value="bot">bot</option>
</select>
</td>
<td>
2016-05-27 22:38:24 +02:00
<input id="url2" type="text" name="player2URL" placeholder="url du bot http://localhost" disabled value="https://botsarena.tinad.fr/StupidIAconnectFour.php"/>
2016-05-24 20:48:31 +02:00
</td>
</tr>
</table>
2016-05-23 21:18:31 +02:00
<p><input type="button" onclick="startGame()" value="Fight"/></p>
</aside>
2016-05-24 20:48:31 +02:00
<article id="fightResult">
</article>
2016-05-23 21:18:31 +02:00
</section>
<footer>
<a href="/p/About">About</a><a href="https://github.com/gnieark/botsArena">Bots'Arena source code</a><a href="/p/legals">Mentions légales</a>
</footer>
</body>
</html>