From ee69c7698cda68390d6a6b69c121f8f54ab924b1 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Wed, 24 Mar 2021 15:33:36 +0100 Subject: [PATCH] Konane: simplify code, easier gameplay --- client/src/translations/rules/Konane/en.pug | 13 +-- client/src/translations/rules/Konane/es.pug | 13 +-- client/src/translations/rules/Konane/fr.pug | 13 +-- client/src/variants/Konane.js | 111 ++++---------------- 4 files changed, 27 insertions(+), 123 deletions(-) diff --git a/client/src/translations/rules/Konane/en.pug b/client/src/translations/rules/Konane/en.pug index 930f8c70..9384ac0c 100644 --- a/client/src/translations/rules/Konane/en.pug +++ b/client/src/translations/rules/Konane/en.pug @@ -24,18 +24,11 @@ p. on an empty square. The intermediate piece is thus removed. You may continue capturing, but only in the same direction, with the same stone. - To stop a chain of captures (while more are available), play a move from the - current capturer until the location of the last captured stone: - g4 on this example. figure.diagram-container - .diagram.diag12 - | fen:PpPpPpPp/pPpPpPpP/PpPpP2p/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP: - .diagram.diag22 - | fen:PpPpPp1p/pPpPpP1P/PpPpP1Pp/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP g4,g7: - figcaption. - Before and after a first capture g8xg7. - The available continuations are indicated. + .diagram + | fen:PpPpPpPp/pPpPpPpP/PpPpP2p/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP g4,g6: + figcaption Available captures from g8. p If a player has no possible capture, he loses. diff --git a/client/src/translations/rules/Konane/es.pug b/client/src/translations/rules/Konane/es.pug index ca177266..6d5ba5ea 100644 --- a/client/src/translations/rules/Konane/es.pug +++ b/client/src/translations/rules/Konane/es.pug @@ -24,18 +24,11 @@ p. en un cuadrado vacío justo detrás. Se quita así la pieza intermedia. Tienes derecho a seguir capturando, pero solo en el mismo dirección, con la misma piedra. - Para detener una cadena de capturas (mientras que otras son posibles), - hacer un movimiento de la captura actual a la ubicación de la última - pieza capturada: g4 en el ejemplo. figure.diagram-container - .diagram.diag12 - | fen:PpPpPpPp/pPpPpPpP/PpPpP2p/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP: - .diagram.diag22 - | fen:PpPpPp1p/pPpPpP1P/PpPpP1Pp/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP g4,g7: - figcaption. - Antes y después de una primera captura g8xg7. - Se indican las continuaciones disponibles. + .diagram + | fen:PpPpPpPp/pPpPpPpP/PpPpP2p/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP g4,g6: + figcaption Capturas posibles desde g8. p Si un jugador no tiene más capturas disponibles, pierde. diff --git a/client/src/translations/rules/Konane/fr.pug b/client/src/translations/rules/Konane/fr.pug index 7cdc286b..8df21e96 100644 --- a/client/src/translations/rules/Konane/fr.pug +++ b/client/src/translations/rules/Konane/fr.pug @@ -24,18 +24,11 @@ p. sur une case vide juste derrière. La pièce intermédiaire est ainsi retirée. Vous avez le droit de continuer de capturer, mais seulement dans la même direction, avec la même pierre. - Pour arrêter une chaîne de captures (alors que d'autres sont possibles), - jouez un coup depuis le capturant actuel vers l'emplacement de la dernière - pièce capturée : g4 sur l'exemple. figure.diagram-container - .diagram.diag12 - | fen:PpPpPpPp/pPpPpPpP/PpPpP2p/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP: - .diagram.diag22 - | fen:PpPpPp1p/pPpPpP1P/PpPpP1Pp/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP g4,g7: - figcaption. - Avant et après une première capture g8xg7. - Les continuations disponibles sont indiquées. + .diagram + | fen:PpPpPpPp/pPpPpPpP/PpPpP2p/pPpPp1pP/PpP1Pp1p/pPpPpP1P/PpPpPpPp/pPpPpPpP g4,g6: + figcaption Captures possibles depuis g8. p Si un joueur n'a plus de captures à disposition, il perd. diff --git a/client/src/variants/Konane.js b/client/src/variants/Konane.js index 1daad7af..80311817 100644 --- a/client/src/variants/Konane.js +++ b/client/src/variants/Konane.js @@ -49,10 +49,6 @@ export class KonaneRules extends ChessRules { ); } - setOtherVariables(fen) { - this.captures = []; //reinit for each move - } - hoverHighlight([x, y], side) { const c = this.turn; if (this.movesCount >= 2 || (!!side && side != c)) return false; @@ -101,57 +97,31 @@ export class KonaneRules extends ChessRules { const mv = this.doClick([x, y]); return (!!mv ? [mv] : []); } - const L = this.captures.length; - const c = (L > 0 ? this.captures[L-1] : null); const color = this.turn; const oppCol = V.GetOppCol(color); - let step = null; let moves = []; - if (!!c) { - if (x != c.end.x || y != c.end.y) return []; - step = [(c.end.x - c.start.x) / 2, (c.end.y - c.start.y) / 2]; - // Add move to adjacent empty square to mark "end of capture" - moves.push( - new Move({ - appear: [], - vanish: [], - start: { x: x, y: y }, - end: { x: x - step[0], y: y - step[1] } - }) - ); - } - // Examine captures from here - for (let s of (!!step ? [step] : V.steps[V.ROOK])) { - let [i, j] = [x + 2*s[0], y + 2*s[1]]; - if ( - !!c || //avoid redundant checks if continuation - ( - V.OnBoard(i, j) && - this.board[i][j] == V.EMPTY && - this.board[i - s[0]][j - s[1]] != V.EMPTY && - this.getColor(i - s[0], j - s[1]) == oppCol - ) - ) { - let mv = new Move({ - appear: [ - new PiPo({ x: i, y: j, c: color, p: V.PAWN }) - ], - vanish: [ - new PiPo({ x: x, y: y, c: color, p: V.PAWN }), - new PiPo({ x: i - s[0], y: j - s[1], c: oppCol, p: V.PAWN }) - ] - }); - // Is there another capture possible then? - [i, j] = [i + 2*s[0], j + 2*s[1]]; + for (let s of V.steps[V.ROOK]) { + let curmv = new Move({ + appear: [ new PiPo({ x: -1, y: -1, c: color, p: V.PAWN }) ], + vanish: [ new PiPo({ x: x, y: y, c: color, p: V.PAWN }) ] + }); + for (let mult = 2; ; mult += 2) { + let [i, j] = [x + mult * s[0], y + mult * s[1]]; if ( V.OnBoard(i, j) && this.board[i][j] == V.EMPTY && this.board[i - s[0]][j - s[1]] != V.EMPTY && this.getColor(i - s[0], j - s[1]) == oppCol ) { - mv.end.moreCapture = true; + curmv.vanish.push( + new PiPo({ x: i - s[0], y: j - s[1], c: oppCol, p: V.PAWN })); + let mv = JSON.parse(JSON.stringify(curmv)); + mv.appear[0].x = i; + mv.appear[0].y = j; + mv.end = { x: i, y: j }; + moves.push(mv); } - moves.push(mv); + else break; } } return moves; @@ -170,59 +140,14 @@ export class KonaneRules extends ChessRules { return (this.turn == "w" ? "0-1" : "1-0"); } - play(move) { - V.PlayOnBoard(this.board, move); - if (!move.end.moreCapture) { - this.turn = V.GetOppCol(this.turn); - this.movesCount++; - this.captures = []; - } - else { - this.captures.push( - { - start: move.start, - end: { x: move.end.x, y: move.end.y } - } - ); - } - } - - undo(move) { - V.UndoOnBoard(this.board, move); - if (!move.end.moreCapture) { - this.turn = V.GetOppCol(this.turn); - this.movesCount--; - } - else this.captures.pop(); - } - - getComputerMove() { - const color = this.turn; - let mvArray = []; - let mv = null; - const undoAll = () => { - for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]); - }; - // Just play random moves (for now at least. TODO?) - while (this.turn == color) { - let moves = super.getAllValidMoves(); - if (moves.length == 0) { - // Shouldn't happen, but... - undoAll(); - return null; - } - mv = moves[randInt(moves.length)]; - mvArray.push(mv); - this.play(mv); - } - undoAll(); - return (mvArray.length > 1 ? mvArray : mvArray[0]); + static get SEARCH_DEPTH() { + return 4; } getNotation(move) { if (this.movesCount <= 1) return V.CoordsToSquare(move.start) + "X"; if (move.vanish.length == 0) return "end"; - return V.CoordsToSquare(move.start) + "x" + V.CoordsToSquare(move.end); + return V.CoordsToSquare(move.start) + V.CoordsToSquare(move.end); } }; -- 2.44.0