This commit is contained in:
Gnieark 2016-05-27 22:38:24 +02:00
parent 40637305ad
commit 367834b29e

View File

@ -38,6 +38,7 @@ pre{ font-style: normal;font-size: 16px; margin-left: 32px;font-family: Consolas
.battleGrid tr{}
.battleGrid tr td{border: 1px dashed green; text-align: center; font-weight: bold;min-width:20px; height:20px;}
.winCase{background-color:red;}
.hidden{display: none;}
</style>
<script>
@ -49,6 +50,28 @@ var grid=[["","","","","","",""],
["","","","","","",""]];
var currentPlayer=1;
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)
@ -64,40 +87,63 @@ function changePlayerType(player,newValue){
}
}
function playingAT(col){
//hide buttons
document.getElementById("playerButtons").setAttribute("class", "hidden");
if(currentPlayer == 1){
var symbol= "X";
}else{
var symbol="O";
}
//find the first line empty
var i=0;
for(i = 0; i < 6 , grid[i][col] !== ""; i++){
//nothing juste a counter
}
grid[i][col]=symbol;
document.getElementById('td' + col + '_' + i).innerHTML = symbol;
//change player
if(currentPlayer == 1){
play(2);
}else{
play(1);
}
}
function play(player){
currentPlayer=player;
if(document.getElementById("player" + player + "Type").value == "bot"){
//call bot url
if(currentPlayer == 1){
var symbol= "X";
}else{
var symbol="O";
}
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
var response=xhr.responseText;
if (response.match('/[0-6]/')){
playingAT(response);
}else{
alert ('la reponse du bot doit etre un digit compris entre 0 et 7, correspondant à la colonne. Voici sa réponse' + response);
}
}else{
alert (xhr.status);
die;
}
}};
xhr.open("POST", document.getElementById('player' + player + 'URL'), true);
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
// only create 7 buttons
var newTr=createElem('tr',{'id':'playerButtons'});
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);
//wait for human response, show buttons
document.getElementById("playerButtons").setAttribute("class", "");
}
}
@ -118,6 +164,15 @@ function startGame(){
document.getElementById('fightResult').appendChild(table);
var divLogs=createElem("div",{"id":"logs"});
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=[["","","","","","",""],
["","","","","","",""],
@ -125,9 +180,7 @@ function startGame(){
["","","","","","",""],
["","","","","","",""],
["","","","","","",""]];
play(1);
}
</script>
</head>
@ -147,7 +200,7 @@ function startGame(){
</select>
</td>
<td>
<input id="url1" type="text" name="player1URL" placeholder="url du bot http://localhost"/>
<input id="url1" type="text" name="player1URL" placeholder="url du bot http://localhost" value="https://botsarena.tinad.fr/StupidIAconnectFour.php"/>
</td>
</tr>
<tr>
@ -159,7 +212,7 @@ function startGame(){
</select>
</td>
<td>
<input id="url2" type="text" name="player2URL" placeholder="url du bot http://localhost" disabled/>
<input id="url2" type="text" name="player2URL" placeholder="url du bot http://localhost" disabled value="https://botsarena.tinad.fr/StupidIAconnectFour.php"/>
</td>
</tr>
</table>