jocly/examples/browser/js/simple.js

67 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-03-29 17:30:37 +02:00
function NotifyWinner(winner) {
2017-04-04 23:55:50 +02:00
var txt = "?";
if(winner==Jocly.PLAYER_A)
txt = "Player A wins";
else if(winner==Jocly.PLAYER_B)
txt = "Player B wins";
else if(winner==Jocly.DRAW)
txt = "Draw";
2017-03-29 17:30:37 +02:00
alert(txt);
}
2017-04-04 23:55:50 +02:00
function RunMatch(match, progressBar) {
function NextMove() {
match.getTurn()
.then( (player) => {
var promise;
if(player==Jocly.PLAYER_A)
promise = match.userTurn();
else {
if(progressBar) {
progressBar.style.display = "block";
progressBar.style.width = 0;
}
promise = match.machineSearch({
progress: (progress) => {
if(progressBar)
progressBar.style.width = progress +"%";
}
})
.then( (result) => {
return match.playMove(result.move);
})
.then( () => {
if(progressBar)
progressBar.style.display = "none";
});
}
promise.then( () => {
return match.getFinished()
})
.then( (result) => {
if(result.finished)
NotifyWinner(result.winner);
else
NextMove();
});
})
2017-03-29 17:30:37 +02:00
}
2017-04-04 23:55:50 +02:00
NextMove();
2017-03-29 17:30:37 +02:00
}
window.addEventListener("DOMContentLoaded", function () {
progressBar = document.getElementById("progress-bar");
2017-04-04 23:55:50 +02:00
var m = /\?game=([^&]+)/.exec(window.location.href);
var gameName = m && m[1] || "classic-chess";
2017-03-29 17:30:37 +02:00
var elementId = "game-area1";
var area = document.getElementById(elementId);
2017-04-04 23:55:50 +02:00
Jocly.createMatch(gameName).then((match) => {
match.attachElement(area)
.then( () => {
RunMatch(match,progressBar);
2017-03-29 17:30:37 +02:00
});
});
});