/* * Displays winner */ function NotifyWinner(winner) { var text = "Draw"; if(winner==Jocly.PLAYER_A) text = "A wins"; else if(winner==Jocly.PLAYER_B) text = "B wins"; $("#game-status").text(text); } /* * Run the game */ var movePending = null; function RunMatch(match, progressBar) { var movePendingResolver; // first make sure there is no user input or machine search in progress var promise = match.abortUserTurn() // just in case one is running .then( () => { return match.abortMachineSearch(); // just in case one is running }); function NextMove() { if(movePending) return; movePending = new Promise((resolve,reject)=>{ movePendingResolver = resolve; }); // whose turn is it ? match.getTurn() .then((player) => { // display whose turn $("#game-status").text(player==Jocly.PLAYER_A?"A playing":"B playing"); var mode = $("#mode").val(); var promise = Promise.resolve(); if((player==Jocly.PLAYER_A && (mode=="self-self" || mode=="self-comp")) || (player==Jocly.PLAYER_B && (mode=="self-self" || mode=="comp-self"))) // user to play promise = promise.then( () => { // reques user input return match.userTurn() }) else { // machine to play if(progressBar) { progressBar.style.display = "block"; progressBar.style.width = 0; } promise = promise.then( () => { return match.getConfig(); }) .then( (config) => { // get machine level var which = player==Jocly.PLAYER_A ? "a" : "b"; var levelIndex = $("#select-level-"+which).val(); var level = { maxDepth: -1, // kind of random } if(levelIndex>=0) level = config.model.levels[levelIndex]; // start machine search return match.machineSearch({ level: level, progress: (progress) => { if (progressBar) progressBar.style.width = progress + "%"; } }) }) .then((result) => { return match.getMoveString(result.move) .then((str)=>{ console.info("Played move:",str); return result; }) }) .then((result) => { // at this point we know the machine move but it has not been played yet // let's play that move return match.playMove(result.move); }); } promise.then(() => { // is game over ? return match.getFinished() }) .then((result) => { movePending = null; movePendingResolver(); if (result.finished) NotifyWinner(result.winner); else NextMove(); }) .catch((e)=>{ movePending = null; movePendingResolver(); console.warn("Turn aborted:",e); }) .then(() => { if (progressBar) progressBar.style.display = "none"; }); }) } match.getFinished() .then( (result) => { // make sure the game is not finished to request next move if(!result.finished) { if(movePending) { movePending.then(()=>{ NextMove(); }) } else NextMove(); } }); } $(document).ready(function () { var progressBar = document.getElementById("progress-bar"); var m = /\?game=([^&]+)/.exec(window.location.href); var gameName = m && m[1] || "classic-chess"; var elementId = "applet"; var area = document.getElementById(elementId); Jocly.createMatch(gameName).then((match) => { // get game configuration to setup control UI match.getConfig() .then( (config) => { $("#game-title").show().text(config.model["title-en"]); $("#close-games span").show(); $("#game-status").show(); var viewOptions = config.view; // fills Skins dropdown with available skins viewOptions.skins.forEach(function(skin) { $("