From b8ddb0cc58f1628e6c1a5e5c7ee3d00e0796d10f Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Thu, 6 Jul 2023 10:25:48 +0200 Subject: [PATCH 1/2] chess bugfixes --- apps/chess/ChangeLog | 2 ++ apps/chess/app.js | 65 ++++++++++++++++++++++++---------------- apps/chess/engine.js | 1 + apps/chess/metadata.json | 2 +- 4 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 apps/chess/ChangeLog diff --git a/apps/chess/ChangeLog b/apps/chess/ChangeLog new file mode 100644 index 000000000..fb08248ff --- /dev/null +++ b/apps/chess/ChangeLog @@ -0,0 +1,2 @@ +0.01: New App! +0.02: Bugfixes diff --git a/apps/chess/app.js b/apps/chess/app.js index bb3ab147d..6d91c7252 100644 --- a/apps/chess/app.js +++ b/apps/chess/app.js @@ -20,11 +20,12 @@ const settings = Object.assign({ computer_level: 0, // default to "stupid" which is the fastest }, require("Storage").readJSON(SETTINGS_FILE,1) || {}); -var ovr = Graphics.createArrayBuffer(Bangle.appRect.w,Bangle.appRect.h,2,{msb:true}); +const ovr = Graphics.createArrayBuffer(Bangle.appRect.w,Bangle.appRect.h,2,{msb:true}); const curfield = [4*FIELD_WIDTH, 6*FIELD_HEIGHT]; // e2 const startfield = Array(2); let piece_sel = 0; let showmenu = false; +let finished = false; const writeSettings = () => { settings.state = engine.p4_state2fen(state); @@ -32,7 +33,7 @@ const writeSettings = () => { }; const generateBgImage = () => { - var buf = Graphics.createArrayBuffer(Bangle.appRect.w,Bangle.appRect.h,1,{msb:true}); + let buf = Graphics.createArrayBuffer(Bangle.appRect.w,Bangle.appRect.h,1,{msb:true}); for(let idxrow=0; idxrow<8; idxrow++) { for(let idxcol=0; idxcol<8; idxcol++) { const bgCol = idxrow % 2 != idxcol % 2 ? 0 : 1; @@ -112,7 +113,7 @@ const roundY = (y) => { const drawSelectedField = () => { ovr.clear(); - if (!showmenu) { + if (!showmenu && !finished) { if (startfield[0] !== undefined && startfield[1] !== undefined) { // remove piece from startfield const x = startfield[0]; @@ -143,28 +144,34 @@ const isInside = (rect, e) => { && e.y>=rect.y && e.y<=rect.y+rect.h; }; -const showAlert = (msg) => { +const showAlert = (msg, cb) => { showmenu = true; drawSelectedField(); E.showAlert(msg).then(function() { showmenu = false; drawBoard(); drawSelectedField(); + if (cb) { + cb(); + } }); }; -const move = (from,to) => { +const move = (from,to,cbok) => { const res = state.move(from, to); //console.log(res); if (!res.ok) { showAlert("Illegal move"); } else { if (res.flags & engine.P4_MOVE_FLAG_MATE) { - showAlert("Checkmate or stalemate"); + finished = true; + showAlert("Checkmate or stalemate", cbok); } else if (res.flags & engine.P4_MOVE_FLAG_CHECK) { - showAlert("A king is in check"); + showAlert("A king is in check", cbok); } else if (res.flags & engine.P4_MOVE_FLAG_DRAW) { - showAlert("A draw is available"); + showAlert("A draw is available", cbok); + } else if (cbok) { + cbok(); } } return res; @@ -217,26 +224,31 @@ Bangle.on('touch', (button, xy) => { const posFrom = idx2Pos(startfield[0]/FIELD_WIDTH, startfield[1]/FIELD_HEIGHT); const posTo = idx2Pos(colTo/FIELD_WIDTH, rowTo/FIELD_HEIGHT); setTimeout(() => { - if (move(posFrom, posTo).ok) { + const cb = () => { // human move ok, update drawBoard(); drawSelectedField(); - // do computer move - Bangle.setLCDTimeout(0.1); // this can take some time, turn off to save power - showMessage(/*LANG*/"Calculating.."); - setTimeout(() => { - const compMove = state.findmove(settings.computer_level+1); - const result = move(compMove[0], compMove[1]); - writeSettings(); - Bangle.setLCDPower(true); - Bangle.setLocked(false); - Bangle.setLCDTimeout(DEFAULT_TIMEOUT/1000); // restore - if (!showmenu) { - showAlert(result.string); - } - }, 200); // execute after display update - } - }, 100); // execute after display update + if (!finished) { + // do computer move + Bangle.setLCDTimeout(0.1); // this can take some time, turn off to save power + showMessage(/*LANG*/"Calculating.."); + setTimeout(() => { + const compMove = state.findmove(settings.computer_level+1); + const result = move(compMove[0], compMove[1]); + if (result.ok) { + writeSettings(); + } + Bangle.setLCDPower(true); + Bangle.setLocked(false); + Bangle.setLCDTimeout(DEFAULT_TIMEOUT/1000); // restore + if (!showmenu) { + showAlert(result.string); + } + }, 200); // execute after display update + } + }; + move(posFrom, posTo,cb); + }, 200); // execute after display update } // piece_sel === 0 startfield[0] = startfield[1] = undefined; piece_sel = 0; @@ -248,6 +260,8 @@ Bangle.on('touch', (button, xy) => { // show menu on button setWatch(() => { showmenu = true; + piece_sel = 0; + startfield[0] = startfield[1] = undefined; drawSelectedField(); const closeMenu = () => { @@ -267,6 +281,7 @@ setWatch(() => { }, /*LANG*/"Undo Move" : () => { state.jump_to_moveno(-2); + writeSettings(); closeMenu(); }, /*LANG*/'Level': { diff --git a/apps/chess/engine.js b/apps/chess/engine.js index 88edf78f5..f67ecc456 100644 --- a/apps/chess/engine.js +++ b/apps/chess/engine.js @@ -1594,6 +1594,7 @@ function p4_random_int(state, top){ exports.p4_new_game = p4_new_game; exports.p4_fen2state = p4_fen2state; exports.p4_state2fen = p4_state2fen; +exports.p4_random_int = p4_random_int; exports.P4_INITIAL_BOARD = P4_INITIAL_BOARD; exports.P4_PAWN = P4_PAWN; exports.P4_ROOK = P4_ROOK; diff --git a/apps/chess/metadata.json b/apps/chess/metadata.json index a66391deb..3c2ea69ac 100644 --- a/apps/chess/metadata.json +++ b/apps/chess/metadata.json @@ -2,7 +2,7 @@ "id": "chess", "name": "Chess", "shortName": "Chess", - "version": "0.01", + "version": "0.02", "description": "Chess game based on the [p4wn engine](https://p4wn.sourceforge.net/). Drag on the touchscreen to move the green cursor onto a piece, select it with a single touch and drag the now red cursor around. Release the piece with another touch to finish the move. The button opens a menu.", "icon": "app.png", "tags": "game", From bcde01f7a586d94f567cc2d28c033b701f88d2ca Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Thu, 6 Jul 2023 21:17:09 +0200 Subject: [PATCH 2/2] chess: Disable button when alert is shown --- apps/chess/app.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/chess/app.js b/apps/chess/app.js index 6d91c7252..3d584b261 100644 --- a/apps/chess/app.js +++ b/apps/chess/app.js @@ -259,6 +259,9 @@ Bangle.on('touch', (button, xy) => { // show menu on button setWatch(() => { + if (showmenu) { + return; + } showmenu = true; piece_sel = 0; startfield[0] = startfield[1] = undefined;