Merge pull request #2867 from nxdefiant/master

chess: Bugfixes
pull/2874/head
Gordon Williams 2023-07-12 09:54:24 +01:00 committed by GitHub
commit 3901c2a142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 26 deletions

2
apps/chess/ChangeLog Normal file
View File

@ -0,0 +1,2 @@
0.01: New App!
0.02: Bugfixes

View File

@ -20,11 +20,12 @@ const settings = Object.assign({
computer_level: 0, // default to "stupid" which is the fastest computer_level: 0, // default to "stupid" which is the fastest
}, require("Storage").readJSON(SETTINGS_FILE,1) || {}); }, 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 curfield = [4*FIELD_WIDTH, 6*FIELD_HEIGHT]; // e2
const startfield = Array(2); const startfield = Array(2);
let piece_sel = 0; let piece_sel = 0;
let showmenu = false; let showmenu = false;
let finished = false;
const writeSettings = () => { const writeSettings = () => {
settings.state = engine.p4_state2fen(state); settings.state = engine.p4_state2fen(state);
@ -32,7 +33,7 @@ const writeSettings = () => {
}; };
const generateBgImage = () => { 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 idxrow=0; idxrow<8; idxrow++) {
for(let idxcol=0; idxcol<8; idxcol++) { for(let idxcol=0; idxcol<8; idxcol++) {
const bgCol = idxrow % 2 != idxcol % 2 ? 0 : 1; const bgCol = idxrow % 2 != idxcol % 2 ? 0 : 1;
@ -112,7 +113,7 @@ const roundY = (y) => {
const drawSelectedField = () => { const drawSelectedField = () => {
ovr.clear(); ovr.clear();
if (!showmenu) { if (!showmenu && !finished) {
if (startfield[0] !== undefined && startfield[1] !== undefined) { if (startfield[0] !== undefined && startfield[1] !== undefined) {
// remove piece from startfield // remove piece from startfield
const x = startfield[0]; const x = startfield[0];
@ -143,28 +144,34 @@ const isInside = (rect, e) => {
&& e.y>=rect.y && e.y<=rect.y+rect.h; && e.y>=rect.y && e.y<=rect.y+rect.h;
}; };
const showAlert = (msg) => { const showAlert = (msg, cb) => {
showmenu = true; showmenu = true;
drawSelectedField(); drawSelectedField();
E.showAlert(msg).then(function() { E.showAlert(msg).then(function() {
showmenu = false; showmenu = false;
drawBoard(); drawBoard();
drawSelectedField(); drawSelectedField();
if (cb) {
cb();
}
}); });
}; };
const move = (from,to) => { const move = (from,to,cbok) => {
const res = state.move(from, to); const res = state.move(from, to);
//console.log(res); //console.log(res);
if (!res.ok) { if (!res.ok) {
showAlert("Illegal move"); showAlert("Illegal move");
} else { } else {
if (res.flags & engine.P4_MOVE_FLAG_MATE) { 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) { } 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) { } 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; return res;
@ -217,26 +224,31 @@ Bangle.on('touch', (button, xy) => {
const posFrom = idx2Pos(startfield[0]/FIELD_WIDTH, startfield[1]/FIELD_HEIGHT); const posFrom = idx2Pos(startfield[0]/FIELD_WIDTH, startfield[1]/FIELD_HEIGHT);
const posTo = idx2Pos(colTo/FIELD_WIDTH, rowTo/FIELD_HEIGHT); const posTo = idx2Pos(colTo/FIELD_WIDTH, rowTo/FIELD_HEIGHT);
setTimeout(() => { setTimeout(() => {
if (move(posFrom, posTo).ok) { const cb = () => {
// human move ok, update // human move ok, update
drawBoard(); drawBoard();
drawSelectedField(); drawSelectedField();
// do computer move if (!finished) {
Bangle.setLCDTimeout(0.1); // this can take some time, turn off to save power // do computer move
showMessage(/*LANG*/"Calculating.."); Bangle.setLCDTimeout(0.1); // this can take some time, turn off to save power
setTimeout(() => { showMessage(/*LANG*/"Calculating..");
const compMove = state.findmove(settings.computer_level+1); setTimeout(() => {
const result = move(compMove[0], compMove[1]); const compMove = state.findmove(settings.computer_level+1);
writeSettings(); const result = move(compMove[0], compMove[1]);
Bangle.setLCDPower(true); if (result.ok) {
Bangle.setLocked(false); writeSettings();
Bangle.setLCDTimeout(DEFAULT_TIMEOUT/1000); // restore }
if (!showmenu) { Bangle.setLCDPower(true);
showAlert(result.string); Bangle.setLocked(false);
} Bangle.setLCDTimeout(DEFAULT_TIMEOUT/1000); // restore
}, 200); // execute after display update if (!showmenu) {
} showAlert(result.string);
}, 100); // execute after display update }
}, 200); // execute after display update
}
};
move(posFrom, posTo,cb);
}, 200); // execute after display update
} // piece_sel === 0 } // piece_sel === 0
startfield[0] = startfield[1] = undefined; startfield[0] = startfield[1] = undefined;
piece_sel = 0; piece_sel = 0;
@ -247,7 +259,12 @@ Bangle.on('touch', (button, xy) => {
// show menu on button // show menu on button
setWatch(() => { setWatch(() => {
if (showmenu) {
return;
}
showmenu = true; showmenu = true;
piece_sel = 0;
startfield[0] = startfield[1] = undefined;
drawSelectedField(); drawSelectedField();
const closeMenu = () => { const closeMenu = () => {
@ -267,6 +284,7 @@ setWatch(() => {
}, },
/*LANG*/"Undo Move" : () => { /*LANG*/"Undo Move" : () => {
state.jump_to_moveno(-2); state.jump_to_moveno(-2);
writeSettings();
closeMenu(); closeMenu();
}, },
/*LANG*/'Level': { /*LANG*/'Level': {

View File

@ -1594,6 +1594,7 @@ function p4_random_int(state, top){
exports.p4_new_game = p4_new_game; exports.p4_new_game = p4_new_game;
exports.p4_fen2state = p4_fen2state; exports.p4_fen2state = p4_fen2state;
exports.p4_state2fen = p4_state2fen; exports.p4_state2fen = p4_state2fen;
exports.p4_random_int = p4_random_int;
exports.P4_INITIAL_BOARD = P4_INITIAL_BOARD; exports.P4_INITIAL_BOARD = P4_INITIAL_BOARD;
exports.P4_PAWN = P4_PAWN; exports.P4_PAWN = P4_PAWN;
exports.P4_ROOK = P4_ROOK; exports.P4_ROOK = P4_ROOK;

View File

@ -2,7 +2,7 @@
"id": "chess", "id": "chess",
"name": "Chess", "name": "Chess",
"shortName": "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.", "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", "icon": "app.png",
"tags": "game", "tags": "game",