connectFour Full KS
This commit is contained in:
parent
317d1d1984
commit
1153b253cf
|
@ -50,6 +50,7 @@ var board=[["","","","","","",""],
|
||||||
["","","","","","",""],
|
["","","","","","",""],
|
||||||
["","","","","","",""]];
|
["","","","","","",""]];
|
||||||
var currentPlayer=1;
|
var currentPlayer=1;
|
||||||
|
var gameId="0";
|
||||||
|
|
||||||
function createElem(type,attributes){
|
function createElem(type,attributes){
|
||||||
var elem=document.createElement(type);
|
var elem=document.createElement(type);
|
||||||
|
@ -140,41 +141,45 @@ function play(player){
|
||||||
currentPlayer=player;
|
currentPlayer=player;
|
||||||
if(document.getElementById("player" + player + "Type").value == "bot"){
|
if(document.getElementById("player" + player + "Type").value == "bot"){
|
||||||
//call bot url
|
//call bot url
|
||||||
|
|
||||||
if(currentPlayer == 1){
|
if(currentPlayer == 1){
|
||||||
var symbol= "X";
|
var symbol= "X";
|
||||||
}else{
|
}else{
|
||||||
var symbol="O";
|
var symbol="O";
|
||||||
}
|
}
|
||||||
|
var arrToSend= {
|
||||||
|
"game-id": "" + gameId,
|
||||||
|
"action" : "play-turn",
|
||||||
|
"game" : "connectFour",
|
||||||
|
"players" : 2,
|
||||||
|
"board" : grid,
|
||||||
|
"you" : symbol,
|
||||||
|
"player-index" : player-1
|
||||||
|
};
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
|
xhr.open("POST", document.getElementById('url' + player).value, false);
|
||||||
if(xhr.status == 200) {
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
var response=xhr.responseText;
|
xhr.send(JSON.stringify(arrToSend) );
|
||||||
var t = new RegExp('^[0-6]$');
|
if(xhr.status == 200) {
|
||||||
if (t.test(response)){
|
try{
|
||||||
playingAT(response);
|
var reponse = JSON.parse(xhr.responseText);
|
||||||
}else{
|
var colTarget= reponse['play'];
|
||||||
alert ('la reponse du bot doit etre un digit compris entre 0 et 7, correspondant à la colonne. Voici sa réponse: "' + response + '"' );
|
}catch(e){
|
||||||
}
|
addLog('player ' + player + ' made a non confom response: ' + xhr.responseText);
|
||||||
}else{
|
return;
|
||||||
alert (xhr.status);
|
}
|
||||||
die;
|
|
||||||
}
|
|
||||||
}};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
xhr.open("POST", document.getElementById('url' + player).value, true);
|
|
||||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
||||||
xhr.send('game=connectFour&match_id=666-' + player + '&you=' + symbol + '&grid=' + JSON.stringify(grid) );
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var t = new RegExp('^[0-6]$');
|
||||||
|
if (t.test(colTarget)){
|
||||||
|
playingAT(colTarget);
|
||||||
|
}else{
|
||||||
|
addLog ('bot response must be a digit betwin 0 and 7, witch indicate the conumn number where he wants play. Voici sa réponse: "' + colTarget + '"' );
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
addLog('le bot ' + player + ' does not respond. ' + xhr.status);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
//wait for human response, show buttons
|
//wait for human response, show buttons
|
||||||
document.getElementById("playerButtons").setAttribute("class", "");
|
document.getElementById("playerButtons").setAttribute("class", "");
|
||||||
|
@ -183,7 +188,9 @@ function play(player){
|
||||||
}
|
}
|
||||||
function startGame(){
|
function startGame(){
|
||||||
//empty div
|
//empty div
|
||||||
document.getElementById("fightResult").innerHTML="";
|
while (document.getElementById("fightResult").firstChild) {
|
||||||
|
document.getElementById("fightResult").removeChild(document.getElementById("fightResult").firstChild);
|
||||||
|
}
|
||||||
//create grid
|
//create grid
|
||||||
var table=createElem('table',{'class':'battleGrid', 'id': 'grid'});
|
var table=createElem('table',{'class':'battleGrid', 'id': 'grid'});
|
||||||
for (var i=6; i > -1; i--){
|
for (var i=6; i > -1; i--){
|
||||||
|
@ -214,7 +221,7 @@ function startGame(){
|
||||||
["","","","","","",""],
|
["","","","","","",""],
|
||||||
["","","","","","",""],
|
["","","","","","",""],
|
||||||
["","","","","","",""]];
|
["","","","","","",""]];
|
||||||
|
gameId=Math.floor((Math.random() * 10000) + 1);
|
||||||
//send init messages
|
//send init messages
|
||||||
for (var p = 1; p < 3 ; p++){
|
for (var p = 1; p < 3 ; p++){
|
||||||
|
|
||||||
|
@ -222,9 +229,9 @@ function startGame(){
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open("POST", document.getElementById('url' + p).value, false);
|
xhr.open("POST", document.getElementById('url' + p).value, false);
|
||||||
xhr.setRequestHeader("Content-Type", "application/json");
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
xhr.send(' {"game-id":"1127","action":"init","game":"connectFour","players":2,"board":"","player-index":' + (p - 1) +'}');
|
xhr.send(' {"game-id":"' + gameId + '","action":"init","game":"connectFour","players":2,"board":"","player-index":' + (p - 1) +'}');
|
||||||
if(xhr.status == 200) {
|
if(xhr.status == 200) {
|
||||||
addLog('Message d\'init envoyé au bot player ' + p );
|
addLog('Message d\'init envoyé au bot player ' + p + ' {"game-id":"' + gameId + '","action":"init","game":"connectFour","players":2,"board":"","player-index":' + (p - 1) +'} <br/> il a répondu ' + xhr.responseText );
|
||||||
}else{
|
}else{
|
||||||
addLog('player ' + p + ' n est pas joignable ' + xhr.status);
|
addLog('player ' + p + ' n est pas joignable ' + xhr.status);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -182,7 +182,6 @@ function play(player){
|
||||||
}
|
}
|
||||||
function startGame(){
|
function startGame(){
|
||||||
//empty div
|
//empty div
|
||||||
document.getElementById("fightResult").innerHTML="";
|
|
||||||
while (document.getElementById("fightResult").firstChild) {
|
while (document.getElementById("fightResult").firstChild) {
|
||||||
document.getElementById("fightResult").removeChild(document.getElementById("fightResult").firstChild);
|
document.getElementById("fightResult").removeChild(document.getElementById("fightResult").firstChild);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user