From 5212758164eaa08e382b7bc281f87999c8af352b Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Sat, 30 Dec 2023 20:58:25 +0100 Subject: [PATCH 01/16] Draft Diamond variant --- base_rules.js | 15 ++++-- variants.js | 2 +- variants/Diamond/class.js | 96 +++++++++++++++++++++++++++++++++++++ variants/Diamond/rules.html | 13 +++++ variants/Diamond/style.css | 1 + 5 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 variants/Diamond/class.js create mode 100644 variants/Diamond/rules.html create mode 100644 variants/Diamond/style.css diff --git a/base_rules.js b/base_rules.js index 757707d..0f89cc4 100644 --- a/base_rules.js +++ b/base_rules.js @@ -579,11 +579,18 @@ export default class ChessRules { // Get SVG board (background, no pieces) getSvgChessboard() { - const flipped = this.flippedBoard; let board = ` `; + board += this.getBaseSvgChessboard(); + board += ""; + return board; + } + + getBaseSvgChessboard() { + let board = ""; + const flipped = this.flippedBoard; for (let i=0; i < this.size.x; i++) { for (let j=0; j < this.size.y; j++) { if (!this.onBoard(i, j)) @@ -605,7 +612,6 @@ export default class ChessRules { />`; } } - board += ""; return board; } @@ -2341,10 +2347,11 @@ export default class ChessRules { if (this.options["teleport"]) { if ( this.subTurnTeleport == 1 && - move.vanish.length > move.appear.length && + move.vanish.length == 2 && + move.appear.length == 1 && move.vanish[1].c == this.turn ) { - const v = move.vanish[move.vanish.length - 1]; + const v = move.vanish[1]; this.captured = {x: v.x, y: v.y, c: v.c, p: v.p}; this.subTurnTeleport = 2; return; diff --git a/variants.js b/variants.js index 524538c..c9cdc98 100644 --- a/variants.js +++ b/variants.js @@ -41,7 +41,7 @@ const variants = [ {name: 'Cylinder', desc: 'Neverending rows'}, {name: 'Cwda', desc: 'New teams', disp: 'Different armies'}, {name: 'Dark', desc: 'In the shadow'}, -// {name: 'Diamond', desc: 'Rotating board'}, + {name: 'Diamond', desc: 'Rotating board'}, // {name: 'Dice', desc: 'Roll the dice'}, // {name: 'Discoduel', desc: 'Enter the disco', disp: 'Disco Duel'}, // {name: 'Dobutsu', desc: "Let's catch the Lion!"}, diff --git a/variants/Diamond/class.js b/variants/Diamond/class.js new file mode 100644 index 0000000..5145184 --- /dev/null +++ b/variants/Diamond/class.js @@ -0,0 +1,96 @@ +import ChessRules from "/base_rules.js"; +import {ArrayFun} from "/utils/array.js"; +import {Random} from "/utils/alea.js"; + +export default class DiamondRules extends ChessRules { + + get hasFlags() { + return false; + } + + get hasEnpassant() { + return false; + } + + getSvgChessboard() { + const diagonal = 10 * this.size.y * Math.sqrt(2); + const halfDiag = 0.5 * diagonal; + const deltaTrans = 10 * this.size.y * (Math.sqrt(2) - 1) / 2; + let board = ` + `; + board += ``; + board += this.getBaseSvgChessboard(); + board += ""; + return board; + } + + getPieceWidth(rwidth) { + return (0.95 * rwidth / (Math.sqrt(2) * this.size.y)); + } + + getPixelPosition(i, j, r) { + if (i < 0 || j < 0 || typeof i == "string") + return super.getPixelPosition(i, j, r); + const sqSize = this.getPieceWidth(r.width) / 0.95; + const flipped = this.flippedBoard; + i = (flipped ? this.size.x - 1 - i : i); + j = (flipped ? this.size.y - 1 - j : j); + const sq2 = Math.sqrt(2); + const shift = [- sqSize / 2, sqSize * (sq2 - 1) / 2]; + const x = (j - i) * sqSize / sq2 + shift[0] + r.width / 2; + const y = (i + j) * sqSize / sq2 + shift[1]; + return [r.x + x, r.y + y]; + } + + genRandInitBaseFen() { + if (this.options["randomness"] == 0) { + return { + fen: "krbp4/rqnp4/nbpp4/pppp4/4PPPP/4PPBN/4PNQR/4PBRK", + o: {} + }; + } + let pieces = { w: new Array(8), b: new Array(8) }; + for (let c of ["w", "b"]) { + if (c == 'b' && options.randomness == 1) { + pieces['b'] = pieces['w']; + break; + } + // Get random squares for every piece, totally freely + let positions = Random.shuffle(ArrayFun.range(8)); + const composition = ['b', 'b', 'r', 'r', 'n', 'n', 'k', 'q']; + const rem2 = positions[0] % 2; + if (rem2 == positions[1] % 2) { + // Fix bishops (on different colors) + for (let i=2; i<8; i++) { + if (positions[i] % 2 != rem2) { + [positions[1], positions[i]] = [positions[i], positions[1]]; + break; + } + } + } + for (let i = 0; i < 8; i++) + pieces[c][positions[i]] = composition[i]; + } + const fen = ( + pieces["b"].slice(0, 3).join("") + "p4/" + + pieces["b"].slice(3, 6).join("") + "p4/" + + pieces["b"].slice(6, 8).join("") + "pp4/" + + "pppp4/4PPPP/" + + "4PP" + pieces["w"].slice(6, 8).reverse().join("").toUpperCase() + "/" + + "4P" + pieces["w"].slice(3, 6).reverse().join("").toUpperCase() + "/" + + "4P" + pieces["w"].slice(0, 3).reverse().join("").toUpperCase()); + return { fen: fen, o: {} }; + } + + pieces(color, x, y) { + let res = super.pieces(color, x, y); + const pawnShift = this.getPawnShift(color || 'w'); + res['p'].moves = [{steps: [[pawnShift, pawnShift]], range: 1}]; + res['p'].attack = [{steps: [[0, pawnShift], [pawnShift, 0]], range: 1}]; + return res; + } + +}; diff --git a/variants/Diamond/rules.html b/variants/Diamond/rules.html new file mode 100644 index 0000000..9f172b1 --- /dev/null +++ b/variants/Diamond/rules.html @@ -0,0 +1,13 @@ +

The board is rotated by 45°, and then the game follow usual rules.

+ +

Pawns move forward "one diagonal", and capture forward "orthogonally".

+ +

+ See + + Diamond Chess + + on chessvariants.com. +

+ +

James Alexander Porterfield Rynd (1886).

diff --git a/variants/Diamond/style.css b/variants/Diamond/style.css new file mode 100644 index 0000000..a3550bc --- /dev/null +++ b/variants/Diamond/style.css @@ -0,0 +1 @@ +@import url("/base_pieces.css"); -- 2.48.1 From d01282a527e60af95f2a71deee1fbac9c0dd26be Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Mon, 1 Jan 2024 08:51:47 +0100 Subject: [PATCH 02/16] Draft Dice chess --- variants.js | 2 +- variants/Dice/class.js | 108 +++++++++++++++++++++++++++++++++++++++ variants/Dice/rules.html | 3 ++ variants/Dice/style.css | 12 +++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 variants/Dice/class.js create mode 100644 variants/Dice/rules.html create mode 100644 variants/Dice/style.css diff --git a/variants.js b/variants.js index c9cdc98..cad081a 100644 --- a/variants.js +++ b/variants.js @@ -42,7 +42,7 @@ const variants = [ {name: 'Cwda', desc: 'New teams', disp: 'Different armies'}, {name: 'Dark', desc: 'In the shadow'}, {name: 'Diamond', desc: 'Rotating board'}, -// {name: 'Dice', desc: 'Roll the dice'}, + {name: 'Dice', desc: 'Roll the dice'}, // {name: 'Discoduel', desc: 'Enter the disco', disp: 'Disco Duel'}, // {name: 'Dobutsu', desc: "Let's catch the Lion!"}, // {name: 'Doublearmy', desc: '64 pieces on the board', disp: 'Double Army'}, diff --git a/variants/Dice/class.js b/variants/Dice/class.js new file mode 100644 index 0000000..8c3eed4 --- /dev/null +++ b/variants/Dice/class.js @@ -0,0 +1,108 @@ +import ChessRules from "/base_rules.js"; +import {Random} from "/utils/alea.js"; + +export default class DiceRules extends ChessRules { + + static get Options() { + let res = C.Options; + res.select["defaut"] = 2; + return { + select: res.select, + input: [ + { + label: "Biased alea", + variable: "biased", + type: "checkbox", + defaut: true + }, + { + label: "Falling pawn", + variable: "pawnfall", + type: "checkbox", + defaut: false + } + ], + styles: [ + "atomic", + "capture", + "crazyhouse", + "cylinder", + "madrasi", + "recycle", + "rifle", + "zen" + ] + }; + } + + getPartFen(o) { + let toplay = ''; + if (o.init) { + let canMove = (this.options["biased"] + ? Array(8).fill('p').concat(Array(2).fill('n')) + : ['p', 'n']); + toplay = canMove[Random.randInt(canMove.length)]; + } + return Object.assign( + { toplay: (o.init ? toplay : this.getRandomPiece(this.turn)) }, + super.getPartFen(o) + ); + } + + constructor(o) { + super(o); + this.afterPlay = (move_s, newTurn, ops) => { + // Movestack contains only one move: + move_s[0].toplay = this.getRandomPiece(this.turn); + super.displayMessage(this.message, move_s[0].toplay); + o.afterPlay(move_s, newTurn, ops); + }; + } + + setOtherVariables(fenParsed) { + super.setOtherVariables(fenParsed); + this.toplay = fenParsed.toplay; + this.message = document.createElement("div"); + C.AddClass_es(this.message, "piece-text"); + this.message.innerHTML = this.toplay; + let container = document.getElementById(this.containerId); + container.appendChild(this.message); + } + + getRandomPiece(color) { + // Find pieces which can move and roll a (biased) dice + let canMove = []; + for (let i=0; i<8; i++) { + for (let j=0; j<8; j++) { + if (this.board[i][j] != "" && this.getColor(i, j) == color) { + const piece = this.getPiece(i, j); + if (this.findDestSquares([i, j], {one: true})) + canMove.push(piece); + } + } + } + if (!this.options["biased"]) + canMove = [...new Set(canMove)]; + return canMove[Random.randInt(canMove.length)]; + } + + postProcessPotentialMoves(moves) { + return super.postProcessPotentialMoves(moves).filter(m => { + return ( + (m.appear.length >= 1 && m.appear[0].p == this.toplay) || + (m.vanish.length >= 1 && m.vanish[0].p == this.toplay) + ); + }); + } + + filterValid(moves) { + return moves; + } + + playReceivedMove(moves, callback) { + this.toplay = moves[0].toplay; //only one move + super.displayMessage(this.message, this.toplay); + super.playReceivedMove(moves, callback); + } + +}; diff --git a/variants/Dice/rules.html b/variants/Dice/rules.html new file mode 100644 index 0000000..04e8682 --- /dev/null +++ b/variants/Dice/rules.html @@ -0,0 +1,3 @@ +

Play the piece type determined by a dice roll.

+ +

There is no check or checkmate: the goal is to capture the king.

diff --git a/variants/Dice/style.css b/variants/Dice/style.css new file mode 100644 index 0000000..5881bc3 --- /dev/null +++ b/variants/Dice/style.css @@ -0,0 +1,12 @@ +@import url("/base_pieces.css"); + +div.piece-text { + position: relative; + margin-top: 15px; + width: 100%; + text-align: center; + background-color: transparent; + color: darkred; + font-weight: bold; + font-size: 2em; +} -- 2.48.1 From 7fbcb53de45ba7b7aed99d6087928779713810b1 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Mon, 1 Jan 2024 16:34:18 +0100 Subject: [PATCH 03/16] update --- variants/Dice/class.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/variants/Dice/class.js b/variants/Dice/class.js index 8c3eed4..02e4f6d 100644 --- a/variants/Dice/class.js +++ b/variants/Dice/class.js @@ -54,7 +54,8 @@ export default class DiceRules extends ChessRules { this.afterPlay = (move_s, newTurn, ops) => { // Movestack contains only one move: move_s[0].toplay = this.getRandomPiece(this.turn); - super.displayMessage(this.message, move_s[0].toplay); + super.displayMessage( + this.message, "To play: " + move_s[0].toplay.toUpperCase()); o.afterPlay(move_s, newTurn, ops); }; } @@ -64,7 +65,7 @@ export default class DiceRules extends ChessRules { this.toplay = fenParsed.toplay; this.message = document.createElement("div"); C.AddClass_es(this.message, "piece-text"); - this.message.innerHTML = this.toplay; + this.message.innerHTML = "To play: " + this.toplay.toUpperCase(); let container = document.getElementById(this.containerId); container.appendChild(this.message); } @@ -101,7 +102,8 @@ export default class DiceRules extends ChessRules { playReceivedMove(moves, callback) { this.toplay = moves[0].toplay; //only one move - super.displayMessage(this.message, this.toplay); + super.displayMessage( + this.message, "To play: " + this.toplay.toUpperCase()); super.playReceivedMove(moves, callback); } -- 2.48.1 From 04d93b7bb3b64ecdf3fb7219eee42879f0200b88 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Tue, 2 Jan 2024 13:08:07 +0100 Subject: [PATCH 04/16] update --- variants/Dice/class.js | 36 +++++++++++++++++++++++++++++++----- variants/Dice/style.css | 13 ++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/variants/Dice/class.js b/variants/Dice/class.js index 02e4f6d..1bb6f1f 100644 --- a/variants/Dice/class.js +++ b/variants/Dice/class.js @@ -54,20 +54,47 @@ export default class DiceRules extends ChessRules { this.afterPlay = (move_s, newTurn, ops) => { // Movestack contains only one move: move_s[0].toplay = this.getRandomPiece(this.turn); - super.displayMessage( - this.message, "To play: " + move_s[0].toplay.toUpperCase()); + this.toplay = move_s[0].toplay; + this.displayMessage(move_s[0].toplay, + C.GetOppTurn(move_s[0].appear[0].c)); o.afterPlay(move_s, newTurn, ops); }; } + static get PieceToUnicode() { + return { + 'K': "♔", + 'Q': "♕", + 'R': "♖", + 'B': "♗", + 'N': "♘", + 'P': "♙", + 'k': "♚", + 'q': "♛", + 'r': "♜", + 'b': "♝", + 'n': "♞", + 'p': "♟" + }; + } + + displayMessage(piece, color) { + if (color == 'w') + piece = piece.toUpperCase(); + super.displayMessage(this.message, + 'to play: ' + + '' + V.PieceToUnicode[piece] + '' + ); + } + setOtherVariables(fenParsed) { super.setOtherVariables(fenParsed); this.toplay = fenParsed.toplay; this.message = document.createElement("div"); C.AddClass_es(this.message, "piece-text"); - this.message.innerHTML = "To play: " + this.toplay.toUpperCase(); let container = document.getElementById(this.containerId); container.appendChild(this.message); + this.displayMessage(this.toplay, fenParsed.turn); } getRandomPiece(color) { @@ -102,8 +129,7 @@ export default class DiceRules extends ChessRules { playReceivedMove(moves, callback) { this.toplay = moves[0].toplay; //only one move - super.displayMessage( - this.message, "To play: " + this.toplay.toUpperCase()); + this.displayMessage(this.toplay, C.GetOppTurn(moves[0].appear[0].c)); super.playReceivedMove(moves, callback); } diff --git a/variants/Dice/style.css b/variants/Dice/style.css index 5881bc3..c9ed898 100644 --- a/variants/Dice/style.css +++ b/variants/Dice/style.css @@ -1,5 +1,11 @@ @import url("/base_pieces.css"); +/* doesn't work: +@font-face { + font-family: chess-font; + src: url(/assets/FreeSerifBold-rdMp.otf); +} */ + div.piece-text { position: relative; margin-top: 15px; @@ -8,5 +14,10 @@ div.piece-text { background-color: transparent; color: darkred; font-weight: bold; - font-size: 2em; + font-size: 1.7em; } + +/* +div.piece-text > span.symb { + font-family: chess-font; +} */ -- 2.48.1 From 03883a0d1495bc6e6fadc3a11aa99286aba5c9e1 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Tue, 2 Jan 2024 16:03:28 +0100 Subject: [PATCH 05/16] Fix Dice display --- variants/Dice/class.js | 32 ++++++++++++-------------------- variants/Dice/style.css | 11 ++++++----- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/variants/Dice/class.js b/variants/Dice/class.js index 1bb6f1f..53836eb 100644 --- a/variants/Dice/class.js +++ b/variants/Dice/class.js @@ -61,29 +61,21 @@ export default class DiceRules extends ChessRules { }; } - static get PieceToUnicode() { - return { - 'K': "♔", - 'Q': "♕", - 'R': "♖", - 'B': "♗", - 'N': "♘", - 'P': "♙", - 'k': "♚", - 'q': "♛", - 'r': "♜", - 'b': "♝", - 'n': "♞", - 'p': "♟" - }; - } - displayMessage(piece, color) { - if (color == 'w') - piece = piece.toUpperCase(); + if (color == 'b') { + const blackPieceToCode = { + 'k': 'l', + 'p': 'o', + 'n': 'm', + 'b': 'v', + 'q': 'w', + 'r': 't' + }; + piece = blackPieceToCode[piece]; + } super.displayMessage(this.message, 'to play: ' + - '' + V.PieceToUnicode[piece] + '' + '' + piece + '' ); } diff --git a/variants/Dice/style.css b/variants/Dice/style.css index c9ed898..ad11010 100644 --- a/variants/Dice/style.css +++ b/variants/Dice/style.css @@ -1,10 +1,9 @@ @import url("/base_pieces.css"); -/* doesn't work: @font-face { font-family: chess-font; - src: url(/assets/FreeSerifBold-rdMp.otf); -} */ + src: url(/assets/MERIFONT.TTF); +} div.piece-text { position: relative; @@ -17,7 +16,9 @@ div.piece-text { font-size: 1.7em; } -/* div.piece-text > span.symb { font-family: chess-font; -} */ + display: inline-block; + position: relative; + top: 5px; +} -- 2.48.1 From 130a166fd08355be5f2dfc923777c1c6d03f09ce Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Wed, 3 Jan 2024 01:40:00 +0100 Subject: [PATCH 06/16] Chakart: fixing attempt --- variants/Chakart/class.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/variants/Chakart/class.js b/variants/Chakart/class.js index 2b502b6..3454d8f 100644 --- a/variants/Chakart/class.js +++ b/variants/Chakart/class.js @@ -130,6 +130,12 @@ export default class ChakartRules extends ChessRules { ); } + isKing(x, y, p) { + if (!p) + p = this.getPiece(x, y); + return ['k', 'l'].includes(p); + } + genRandInitBaseFen() { const s = FenUtil.setupPieces( ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], @@ -630,19 +636,25 @@ export default class ChakartRules extends ChessRules { }); break; case "koopa": - // Reverse move + // Reverse move, if possible em = new Move({ - appear: [ - new PiPo({ - x: move.start.x, y: move.start.y, c: color, p: move.appear[0].p - }) - ], + appear: [], vanish: [ new PiPo({ x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p }) - ] + ], + end: {x: move.start.x, y: move.start.y} //may be irrelevant }); + em.koopa = true; //avoid applying effect + if (move.vanish.length == 0) + // After toadette+drop, just erase piece + break; + em.appear.push( + new PiPo({ + x: move.start.x, y: move.start.y, c: color, p: move.appear[0].p + }) + ); if (this.board[move.start.x][move.start.y] != "") { // Pawn or knight let something on init square em.vanish.push(new PiPo({ @@ -652,7 +664,6 @@ export default class ChakartRules extends ChessRules { p: this.getPiece(move.start.x, move.start.y) })); } - em.koopa = true; //avoid applying effect break; case "chomp": // Eat piece -- 2.48.1 From 3232aba3419f129c70d5edd9a4ded1fefc146ea0 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Wed, 3 Jan 2024 15:38:58 +0100 Subject: [PATCH 07/16] Add Discoduel, draft Dobutsu, some code cleaning --- base_rules.js | 20 +- pieces/Dobutsu/LICENSE.txt | 395 ++++++++++++++++++++++++++++++++ pieces/Dobutsu/README.md | 13 ++ pieces/Dobutsu/chick.svg | 139 +++++++++++ pieces/Dobutsu/elephant.svg | 176 ++++++++++++++ pieces/Dobutsu/giraffe.svg | 186 +++++++++++++++ pieces/Dobutsu/hen.svg | 156 +++++++++++++ pieces/Dobutsu/lion.svg | 201 ++++++++++++++++ pieces/Dobutsu/rev_chick.svg | 139 +++++++++++ pieces/Dobutsu/rev_elephant.svg | 176 ++++++++++++++ pieces/Dobutsu/rev_giraffe.svg | 186 +++++++++++++++ pieces/Dobutsu/rev_hen.svg | 156 +++++++++++++ pieces/Dobutsu/rev_lion.svg | 201 ++++++++++++++++ utils/array.js | 8 +- variants.js | 4 +- variants/Avalanche/class.js | 2 +- variants/Chaining/class.js | 4 +- variants/Clorange/class.js | 13 +- variants/Convert/class.js | 4 +- variants/Coregal/class.js | 4 +- variants/Discoduel/class.js | 51 +++++ variants/Discoduel/rules.html | 5 + variants/Discoduel/style.css | 1 + variants/Dobutsu/class.js | 100 ++++++++ variants/Dobutsu/rules.html | 12 + variants/Dobutsu/style.css | 34 +++ 26 files changed, 2355 insertions(+), 31 deletions(-) create mode 100644 pieces/Dobutsu/LICENSE.txt create mode 100644 pieces/Dobutsu/README.md create mode 100644 pieces/Dobutsu/chick.svg create mode 100644 pieces/Dobutsu/elephant.svg create mode 100644 pieces/Dobutsu/giraffe.svg create mode 100644 pieces/Dobutsu/hen.svg create mode 100644 pieces/Dobutsu/lion.svg create mode 100644 pieces/Dobutsu/rev_chick.svg create mode 100644 pieces/Dobutsu/rev_elephant.svg create mode 100644 pieces/Dobutsu/rev_giraffe.svg create mode 100644 pieces/Dobutsu/rev_hen.svg create mode 100644 pieces/Dobutsu/rev_lion.svg create mode 100644 variants/Discoduel/class.js create mode 100644 variants/Discoduel/rules.html create mode 100644 variants/Discoduel/style.css create mode 100644 variants/Dobutsu/class.js create mode 100644 variants/Dobutsu/rules.html create mode 100644 variants/Dobutsu/style.css diff --git a/base_rules.js b/base_rules.js index 0f89cc4..217d055 100644 --- a/base_rules.js +++ b/base_rules.js @@ -338,7 +338,7 @@ export default class ChessRules { getReserveFen(o) { if (o.init) - return "000000000000"; + return Array(2 * V.ReserveArray.length).fill('0').join(""); return ( ['w', 'b'].map(c => Object.values(this.reserve[c]).join("")).join("") ); @@ -416,14 +416,14 @@ export default class ChessRules { } // Some additional variables from FEN (variant dependant) - setOtherVariables(fenParsed, pieceArray) { + setOtherVariables(fenParsed) { // Set flags and enpassant: if (this.hasFlags) this.setFlags(fenParsed.flags); if (this.hasEnpassant) this.epSquare = this.getEpSquare(fenParsed.enpassant); if (this.hasReserve && !this.isDiagram) - this.initReserves(fenParsed.reserve, pieceArray); + this.initReserves(fenParsed.reserve); if (this.options["crazyhouse"]) this.initIspawn(fenParsed.ispawn); if (this.options["teleport"]) { @@ -441,14 +441,16 @@ export default class ChessRules { } // ordering as in pieces() p,r,n,b,q,k - initReserves(reserveStr, pieceArray) { - if (!pieceArray) - pieceArray = ['p', 'r', 'n', 'b', 'q', 'k']; + static get ReserveArray() { + return ['p', 'r', 'n', 'b', 'q', 'k']; + } + + initReserves(reserveStr) { const counts = reserveStr.split("").map(c => parseInt(c, 36)); - const L = pieceArray.length; + const L = V.ReserveArray.length; this.reserve = { - w: ArrayFun.toObject(pieceArray, counts.slice(0, L)), - b: ArrayFun.toObject(pieceArray, counts.slice(L, 2 * L)) + w: ArrayFun.toObject(V.ReserveArray, counts.slice(0, L)), + b: ArrayFun.toObject(V.ReserveArray, counts.slice(L, 2 * L)) }; } diff --git a/pieces/Dobutsu/LICENSE.txt b/pieces/Dobutsu/LICENSE.txt new file mode 100644 index 0000000..2f244ac --- /dev/null +++ b/pieces/Dobutsu/LICENSE.txt @@ -0,0 +1,395 @@ +Attribution 4.0 International + +======================================================================= + +Creative Commons Corporation ("Creative Commons") is not a law firm and +does not provide legal services or legal advice. Distribution of +Creative Commons public licenses does not create a lawyer-client or +other relationship. Creative Commons makes its licenses and related +information available on an "as-is" basis. Creative Commons gives no +warranties regarding its licenses, any material licensed under their +terms and conditions, or any related information. Creative Commons +disclaims all liability for damages resulting from their use to the +fullest extent possible. + +Using Creative Commons Public Licenses + +Creative Commons public licenses provide a standard set of terms and +conditions that creators and other rights holders may use to share +original works of authorship and other material subject to copyright +and certain other rights specified in the public license below. The +following considerations are for informational purposes only, are not +exhaustive, and do not form part of our licenses. + + Considerations for licensors: Our public licenses are + intended for use by those authorized to give the public + permission to use material in ways otherwise restricted by + copyright and certain other rights. Our licenses are + irrevocable. Licensors should read and understand the terms + and conditions of the license they choose before applying it. + Licensors should also secure all rights necessary before + applying our licenses so that the public can reuse the + material as expected. Licensors should clearly mark any + material not subject to the license. This includes other CC- + licensed material, or material used under an exception or + limitation to copyright. More considerations for licensors: + wiki.creativecommons.org/Considerations_for_licensors + + Considerations for the public: By using one of our public + licenses, a licensor grants the public permission to use the + licensed material under specified terms and conditions. If + the licensor's permission is not necessary for any reason--for + example, because of any applicable exception or limitation to + copyright--then that use is not regulated by the license. Our + licenses grant only permissions under copyright and certain + other rights that a licensor has authority to grant. Use of + the licensed material may still be restricted for other + reasons, including because others have copyright or other + rights in the material. A licensor may make special requests, + such as asking that all changes be marked or described. + Although not required by our licenses, you are encouraged to + respect those requests where reasonable. More_considerations + for the public: + wiki.creativecommons.org/Considerations_for_licensees + +======================================================================= + +Creative Commons Attribution 4.0 International Public License + +By exercising the Licensed Rights (defined below), You accept and agree +to be bound by the terms and conditions of this Creative Commons +Attribution 4.0 International Public License ("Public License"). To the +extent this Public License may be interpreted as a contract, You are +granted the Licensed Rights in consideration of Your acceptance of +these terms and conditions, and the Licensor grants You such rights in +consideration of benefits the Licensor receives from making the +Licensed Material available under these terms and conditions. + + +Section 1 -- Definitions. + + a. Adapted Material means material subject to Copyright and Similar + Rights that is derived from or based upon the Licensed Material + and in which the Licensed Material is translated, altered, + arranged, transformed, or otherwise modified in a manner requiring + permission under the Copyright and Similar Rights held by the + Licensor. For purposes of this Public License, where the Licensed + Material is a musical work, performance, or sound recording, + Adapted Material is always produced where the Licensed Material is + synched in timed relation with a moving image. + + b. Adapter's License means the license You apply to Your Copyright + and Similar Rights in Your contributions to Adapted Material in + accordance with the terms and conditions of this Public License. + + c. Copyright and Similar Rights means copyright and/or similar rights + closely related to copyright including, without limitation, + performance, broadcast, sound recording, and Sui Generis Database + Rights, without regard to how the rights are labeled or + categorized. For purposes of this Public License, the rights + specified in Section 2(b)(1)-(2) are not Copyright and Similar + Rights. + + d. Effective Technological Measures means those measures that, in the + absence of proper authority, may not be circumvented under laws + fulfilling obligations under Article 11 of the WIPO Copyright + Treaty adopted on December 20, 1996, and/or similar international + agreements. + + e. Exceptions and Limitations means fair use, fair dealing, and/or + any other exception or limitation to Copyright and Similar Rights + that applies to Your use of the Licensed Material. + + f. Licensed Material means the artistic or literary work, database, + or other material to which the Licensor applied this Public + License. + + g. Licensed Rights means the rights granted to You subject to the + terms and conditions of this Public License, which are limited to + all Copyright and Similar Rights that apply to Your use of the + Licensed Material and that the Licensor has authority to license. + + h. Licensor means the individual(s) or entity(ies) granting rights + under this Public License. + + i. Share means to provide material to the public by any means or + process that requires permission under the Licensed Rights, such + as reproduction, public display, public performance, distribution, + dissemination, communication, or importation, and to make material + available to the public including in ways that members of the + public may access the material from a place and at a time + individually chosen by them. + + j. Sui Generis Database Rights means rights other than copyright + resulting from Directive 96/9/EC of the European Parliament and of + the Council of 11 March 1996 on the legal protection of databases, + as amended and/or succeeded, as well as other essentially + equivalent rights anywhere in the world. + + k. You means the individual or entity exercising the Licensed Rights + under this Public License. Your has a corresponding meaning. + + +Section 2 -- Scope. + + a. License grant. + + 1. Subject to the terms and conditions of this Public License, + the Licensor hereby grants You a worldwide, royalty-free, + non-sublicensable, non-exclusive, irrevocable license to + exercise the Licensed Rights in the Licensed Material to: + + a. reproduce and Share the Licensed Material, in whole or + in part; and + + b. produce, reproduce, and Share Adapted Material. + + 2. Exceptions and Limitations. For the avoidance of doubt, where + Exceptions and Limitations apply to Your use, this Public + License does not apply, and You do not need to comply with + its terms and conditions. + + 3. Term. The term of this Public License is specified in Section + 6(a). + + 4. Media and formats; technical modifications allowed. The + Licensor authorizes You to exercise the Licensed Rights in + all media and formats whether now known or hereafter created, + and to make technical modifications necessary to do so. The + Licensor waives and/or agrees not to assert any right or + authority to forbid You from making technical modifications + necessary to exercise the Licensed Rights, including + technical modifications necessary to circumvent Effective + Technological Measures. For purposes of this Public License, + simply making modifications authorized by this Section 2(a) + (4) never produces Adapted Material. + + 5. Downstream recipients. + + a. Offer from the Licensor -- Licensed Material. Every + recipient of the Licensed Material automatically + receives an offer from the Licensor to exercise the + Licensed Rights under the terms and conditions of this + Public License. + + b. No downstream restrictions. You may not offer or impose + any additional or different terms or conditions on, or + apply any Effective Technological Measures to, the + Licensed Material if doing so restricts exercise of the + Licensed Rights by any recipient of the Licensed + Material. + + 6. No endorsement. Nothing in this Public License constitutes or + may be construed as permission to assert or imply that You + are, or that Your use of the Licensed Material is, connected + with, or sponsored, endorsed, or granted official status by, + the Licensor or others designated to receive attribution as + provided in Section 3(a)(1)(A)(i). + + b. Other rights. + + 1. Moral rights, such as the right of integrity, are not + licensed under this Public License, nor are publicity, + privacy, and/or other similar personality rights; however, to + the extent possible, the Licensor waives and/or agrees not to + assert any such rights held by the Licensor to the limited + extent necessary to allow You to exercise the Licensed + Rights, but not otherwise. + + 2. Patent and trademark rights are not licensed under this + Public License. + + 3. To the extent possible, the Licensor waives any right to + collect royalties from You for the exercise of the Licensed + Rights, whether directly or through a collecting society + under any voluntary or waivable statutory or compulsory + licensing scheme. In all other cases the Licensor expressly + reserves any right to collect such royalties. + + +Section 3 -- License Conditions. + +Your exercise of the Licensed Rights is expressly made subject to the +following conditions. + + a. Attribution. + + 1. If You Share the Licensed Material (including in modified + form), You must: + + a. retain the following if it is supplied by the Licensor + with the Licensed Material: + + i. identification of the creator(s) of the Licensed + Material and any others designated to receive + attribution, in any reasonable manner requested by + the Licensor (including by pseudonym if + designated); + + ii. a copyright notice; + + iii. a notice that refers to this Public License; + + iv. a notice that refers to the disclaimer of + warranties; + + v. a URI or hyperlink to the Licensed Material to the + extent reasonably practicable; + + b. indicate if You modified the Licensed Material and + retain an indication of any previous modifications; and + + c. indicate the Licensed Material is licensed under this + Public License, and include the text of, or the URI or + hyperlink to, this Public License. + + 2. You may satisfy the conditions in Section 3(a)(1) in any + reasonable manner based on the medium, means, and context in + which You Share the Licensed Material. For example, it may be + reasonable to satisfy the conditions by providing a URI or + hyperlink to a resource that includes the required + information. + + 3. If requested by the Licensor, You must remove any of the + information required by Section 3(a)(1)(A) to the extent + reasonably practicable. + + 4. If You Share Adapted Material You produce, the Adapter's + License You apply must not prevent recipients of the Adapted + Material from complying with this Public License. + + +Section 4 -- Sui Generis Database Rights. + +Where the Licensed Rights include Sui Generis Database Rights that +apply to Your use of the Licensed Material: + + a. for the avoidance of doubt, Section 2(a)(1) grants You the right + to extract, reuse, reproduce, and Share all or a substantial + portion of the contents of the database; + + b. if You include all or a substantial portion of the database + contents in a database in which You have Sui Generis Database + Rights, then the database in which You have Sui Generis Database + Rights (but not its individual contents) is Adapted Material; and + + c. You must comply with the conditions in Section 3(a) if You Share + all or a substantial portion of the contents of the database. + +For the avoidance of doubt, this Section 4 supplements and does not +replace Your obligations under this Public License where the Licensed +Rights include other Copyright and Similar Rights. + + +Section 5 -- Disclaimer of Warranties and Limitation of Liability. + + a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE + EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS + AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF + ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, + IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, + WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR + PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, + ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT + KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT + ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. + + b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE + TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, + NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, + INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, + COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR + USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN + ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR + DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR + IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. + + c. The disclaimer of warranties and limitation of liability provided + above shall be interpreted in a manner that, to the extent + possible, most closely approximates an absolute disclaimer and + waiver of all liability. + + +Section 6 -- Term and Termination. + + a. This Public License applies for the term of the Copyright and + Similar Rights licensed here. However, if You fail to comply with + this Public License, then Your rights under this Public License + terminate automatically. + + b. Where Your right to use the Licensed Material has terminated under + Section 6(a), it reinstates: + + 1. automatically as of the date the violation is cured, provided + it is cured within 30 days of Your discovery of the + violation; or + + 2. upon express reinstatement by the Licensor. + + For the avoidance of doubt, this Section 6(b) does not affect any + right the Licensor may have to seek remedies for Your violations + of this Public License. + + c. For the avoidance of doubt, the Licensor may also offer the + Licensed Material under separate terms or conditions or stop + distributing the Licensed Material at any time; however, doing so + will not terminate this Public License. + + d. Sections 1, 5, 6, 7, and 8 survive termination of this Public + License. + + +Section 7 -- Other Terms and Conditions. + + a. The Licensor shall not be bound by any additional or different + terms or conditions communicated by You unless expressly agreed. + + b. Any arrangements, understandings, or agreements regarding the + Licensed Material not stated herein are separate from and + independent of the terms and conditions of this Public License. + + +Section 8 -- Interpretation. + + a. For the avoidance of doubt, this Public License does not, and + shall not be interpreted to, reduce, limit, restrict, or impose + conditions on any use of the Licensed Material that could lawfully + be made without permission under this Public License. + + b. To the extent possible, if any provision of this Public License is + deemed unenforceable, it shall be automatically reformed to the + minimum extent necessary to make it enforceable. If the provision + cannot be reformed, it shall be severed from this Public License + without affecting the enforceability of the remaining terms and + conditions. + + c. No term or condition of this Public License will be waived and no + failure to comply consented to unless expressly agreed to by the + Licensor. + + d. Nothing in this Public License constitutes or may be interpreted + as a limitation upon, or waiver of, any privileges and immunities + that apply to the Licensor or You, including from the legal + processes of any jurisdiction or authority. + + +======================================================================= + +Creative Commons is not a party to its public +licenses. Notwithstanding, Creative Commons may elect to apply one of +its public licenses to material it publishes and in those instances +will be considered the “Licensor.” The text of the Creative Commons +public licenses is dedicated to the public domain under the CC0 Public +Domain Dedication. Except for the limited purpose of indicating that +material is shared under a Creative Commons public license or as +otherwise permitted by the Creative Commons policies published at +creativecommons.org/policies, Creative Commons does not authorize the +use of the trademark "Creative Commons" or any other trademark or logo +of Creative Commons without its prior written consent including, +without limitation, in connection with any unauthorized modifications +to any of its public licenses or any other arrangements, +understandings, or agreements concerning use of licensed material. For +the avoidance of doubt, this paragraph does not form part of the +public licenses. + +Creative Commons may be contacted at creativecommons.org. diff --git a/pieces/Dobutsu/README.md b/pieces/Dobutsu/README.md new file mode 100644 index 0000000..73ed708 --- /dev/null +++ b/pieces/Dobutsu/README.md @@ -0,0 +1,13 @@ +[Couch Tomato - I presume] + +I recreated all of the original [Doubutsu](https://en.wikipedia.org/wiki/D%C5%8Dbutsu_sh%C5%8Dgi) pieces in Inkscape SVG format. + +Credits: the original Doubutsu pieces were created by [Madoka Kitato](https://en.wikipedia.org/wiki/Madoka_Kitao). + +(also: [screenshot of the gshogi version](https://raw.githubusercontent.com/Ka-hu/shogi-pieces/master/_screenshots/scrot_doubutsu_gshogi.png)) + +![doubutsu screenshot](https://raw.githubusercontent.com/Ka-hu/shogi-pieces/master/_screenshots/scrot_doubutsu_xboard.png) + +## License + +Where it's not stated otherwise, my work is lincensed under [CC-BY-4.0](https://choosealicense.com/licenses/cc-by-4.0) diff --git a/pieces/Dobutsu/chick.svg b/pieces/Dobutsu/chick.svg new file mode 100644 index 0000000..69358c7 --- /dev/null +++ b/pieces/Dobutsu/chick.svg @@ -0,0 +1,139 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/elephant.svg b/pieces/Dobutsu/elephant.svg new file mode 100644 index 0000000..c7eb6dc --- /dev/null +++ b/pieces/Dobutsu/elephant.svg @@ -0,0 +1,176 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/giraffe.svg b/pieces/Dobutsu/giraffe.svg new file mode 100644 index 0000000..14a55a9 --- /dev/null +++ b/pieces/Dobutsu/giraffe.svg @@ -0,0 +1,186 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/hen.svg b/pieces/Dobutsu/hen.svg new file mode 100644 index 0000000..7256bc8 --- /dev/null +++ b/pieces/Dobutsu/hen.svg @@ -0,0 +1,156 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/lion.svg b/pieces/Dobutsu/lion.svg new file mode 100644 index 0000000..8324454 --- /dev/null +++ b/pieces/Dobutsu/lion.svg @@ -0,0 +1,201 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/rev_chick.svg b/pieces/Dobutsu/rev_chick.svg new file mode 100644 index 0000000..bbefcfc --- /dev/null +++ b/pieces/Dobutsu/rev_chick.svg @@ -0,0 +1,139 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/rev_elephant.svg b/pieces/Dobutsu/rev_elephant.svg new file mode 100644 index 0000000..c8b0b3c --- /dev/null +++ b/pieces/Dobutsu/rev_elephant.svg @@ -0,0 +1,176 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/rev_giraffe.svg b/pieces/Dobutsu/rev_giraffe.svg new file mode 100644 index 0000000..f6eb024 --- /dev/null +++ b/pieces/Dobutsu/rev_giraffe.svg @@ -0,0 +1,186 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/rev_hen.svg b/pieces/Dobutsu/rev_hen.svg new file mode 100644 index 0000000..e172f8b --- /dev/null +++ b/pieces/Dobutsu/rev_hen.svg @@ -0,0 +1,156 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/Dobutsu/rev_lion.svg b/pieces/Dobutsu/rev_lion.svg new file mode 100644 index 0000000..33c11d3 --- /dev/null +++ b/pieces/Dobutsu/rev_lion.svg @@ -0,0 +1,201 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/utils/array.js b/utils/array.js index c2cb25f..af8462e 100644 --- a/utils/array.js +++ b/utils/array.js @@ -5,8 +5,12 @@ export const ArrayFun = { return [...Array(size1)].map(() => Array(size2).fill(initElem)); }, - range: function(max) { - return [...Array(max).keys()]; + range: function(min, max) { + if (!max) { + max = min; + min = 0; + } + return [...Array(max - min).keys()].map(k => k + min); }, toObject: function(keys, values) { diff --git a/variants.js b/variants.js index cad081a..f42033e 100644 --- a/variants.js +++ b/variants.js @@ -43,8 +43,8 @@ const variants = [ {name: 'Dark', desc: 'In the shadow'}, {name: 'Diamond', desc: 'Rotating board'}, {name: 'Dice', desc: 'Roll the dice'}, -// {name: 'Discoduel', desc: 'Enter the disco', disp: 'Disco Duel'}, -// {name: 'Dobutsu', desc: "Let's catch the Lion!"}, + {name: 'Discoduel', desc: 'Enter the disco', disp: 'Disco Duel'}, + {name: 'Dobutsu', desc: "Let's catch the Lion!"}, // {name: 'Doublearmy', desc: '64 pieces on the board', disp: 'Double Army'}, {name: 'Doublemove', desc: 'Double moves'}, // {name: 'Dynamo', desc: 'Push and pull'}, diff --git a/variants/Avalanche/class.js b/variants/Avalanche/class.js index ee80550..8a4981f 100644 --- a/variants/Avalanche/class.js +++ b/variants/Avalanche/class.js @@ -167,7 +167,7 @@ export default class AvalancheRules extends ChessRules { } } - atLeastOneMove(color, lastMove) { + atLeastOneMove(color) { if (this.subTurn == 0) return true; return super.atLeastOneMove(color); diff --git a/variants/Chaining/class.js b/variants/Chaining/class.js index 9d771da..4ae7208 100644 --- a/variants/Chaining/class.js +++ b/variants/Chaining/class.js @@ -20,8 +20,8 @@ export default class ChainingRules extends ChessRules { return true; //self captures induce chaining } - setOtherVariables(fenParsed, pieceArray) { - super.setOtherVariables(fenParsed, pieceArray); + setOtherVariables(fenParsed) { + super.setOtherVariables(fenParsed); // Stack of "last move" only for intermediate chaining this.lastMoveEnd = []; } diff --git a/variants/Clorange/class.js b/variants/Clorange/class.js index 485bbd6..e8506db 100644 --- a/variants/Clorange/class.js +++ b/variants/Clorange/class.js @@ -14,14 +14,6 @@ export default class ClorangeRules extends ChessRules { return true; } - getReserveFen(o) { - if (o.init) - return "00000000000000000000"; - return ( - ["w","b"].map(c => Object.values(this.reserve[c]).join("")).join("") - ); - } - pieces(color, x, y) { let res = super.pieces(color, x, y); res['s'] = {"class": "nv-pawn", moveas: "p"}; @@ -38,9 +30,8 @@ export default class ClorangeRules extends ChessRules { static get NV_PIECES() { return ['s', 'u', 'o', 'c', 't']; } - - setOtherVariables(fen) { - super.setOtherVariables(fen, V.V_PIECES.concat(V.NV_PIECES)); + static get ReserveArray() { + return V.V_PIECES.concat(V.NV_PIECES); } // Forbid non-violent pieces to capture diff --git a/variants/Convert/class.js b/variants/Convert/class.js index bc99c63..cd2d1a9 100644 --- a/variants/Convert/class.js +++ b/variants/Convert/class.js @@ -15,8 +15,8 @@ export default class ConvertRules extends ChessRules { return false; } - setOtherVariables(fenParsed, pieceArray) { - super.setOtherVariables(fenParsed, pieceArray); + setOtherVariables(fenParsed) { + super.setOtherVariables(fenParsed); // Stack of "last move" only for intermediate chaining this.lastMoveEnd = []; } diff --git a/variants/Coregal/class.js b/variants/Coregal/class.js index 23f67c7..bbc35a4 100644 --- a/variants/Coregal/class.js +++ b/variants/Coregal/class.js @@ -61,8 +61,8 @@ export default class CoregalRules extends ChessRules { ); } - setOtherVariables(fenParsed, pieceArray) { - super.setOtherVariables(fenParsed, pieceArray); + setOtherVariables(fenParsed) { + super.setOtherVariables(fenParsed); this.relPos = { 'w': { 'k': fenParsed.relpos[0], diff --git a/variants/Discoduel/class.js b/variants/Discoduel/class.js new file mode 100644 index 0000000..c081b31 --- /dev/null +++ b/variants/Discoduel/class.js @@ -0,0 +1,51 @@ +import ChessRules from "/base_rules.js"; +import {ArrayFun} from "/utils/array.js" + +export default class DiscoduelRules extends ChessRules { + + static get Options() { + return {}; //nothing would make sense + } + + get pawnPromotions() { + return ['p']; + } + + get hasFlags() { + return false; + } + + genRandInitBaseFen() { + return { + fen: "1n4n1/8/8/8/8/8/PPPPPPPP/8", + o: {} + }; + } + + getPotentialMovesFrom([x, y]) { + const moves = super.getPotentialMovesFrom([x, y]); + if (this.turn == 'b') + // Prevent pawn captures on last rank: + return moves.filter(m => m.vanish.length == 1 || m.vanish[1].x != 0); + return moves; + } + + filterValid(moves) { + return moves; + } + + getCurrentScore() { + // No real winning condition (promotions count...) + if ( + ArrayFun.range(1, this.size.x).every(row_idx => { + this.board[row_idx].every(square => square.charAt(0) != 'w') + }) + || + !this.atLeastOneMove(this.turn) + ) { + return "1/2"; + } + return "*"; + } + +}; diff --git a/variants/Discoduel/rules.html b/variants/Discoduel/rules.html new file mode 100644 index 0000000..490d279 --- /dev/null +++ b/variants/Discoduel/rules.html @@ -0,0 +1,5 @@ +

+ Eight pawns try to promote, while the knights attempts to prevent them. + A pawn reaching last rank does not transform, but become immune to captures. +

+

Goal: "promoting" as many pawns as possible.

diff --git a/variants/Discoduel/style.css b/variants/Discoduel/style.css new file mode 100644 index 0000000..a3550bc --- /dev/null +++ b/variants/Discoduel/style.css @@ -0,0 +1 @@ +@import url("/base_pieces.css"); diff --git a/variants/Dobutsu/class.js b/variants/Dobutsu/class.js new file mode 100644 index 0000000..7632a17 --- /dev/null +++ b/variants/Dobutsu/class.js @@ -0,0 +1,100 @@ +import ChessRules from "/base_rules.js"; + +export default class DobutsuRules extends ChessRules { + + static get Options() { + return {}; + } + + get hasFlags() { + return false; + } + + get hasEnpassant() { + return false; + } + + pieces(color, x, y) { + const pawnShift = this.getPawnShift(color || 'w'); + // NOTE: classs change according to playerColor (orientation) + const mySide = (this.playerColor == color); + return { + 'c': { + "class": (mySide ? "" : "rev-") + "chick", + both: [{steps: [[pawnShift, 0]], range: 1}] + }, + 'h': { + "class": (mySide ? "" : "rev-") + "hen", + both: [ + { + steps: [ + [pawnShift, 1], [pawnShift, -1], + [0, 1], [0, -1], [1, 0], [-1, 0] + ], + range: 1 + } + ] + }, + 'e': { + "class": (mySide ? "" : "rev-") + "elephant", + both: [{steps: [[-1, 1], [-1, -1], [1, 1], [1, -1]], range: 1}] + }, + 'g': { + "class": (mySide ? "" : "rev-") + "giraffe", + both: [{steps: [[0, 1], [0, -1], [1, 0], [-1, 0]], range: 1}] + }, + 'l': { + "class": (mySide ? "" : "rev-") + "lion", + both: [{ + steps: [[-1, 1], [-1, -1], [1, 1], [1, -1], + [0, 1], [0, -1], [1, 0], [-1, 0]], + range: 1 + }] + } + }; + } + + isKing(x, y, p) { + if (!p) + p = this.getPiece(x, y); + return (p == 'l'); + } + + static get ReserveArray() { + return ['p', 'h', 'e', 'g']; + } + + constructor(o) { + o.options = {crazyhouse: true, taking: true}; + super(o); + } + + get pawnPromotions() { + return ['h']; + } + + genRandInitBaseFen() { + return { + fen: "gle/1c1/1C1/ELG", + o: {} + }; + } + + get size() { + return {x: 4, y: 4}; + } + + getCurrentScore(move_s) { + const res = super.getCurrentScore(move_s); + if (res != '*') + return res; + const oppCol = C.GetOppTurn(this.turn); + const oppLastRank = (oppCol == 'b' ? 3 : 0); + for (let j=0; j < this.size.y; j++) { + if (this.board[oppLastRank][j] == oppCol + 'l') + return (oppCol == 'w' ? "1-0" : "0-1"); + } + return "*"; + } + +}; diff --git a/variants/Dobutsu/rules.html b/variants/Dobutsu/rules.html new file mode 100644 index 0000000..45bbc5d --- /dev/null +++ b/variants/Dobutsu/rules.html @@ -0,0 +1,12 @@ +

+ Simplified Shogi game. Goal: capture the Lion. + Pieces move as indicated on them (red arrows). +

+ +

Captured units can be landed on the board later.

+ +

+ + Wikipedia page + . +

diff --git a/variants/Dobutsu/style.css b/variants/Dobutsu/style.css new file mode 100644 index 0000000..fb861f1 --- /dev/null +++ b/variants/Dobutsu/style.css @@ -0,0 +1,34 @@ +piece.chick { + background-image: url('/pieces/Dobutsu/chick.svg'); +} +piece.rev-chick { + background-image: url('/pieces/Dobutsu/rev_chick.svg'); +} + +piece.hen { + background-image: url('/pieces/Dobutsu/hen.svg'); +} +piece.rev-hen { + background-image: url('/pieces/Dobutsu/rev_hen.svg'); +} + +piece.elephant { + background-image: url('/pieces/Dobutsu/elephant.svg'); +} +piece.rev-elephant { + background-image: url('/pieces/Dobutsu/rev_elephant.svg'); +} + +piece.giraffe { + background-image: url('/pieces/Dobutsu/giraffe.svg'); +} +piece.rev-giraffe { + background-image: url('/pieces/Dobutsu/rev_giraffe.svg'); +} + +piece.lion { + background-image: url('/pieces/Dobutsu/lion.svg'); +} +piece.rev-lion { + background-image: url('/pieces/Dobutsu/rev_lion.svg'); +} -- 2.48.1 From c4d2eb5bdf1b23d8c4a9d09322f84a9e0da9d60c Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Wed, 3 Jan 2024 17:10:33 +0100 Subject: [PATCH 08/16] Fix Dobutsu --- base_rules.js | 17 +++++++++-------- variants/Dobutsu/class.js | 21 ++++++++++----------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/base_rules.js b/base_rules.js index 217d055..9a364e8 100644 --- a/base_rules.js +++ b/base_rules.js @@ -465,7 +465,7 @@ export default class ChessRules { // VISUAL UTILS getPieceWidth(rwidth) { - return (rwidth / this.size.y); + return (rwidth / Math.max(this.size.x, this.size.y)); } getReserveSquareSize(rwidth, nbR) { @@ -856,9 +856,10 @@ export default class ChessRules { y = (this.playerColor == i ? y = r.height + 5 : - 5 - rsqSize); } else { - const sqSize = r.width / this.size.y; + const sqSize = r.width / Math.max(this.size.x, this.size.y); const flipped = this.flippedBoard; - x = (flipped ? this.size.y - 1 - j : j) * sqSize; + x = (flipped ? this.size.y - 1 - j : j) * sqSize + + Math.abs(this.size.x - this.size.y) * sqSize / 2; y = (flipped ? this.size.x - 1 - i : i) * sqSize; } return [r.x + x, r.y + y]; @@ -2284,11 +2285,6 @@ export default class ChessRules { if (this.hasCastle) this.updateCastleFlags(move); if (this.options["crazyhouse"]) { - move.vanish.forEach(v => { - const square = C.CoordsToSquare({x: v.x, y: v.y}); - if (this.ispawn[square]) - delete this.ispawn[square]; - }); if (move.appear.length > 0 && move.vanish.length > 0) { // Assumption: something is moving const initSquare = C.CoordsToSquare(move.start); @@ -2307,6 +2303,11 @@ export default class ChessRules { delete this.ispawn[destSquare]; } } + move.vanish.forEach(v => { + const square = C.CoordsToSquare({x: v.x, y: v.y}); + if (this.ispawn[square]) + delete this.ispawn[square]; + }); } const minSize = Math.min(move.appear.length, move.vanish.length); if ( diff --git a/variants/Dobutsu/class.js b/variants/Dobutsu/class.js index 7632a17..29b0708 100644 --- a/variants/Dobutsu/class.js +++ b/variants/Dobutsu/class.js @@ -19,7 +19,7 @@ export default class DobutsuRules extends ChessRules { // NOTE: classs change according to playerColor (orientation) const mySide = (this.playerColor == color); return { - 'c': { + 'p': { "class": (mySide ? "" : "rev-") + "chick", both: [{steps: [[pawnShift, 0]], range: 1}] }, @@ -43,7 +43,7 @@ export default class DobutsuRules extends ChessRules { "class": (mySide ? "" : "rev-") + "giraffe", both: [{steps: [[0, 1], [0, -1], [1, 0], [-1, 0]], range: 1}] }, - 'l': { + 'k': { "class": (mySide ? "" : "rev-") + "lion", both: [{ steps: [[-1, 1], [-1, -1], [1, 1], [1, -1], @@ -54,16 +54,15 @@ export default class DobutsuRules extends ChessRules { }; } - isKing(x, y, p) { - if (!p) - p = this.getPiece(x, y); - return (p == 'l'); - } - static get ReserveArray() { return ['p', 'h', 'e', 'g']; } + updateReserve(color, piece, count) { + if (piece != 'k') + super.updateReserve(color, piece, count); + } + constructor(o) { o.options = {crazyhouse: true, taking: true}; super(o); @@ -75,13 +74,13 @@ export default class DobutsuRules extends ChessRules { genRandInitBaseFen() { return { - fen: "gle/1c1/1C1/ELG", + fen: "gke/1p1/1P1/EKG", o: {} }; } get size() { - return {x: 4, y: 4}; + return {x: 4, y: 3}; } getCurrentScore(move_s) { @@ -91,7 +90,7 @@ export default class DobutsuRules extends ChessRules { const oppCol = C.GetOppTurn(this.turn); const oppLastRank = (oppCol == 'b' ? 3 : 0); for (let j=0; j < this.size.y; j++) { - if (this.board[oppLastRank][j] == oppCol + 'l') + if (this.board[oppLastRank][j] == oppCol + 'k') return (oppCol == 'w' ? "1-0" : "0-1"); } return "*"; -- 2.48.1 From 4fcd7ab062f5250757804d633df01bd0d06da137 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Thu, 4 Jan 2024 10:23:47 +0100 Subject: [PATCH 09/16] Fix Discoduel --- variants/Discoduel/class.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/variants/Discoduel/class.js b/variants/Discoduel/class.js index c081b31..ec96a43 100644 --- a/variants/Discoduel/class.js +++ b/variants/Discoduel/class.js @@ -38,7 +38,9 @@ export default class DiscoduelRules extends ChessRules { // No real winning condition (promotions count...) if ( ArrayFun.range(1, this.size.x).every(row_idx => { - this.board[row_idx].every(square => square.charAt(0) != 'w') + return this.board[row_idx].every(square => { + return (!square || square.charAt(0) != 'w'); + }) }) || !this.atLeastOneMove(this.turn) -- 2.48.1 From d66135396f3a6e140947545630004ce11f8eee7b Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Thu, 4 Jan 2024 10:37:28 +0100 Subject: [PATCH 10/16] Fix Dobutsu, extend Align4 --- variants/Align4/class.js | 19 +++++++++++++++++++ variants/Dobutsu/class.js | 15 ++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/variants/Align4/class.js b/variants/Align4/class.js index 09a79ed..f5b8f83 100644 --- a/variants/Align4/class.js +++ b/variants/Align4/class.js @@ -13,6 +13,14 @@ export default class Align4Rules extends ChessRules { {label: "Random", value: 1} ] }], + input: [ + { + label: "Pawn first", + variable: "pawnfirst", + type: "checkbox", + defaut: false + } + ], styles: ["atomic", "capture", "cylinder"] }; } @@ -39,6 +47,17 @@ export default class Align4Rules extends ChessRules { // Just do not update any reserve (infinite supply) updateReserve() {} + canDrop([c, p], [i, j]) { + return ( + this.board[i][j] == "" && + ( + p != "p" || this.options["pawnfirst"] || + (c == 'w' && i < this.size.x - 1) || + (c == 'b' && i > 0) + ) + ); + } + getCurrentScore(move_s) { const score = super.getCurrentScore(move_s); if (score != "*") diff --git a/variants/Dobutsu/class.js b/variants/Dobutsu/class.js index 29b0708..d8ceafa 100644 --- a/variants/Dobutsu/class.js +++ b/variants/Dobutsu/class.js @@ -87,11 +87,16 @@ export default class DobutsuRules extends ChessRules { const res = super.getCurrentScore(move_s); if (res != '*') return res; - const oppCol = C.GetOppTurn(this.turn); - const oppLastRank = (oppCol == 'b' ? 3 : 0); - for (let j=0; j < this.size.y; j++) { - if (this.board[oppLastRank][j] == oppCol + 'k') - return (oppCol == 'w' ? "1-0" : "0-1"); + for (let lastRank of [0, 3]) { + const color = (lastRank == 0 ? 'w' : 'b'); + for (let j=0; j < this.size.y; j++) { + if ( + this.board[lastRank][j] == color + 'k' && + !this.underAttack([lastRank, j], [C.GetOppTurn(color)]) + ) { + return (color == 'w' ? "1-0" : "0-1"); + } + } } return "*"; } -- 2.48.1 From 66ab134b7ab3ba00204fb316ba7636c904331d6c Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Thu, 4 Jan 2024 11:19:10 +0100 Subject: [PATCH 11/16] Add Doublearmy. Start thinking about Dynamo --- README.md | 10 +- TODO | 3 - initialize.sh | 8 + pieces/black_commoner.svg | 105 ++++ pieces/white_commoner.svg | 94 +++ variants.js | 4 +- variants/Doublearmy/class.js | 43 ++ variants/Doublearmy/rules.html | 6 + variants/Doublearmy/style.css | 9 + variants/Dynamo/class.js | 921 ++++++++++++++++++++++++++++ variants/Dynamo/complete_rules.html | 142 +++++ variants/Dynamo/rules.html | 24 + variants/Dynamo/style.css | 1 + 13 files changed, 1359 insertions(+), 11 deletions(-) create mode 100755 initialize.sh create mode 100644 pieces/black_commoner.svg create mode 100644 pieces/white_commoner.svg create mode 100644 variants/Doublearmy/class.js create mode 100644 variants/Doublearmy/rules.html create mode 100644 variants/Doublearmy/style.css create mode 100644 variants/Dynamo/class.js create mode 100644 variants/Dynamo/complete_rules.html create mode 100644 variants/Dynamo/rules.html create mode 100644 variants/Dynamo/style.css diff --git a/README.md b/README.md index 4636ac4..0b900c5 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,10 @@ PHP + Node.js + npm. ## Usage -```wget https://xogo.live/assets.zip && unzip assets.zip```
-```wget https://xogo.live/extras.zip && unzip extras.zip```
-Rename parameters.js.dist → parameters.js, and edit file.
-```npm i``` +Initialisation (done once): -Generate some pieces:
-```python generateSVG.py``` in pieces/Avalam +```./initialize.sh``` + +You may want to edit the parameters.js file. Then: ```./start.sh``` (and later, ```./stop.sh```) diff --git a/TODO b/TODO index fac46e3..8afe466 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,3 @@ -add variants : -Dark Racing Kings ? Checkered-Teleport ? - Hmm... non ? --> Otage, Emergo, Pacosako : fonction "buildPiece(arg1, arg2)" returns HTML element with 2 SVG or SVG + number ==> plus simple : deux classes, images superposées. diff --git a/initialize.sh b/initialize.sh new file mode 100755 index 0000000..b7a9f4f --- /dev/null +++ b/initialize.sh @@ -0,0 +1,8 @@ +!#/bin/sh + +wget https://xogo.live/assets.zip && unzip assets.zip +wget https://xogo.live/extras.zip && unzip extras.zip +cp parameters.js.dist parameters.js +npm i +cd pieces/Avalam && python generateSVG.py +#cd pieces/Emergo && python generateSVG.py diff --git a/pieces/black_commoner.svg b/pieces/black_commoner.svg new file mode 100644 index 0000000..0995449 --- /dev/null +++ b/pieces/black_commoner.svg @@ -0,0 +1,105 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/white_commoner.svg b/pieces/white_commoner.svg new file mode 100644 index 0000000..12f2b27 --- /dev/null +++ b/pieces/white_commoner.svg @@ -0,0 +1,94 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + diff --git a/variants.js b/variants.js index f42033e..10cf42a 100644 --- a/variants.js +++ b/variants.js @@ -45,9 +45,9 @@ const variants = [ {name: 'Dice', desc: 'Roll the dice'}, {name: 'Discoduel', desc: 'Enter the disco', disp: 'Disco Duel'}, {name: 'Dobutsu', desc: "Let's catch the Lion!"}, -// {name: 'Doublearmy', desc: '64 pieces on the board', disp: 'Double Army'}, + {name: 'Doublearmy', desc: '64 pieces on the board', disp: 'Double Army'}, {name: 'Doublemove', desc: 'Double moves'}, -// {name: 'Dynamo', desc: 'Push and pull'}, + {name: 'Dynamo', desc: 'Push and pull'}, // {name: 'Eightpieces', desc: 'Each piece is unique', disp: '8 Pieces'}, // {name: 'Emergo', desc: 'Stacking Checkers variant'}, // {name: 'Empire', desc: 'Empire versus Kingdom'}, diff --git a/variants/Doublearmy/class.js b/variants/Doublearmy/class.js new file mode 100644 index 0000000..abbb564 --- /dev/null +++ b/variants/Doublearmy/class.js @@ -0,0 +1,43 @@ +import ChessRules from "/base_rules.js"; + +export default class DoublearmyRules extends ChessRules { + + static get Options() { + return { + select: C.Options.select, + input: C.Options.input, + styles: C.Options.styles.filter(s => s != "madrasi") + }; + } + + pieces(color, x, y) { + let res = super.pieces(color, x, y); + return Object.assign( + { + 'c': { + "class": "commoner", + moveas: 'k' + } + }, + res + ); + } + + genRandInitBaseFen() { + const s = super.genRandInitBaseFen(); + const rows = s.fen.split('/'); + return { + fen: + rows[0] + "/" + + rows[1] + "/" + + rows[0].replace('k', 'c') + "/" + + rows[1] + "/" + + rows[6] + "/" + + rows[7].replace('K', 'C') + "/" + + rows[6] + "/" + + rows[7], + o: s.o + }; + } + +}; diff --git a/variants/Doublearmy/rules.html b/variants/Doublearmy/rules.html new file mode 100644 index 0000000..b9908ab --- /dev/null +++ b/variants/Doublearmy/rules.html @@ -0,0 +1,6 @@ +

+ The four middle ranks contain a replica of the initial pieces. + The central "king" has no royal status, and is thus named "commoner". +

+ +

Vincent Rothuis (2020).

diff --git a/variants/Doublearmy/style.css b/variants/Doublearmy/style.css new file mode 100644 index 0000000..e50c2f4 --- /dev/null +++ b/variants/Doublearmy/style.css @@ -0,0 +1,9 @@ +@import url("/base_pieces.css"); + +piece.black.commoner { + background-image: url('/pieces/black_commoner.svg'); +} + +piece.white.commoner { + background-image: url('/pieces/white_commoner.svg'); +} diff --git a/variants/Dynamo/class.js b/variants/Dynamo/class.js new file mode 100644 index 0000000..0996a70 --- /dev/null +++ b/variants/Dynamo/class.js @@ -0,0 +1,921 @@ +import ChessRules from "/base_rules.js"; + +export default class DynamoRules extends ChessRules { + + // TODO? later, allow to push out pawns on a and h files + get hasEnpassant() { + return false; + } + +/// TODO::: + + canIplay(side, [x, y]) { + // Sometimes opponent's pieces can be moved directly + return this.turn == side; + } + + setOtherVariables(fen) { + super.setOtherVariables(fen); + this.subTurn = 1; + // Local stack of "action moves" + this.amoves = []; + const amove = V.ParseFen(fen).amove; + if (amove != "-") { + const amoveParts = amove.split("/"); + let move = { + // No need for start & end + appear: [], + vanish: [] + }; + [0, 1].map(i => { + if (amoveParts[i] != "-") { + amoveParts[i].split(".").forEach(av => { + // Format is "bpe3" + const xy = V.SquareToCoords(av.substr(2)); + move[i == 0 ? "appear" : "vanish"].push( + new PiPo({ + x: xy.x, + y: xy.y, + c: av[0], + p: av[1] + }) + ); + }); + } + }); + this.amoves.push(move); + } + // Stack "first moves" (on subTurn 1) to merge and check opposite moves + this.firstMove = []; + } + + static ParseFen(fen) { + return Object.assign( + ChessRules.ParseFen(fen), + { amove: fen.split(" ")[4] } + ); + } + + static IsGoodFen(fen) { + if (!ChessRules.IsGoodFen(fen)) return false; + const fenParts = fen.split(" "); + if (fenParts.length != 5) return false; + if (fenParts[4] != "-") { + // TODO: a single regexp instead. + // Format is [bpa2[.wpd3]] || '-'/[bbc3[.wrd5]] || '-' + const amoveParts = fenParts[4].split("/"); + if (amoveParts.length != 2) return false; + for (let part of amoveParts) { + if (part != "-") { + for (let psq of part.split(".")) + if (!psq.match(/^[a-z]{3}[1-8]$/)) return false; + } + } + } + return true; + } + + getFen() { + return super.getFen() + " " + this.getAmoveFen(); + } + + getFenForRepeat() { + return super.getFenForRepeat() + "_" + this.getAmoveFen(); + } + + getAmoveFen() { + const L = this.amoves.length; + if (L == 0) return "-"; + return ( + ["appear","vanish"].map( + mpart => { + if (this.amoves[L-1][mpart].length == 0) return "-"; + return ( + this.amoves[L-1][mpart].map( + av => { + const square = V.CoordsToSquare({ x: av.x, y: av.y }); + return av.c + av.p + square; + } + ).join(".") + ); + } + ).join("/") + ); + } + + canTake() { + // Captures don't occur (only pulls & pushes) + return false; + } + + // Step is right, just add (push/pull) moves in this direction + // Direction is assumed normalized. + getMovesInDirection([x, y], [dx, dy], nbSteps) { + nbSteps = nbSteps || 8; //max 8 steps anyway + let [i, j] = [x + dx, y + dy]; + let moves = []; + const color = this.getColor(x, y); + const piece = this.getPiece(x, y); + const lastRank = (color == 'w' ? 0 : 7); + let counter = 1; + while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + if (i == lastRank && piece == V.PAWN) { + // Promotion by push or pull + V.PawnSpecs.promotions.forEach(p => { + let move = super.getBasicMove([x, y], [i, j], { c: color, p: p }); + moves.push(move); + }); + } + else moves.push(super.getBasicMove([x, y], [i, j])); + if (++counter > nbSteps) break; + i += dx; + j += dy; + } + if (!V.OnBoard(i, j) && piece != V.KING) { + // Add special "exit" move, by "taking king" + moves.push( + new Move({ + start: { x: x, y: y }, + end: { x: this.kingPos[color][0], y: this.kingPos[color][1] }, + appear: [], + vanish: [{ x: x, y: y, c: color, p: piece }] + }) + ); + } + return moves; + } + + // Normalize direction to know the step + getNormalizedDirection([dx, dy]) { + const absDir = [Math.abs(dx), Math.abs(dy)]; + let divisor = 0; + if (absDir[0] != 0 && absDir[1] != 0 && absDir[0] != absDir[1]) + // Knight + divisor = Math.min(absDir[0], absDir[1]); + else + // Standard slider (or maybe a pawn or king: same) + divisor = Math.max(absDir[0], absDir[1]); + return [dx / divisor, dy / divisor]; + } + + // There was something on x2,y2, maybe our color, pushed or (self)pulled + isAprioriValidExit([x1, y1], [x2, y2], color2, piece2) { + const color1 = this.getColor(x1, y1); + const pawnShift = (color1 == 'w' ? -1 : 1); + const lastRank = (color1 == 'w' ? 0 : 7); + const deltaX = Math.abs(x1 - x2); + const deltaY = Math.abs(y1 - y2); + const checkSlider = () => { + const dir = this.getNormalizedDirection([x2 - x1, y2 - y1]); + let [i, j] = [x1 + dir[0], y1 + dir[1]]; + while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + i += dir[0]; + j += dir[1]; + } + return !V.OnBoard(i, j); + }; + switch (piece2 || this.getPiece(x1, y1)) { + case V.PAWN: + return ( + x1 + pawnShift == x2 && + ( + (color1 == color2 && x2 == lastRank && y1 == y2) || + ( + color1 != color2 && + deltaY == 1 && + !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) + ) + ) + ); + case V.ROOK: + if (x1 != x2 && y1 != y2) return false; + return checkSlider(); + case V.KNIGHT: + return ( + deltaX + deltaY == 3 && + (deltaX == 1 || deltaY == 1) && + !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) + ); + case V.BISHOP: + if (deltaX != deltaY) return false; + return checkSlider(); + case V.QUEEN: + if (deltaX != 0 && deltaY != 0 && deltaX != deltaY) return false; + return checkSlider(); + case V.KING: + return ( + deltaX <= 1 && + deltaY <= 1 && + !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) + ); + } + return false; + } + + isAprioriValidVertical([x1, y1], x2) { + const piece = this.getPiece(x1, y1); + const deltaX = Math.abs(x1 - x2); + const startRank = (this.getColor(x1, y1) == 'w' ? 6 : 1); + return ( + [V.QUEEN, V.ROOK].includes(piece) || + ( + [V.KING, V.PAWN].includes(piece) && + ( + deltaX == 1 || + (deltaX == 2 && piece == V.PAWN && x1 == startRank) + ) + ) + ); + } + + // NOTE: for pushes, play the pushed piece first. + // for pulls: play the piece doing the action first + // NOTE: to push a piece out of the board, make it slide until its king + getPotentialMovesFrom([x, y]) { + const color = this.turn; + const sqCol = this.getColor(x, y); + const pawnShift = (color == 'w' ? -1 : 1); + const pawnStartRank = (color == 'w' ? 6 : 1); + const getMoveHash = (m) => { + return V.CoordsToSquare(m.start) + V.CoordsToSquare(m.end); + }; + if (this.subTurn == 1) { + const addMoves = (dir, nbSteps) => { + const newMoves = + this.getMovesInDirection([x, y], [-dir[0], -dir[1]], nbSteps) + .filter(m => !movesHash[getMoveHash(m)]); + newMoves.forEach(m => { movesHash[getMoveHash(m)] = true; }); + Array.prototype.push.apply(moves, newMoves); + }; + // Free to play any move (if piece of my color): + let moves = + sqCol == color + ? super.getPotentialMovesFrom([x, y]) + : []; + // There may be several suicide moves: keep only one + let hasExit = false; + moves = moves.filter(m => { + const suicide = (m.appear.length == 0); + if (suicide) { + if (hasExit) return false; + hasExit = true; + } + return true; + }); + // Structure to avoid adding moves twice (can be action & move) + let movesHash = {}; + moves.forEach(m => { movesHash[getMoveHash(m)] = true; }); + // [x, y] is pushed by 'color' + for (let step of V.steps[V.KNIGHT]) { + const [i, j] = [x + step[0], y + step[1]]; + if ( + V.OnBoard(i, j) && + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == color && + this.getPiece(i, j) == V.KNIGHT + ) { + addMoves(step, 1); + } + } + for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { + let [i, j] = [x + step[0], y + step[1]]; + while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + i += step[0]; + j += step[1]; + } + if ( + V.OnBoard(i, j) && + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == color + ) { + const deltaX = Math.abs(i - x); + const deltaY = Math.abs(j - y); + switch (this.getPiece(i, j)) { + case V.PAWN: + if ( + (x - i) / deltaX == pawnShift && + deltaX <= 2 && + deltaY <= 1 + ) { + if (sqCol == color && deltaY == 0) { + // Pushed forward + const maxSteps = (i == pawnStartRank && deltaX == 1 ? 2 : 1); + addMoves(step, maxSteps); + } + else if (sqCol != color && deltaY == 1 && deltaX == 1) + // Pushed diagonally + addMoves(step, 1); + } + break; + case V.ROOK: + if (deltaX == 0 || deltaY == 0) addMoves(step); + break; + case V.BISHOP: + if (deltaX == deltaY) addMoves(step); + break; + case V.QUEEN: + // All steps are valid for a queen: + addMoves(step); + break; + case V.KING: + if (deltaX <= 1 && deltaY <= 1) addMoves(step, 1); + break; + } + } + } + return moves; + } + // If subTurn == 2 then we should have a first move, + // which restrict what we can play now: only in the first move direction + const L = this.firstMove.length; + const fm = this.firstMove[L-1]; + if ( + (fm.appear.length == 2 && fm.vanish.length == 2) || + (fm.vanish[0].c == sqCol && sqCol != color) + ) { + // Castle or again opponent color: no move playable then. + return []; + } + const piece = this.getPiece(x, y); + const getPushExit = () => { + // Piece at subTurn 1 exited: can I have caused the exit? + if ( + this.isAprioriValidExit( + [x, y], + [fm.start.x, fm.start.y], + fm.vanish[0].c + ) + ) { + // Seems so: + const dir = this.getNormalizedDirection( + [fm.start.x - x, fm.start.y - y]); + const nbSteps = + [V.PAWN, V.KING, V.KNIGHT].includes(piece) + ? 1 + : null; + return this.getMovesInDirection([x, y], dir, nbSteps); + } + return []; + } + const getPushMoves = () => { + // Piece from subTurn 1 is still on board: + const dirM = this.getNormalizedDirection( + [fm.end.x - fm.start.x, fm.end.y - fm.start.y]); + const dir = this.getNormalizedDirection( + [fm.start.x - x, fm.start.y - y]); + // Normalized directions should match + if (dir[0] == dirM[0] && dir[1] == dirM[1]) { + // We don't know if first move is a pushed piece or normal move, + // so still must check if the push is valid. + const deltaX = Math.abs(fm.start.x - x); + const deltaY = Math.abs(fm.start.y - y); + switch (piece) { + case V.PAWN: + if (x == pawnStartRank) { + if ( + (fm.start.x - x) * pawnShift < 0 || + deltaX >= 3 || + deltaY >= 2 || + (fm.vanish[0].c == color && deltaY > 0) || + (fm.vanish[0].c != color && deltaY == 0) || + Math.abs(fm.end.x - fm.start.x) > deltaX || + fm.end.y - fm.start.y != fm.start.y - y + ) { + return []; + } + } + else { + if ( + fm.start.x - x != pawnShift || + deltaY >= 2 || + (fm.vanish[0].c == color && deltaY == 1) || + (fm.vanish[0].c != color && deltaY == 0) || + fm.end.x - fm.start.x != pawnShift || + fm.end.y - fm.start.y != fm.start.y - y + ) { + return []; + } + } + break; + case V.KNIGHT: + if ( + (deltaX + deltaY != 3 || (deltaX == 0 && deltaY == 0)) || + (fm.end.x - fm.start.x != fm.start.x - x) || + (fm.end.y - fm.start.y != fm.start.y - y) + ) { + return []; + } + break; + case V.KING: + if ( + (deltaX >= 2 || deltaY >= 2) || + (fm.end.x - fm.start.x != fm.start.x - x) || + (fm.end.y - fm.start.y != fm.start.y - y) + ) { + return []; + } + break; + case V.BISHOP: + if (deltaX != deltaY) return []; + break; + case V.ROOK: + if (deltaX != 0 && deltaY != 0) return []; + break; + case V.QUEEN: + if (deltaX != deltaY && deltaX != 0 && deltaY != 0) return []; + break; + } + // Nothing should stand between [x, y] and the square fm.start + let [i, j] = [x + dir[0], y + dir[1]]; + while ( + (i != fm.start.x || j != fm.start.y) && + this.board[i][j] == V.EMPTY + ) { + i += dir[0]; + j += dir[1]; + } + if (i == fm.start.x && j == fm.start.y) + return this.getMovesInDirection([x, y], dir); + } + return []; + } + const getPullExit = () => { + // Piece at subTurn 1 exited: can I be pulled? + // Note: kings cannot suicide, so fm.vanish[0].p is not KING. + // Could be PAWN though, if a pawn was pushed out of board. + if ( + fm.vanish[0].p != V.PAWN && //pawns cannot pull + this.isAprioriValidExit( + [x, y], + [fm.start.x, fm.start.y], + fm.vanish[0].c, + fm.vanish[0].p + ) + ) { + // Seems so: + const dir = this.getNormalizedDirection( + [fm.start.x - x, fm.start.y - y]); + const nbSteps = (fm.vanish[0].p == V.KNIGHT ? 1 : null); + return this.getMovesInDirection([x, y], dir, nbSteps); + } + return []; + }; + const getPullMoves = () => { + if (fm.vanish[0].p == V.PAWN) + // pawns cannot pull + return []; + const dirM = this.getNormalizedDirection( + [fm.end.x - fm.start.x, fm.end.y - fm.start.y]); + const dir = this.getNormalizedDirection( + [fm.start.x - x, fm.start.y - y]); + // Normalized directions should match + if (dir[0] == dirM[0] && dir[1] == dirM[1]) { + // Am I at the right distance? + const deltaX = Math.abs(x - fm.start.x); + const deltaY = Math.abs(y - fm.start.y); + if ( + (fm.vanish[0].p == V.KING && (deltaX > 1 || deltaY > 1)) || + (fm.vanish[0].p == V.KNIGHT && + (deltaX + deltaY != 3 || deltaX == 0 || deltaY == 0)) + ) { + return []; + } + // Nothing should stand between [x, y] and the square fm.start + let [i, j] = [x + dir[0], y + dir[1]]; + while ( + (i != fm.start.x || j != fm.start.y) && + this.board[i][j] == V.EMPTY + ) { + i += dir[0]; + j += dir[1]; + } + if (i == fm.start.x && j == fm.start.y) + return this.getMovesInDirection([x, y], dir); + } + return []; + }; + if (fm.vanish[0].c != color) { + // Only possible action is a push: + if (fm.appear.length == 0) return getPushExit(); + return getPushMoves(); + } + else if (sqCol != color) { + // Only possible action is a pull, considering moving piece abilities + if (fm.appear.length == 0) return getPullExit(); + return getPullMoves(); + } + else { + // My color + my color: both actions possible + // Structure to avoid adding moves twice (can be action & move) + let movesHash = {}; + if (fm.appear.length == 0) { + const pushes = getPushExit(); + pushes.forEach(m => { movesHash[getMoveHash(m)] = true; }); + return ( + pushes.concat(getPullExit().filter(m => !movesHash[getMoveHash(m)])) + ); + } + const pushes = getPushMoves(); + pushes.forEach(m => { movesHash[getMoveHash(m)] = true; }); + return ( + pushes.concat(getPullMoves().filter(m => !movesHash[getMoveHash(m)])) + ); + } + return []; + } + + getSlideNJumpMoves([x, y], steps, oneStep) { + let moves = []; + const c = this.getColor(x, y); + const piece = this.getPiece(x, y); + outerLoop: for (let step of steps) { + let i = x + step[0]; + let j = y + step[1]; + while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + moves.push(this.getBasicMove([x, y], [i, j])); + if (oneStep) continue outerLoop; + i += step[0]; + j += step[1]; + } + if (V.OnBoard(i, j)) { + if (this.canTake([x, y], [i, j])) + moves.push(this.getBasicMove([x, y], [i, j])); + } + else { + // Add potential board exit (suicide), except for the king + if (piece != V.KING) { + moves.push({ + start: { x: x, y: y}, + end: { x: this.kingPos[c][0], y: this.kingPos[c][1] }, + appear: [], + vanish: [ + new PiPo({ + x: x, + y: y, + c: c, + p: piece + }) + ] + }); + } + } + } + return moves; + } + + // Does m2 un-do m1 ? (to disallow undoing actions) + oppositeMoves(m1, m2) { + const isEqual = (av1, av2) => { + for (let av of av1) { + const avInAv2 = av2.find(elt => { + return ( + elt.x == av.x && + elt.y == av.y && + elt.c == av.c && + elt.p == av.p + ); + }); + if (!avInAv2) return false; + } + return true; + }; + // All appear and vanish arrays must have the same length + const mL = m1.appear.length; + return ( + m2.appear.length == mL && + m1.vanish.length == mL && + m2.vanish.length == mL && + isEqual(m1.appear, m2.vanish) && + isEqual(m1.vanish, m2.appear) + ); + } + + getAmove(move1, move2) { + // Just merge (one is action one is move, one may be empty) + return { + appear: move1.appear.concat(move2.appear), + vanish: move1.vanish.concat(move2.vanish) + } + } + + filterValid(moves) { + const color = this.turn; + const La = this.amoves.length; + if (this.subTurn == 1) { + return moves.filter(m => { + // A move is valid either if it doesn't result in a check, + // or if a second move is possible to counter the check + // (not undoing a potential move + action of the opponent) + this.play(m); + let res = this.underCheck(color); + if (this.subTurn == 2) { + let isOpposite = La > 0 && this.oppositeMoves(this.amoves[La-1], m); + if (res || isOpposite) { + const moves2 = this.getAllPotentialMoves(); + for (let m2 of moves2) { + this.play(m2); + const res2 = this.underCheck(color); + const amove = this.getAmove(m, m2); + isOpposite = + La > 0 && this.oppositeMoves(this.amoves[La-1], amove); + this.undo(m2); + if (!res2 && !isOpposite) { + res = false; + break; + } + } + } + } + this.undo(m); + return !res; + }); + } + if (La == 0) return super.filterValid(moves); + const Lf = this.firstMove.length; + return ( + super.filterValid( + moves.filter(m => { + // Move shouldn't undo another: + const amove = this.getAmove(this.firstMove[Lf-1], m); + return !this.oppositeMoves(this.amoves[La-1], amove); + }) + ) + ); + } + + isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { + for (let step of steps) { + let rx = x + step[0], + ry = y + step[1]; + while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { + rx += step[0]; + ry += step[1]; + } + if ( + V.OnBoard(rx, ry) && + this.getPiece(rx, ry) == piece && + this.getColor(rx, ry) == color + ) { + // Continue some steps in the same direction (pull) + rx += step[0]; + ry += step[1]; + while ( + V.OnBoard(rx, ry) && + this.board[rx][ry] == V.EMPTY && + !oneStep + ) { + rx += step[0]; + ry += step[1]; + } + if (!V.OnBoard(rx, ry)) return true; + // Step in the other direction (push) + rx = x - step[0]; + ry = y - step[1]; + while ( + V.OnBoard(rx, ry) && + this.board[rx][ry] == V.EMPTY && + !oneStep + ) { + rx -= step[0]; + ry -= step[1]; + } + if (!V.OnBoard(rx, ry)) return true; + } + } + return false; + } + + isAttackedByPawn([x, y], color) { + // The king can be pushed out by a pawn on last rank or near the edge + const pawnShift = (color == "w" ? 1 : -1); + for (let i of [-1, 1]) { + if ( + V.OnBoard(x + pawnShift, y + i) && + this.board[x + pawnShift][y + i] != V.EMPTY && + this.getPiece(x + pawnShift, y + i) == V.PAWN && + this.getColor(x + pawnShift, y + i) == color + ) { + if (!V.OnBoard(x - pawnShift, y - i)) return true; + } + } + return false; + } + + static OnTheEdge(x, y) { + return (x == 0 || x == 7 || y == 0 || y == 7); + } + + isAttackedByKing([x, y], color) { + // Attacked if I'm on the edge and the opponent king just next, + // but not on the edge. + if (V.OnTheEdge(x, y)) { + for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { + const [i, j] = [x + step[0], y + step[1]]; + if ( + V.OnBoard(i, j) && + !V.OnTheEdge(i, j) && + this.board[i][j] != V.EMPTY && + this.getPiece(i, j) == V.KING + // NOTE: since only one king of each color, and (x, y) is occupied + // by our king, no need to check other king's color. + ) { + return true; + } + } + } + return false; + } + + // No consideration of color: all pieces could be played + getAllPotentialMoves() { + let potentialMoves = []; + for (let i = 0; i < V.size.x; i++) { + for (let j = 0; j < V.size.y; j++) { + if (this.board[i][j] != V.EMPTY) { + Array.prototype.push.apply( + potentialMoves, + this.getPotentialMovesFrom([i, j]) + ); + } + } + } + return potentialMoves; + } + + getEmptyMove() { + return new Move({ + start: { x: -1, y: -1 }, + end: { x: -1, y: -1 }, + appear: [], + vanish: [] + }); + } + + doClick(square) { + // A click to promote a piece on subTurn 2 would trigger this. + // For now it would then return [NaN, NaN] because surrounding squares + // have no IDs in the promotion modal. TODO: improve this? + if (isNaN(square[0])) return null; + // If subTurn == 2 && square is empty && !underCheck && !isOpposite, + // then return an empty move, allowing to "pass" subTurn2 + const La = this.amoves.length; + const Lf = this.firstMove.length; + if ( + this.subTurn == 2 && + this.board[square[0]][square[1]] == V.EMPTY && + !this.underCheck(this.turn) && + (La == 0 || !this.oppositeMoves(this.amoves[La-1], this.firstMove[Lf-1])) + ) { + return this.getEmptyMove(); + } + return null; + } + + play(move) { + if (this.subTurn == 1 && move.vanish.length == 0) { + // Patch to work with old format: (TODO: remove later) + move.ignore = true; + return; + } + const color = this.turn; + move.subTurn = this.subTurn; //for undo + const gotoNext = (mv) => { + const L = this.firstMove.length; + this.amoves.push(this.getAmove(this.firstMove[L-1], mv)); + this.turn = V.GetOppCol(color); + this.subTurn = 1; + this.movesCount++; + }; + move.flags = JSON.stringify(this.aggregateFlags()); + V.PlayOnBoard(this.board, move); + if (this.subTurn == 2) gotoNext(move); + else { + this.subTurn = 2; + this.firstMove.push(move); + this.toNewKingPos(move); + if ( + // Condition is true on empty arrays: + this.getAllPotentialMoves().every(m => { + V.PlayOnBoard(this.board, m); + this.toNewKingPos(m); + const res = this.underCheck(color); + V.UndoOnBoard(this.board, m); + this.toOldKingPos(m); + return res; + }) + ) { + // No valid move at subTurn 2 + gotoNext(this.getEmptyMove()); + } + this.toOldKingPos(move); + } + this.postPlay(move); + } + + toNewKingPos(move) { + for (let a of move.appear) + if (a.p == V.KING) this.kingPos[a.c] = [a.x, a.y]; + } + + postPlay(move) { + if (move.start.x < 0) return; + this.toNewKingPos(move); + this.updateCastleFlags(move); + } + + updateCastleFlags(move) { + const firstRank = { 'w': V.size.x - 1, 'b': 0 }; + for (let v of move.vanish) { + if (v.p == V.KING) this.castleFlags[v.c] = [V.size.y, V.size.y]; + else if (v.x == firstRank[v.c] && this.castleFlags[v.c].includes(v.y)) { + const flagIdx = (v.y == this.castleFlags[v.c][0] ? 0 : 1); + this.castleFlags[v.c][flagIdx] = V.size.y; + } + } + } + + undo(move) { + if (!!move.ignore) return; //TODO: remove that later + this.disaggregateFlags(JSON.parse(move.flags)); + V.UndoOnBoard(this.board, move); + if (this.subTurn == 1) { + this.amoves.pop(); + this.turn = V.GetOppCol(this.turn); + this.movesCount--; + } + if (move.subTurn == 1) this.firstMove.pop(); + this.subTurn = move.subTurn; + this.toOldKingPos(move); + } + + toOldKingPos(move) { + // (Potentially) Reset king position + for (let v of move.vanish) + if (v.p == V.KING) this.kingPos[v.c] = [v.x, v.y]; + } + + getComputerMove() { + let moves = this.getAllValidMoves(); + if (moves.length == 0) return null; + // "Search" at depth 1 for now + const maxeval = V.INFINITY; + const color = this.turn; + const emptyMove = { + start: { x: -1, y: -1 }, + end: { x: -1, y: -1 }, + appear: [], + vanish: [] + }; + moves.forEach(m => { + this.play(m); + if (this.turn != color) m.eval = this.evalPosition(); + else { + m.eval = (color == "w" ? -1 : 1) * maxeval; + const moves2 = this.getAllValidMoves().concat([emptyMove]); + m.next = moves2[0]; + moves2.forEach(m2 => { + this.play(m2); + const score = this.getCurrentScore(); + let mvEval = 0; + if (score != "1/2") { + if (score != "*") mvEval = (score == "1-0" ? 1 : -1) * maxeval; + else mvEval = this.evalPosition(); + } + if ( + (color == 'w' && mvEval > m.eval) || + (color == 'b' && mvEval < m.eval) + ) { + m.eval = mvEval; + m.next = m2; + } + this.undo(m2); + }); + } + this.undo(m); + }); + moves.sort((a, b) => { + return (color == "w" ? 1 : -1) * (b.eval - a.eval); + }); + let candidates = [0]; + for (let i = 1; i < moves.length && moves[i].eval == moves[0].eval; i++) + candidates.push(i); + const mIdx = candidates[randInt(candidates.length)]; + if (!moves[mIdx].next) return moves[mIdx]; + const move2 = moves[mIdx].next; + delete moves[mIdx]["next"]; + return [moves[mIdx], move2]; + } + + getNotation(move) { + if (move.start.x < 0) + // A second move is always required, but may be empty + return "-"; + const initialSquare = V.CoordsToSquare(move.start); + const finalSquare = V.CoordsToSquare(move.end); + if (move.appear.length == 0) + // Pushed or pulled out of the board + return initialSquare + "R"; + return move.appear[0].p.toUpperCase() + initialSquare + finalSquare; + } + +}; diff --git a/variants/Dynamo/complete_rules.html b/variants/Dynamo/complete_rules.html new file mode 100644 index 0000000..68e9cc0 --- /dev/null +++ b/variants/Dynamo/complete_rules.html @@ -0,0 +1,142 @@ + + + Dynamo Rules + + + + +
+

Dynamo Rules

+ +

+ Pieces have the same movement as in orthodox chess, but they cannot + take other pieces in the usual way. Instead of the normal captures, pieces + can pull or push other pieces, potentially off the board. + The goal is to send the enemy king off the board. +

+ +

Each turn, a player has the following options:

+
    +
  • + Move one of his pieces normally, then optionally pull something as an + effect of this move. +
  • +
  • + Push any piece with one of his pieces, then optionally follow the pushed + piece. +
  • + +

    + It seems easier to understand with some examples. For a detailed + introduction please visit + + this page + (in French). +

    + +
    +
    +
    +
    Possible "pawn moves" in the initial position.
    +
    + +

    + The e2 pawn can move to e3 and e4 as usual. It can also slide diagonally, + being pushed by the bishop or the queen (which may or may not move along + this line afterward). It can also go to c3, being pushed by the knight from + g1; then the knight can move to e2, or stay motionless. + Finally, the pawn can "take the king": this is a special move indicating that + you want it to exit the board. Indeed it could be pushed off the board by the + bishop or the queen. +

    + +

    + Note: if an action is possible but you don't want to play a second part in + a move, click on any empty square: this will send an empty move. +

    + +
    +
    +
    +
    +
    +
    + Pulling the d5 pawn to c3 (left: before, right: after). +
    +
    + +
      +
    • Pawns cannot pull (because they only move forward).
    • +
    • + When they could reach the square beyond the edge, + pieces can exit the board by themselves, possibly dragging another piece + out (friendly or enemy). +
    • +
    + +
    +
    +
    +
    + Check: the queen threatens to pull the king off the board + along the a4-e8 diagonal. +
    +
    + +

    + It is forbidden to undo a "move + action". For example here, white could + push back the black bishop on g7 but not return to d4 then. +

    + +
    +
    +
    +
    +
    + Pushing the d4 bishop to b2 (left: before, right: after). +
    +
    + +

    + Castling is possible as long as the king and rook have not moved and + haven't been pushed or pulled (this differs from the chessvariants + description). +

    + +

    End of the game

    + +

    + The game ends when a push or pull action threatens to send the king off the + board, and he has no way to escape it. +

    + +
    +
    +
    +
    Dynamo checkmate ("Dynamate" :) )
    +
    + +

    + The king cannot "take" on g4: this would just push the queen one step to the + left, and she would then push the king beyond the 'h' file. + There are no en-passant captures. +

    + +

    Source

    + +

    + + Dynamo chess + + on chessvariants.com. The short description given on + this page + might help too. +

    diff --git a/variants/Dynamo/rules.html b/variants/Dynamo/rules.html new file mode 100644 index 0000000..4a78e5a --- /dev/null +++ b/variants/Dynamo/rules.html @@ -0,0 +1,24 @@ +

    + Moves are potentially played in two times: + move a piece, and / or push or pull something with that unit. +

    + +

    Each turn, a player has the following options:

    +
      +
    • + Move one of his pieces normally, + then optionally pull something as an effect of this move. +
    • +
    • + Push any piece with one of his pieces, + then optionally follow the pushed piece. +
    • +
    + +

    + + Full rules description. + +

    + +

    Hans Kluever and Peter Kahl (1968).

    diff --git a/variants/Dynamo/style.css b/variants/Dynamo/style.css new file mode 100644 index 0000000..a3550bc --- /dev/null +++ b/variants/Dynamo/style.css @@ -0,0 +1 @@ +@import url("/base_pieces.css"); -- 2.48.1 From 939e06bf9febef0a7935c7f6a58c5e28dee4dedc Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Fri, 5 Jan 2024 09:58:16 +0100 Subject: [PATCH 12/16] Start working on Dynamo --- base_rules.js | 22 ++- variants/Dynamo/class.js | 289 +++++---------------------------------- 2 files changed, 52 insertions(+), 259 deletions(-) diff --git a/base_rules.js b/base_rules.js index 9a364e8..3d8e463 100644 --- a/base_rules.js +++ b/base_rules.js @@ -175,6 +175,22 @@ export default class ChessRules { return Object.values(cd).map(c => c.toString(36)).join(""); } + // c10 --> 02 (assuming 10 rows) + static SquareFromUsual(sq) { + return ( + (this.size.x - parseInt(sq.substring(1), 10)).toString(36) + + (sq.charCodeAt(0) - 97).toString(36) + ); + } + + // 02 --> c10 + static UsualFromSquare(sq) { + return ( + String.fromCharCode(parseInt(sq.charAt(1), 36) + 97) + + (this.size.x - parseInt(sq.charAt(0), 36)).toString(10) + ); + } + coordsToId(cd) { if (typeof cd.x == "number") { return ( @@ -651,10 +667,8 @@ export default class ChessRules { this[arrName] = ArrayFun.init(this.size.x, this.size.y, null); if (arrName == "d_pieces") this.marks.forEach((m) => { - const formattedSquare = - (this.size.x - parseInt(m.substring(1), 10)).toString(36) + - (m.charCodeAt(0) - 97).toString(36); - const mCoords = V.SquareToCoords(formattedSquare); + const formattedSquare = C.SquareFromUsual(m); + const mCoords = C.SquareToCoords(formattedSquare); addPiece(mCoords.x, mCoords.y, arrName, "mark"); }); }; diff --git a/variants/Dynamo/class.js b/variants/Dynamo/class.js index 0996a70..05f2aed 100644 --- a/variants/Dynamo/class.js +++ b/variants/Dynamo/class.js @@ -2,110 +2,51 @@ import ChessRules from "/base_rules.js"; export default class DynamoRules extends ChessRules { - // TODO? later, allow to push out pawns on a and h files - get hasEnpassant() { - return false; + static get Options() { + // TODO } -/// TODO::: + get hasEnpassant() { + return this.options["enpassant"]; + } - canIplay(side, [x, y]) { + canIplay(x, y) { // Sometimes opponent's pieces can be moved directly - return this.turn == side; + return this.playerColor == this.turn; + } + + canTake() { + // Captures don't occur (only pulls & pushes) + return false; } - setOtherVariables(fen) { - super.setOtherVariables(fen); + setOtherVariables(fenParsed) { + super.setOtherVariables(fenParsed); this.subTurn = 1; - // Local stack of "action moves" - this.amoves = []; - const amove = V.ParseFen(fen).amove; - if (amove != "-") { - const amoveParts = amove.split("/"); - let move = { - // No need for start & end - appear: [], - vanish: [] - }; - [0, 1].map(i => { - if (amoveParts[i] != "-") { - amoveParts[i].split(".").forEach(av => { - // Format is "bpe3" - const xy = V.SquareToCoords(av.substr(2)); - move[i == 0 ? "appear" : "vanish"].push( - new PiPo({ - x: xy.x, - y: xy.y, - c: av[0], - p: av[1] - }) - ); - }); - } + // Last action format: e2h5/d1g4 for queen on d1 pushing pawn to h5 + // for example, and moving herself to g4. If just move: e2h5 + this.lastAction = []; + if (fenParsed.amove != '-') { + this.lastAction = fenParsed.amove.split('/').map(a => { + return { + c1: C.SquareToCoords(C.SquareFromUsual(a.substr(0, 2))), + c2: C.SquareToCoords(C.SquareFromUsual(a.substr(2, 2))) + }; }); - this.amoves.push(move); } - // Stack "first moves" (on subTurn 1) to merge and check opposite moves - this.firstMove = []; - } - - static ParseFen(fen) { - return Object.assign( - ChessRules.ParseFen(fen), - { amove: fen.split(" ")[4] } - ); } - static IsGoodFen(fen) { - if (!ChessRules.IsGoodFen(fen)) return false; - const fenParts = fen.split(" "); - if (fenParts.length != 5) return false; - if (fenParts[4] != "-") { - // TODO: a single regexp instead. - // Format is [bpa2[.wpd3]] || '-'/[bbc3[.wrd5]] || '-' - const amoveParts = fenParts[4].split("/"); - if (amoveParts.length != 2) return false; - for (let part of amoveParts) { - if (part != "-") { - for (let psq of part.split(".")) - if (!psq.match(/^[a-z]{3}[1-8]$/)) return false; - } - } + getPartFen(o) { + let res = super.getPartFen(o); + if (o.init) + res["amove"] = '-'; + else { + res["amove"] = this.lastAction.map(a => { + C.UsualFromSquare(C.CoordsToSquare(a.c1)) + + C.UsualFromSquare(C.CoordsToSquare(a.c2)) + }).join('/'); } - return true; - } - - getFen() { - return super.getFen() + " " + this.getAmoveFen(); - } - - getFenForRepeat() { - return super.getFenForRepeat() + "_" + this.getAmoveFen(); - } - - getAmoveFen() { - const L = this.amoves.length; - if (L == 0) return "-"; - return ( - ["appear","vanish"].map( - mpart => { - if (this.amoves[L-1][mpart].length == 0) return "-"; - return ( - this.amoves[L-1][mpart].map( - av => { - const square = V.CoordsToSquare({ x: av.x, y: av.y }); - return av.c + av.p + square; - } - ).join(".") - ); - } - ).join("/") - ); - } - - canTake() { - // Captures don't occur (only pulls & pushes) - return false; + return res; } // Step is right, just add (push/pull) moves in this direction @@ -590,6 +531,7 @@ export default class DynamoRules extends ChessRules { ); } + // TODO: just stack in this.lastAction instead getAmove(move1, move2) { // Just merge (one is action one is move, one may be empty) return { @@ -643,105 +585,6 @@ export default class DynamoRules extends ChessRules { ); } - isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { - for (let step of steps) { - let rx = x + step[0], - ry = y + step[1]; - while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { - rx += step[0]; - ry += step[1]; - } - if ( - V.OnBoard(rx, ry) && - this.getPiece(rx, ry) == piece && - this.getColor(rx, ry) == color - ) { - // Continue some steps in the same direction (pull) - rx += step[0]; - ry += step[1]; - while ( - V.OnBoard(rx, ry) && - this.board[rx][ry] == V.EMPTY && - !oneStep - ) { - rx += step[0]; - ry += step[1]; - } - if (!V.OnBoard(rx, ry)) return true; - // Step in the other direction (push) - rx = x - step[0]; - ry = y - step[1]; - while ( - V.OnBoard(rx, ry) && - this.board[rx][ry] == V.EMPTY && - !oneStep - ) { - rx -= step[0]; - ry -= step[1]; - } - if (!V.OnBoard(rx, ry)) return true; - } - } - return false; - } - - isAttackedByPawn([x, y], color) { - // The king can be pushed out by a pawn on last rank or near the edge - const pawnShift = (color == "w" ? 1 : -1); - for (let i of [-1, 1]) { - if ( - V.OnBoard(x + pawnShift, y + i) && - this.board[x + pawnShift][y + i] != V.EMPTY && - this.getPiece(x + pawnShift, y + i) == V.PAWN && - this.getColor(x + pawnShift, y + i) == color - ) { - if (!V.OnBoard(x - pawnShift, y - i)) return true; - } - } - return false; - } - - static OnTheEdge(x, y) { - return (x == 0 || x == 7 || y == 0 || y == 7); - } - - isAttackedByKing([x, y], color) { - // Attacked if I'm on the edge and the opponent king just next, - // but not on the edge. - if (V.OnTheEdge(x, y)) { - for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { - const [i, j] = [x + step[0], y + step[1]]; - if ( - V.OnBoard(i, j) && - !V.OnTheEdge(i, j) && - this.board[i][j] != V.EMPTY && - this.getPiece(i, j) == V.KING - // NOTE: since only one king of each color, and (x, y) is occupied - // by our king, no need to check other king's color. - ) { - return true; - } - } - } - return false; - } - - // No consideration of color: all pieces could be played - getAllPotentialMoves() { - let potentialMoves = []; - for (let i = 0; i < V.size.x; i++) { - for (let j = 0; j < V.size.y; j++) { - if (this.board[i][j] != V.EMPTY) { - Array.prototype.push.apply( - potentialMoves, - this.getPotentialMovesFrom([i, j]) - ); - } - } - } - return potentialMoves; - } - getEmptyMove() { return new Move({ start: { x: -1, y: -1 }, @@ -854,68 +697,4 @@ export default class DynamoRules extends ChessRules { if (v.p == V.KING) this.kingPos[v.c] = [v.x, v.y]; } - getComputerMove() { - let moves = this.getAllValidMoves(); - if (moves.length == 0) return null; - // "Search" at depth 1 for now - const maxeval = V.INFINITY; - const color = this.turn; - const emptyMove = { - start: { x: -1, y: -1 }, - end: { x: -1, y: -1 }, - appear: [], - vanish: [] - }; - moves.forEach(m => { - this.play(m); - if (this.turn != color) m.eval = this.evalPosition(); - else { - m.eval = (color == "w" ? -1 : 1) * maxeval; - const moves2 = this.getAllValidMoves().concat([emptyMove]); - m.next = moves2[0]; - moves2.forEach(m2 => { - this.play(m2); - const score = this.getCurrentScore(); - let mvEval = 0; - if (score != "1/2") { - if (score != "*") mvEval = (score == "1-0" ? 1 : -1) * maxeval; - else mvEval = this.evalPosition(); - } - if ( - (color == 'w' && mvEval > m.eval) || - (color == 'b' && mvEval < m.eval) - ) { - m.eval = mvEval; - m.next = m2; - } - this.undo(m2); - }); - } - this.undo(m); - }); - moves.sort((a, b) => { - return (color == "w" ? 1 : -1) * (b.eval - a.eval); - }); - let candidates = [0]; - for (let i = 1; i < moves.length && moves[i].eval == moves[0].eval; i++) - candidates.push(i); - const mIdx = candidates[randInt(candidates.length)]; - if (!moves[mIdx].next) return moves[mIdx]; - const move2 = moves[mIdx].next; - delete moves[mIdx]["next"]; - return [moves[mIdx], move2]; - } - - getNotation(move) { - if (move.start.x < 0) - // A second move is always required, but may be empty - return "-"; - const initialSquare = V.CoordsToSquare(move.start); - const finalSquare = V.CoordsToSquare(move.end); - if (move.appear.length == 0) - // Pushed or pulled out of the board - return initialSquare + "R"; - return move.appear[0].p.toUpperCase() + initialSquare + finalSquare; - } - }; -- 2.48.1 From 253e65f6c4f342e5ac8230d7340ed413354f9c7f Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Fri, 19 Jan 2024 17:53:42 +0100 Subject: [PATCH 13/16] New variant idea --- TODO | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TODO b/TODO index 8afe466..f49a584 100644 --- a/TODO +++ b/TODO @@ -4,3 +4,7 @@ Otage, Emergo, Pacosako : fonction "buildPiece(arg1, arg2)" returns HTML element https://fr.wikipedia.org/wiki/Unlur Yoxii ? + +Idée new variant: "bed" random moves and "capture" (opponent?) pieces which cannot move on next turn (sleeping). (Crazybed ?) +one "bed" per player. +one turn = place bed (potentially with opponent piece), then normal move, then random bed move - dropping potential piece on initial square (awaken). -- 2.48.1 From d4be1a764e43f208ef34ee5c7298249b98e0baf1 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Tue, 28 Jan 2025 18:50:55 +0100 Subject: [PATCH 14/16] Work on Dynamo --- variants/Dynamo/class.js | 227 +++++++++++++++++++-------------------- 1 file changed, 113 insertions(+), 114 deletions(-) diff --git a/variants/Dynamo/class.js b/variants/Dynamo/class.js index 05f2aed..ed0ea27 100644 --- a/variants/Dynamo/class.js +++ b/variants/Dynamo/class.js @@ -3,7 +3,11 @@ import ChessRules from "/base_rules.js"; export default class DynamoRules extends ChessRules { static get Options() { - // TODO + return { + select: C.Options.select, + input: [], + styles: ["cylinder", "doublemove", "progressive"] + }; } get hasEnpassant() { @@ -51,7 +55,7 @@ export default class DynamoRules extends ChessRules { // Step is right, just add (push/pull) moves in this direction // Direction is assumed normalized. - getMovesInDirection([x, y], [dx, dy], nbSteps) { + getMovesInDirection([x, y], [dx, dy], nbSteps, kp) { nbSteps = nbSteps || 8; //max 8 steps anyway let [i, j] = [x + dx, y + dy]; let moves = []; @@ -59,25 +63,27 @@ export default class DynamoRules extends ChessRules { const piece = this.getPiece(x, y); const lastRank = (color == 'w' ? 0 : 7); let counter = 1; - while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { - if (i == lastRank && piece == V.PAWN) { + while (this.onBoard(i, j) && this.board[i][j] == "") { + if (i == lastRank && piece == 'p') { // Promotion by push or pull - V.PawnSpecs.promotions.forEach(p => { + this.pawnPromotions.forEach(p => { let move = super.getBasicMove([x, y], [i, j], { c: color, p: p }); moves.push(move); }); } - else moves.push(super.getBasicMove([x, y], [i, j])); - if (++counter > nbSteps) break; + else + moves.push(super.getBasicMove([x, y], [i, j])); + if (++counter > nbSteps) + break; i += dx; j += dy; } - if (!V.OnBoard(i, j) && piece != V.KING) { + if (!this.onBoard(i, j) && piece != 'k') { // Add special "exit" move, by "taking king" moves.push( new Move({ start: { x: x, y: y }, - end: { x: this.kingPos[color][0], y: this.kingPos[color][1] }, + end: { x: kp[0], y: kp[1] }, appear: [], vanish: [{ x: x, y: y, c: color, p: piece }] }) @@ -109,14 +115,14 @@ export default class DynamoRules extends ChessRules { const checkSlider = () => { const dir = this.getNormalizedDirection([x2 - x1, y2 - y1]); let [i, j] = [x1 + dir[0], y1 + dir[1]]; - while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + while (this.onBoard(i, j) && this.board[i][j] == "") { i += dir[0]; j += dir[1]; } - return !V.OnBoard(i, j); + return !this.onBoard(i, j); }; switch (piece2 || this.getPiece(x1, y1)) { - case V.PAWN: + case 'p': return ( x1 + pawnShift == x2 && ( @@ -124,30 +130,33 @@ export default class DynamoRules extends ChessRules { ( color1 != color2 && deltaY == 1 && - !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) + !this.onBoard(2 * x2 - x1, 2 * y2 - y1) ) ) ); - case V.ROOK: - if (x1 != x2 && y1 != y2) return false; + case 'r': + if (x1 != x2 && y1 != y2) + return false; return checkSlider(); - case V.KNIGHT: + case 'n': return ( deltaX + deltaY == 3 && (deltaX == 1 || deltaY == 1) && - !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) + !this.onBoard(2 * x2 - x1, 2 * y2 - y1) ); - case V.BISHOP: - if (deltaX != deltaY) return false; + case 'b': + if (deltaX != deltaY) + return false; return checkSlider(); - case V.QUEEN: - if (deltaX != 0 && deltaY != 0 && deltaX != deltaY) return false; + case 'q': + if (deltaX != 0 && deltaY != 0 && deltaX != deltaY) + return false; return checkSlider(); - case V.KING: + case 'k': return ( deltaX <= 1 && deltaY <= 1 && - !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) + !this.onBoard(2 * x2 - x1, 2 * y2 - y1) ); } return false; @@ -158,12 +167,12 @@ export default class DynamoRules extends ChessRules { const deltaX = Math.abs(x1 - x2); const startRank = (this.getColor(x1, y1) == 'w' ? 6 : 1); return ( - [V.QUEEN, V.ROOK].includes(piece) || + ['q', 'r'].includes(piece) || ( - [V.KING, V.PAWN].includes(piece) && + ['k', 'p'].includes(piece) && ( deltaX == 1 || - (deltaX == 2 && piece == V.PAWN && x1 == startRank) + (deltaX == 2 && piece == 'p' && x1 == startRank) ) ) ); @@ -178,12 +187,13 @@ export default class DynamoRules extends ChessRules { const pawnShift = (color == 'w' ? -1 : 1); const pawnStartRank = (color == 'w' ? 6 : 1); const getMoveHash = (m) => { - return V.CoordsToSquare(m.start) + V.CoordsToSquare(m.end); + return C.CoordsToSquare(m.start) + C.CoordsToSquare(m.end); }; if (this.subTurn == 1) { + const kp = this.searchKingPos(color); const addMoves = (dir, nbSteps) => { const newMoves = - this.getMovesInDirection([x, y], [-dir[0], -dir[1]], nbSteps) + this.getMovesInDirection([x, y], [-dir[0], -dir[1]], nbSteps, kp) .filter(m => !movesHash[getMoveHash(m)]); newMoves.forEach(m => { movesHash[getMoveHash(m)] = true; }); Array.prototype.push.apply(moves, newMoves); @@ -198,7 +208,8 @@ export default class DynamoRules extends ChessRules { moves = moves.filter(m => { const suicide = (m.appear.length == 0); if (suicide) { - if (hasExit) return false; + if (hasExit) + return false; hasExit = true; } return true; @@ -207,32 +218,34 @@ export default class DynamoRules extends ChessRules { let movesHash = {}; moves.forEach(m => { movesHash[getMoveHash(m)] = true; }); // [x, y] is pushed by 'color' - for (let step of V.steps[V.KNIGHT]) { + for (let step of this.pieces()['n'].both[0].steps) { const [i, j] = [x + step[0], y + step[1]]; if ( - V.OnBoard(i, j) && - this.board[i][j] != V.EMPTY && + this.onBoard(i, j) && + this.board[i][j] != "" && this.getColor(i, j) == color && - this.getPiece(i, j) == V.KNIGHT + this.getPiece(i, j) == 'n' ) { addMoves(step, 1); } } - for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { + for (let step of this.pieces()['r'].both[0].steps.concat( + this.pieces()['b'].both[0].steps)) + { let [i, j] = [x + step[0], y + step[1]]; - while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + while (this.onBoard(i, j) && this.board[i][j] == "") { i += step[0]; j += step[1]; } if ( - V.OnBoard(i, j) && - this.board[i][j] != V.EMPTY && + this.onBoard(i, j) && + this.board[i][j] != "" && this.getColor(i, j) == color ) { const deltaX = Math.abs(i - x); const deltaY = Math.abs(j - y); switch (this.getPiece(i, j)) { - case V.PAWN: + case 'p': if ( (x - i) / deltaX == pawnShift && deltaX <= 2 && @@ -248,18 +261,21 @@ export default class DynamoRules extends ChessRules { addMoves(step, 1); } break; - case V.ROOK: - if (deltaX == 0 || deltaY == 0) addMoves(step); + case 'r': + if (deltaX == 0 || deltaY == 0) + addMoves(step); break; - case V.BISHOP: - if (deltaX == deltaY) addMoves(step); + case 'b': + if (deltaX == deltaY) + addMoves(step); break; - case V.QUEEN: + case 'q': // All steps are valid for a queen: addMoves(step); break; - case V.KING: - if (deltaX <= 1 && deltaY <= 1) addMoves(step, 1); + case 'k': + if (deltaX <= 1 && deltaY <= 1) + addMoves(step, 1); break; } } @@ -357,13 +373,16 @@ export default class DynamoRules extends ChessRules { } break; case V.BISHOP: - if (deltaX != deltaY) return []; + if (deltaX != deltaY) + return []; break; case V.ROOK: - if (deltaX != 0 && deltaY != 0) return []; + if (deltaX != 0 && deltaY != 0) + return []; break; case V.QUEEN: - if (deltaX != deltaY && deltaX != 0 && deltaY != 0) return []; + if (deltaX != deltaY && deltaX != 0 && deltaY != 0) + return []; break; } // Nothing should stand between [x, y] and the square fm.start @@ -437,12 +456,14 @@ export default class DynamoRules extends ChessRules { }; if (fm.vanish[0].c != color) { // Only possible action is a push: - if (fm.appear.length == 0) return getPushExit(); + if (fm.appear.length == 0) + return getPushExit(); return getPushMoves(); } else if (sqCol != color) { // Only possible action is a pull, considering moving piece abilities - if (fm.appear.length == 0) return getPullExit(); + if (fm.appear.length == 0) + return getPullExit(); return getPullMoves(); } else { @@ -474,7 +495,8 @@ export default class DynamoRules extends ChessRules { let j = y + step[1]; while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { moves.push(this.getBasicMove([x, y], [i, j])); - if (oneStep) continue outerLoop; + if (oneStep) + continue outerLoop; i += step[0]; j += step[1]; } @@ -516,7 +538,8 @@ export default class DynamoRules extends ChessRules { elt.p == av.p ); }); - if (!avInAv2) return false; + if (!avInAv2) + return false; } return true; }; @@ -540,6 +563,7 @@ export default class DynamoRules extends ChessRules { } } + // TODO :: filterValid(moves) { const color = this.turn; const La = this.amoves.length; @@ -572,7 +596,8 @@ export default class DynamoRules extends ChessRules { return !res; }); } - if (La == 0) return super.filterValid(moves); + if (La == 0) + return super.filterValid(moves); const Lf = this.firstMove.length; return ( super.filterValid( @@ -598,7 +623,8 @@ export default class DynamoRules extends ChessRules { // A click to promote a piece on subTurn 2 would trigger this. // For now it would then return [NaN, NaN] because surrounding squares // have no IDs in the promotion modal. TODO: improve this? - if (isNaN(square[0])) return null; + if (isNaN(square[0])) + return null; // If subTurn == 2 && square is empty && !underCheck && !isOpposite, // then return an empty move, allowing to "pass" subTurn2 const La = this.amoves.length; @@ -614,87 +640,60 @@ export default class DynamoRules extends ChessRules { return null; } - play(move) { - if (this.subTurn == 1 && move.vanish.length == 0) { - // Patch to work with old format: (TODO: remove later) - move.ignore = true; - return; + updateCastleFlags(move) { + if (move.start.x < 0) + return; //empty move (pass subTurn 2) + const firstRank = { 'w': V.size.x - 1, 'b': 0 }; + for (let v of move.vanish) { + if (v.p == 'k') + this.castleFlags[v.c] = [this.size.y, this.size.y]; + else if (v.x == firstRank[v.c] && this.castleFlags[v.c].includes(v.y)) { + const flagIdx = (v.y == this.castleFlags[v.c][0] ? 0 : 1); + this.castleFlags[v.c][flagIdx] = this.size.y; + } } + } + + play(move) { +// if (this.subTurn == 1 && move.vanish.length == 0) { +// // Patch to work with old format: (TODO: remove later) +// move.ignore = true; +// return; +// } + + // In preplay ? + this.updateCastleFlags(move); + + const oppCol = C.GetOppTurn(color); + const color = this.turn; - move.subTurn = this.subTurn; //for undo const gotoNext = (mv) => { const L = this.firstMove.length; this.amoves.push(this.getAmove(this.firstMove[L-1], mv)); - this.turn = V.GetOppCol(color); + this.turn = oppCol; this.subTurn = 1; this.movesCount++; }; - move.flags = JSON.stringify(this.aggregateFlags()); - V.PlayOnBoard(this.board, move); - if (this.subTurn == 2) gotoNext(move); + this.playOnBoard(move); + if (this.subTurn == 2) + gotoNext(move); else { this.subTurn = 2; this.firstMove.push(move); - this.toNewKingPos(move); if ( - // Condition is true on empty arrays: + // Condition is true on empty arrays: //TODO: getAllPotentialMoves doesn't exist this.getAllPotentialMoves().every(m => { - V.PlayOnBoard(this.board, m); - this.toNewKingPos(m); - const res = this.underCheck(color); - V.UndoOnBoard(this.board, m); - this.toOldKingPos(m); + this.playOnBoard(m); + const res = this.underCheck([kp], oppCol); //TODO: find kp first + this.undoOnBoard(m); return res; }) ) { // No valid move at subTurn 2 gotoNext(this.getEmptyMove()); } - this.toOldKingPos(move); } this.postPlay(move); } - toNewKingPos(move) { - for (let a of move.appear) - if (a.p == V.KING) this.kingPos[a.c] = [a.x, a.y]; - } - - postPlay(move) { - if (move.start.x < 0) return; - this.toNewKingPos(move); - this.updateCastleFlags(move); - } - - updateCastleFlags(move) { - const firstRank = { 'w': V.size.x - 1, 'b': 0 }; - for (let v of move.vanish) { - if (v.p == V.KING) this.castleFlags[v.c] = [V.size.y, V.size.y]; - else if (v.x == firstRank[v.c] && this.castleFlags[v.c].includes(v.y)) { - const flagIdx = (v.y == this.castleFlags[v.c][0] ? 0 : 1); - this.castleFlags[v.c][flagIdx] = V.size.y; - } - } - } - - undo(move) { - if (!!move.ignore) return; //TODO: remove that later - this.disaggregateFlags(JSON.parse(move.flags)); - V.UndoOnBoard(this.board, move); - if (this.subTurn == 1) { - this.amoves.pop(); - this.turn = V.GetOppCol(this.turn); - this.movesCount--; - } - if (move.subTurn == 1) this.firstMove.pop(); - this.subTurn = move.subTurn; - this.toOldKingPos(move); - } - - toOldKingPos(move) { - // (Potentially) Reset king position - for (let v of move.vanish) - if (v.p == V.KING) this.kingPos[v.c] = [v.x, v.y]; - } - }; -- 2.48.1 From ab15cf71bdd9be67bfa0df1d7137ff2a6de7139f Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Thu, 30 Jan 2025 12:00:00 +0100 Subject: [PATCH 15/16] Some further transformation on Dynamo, prepare next variants too --- variants/Dynamo/class.js | 41 +- variants/Eightpieces/class.js | 1136 ++++++++++++++++++++++ variants/Eightpieces/complete_rules.html | 48 + variants/Eightpieces/rules.html | 3 + variants/Eightpieces/style.css | 1 + variants/Emergo/class.js | 576 +++++++++++ variants/Empire/class.js | 432 ++++++++ variants/Enpassant/class.js | 209 ++++ variants/Evolution/class.js | 34 + variants/Extinction/class.js | 118 +++ variants/Fanorona/class.js | 342 +++++++ variants/Sleepy/class.js | 110 +++ variants/Sleepy/rules.html | 7 + variants/Sleepy/style.css | 1 + 14 files changed, 3038 insertions(+), 20 deletions(-) create mode 100644 variants/Eightpieces/class.js create mode 100644 variants/Eightpieces/complete_rules.html create mode 100644 variants/Eightpieces/rules.html create mode 100644 variants/Eightpieces/style.css create mode 100644 variants/Emergo/class.js create mode 100644 variants/Empire/class.js create mode 100644 variants/Enpassant/class.js create mode 100644 variants/Evolution/class.js create mode 100644 variants/Extinction/class.js create mode 100644 variants/Fanorona/class.js create mode 100644 variants/Sleepy/class.js create mode 100644 variants/Sleepy/rules.html create mode 100644 variants/Sleepy/style.css diff --git a/variants/Dynamo/class.js b/variants/Dynamo/class.js index ed0ea27..7b835e0 100644 --- a/variants/Dynamo/class.js +++ b/variants/Dynamo/class.js @@ -307,7 +307,7 @@ export default class DynamoRules extends ChessRules { const dir = this.getNormalizedDirection( [fm.start.x - x, fm.start.y - y]); const nbSteps = - [V.PAWN, V.KING, V.KNIGHT].includes(piece) + ['p', 'k', 'n'].includes(piece) ? 1 : null; return this.getMovesInDirection([x, y], dir, nbSteps); @@ -327,7 +327,7 @@ export default class DynamoRules extends ChessRules { const deltaX = Math.abs(fm.start.x - x); const deltaY = Math.abs(fm.start.y - y); switch (piece) { - case V.PAWN: + case 'p': if (x == pawnStartRank) { if ( (fm.start.x - x) * pawnShift < 0 || @@ -354,7 +354,7 @@ export default class DynamoRules extends ChessRules { } } break; - case V.KNIGHT: + case 'n': if ( (deltaX + deltaY != 3 || (deltaX == 0 && deltaY == 0)) || (fm.end.x - fm.start.x != fm.start.x - x) || @@ -363,7 +363,7 @@ export default class DynamoRules extends ChessRules { return []; } break; - case V.KING: + case 'k': if ( (deltaX >= 2 || deltaY >= 2) || (fm.end.x - fm.start.x != fm.start.x - x) || @@ -372,15 +372,15 @@ export default class DynamoRules extends ChessRules { return []; } break; - case V.BISHOP: + case 'b': if (deltaX != deltaY) return []; break; - case V.ROOK: + case 'r': if (deltaX != 0 && deltaY != 0) return []; break; - case V.QUEEN: + case 'q': if (deltaX != deltaY && deltaX != 0 && deltaY != 0) return []; break; @@ -404,7 +404,7 @@ export default class DynamoRules extends ChessRules { // Note: kings cannot suicide, so fm.vanish[0].p is not KING. // Could be PAWN though, if a pawn was pushed out of board. if ( - fm.vanish[0].p != V.PAWN && //pawns cannot pull + fm.vanish[0].p != 'p' && //pawns cannot pull this.isAprioriValidExit( [x, y], [fm.start.x, fm.start.y], @@ -415,13 +415,13 @@ export default class DynamoRules extends ChessRules { // Seems so: const dir = this.getNormalizedDirection( [fm.start.x - x, fm.start.y - y]); - const nbSteps = (fm.vanish[0].p == V.KNIGHT ? 1 : null); + const nbSteps = (fm.vanish[0].p == 'n' ? 1 : null); return this.getMovesInDirection([x, y], dir, nbSteps); } return []; }; const getPullMoves = () => { - if (fm.vanish[0].p == V.PAWN) + if (fm.vanish[0].p == 'p') // pawns cannot pull return []; const dirM = this.getNormalizedDirection( @@ -434,8 +434,8 @@ export default class DynamoRules extends ChessRules { const deltaX = Math.abs(x - fm.start.x); const deltaY = Math.abs(y - fm.start.y); if ( - (fm.vanish[0].p == V.KING && (deltaX > 1 || deltaY > 1)) || - (fm.vanish[0].p == V.KNIGHT && + (fm.vanish[0].p == 'k' && (deltaX > 1 || deltaY > 1)) || + (fm.vanish[0].p == 'n' && (deltaX + deltaY != 3 || deltaX == 0 || deltaY == 0)) ) { return []; @@ -444,7 +444,7 @@ export default class DynamoRules extends ChessRules { let [i, j] = [x + dir[0], y + dir[1]]; while ( (i != fm.start.x || j != fm.start.y) && - this.board[i][j] == V.EMPTY + this.board[i][j] == "" ) { i += dir[0]; j += dir[1]; @@ -493,20 +493,20 @@ export default class DynamoRules extends ChessRules { outerLoop: for (let step of steps) { let i = x + step[0]; let j = y + step[1]; - while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + while (this.onBoard(i, j) && this.board[i][j] == "") { moves.push(this.getBasicMove([x, y], [i, j])); if (oneStep) continue outerLoop; i += step[0]; j += step[1]; } - if (V.OnBoard(i, j)) { + if (this.onBoard(i, j)) { if (this.canTake([x, y], [i, j])) moves.push(this.getBasicMove([x, y], [i, j])); } else { // Add potential board exit (suicide), except for the king - if (piece != V.KING) { + if (piece != 'k') { moves.push({ start: { x: x, y: y}, end: { x: this.kingPos[c][0], y: this.kingPos[c][1] }, @@ -563,7 +563,8 @@ export default class DynamoRules extends ChessRules { } } - // TODO :: +// TODO: re-write just for here getAllPotentialMoves() ? + filterValid(moves) { const color = this.turn; const La = this.amoves.length; @@ -572,7 +573,7 @@ export default class DynamoRules extends ChessRules { // A move is valid either if it doesn't result in a check, // or if a second move is possible to counter the check // (not undoing a potential move + action of the opponent) - this.play(m); + this.playOnBoard(m); let res = this.underCheck(color); if (this.subTurn == 2) { let isOpposite = La > 0 && this.oppositeMoves(this.amoves[La-1], m); @@ -580,7 +581,7 @@ export default class DynamoRules extends ChessRules { const moves2 = this.getAllPotentialMoves(); for (let m2 of moves2) { this.play(m2); - const res2 = this.underCheck(color); + const res2 = this.underCheck(color); //TODO: + square const amove = this.getAmove(m, m2); isOpposite = La > 0 && this.oppositeMoves(this.amoves[La-1], amove); @@ -592,7 +593,7 @@ export default class DynamoRules extends ChessRules { } } } - this.undo(m); + this.undoOnBoard(m); return !res; }); } diff --git a/variants/Eightpieces/class.js b/variants/Eightpieces/class.js new file mode 100644 index 0000000..3808a16 --- /dev/null +++ b/variants/Eightpieces/class.js @@ -0,0 +1,1136 @@ +import { randInt, sample } from "@/utils/alea"; +import { ChessRules, PiPo, Move } from "@/base_rules"; + +export class EightpiecesRules extends ChessRules { + + static get JAILER() { + return "j"; + } + static get SENTRY() { + return "s"; + } + static get LANCER() { + return "l"; + } + + static get IMAGE_EXTENSION() { + // Temporarily, for the time SVG pieces are being designed: + return ".png"; + } + + // Lancer directions *from white perspective* + static get LANCER_DIRS() { + return { + 'c': [-1, 0], //north + 'd': [-1, 1], //N-E + 'e': [0, 1], //east + 'f': [1, 1], //S-E + 'g': [1, 0], //south + 'h': [1, -1], //S-W + 'm': [0, -1], //west + 'o': [-1, -1] //N-W + }; + } + + static get PIECES() { + return ChessRules.PIECES + .concat([V.JAILER, V.SENTRY]) + .concat(Object.keys(V.LANCER_DIRS)); + } + + getPiece(i, j) { + const piece = this.board[i][j].charAt(1); + // Special lancer case: 8 possible orientations + if (Object.keys(V.LANCER_DIRS).includes(piece)) return V.LANCER; + return piece; + } + + getPpath(b, color, score, orientation) { + if ([V.JAILER, V.SENTRY].includes(b[1])) return "Eightpieces/tmp_png/" + b; + if (Object.keys(V.LANCER_DIRS).includes(b[1])) { + if (orientation == 'w') return "Eightpieces/tmp_png/" + b; + // Find opposite direction for adequate display: + let oppDir = ''; + switch (b[1]) { + case 'c': + oppDir = 'g'; + break; + case 'g': + oppDir = 'c'; + break; + case 'd': + oppDir = 'h'; + break; + case 'h': + oppDir = 'd'; + break; + case 'e': + oppDir = 'm'; + break; + case 'm': + oppDir = 'e'; + break; + case 'f': + oppDir = 'o'; + break; + case 'o': + oppDir = 'f'; + break; + } + return "Eightpieces/tmp_png/" + b[0] + oppDir; + } + // TODO: after we have SVG pieces, remove the folder and next prefix: + return "Eightpieces/tmp_png/" + b; + } + + getPPpath(m, orientation) { + return ( + this.getPpath( + m.appear[0].c + m.appear[0].p, + null, + null, + orientation + ) + ); + } + + static ParseFen(fen) { + const fenParts = fen.split(" "); + return Object.assign( + ChessRules.ParseFen(fen), + { sentrypush: fenParts[5] } + ); + } + + static IsGoodFen(fen) { + if (!ChessRules.IsGoodFen(fen)) return false; + const fenParsed = V.ParseFen(fen); + // 5) Check sentry push (if any) + if ( + fenParsed.sentrypush != "-" && + !fenParsed.sentrypush.match(/^([a-h][1-8]){2,2}$/) + ) { + return false; + } + return true; + } + + getFen() { + return super.getFen() + " " + this.getSentrypushFen(); + } + + getFenForRepeat() { + return super.getFenForRepeat() + "_" + this.getSentrypushFen(); + } + + getSentrypushFen() { + const L = this.sentryPush.length; + if (!this.sentryPush[L-1]) return "-"; + let res = ""; + const spL = this.sentryPush[L-1].length; + // Condensate path: just need initial and final squares: + return [0, spL - 1] + .map(i => V.CoordsToSquare(this.sentryPush[L-1][i])) + .join(""); + } + + setOtherVariables(fen) { + super.setOtherVariables(fen); + // subTurn == 2 only when a sentry moved, and is about to push something + this.subTurn = 1; + // Sentry position just after a "capture" (subTurn from 1 to 2) + this.sentryPos = null; + // Stack pieces' forbidden squares after a sentry move at each turn + const parsedFen = V.ParseFen(fen); + if (parsedFen.sentrypush == "-") this.sentryPush = [null]; + else { + // Expand init + dest squares into a full path: + const init = V.SquareToCoords(parsedFen.sentrypush.substr(0, 2)), + dest = V.SquareToCoords(parsedFen.sentrypush.substr(2)); + let newPath = [init]; + const delta = ['x', 'y'].map(i => Math.abs(dest[i] - init[i])); + // Check that it's not a knight movement: + if (delta[0] == 0 || delta[1] == 0 || delta[0] == delta[1]) { + const step = ['x', 'y'].map((i, idx) => { + return (dest[i] - init[i]) / delta[idx] || 0 + }); + let x = init.x + step[0], + y = init.y + step[1]; + while (x != dest.x || y != dest.y) { + newPath.push({ x: x, y: y }); + x += step[0]; + y += step[1]; + } + } + newPath.push(dest); + this.sentryPush = [newPath]; + } + } + + static GenRandInitFen(options) { + if (options.randomness == 0) + return "jfsqkbnr/pppppppp/8/8/8/8/PPPPPPPP/JDSQKBNR w 0 ahah - -"; + + const baseFen = ChessRules.GenRandInitFen(options); + const fenParts = baseFen.split(' '); + const posParts = fenParts[0].split('/'); + + // Replace one bishop by sentry, so that sentries on different colors + // Also replace one random rook by jailer, + // and one random knight by lancer (facing north/south) + let pieceLine = { b: posParts[0], w: posParts[7].toLowerCase() }; + let posBlack = { r: -1, n: -1, b: -1 }; + const mapP = { r: 'j', n: 'l', b: 's' }; + ['b', 'w'].forEach(c => { + ['r', 'n', 'b'].forEach(p => { + let pl = pieceLine[c]; + let pos = -1; + if (options.randomness == 2 || c == 'b') + pos = (randInt(2) == 0 ? pl.indexOf(p) : pl.lastIndexOf(p)); + else pos = posBlack[p]; + pieceLine[c] = + pieceLine[c].substr(0, pos) + mapP[p] + pieceLine[c].substr(pos+1); + if (options.randomness == 1 && c == 'b') posBlack[p] = pos; + }); + }); + // Rename 'l' into 'g' (black) or 'c' (white) + pieceLine['w'] = pieceLine['w'].replace('l', 'c'); + pieceLine['b'] = pieceLine['b'].replace('l', 'g'); + if (options.randomness == 2) { + const ws = pieceLine['w'].indexOf('s'); + const bs = pieceLine['b'].indexOf('s'); + if (ws % 2 != bs % 2) { + // Fix sentry: should be on different colors. + // => move sentry on other bishop for random color + const c = sample(['w', 'b'], 1); + pieceLine[c] = pieceLine[c] + .replace('b', 't') //tmp + .replace('s', 'b') + .replace('t', 's'); + } + } + + return ( + pieceLine['b'] + "/" + + posParts.slice(1, 7).join('/') + "/" + + pieceLine['w'].toUpperCase() + " " + + fenParts.slice(1, 5).join(' ') + " -" + ); + } + + canTake([x1, y1], [x2, y2]) { + if (this.subTurn == 2) + // Only self captures on this subturn: + return this.getColor(x1, y1) == this.getColor(x2, y2); + return super.canTake([x1, y1], [x2, y2]); + } + + // Is piece on square (x,y) immobilized? + isImmobilized([x, y]) { + const color = this.getColor(x, y); + const oppCol = V.GetOppCol(color); + for (let step of V.steps[V.ROOK]) { + const [i, j] = [x + step[0], y + step[1]]; + if ( + V.OnBoard(i, j) && + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == oppCol + ) { + if (this.getPiece(i, j) == V.JAILER) return [i, j]; + } + } + return null; + } + + canIplay(side, [x, y]) { + return ( + (this.subTurn == 1 && this.turn == side && this.getColor(x, y) == side) + || + (this.subTurn == 2 && x == this.sentryPos.x && y == this.sentryPos.y) + ); + } + + getPotentialMovesFrom([x, y]) { + const piece = this.getPiece(x, y); + const L = this.sentryPush.length; + // At subTurn == 2, jailers aren't effective (Jeff K) + if (this.subTurn == 1) { + const jsq = this.isImmobilized([x, y]); + if (!!jsq) { + let moves = []; + // Special pass move if king: + if (piece == V.KING) { + moves.push( + new Move({ + appear: [], + vanish: [], + start: { x: x, y: y }, + end: { x: jsq[0], y: jsq[1] } + }) + ); + } + else if (piece == V.LANCER && !!this.sentryPush[L-1]) { + // A pushed lancer next to the jailer: reorient + const color = this.getColor(x, y); + const curDir = this.board[x][y].charAt(1); + Object.keys(V.LANCER_DIRS).forEach(k => { + moves.push( + new Move({ + appear: [{ x: x, y: y, c: color, p: k }], + vanish: [{ x: x, y: y, c: color, p: curDir }], + start: { x: x, y: y }, + end: { x: jsq[0], y: jsq[1] } + }) + ); + }); + } + return moves; + } + } + let moves = []; + switch (piece) { + case V.JAILER: + moves = this.getPotentialJailerMoves([x, y]); + break; + case V.SENTRY: + moves = this.getPotentialSentryMoves([x, y]); + break; + case V.LANCER: + moves = this.getPotentialLancerMoves([x, y]); + break; + default: + moves = super.getPotentialMovesFrom([x, y]); + break; + } + if (!!this.sentryPush[L-1]) { + // Delete moves walking back on sentry push path, + // only if not a pawn, and the piece is the pushed one. + const pl = this.sentryPush[L-1].length; + const finalPushedSq = this.sentryPush[L-1][pl-1]; + moves = moves.filter(m => { + if ( + m.vanish[0].p != V.PAWN && + m.start.x == finalPushedSq.x && m.start.y == finalPushedSq.y && + this.sentryPush[L-1].some(sq => sq.x == m.end.x && sq.y == m.end.y) + ) { + return false; + } + return true; + }); + } + else if (this.subTurn == 2) { + // Put back the sentinel on board: + const color = this.turn; + moves.forEach(m => { + m.appear.push({x: x, y: y, p: V.SENTRY, c: color}); + }); + } + return moves; + } + + getPotentialPawnMoves([x, y]) { + const color = this.getColor(x, y); + let moves = []; + const [sizeX, sizeY] = [V.size.x, V.size.y]; + let shiftX = (color == "w" ? -1 : 1); + if (this.subTurn == 2) shiftX *= -1; + const firstRank = color == "w" ? sizeX - 1 : 0; + const startRank = color == "w" ? sizeX - 2 : 1; + const lastRank = color == "w" ? 0 : sizeX - 1; + + // Pawns might be pushed on 1st rank and attempt to move again: + if (!V.OnBoard(x + shiftX, y)) return []; + + // A push cannot put a pawn on last rank (it goes backward) + let finalPieces = [V.PAWN]; + if (x + shiftX == lastRank) { + // Only allow direction facing inside board: + const allowedLancerDirs = + lastRank == 0 + ? ['e', 'f', 'g', 'h', 'm'] + : ['c', 'd', 'e', 'm', 'o']; + finalPieces = + allowedLancerDirs + .concat([V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.SENTRY, V.JAILER]); + } + if (this.board[x + shiftX][y] == V.EMPTY) { + // One square forward + for (let piece of finalPieces) { + moves.push( + this.getBasicMove([x, y], [x + shiftX, y], { + c: color, + p: piece + }) + ); + } + if ( + // 2-squares jumps forbidden if pawn push + this.subTurn == 1 && + [startRank, firstRank].includes(x) && + this.board[x + 2 * shiftX][y] == V.EMPTY + ) { + // Two squares jump + moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); + } + } + // Captures + for (let shiftY of [-1, 1]) { + if ( + y + shiftY >= 0 && + y + shiftY < sizeY && + this.board[x + shiftX][y + shiftY] != V.EMPTY && + this.canTake([x, y], [x + shiftX, y + shiftY]) + ) { + for (let piece of finalPieces) { + moves.push( + this.getBasicMove([x, y], [x + shiftX, y + shiftY], { + c: color, + p: piece + }) + ); + } + } + } + + // En passant: only on subTurn == 1 + const Lep = this.epSquares.length; + const epSquare = this.epSquares[Lep - 1]; + if ( + this.subTurn == 1 && + !!epSquare && + epSquare.x == x + shiftX && + Math.abs(epSquare.y - y) == 1 + ) { + let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]); + enpassantMove.vanish.push({ + x: x, + y: epSquare.y, + p: "p", + c: this.getColor(x, epSquare.y) + }); + moves.push(enpassantMove); + } + + return moves; + } + + doClick(square) { + if (isNaN(square[0])) return null; + const L = this.sentryPush.length; + const [x, y] = [square[0], square[1]]; + const color = this.turn; + if ( + this.subTurn == 2 || + this.board[x][y] == V.EMPTY || + this.getPiece(x, y) != V.LANCER || + this.getColor(x, y) != color || + !!this.sentryPush[L-1] + ) { + return null; + } + // Stuck lancer? + const orientation = this.board[x][y][1]; + const step = V.LANCER_DIRS[orientation]; + if (!V.OnBoard(x + step[0], y + step[1])) { + let choices = []; + Object.keys(V.LANCER_DIRS).forEach(k => { + const dir = V.LANCER_DIRS[k]; + if ( + (dir[0] != step[0] || dir[1] != step[1]) && + V.OnBoard(x + dir[0], y + dir[1]) + ) { + choices.push( + new Move({ + vanish: [ + new PiPo({ + x: x, + y: y, + c: color, + p: orientation + }) + ], + appear: [ + new PiPo({ + x: x, + y: y, + c: color, + p: k + }) + ], + start: { x: x, y : y }, + end: { x: -1, y: -1 } + }) + ); + } + }); + return choices; + } + return null; + } + + // Obtain all lancer moves in "step" direction + getPotentialLancerMoves_aux([x, y], step, tr) { + let moves = []; + // Add all moves to vacant squares until opponent is met: + const color = this.getColor(x, y); + const oppCol = + this.subTurn == 1 + ? V.GetOppCol(color) + // at subTurn == 2, consider own pieces as opponent + : color; + let sq = [x + step[0], y + step[1]]; + while (V.OnBoard(sq[0], sq[1]) && this.getColor(sq[0], sq[1]) != oppCol) { + if (this.board[sq[0]][sq[1]] == V.EMPTY) + moves.push(this.getBasicMove([x, y], sq, tr)); + sq[0] += step[0]; + sq[1] += step[1]; + } + if (V.OnBoard(sq[0], sq[1])) + // Add capturing move + moves.push(this.getBasicMove([x, y], sq, tr)); + return moves; + } + + getPotentialLancerMoves([x, y]) { + let moves = []; + // Add all lancer possible orientations, similar to pawn promotions. + // Except if just after a push: allow all movements from init square then + const L = this.sentryPush.length; + const color = this.getColor(x, y); + const dirCode = this.board[x][y][1]; + const curDir = V.LANCER_DIRS[dirCode]; + if (!!this.sentryPush[L-1]) { + // Maybe I was pushed + const pl = this.sentryPush[L-1].length; + if ( + this.sentryPush[L-1][pl-1].x == x && + this.sentryPush[L-1][pl-1].y == y + ) { + // I was pushed: allow all directions (for this move only), but + // do not change direction after moving, *except* if I keep the + // same orientation in which I was pushed. + // Also allow simple reorientation ("capturing king"): + if (!V.OnBoard(x + curDir[0], y + curDir[1])) { + const kp = this.kingPos[color]; + let reorientMoves = []; + Object.keys(V.LANCER_DIRS).forEach(k => { + const dir = V.LANCER_DIRS[k]; + if ( + (dir[0] != curDir[0] || dir[1] != curDir[1]) && + V.OnBoard(x + dir[0], y + dir[1]) + ) { + reorientMoves.push( + new Move({ + vanish: [ + new PiPo({ + x: x, + y: y, + c: color, + p: dirCode + }) + ], + appear: [ + new PiPo({ + x: x, + y: y, + c: color, + p: k + }) + ], + start: { x: x, y : y }, + end: { x: kp[0], y: kp[1] } + }) + ); + } + }); + Array.prototype.push.apply(moves, reorientMoves); + } + Object.values(V.LANCER_DIRS).forEach(step => { + const dirCode = Object.keys(V.LANCER_DIRS).find(k => { + return ( + V.LANCER_DIRS[k][0] == step[0] && + V.LANCER_DIRS[k][1] == step[1] + ); + }); + const dirMoves = + this.getPotentialLancerMoves_aux( + [x, y], + step, + { p: dirCode, c: color } + ); + if (curDir[0] == step[0] && curDir[1] == step[1]) { + // Keeping same orientation: can choose after + let chooseMoves = []; + dirMoves.forEach(m => { + Object.keys(V.LANCER_DIRS).forEach(k => { + const newDir = V.LANCER_DIRS[k]; + // Prevent orientations toward outer board: + if (V.OnBoard(m.end.x + newDir[0], m.end.y + newDir[1])) { + let mk = JSON.parse(JSON.stringify(m)); + mk.appear[0].p = k; + chooseMoves.push(mk); + } + }); + }); + Array.prototype.push.apply(moves, chooseMoves); + } + else Array.prototype.push.apply(moves, dirMoves); + }); + return moves; + } + } + // I wasn't pushed: standard lancer move + const monodirMoves = + this.getPotentialLancerMoves_aux([x, y], V.LANCER_DIRS[dirCode]); + // Add all possible orientations aftermove except if I'm being pushed + if (this.subTurn == 1) { + monodirMoves.forEach(m => { + Object.keys(V.LANCER_DIRS).forEach(k => { + const newDir = V.LANCER_DIRS[k]; + // Prevent orientations toward outer board: + if (V.OnBoard(m.end.x + newDir[0], m.end.y + newDir[1])) { + let mk = JSON.parse(JSON.stringify(m)); + mk.appear[0].p = k; + moves.push(mk); + } + }); + }); + return moves; + } + else { + // I'm pushed: add potential nudges, except for current orientation + let potentialNudges = []; + for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { + if ( + (step[0] != curDir[0] || step[1] != curDir[1]) && + V.OnBoard(x + step[0], y + step[1]) && + this.board[x + step[0]][y + step[1]] == V.EMPTY + ) { + const newDirCode = Object.keys(V.LANCER_DIRS).find(k => { + const codeStep = V.LANCER_DIRS[k]; + return (codeStep[0] == step[0] && codeStep[1] == step[1]); + }); + potentialNudges.push( + this.getBasicMove( + [x, y], + [x + step[0], y + step[1]], + { c: color, p: newDirCode } + ) + ); + } + } + return monodirMoves.concat(potentialNudges); + } + } + + getPotentialSentryMoves([x, y]) { + // The sentry moves a priori like a bishop: + let moves = super.getPotentialBishopMoves([x, y]); + // ...but captures are replaced by special move, if and only if + // "captured" piece can move now, considered as the capturer unit. + // --> except is subTurn == 2, in this case I don't push anything. + if (this.subTurn == 2) return moves.filter(m => m.vanish.length == 1); + moves.forEach(m => { + if (m.vanish.length == 2) { + // Temporarily cancel the sentry capture: + m.appear.pop(); + m.vanish.pop(); + } + }); + const color = this.getColor(x, y); + const fMoves = moves.filter(m => { + // Can the pushed unit make any move? ...resulting in a non-self-check? + if (m.appear.length == 0) { + let res = false; + this.play(m); + let moves2 = this.getPotentialMovesFrom([m.end.x, m.end.y]); + for (let m2 of moves2) { + this.play(m2); + res = !this.underCheck(color); + this.undo(m2); + if (res) break; + } + this.undo(m); + return res; + } + return true; + }); + return fMoves; + } + + getPotentialJailerMoves([x, y]) { + return super.getPotentialRookMoves([x, y]).filter(m => { + // Remove jailer captures + return m.vanish[0].p != V.JAILER || m.vanish.length == 1; + }); + } + + getPotentialKingMoves(sq) { + const moves = this.getSlideNJumpMoves( + sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1); + return ( + this.subTurn == 1 + ? moves.concat(this.getCastleMoves(sq)) + : moves + ); + } + + atLeastOneMove() { + // If in second-half of a move, we already know that a move is possible + if (this.subTurn == 2) return true; + return super.atLeastOneMove(); + } + + filterValid(moves) { + if (moves.length == 0) return []; + const basicFilter = (m, c) => { + this.play(m); + const res = !this.underCheck(c); + this.undo(m); + return res; + }; + // Disable check tests for sentry pushes, + // because in this case the move isn't finished + let movesWithoutSentryPushes = []; + let movesWithSentryPushes = []; + moves.forEach(m => { + // Second condition below for special king "pass" moves + if (m.appear.length > 0 || m.vanish.length == 0) + movesWithoutSentryPushes.push(m); + else movesWithSentryPushes.push(m); + }); + const color = this.turn; + const oppCol = V.GetOppCol(color); + const filteredMoves = + movesWithoutSentryPushes.filter(m => basicFilter(m, color)); + // If at least one full move made, everything is allowed. + // Else: forbid checks and captures. + return ( + this.movesCount >= 2 + ? filteredMoves + : filteredMoves.filter(m => { + return (m.vanish.length <= 1 && basicFilter(m, oppCol)); + }) + ).concat(movesWithSentryPushes); + } + + getAllValidMoves() { + if (this.subTurn == 1) return super.getAllValidMoves(); + // Sentry push: + const sentrySq = [this.sentryPos.x, this.sentryPos.y]; + return this.filterValid(this.getPotentialMovesFrom(sentrySq)); + } + + isAttacked(sq, color) { + return ( + super.isAttacked(sq, color) || + this.isAttackedByLancer(sq, color) || + this.isAttackedBySentry(sq, color) + // The jailer doesn't capture. + ); + } + + isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { + for (let step of steps) { + let rx = x + step[0], + ry = y + step[1]; + while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { + rx += step[0]; + ry += step[1]; + } + if ( + V.OnBoard(rx, ry) && + this.getPiece(rx, ry) == piece && + this.getColor(rx, ry) == color && + !this.isImmobilized([rx, ry]) + ) { + return true; + } + } + return false; + } + + isAttackedByPawn([x, y], color) { + const pawnShift = (color == "w" ? 1 : -1); + if (x + pawnShift >= 0 && x + pawnShift < V.size.x) { + for (let i of [-1, 1]) { + if ( + y + i >= 0 && + y + i < V.size.y && + this.getPiece(x + pawnShift, y + i) == V.PAWN && + this.getColor(x + pawnShift, y + i) == color && + !this.isImmobilized([x + pawnShift, y + i]) + ) { + return true; + } + } + } + return false; + } + + isAttackedByLancer([x, y], color) { + for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { + // If in this direction there are only enemy pieces and empty squares, + // and we meet a lancer: can he reach us? + // NOTE: do not stop at first lancer, there might be several! + let coord = { x: x + step[0], y: y + step[1] }; + let lancerPos = []; + while ( + V.OnBoard(coord.x, coord.y) && + ( + this.board[coord.x][coord.y] == V.EMPTY || + this.getColor(coord.x, coord.y) == color + ) + ) { + if ( + this.getPiece(coord.x, coord.y) == V.LANCER && + !this.isImmobilized([coord.x, coord.y]) + ) { + lancerPos.push({x: coord.x, y: coord.y}); + } + coord.x += step[0]; + coord.y += step[1]; + } + const L = this.sentryPush.length; + const pl = (!!this.sentryPush[L-1] ? this.sentryPush[L-1].length : 0); + for (let xy of lancerPos) { + const dir = V.LANCER_DIRS[this.board[xy.x][xy.y].charAt(1)]; + if ( + (dir[0] == -step[0] && dir[1] == -step[1]) || + // If the lancer was just pushed, this is an attack too: + ( + !!this.sentryPush[L-1] && + this.sentryPush[L-1][pl-1].x == xy.x && + this.sentryPush[L-1][pl-1].y == xy.y + ) + ) { + return true; + } + } + } + return false; + } + + // Helper to check sentries attacks: + selfAttack([x1, y1], [x2, y2]) { + const color = this.getColor(x1, y1); + const oppCol = V.GetOppCol(color); + const sliderAttack = (allowedSteps, lancer) => { + const deltaX = x2 - x1, + deltaY = y2 - y1; + const absDeltaX = Math.abs(deltaX), + absDeltaY = Math.abs(deltaY); + const step = [ deltaX / absDeltaX || 0, deltaY / absDeltaY || 0 ]; + if ( + // Check that the step is a priori valid: + (absDeltaX != absDeltaY && deltaX != 0 && deltaY != 0) || + allowedSteps.every(st => st[0] != step[0] || st[1] != step[1]) + ) { + return false; + } + let sq = [ x1 + step[0], y1 + step[1] ]; + while (sq[0] != x2 || sq[1] != y2) { + // NOTE: no need to check OnBoard in this special case + if (this.board[sq[0]][sq[1]] != V.EMPTY) { + const p = this.getPiece(sq[0], sq[1]); + const pc = this.getColor(sq[0], sq[1]); + if ( + // Enemy sentry on the way will be gone: + (p != V.SENTRY || pc != oppCol) && + // Lancer temporarily "changed color": + (!lancer || pc == color) + ) { + return false; + } + } + sq[0] += step[0]; + sq[1] += step[1]; + } + return true; + }; + switch (this.getPiece(x1, y1)) { + case V.PAWN: { + // Pushed pawns move as enemy pawns + const shift = (color == 'w' ? 1 : -1); + return (x1 + shift == x2 && Math.abs(y1 - y2) == 1); + } + case V.KNIGHT: { + const deltaX = Math.abs(x1 - x2); + const deltaY = Math.abs(y1 - y2); + return ( + deltaX + deltaY == 3 && + [1, 2].includes(deltaX) && + [1, 2].includes(deltaY) + ); + } + case V.ROOK: + return sliderAttack(V.steps[V.ROOK]); + case V.BISHOP: + return sliderAttack(V.steps[V.BISHOP]); + case V.QUEEN: + return sliderAttack(V.steps[V.ROOK].concat(V.steps[V.BISHOP])); + case V.LANCER: { + // Special case: as long as no enemy units stands in-between, + // it attacks (if it points toward the king). + const allowedStep = V.LANCER_DIRS[this.board[x1][y1].charAt(1)]; + return sliderAttack([allowedStep], "lancer"); + } + // No sentries or jailer tests: they cannot self-capture + } + return false; + } + + isAttackedBySentry([x, y], color) { + // Attacked by sentry means it can self-take our king. + // Just check diagonals of enemy sentry(ies), and if it reaches + // one of our pieces: can I self-take? + const myColor = V.GetOppCol(color); + let candidates = []; + for (let i=0; i { + const score = this.getCurrentScore(); + const curEval = move.eval; + if (score != "*") { + move.eval = + score == "1/2" + ? 0 + : (score == "1-0" ? 1 : -1) * maxeval; + } else move.eval = this.evalPosition(); + if ( + // "next" is defined after sentry pushes + !!next && ( + !curEval || + color == 'w' && move.eval > curEval || + color == 'b' && move.eval < curEval + ) + ) { + move.second = next; + } + }; + + // Just search_depth == 1 (because of sentries. TODO: can do better...) + moves1.forEach(m1 => { + this.play(m1); + if (this.subTurn == 1) setEval(m1); + else { + // Need to play every pushes and count: + const moves2 = this.getAllValidMoves(); + moves2.forEach(m2 => { + this.play(m2); + setEval(m1, m2); + this.undo(m2); + }); + } + this.undo(m1); + }); + + moves1.sort((a, b) => { + return (color == "w" ? 1 : -1) * (b.eval - a.eval); + }); + let candidates = [0]; + for (let j = 1; j < moves1.length && moves1[j].eval == moves1[0].eval; j++) + candidates.push(j); + const choice = moves1[candidates[randInt(candidates.length)]]; + return (!choice.second ? choice : [choice, choice.second]); + } + + // For moves notation: + static get LANCER_DIRNAMES() { + return { + 'c': "N", + 'd': "NE", + 'e': "E", + 'f': "SE", + 'g': "S", + 'h': "SW", + 'm': "W", + 'o': "NW" + }; + } + + getNotation(move) { + // Special case "king takes jailer" is a pass move + if (move.appear.length == 0 && move.vanish.length == 0) return "pass"; + let notation = undefined; + if (this.subTurn == 2) { + // Do not consider appear[1] (sentry) for sentry pushes + const simpleMove = { + appear: [move.appear[0]], + vanish: move.vanish, + start: move.start, + end: move.end + }; + notation = super.getNotation(simpleMove); + } + else if ( + move.appear.length > 0 && + move.vanish[0].x == move.appear[0].x && + move.vanish[0].y == move.appear[0].y + ) { + // Lancer in-place reorientation: + notation = "L" + V.CoordsToSquare(move.start) + ":R"; + } + else notation = super.getNotation(move); + if (Object.keys(V.LANCER_DIRNAMES).includes(move.vanish[0].p)) + // Lancer: add direction info + notation += "=" + V.LANCER_DIRNAMES[move.appear[0].p]; + else if ( + move.vanish[0].p == V.PAWN && + Object.keys(V.LANCER_DIRNAMES).includes(move.appear[0].p) + ) { + // Fix promotions in lancer: + notation = notation.slice(0, -1) + + "L:" + V.LANCER_DIRNAMES[move.appear[0].p]; + } + return notation; + } + +}; diff --git a/variants/Eightpieces/complete_rules.html b/variants/Eightpieces/complete_rules.html new file mode 100644 index 0000000..c1d12ab --- /dev/null +++ b/variants/Eightpieces/complete_rules.html @@ -0,0 +1,48 @@ +p.boxed + | Three new pieces appear. All pieces are unique. + +p. + There are only one rook, one bishop and one knight per side in this variant. + That explains the name. The king and queen are still there, + and the three remaining slots are taken by new pieces: + +ul + li. + The lancer 'L' is oriented and can only move in the direction it points, + by any number of squares as long as an enemy isn't met + (it can jump over friendly pieces). If an opponent' piece is found, + it can be captured. After moving you can reorient the lancer. + li. + The sentry 'S' moves like a bishop but doesn't capture directly. + It "pushes" enemy pieces instead, either on an empty square or on other + enemy pieces which are thus (self-)captured. + li. + The jailer 'J' moves like a rook but also doesn't capture. + It immobilizes enemy pieces which are vertically or horizontally adjacent. + +p. + On the following diagram the white sentry can push the black lancer to + capture the black pawn on b4. The lancer is then immobilized + by the white jailer at a4. + +figure.diagram-container + .diagram.diag12 + | fen:7k/8/8/8/Jp3m2/8/3S4/K7: + .diagram.diag22 + | fen:7k/8/8/8/Jm3S2/8/8/K7: + figcaption Left: before white move S"push"f4. Right: after this move. + +p To reorient a stuck lancer, +ul + li Just after being pushed: play a move which 'capture your king". + li Later in the game: click on the lancer. + +h3 Complete rules + +p + | The rules were invented by Jeff Kubach (2020), who described them much + | more precisely on the + a(href="https://www.chessvariants.com/rules/8-piece-chess") + | chessvariants page + | . While the summary given above may suffice to start playing, + | you should read the complete rules to fully understand this variant. diff --git a/variants/Eightpieces/rules.html b/variants/Eightpieces/rules.html new file mode 100644 index 0000000..41e9d42 --- /dev/null +++ b/variants/Eightpieces/rules.html @@ -0,0 +1,3 @@ +Three new pieces appear: the lancer is a rook with a constrained direction, the sentry moves like a bishop and pushes pieces, and the jailer immobilizes pieces orthogonally adjacent. + +The goal is still to checkmate. diff --git a/variants/Eightpieces/style.css b/variants/Eightpieces/style.css new file mode 100644 index 0000000..a3550bc --- /dev/null +++ b/variants/Eightpieces/style.css @@ -0,0 +1 @@ +@import url("/base_pieces.css"); diff --git a/variants/Emergo/class.js b/variants/Emergo/class.js new file mode 100644 index 0000000..13069de --- /dev/null +++ b/variants/Emergo/class.js @@ -0,0 +1,576 @@ +import { ChessRules, Move, PiPo } from "@/base_rules"; +import { randInt } from "@/utils/alea"; +import { ArrayFun } from "@/utils/array"; + +export class EmergoRules extends ChessRules { + + // Simple encoding: A to L = 1 to 12, from left to right, if white controls. + // Lowercase if black controls. + // Single piece (no prisoners): A@ to L@ (+ lowercase) + + static get Options() { + return null; + } + + static get HasFlags() { + return false; + } + + static get HasEnpassant() { + return false; + } + + static get DarkBottomRight() { + return true; + } + + // board element == file name: + static board2fen(b) { + return b; + } + static fen2board(f) { + return f; + } + + static IsGoodPosition(position) { + if (position.length == 0) return false; + const rows = position.split("/"); + if (rows.length != V.size.x) return false; + for (let row of rows) { + let sumElts = 0; + for (let i = 0; i < row.length; i++) { + // Add only 0.5 per symbol because 2 per piece + if (row[i].toLowerCase().match(/^[a-lA-L@]$/)) sumElts += 0.5; + else { + const num = parseInt(row[i], 10); + if (isNaN(num) || num <= 0) return false; + sumElts += num; + } + } + if (sumElts != V.size.y) return false; + } + return true; + } + + static GetBoard(position) { + const rows = position.split("/"); + let board = ArrayFun.init(V.size.x, V.size.y, ""); + for (let i = 0; i < rows.length; i++) { + let j = 0; + for (let indexInRow = 0; indexInRow < rows[i].length; indexInRow++) { + const character = rows[i][indexInRow]; + const num = parseInt(character, 10); + // If num is a number, just shift j: + if (!isNaN(num)) j += num; + else + // Something at position i,j + board[i][j++] = V.fen2board(character + rows[i][++indexInRow]); + } + } + return board; + } + + getPpath(b) { + return "Emergo/" + b; + } + + getColor(x, y) { + if (x >= V.size.x) return x == V.size.x ? "w" : "b"; + if (this.board[x][y].charCodeAt(0) < 97) return 'w'; + return 'b'; + } + + getPiece() { + return V.PAWN; //unused + } + + static IsGoodFen(fen) { + if (!ChessRules.IsGoodFen(fen)) return false; + const fenParsed = V.ParseFen(fen); + // 3) Check reserves + if ( + !fenParsed.reserve || + !fenParsed.reserve.match(/^([0-9]{1,2},?){2,2}$/) + ) { + return false; + } + return true; + } + + static ParseFen(fen) { + const fenParts = fen.split(" "); + return Object.assign( + ChessRules.ParseFen(fen), + { reserve: fenParts[3] } + ); + } + + static get size() { + return { x: 9, y: 9 }; + } + + static GenRandInitFen() { + return "9/9/9/9/9/9/9/9/9 w 0 12,12"; + } + + getFen() { + return super.getFen() + " " + this.getReserveFen(); + } + + getFenForRepeat() { + return super.getFenForRepeat() + "_" + this.getReserveFen(); + } + + getReserveFen() { + return ( + (!this.reserve["w"] ? 0 : this.reserve["w"][V.PAWN]) + "," + + (!this.reserve["b"] ? 0 : this.reserve["b"][V.PAWN]) + ); + } + + getReservePpath(index, color) { + return "Emergo/" + (color == 'w' ? 'A' : 'a') + '@'; + } + + static get RESERVE_PIECES() { + return [V.PAWN]; //only array length matters + } + + setOtherVariables(fen) { + const reserve = + V.ParseFen(fen).reserve.split(",").map(x => parseInt(x, 10)); + this.reserve = { w: null, b: null }; + if (reserve[0] > 0) this.reserve['w'] = { [V.PAWN]: reserve[0] }; + if (reserve[1] > 0) this.reserve['b'] = { [V.PAWN]: reserve[1] }; + // Local stack of captures during a turn (squares + directions) + this.captures = [ [] ]; + } + + atLeastOneCaptureFrom([x, y], color, forbiddenStep) { + for (let s of V.steps[V.BISHOP]) { + if ( + !forbiddenStep || + (s[0] != -forbiddenStep[0] || s[1] != -forbiddenStep[1]) + ) { + const [i, j] = [x + s[0], y + s[1]]; + if ( + V.OnBoard(i + s[0], j + s[1]) && + this.board[i][j] != V.EMPTY && + this.getColor(i, j) != color && + this.board[i + s[0]][j + s[1]] == V.EMPTY + ) { + return true; + } + } + } + return false; + } + + atLeastOneCapture(color) { + const L0 = this.captures.length; + const captures = this.captures[L0 - 1]; + const L = captures.length; + if (L > 0) { + return ( + this.atLeastOneCaptureFrom( + captures[L-1].square, color, captures[L-1].step) + ); + } + for (let i = 0; i < V.size.x; i++) { + for (let j=0; j< V.size.y; j++) { + if ( + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == color && + this.atLeastOneCaptureFrom([i, j], color) + ) { + return true; + } + } + } + return false; + } + + maxLengthIndices(caps) { + let maxLength = 0; + let res = []; + for (let i = 0; i < caps.length; i++) { + if (caps[i].length > maxLength) { + res = [i]; + maxLength = caps[i].length; + } + else if (caps[i].length == maxLength) res.push(i); + } + return res; + }; + + getLongestCaptures_aux([x, y], color, locSteps) { + let res = []; + const L = locSteps.length; + const lastStep = (L > 0 ? locSteps[L-1] : null); + for (let s of V.steps[V.BISHOP]) { + if (!!lastStep && s[0] == -lastStep[0] && s[1] == -lastStep[1]) continue; + const [i, j] = [x + s[0], y + s[1]]; + if ( + V.OnBoard(i + s[0], j + s[1]) && + this.board[i + s[0]][j + s[1]] == V.EMPTY && + this.board[i][j] != V.EMPTY && + this.getColor(i, j) != color + ) { + const move = this.getBasicMove([x, y], [i + s[0], j + s[1]], [i, j]); + locSteps.push(s); + V.PlayOnBoard(this.board, move); + const nextRes = + this.getLongestCaptures_aux([i + s[0], j + s[1]], color, locSteps); + res.push(1 + nextRes); + locSteps.pop(); + V.UndoOnBoard(this.board, move); + } + } + if (res.length == 0) return 0; + return Math.max(...res); + } + + getLongestCapturesFrom([x, y], color, locSteps) { + let res = []; + const L = locSteps.length; + const lastStep = (L > 0 ? locSteps[L-1] : null); + for (let s of V.steps[V.BISHOP]) { + if (!!lastStep && s[0] == -lastStep[0] && s[1] == -lastStep[1]) continue; + const [i, j] = [x + s[0], y + s[1]]; + if ( + V.OnBoard(i + s[0], j + s[1]) && + this.board[i + s[0]][j + s[1]] == V.EMPTY && + this.board[i][j] != V.EMPTY && + this.getColor(i, j) != color + ) { + const move = this.getBasicMove([x, y], [i + s[0], j + s[1]], [i, j]); + locSteps.push(s); + V.PlayOnBoard(this.board, move); + const stepRes = + this.getLongestCaptures_aux([i + s[0], j + s[1]], color, locSteps); + res.push({ step: s, length: 1 + stepRes }); + locSteps.pop(); + V.UndoOnBoard(this.board, move); + } + } + return this.maxLengthIndices(res).map(i => res[i]);; + } + + getAllLongestCaptures(color) { + const L0 = this.captures.length; + const captures = this.captures[L0 - 1]; + const L = captures.length; + let caps = []; + if (L > 0) { + let locSteps = [ captures[L-1].step ]; + let res = + this.getLongestCapturesFrom(captures[L-1].square, color, locSteps); + Array.prototype.push.apply( + caps, + res.map(r => Object.assign({ square: captures[L-1].square }, r)) + ); + } + else { + for (let i = 0; i < V.size.x; i++) { + for (let j=0; j < V.size.y; j++) { + if ( + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == color + ) { + let locSteps = []; + let res = this.getLongestCapturesFrom([i, j], color, locSteps); + Array.prototype.push.apply( + caps, + res.map(r => Object.assign({ square: [i, j] }, r)) + ); + } + } + } + } + return this.maxLengthIndices(caps).map(i => caps[i]); + } + + getBasicMove([x1, y1], [x2, y2], capt) { + const cp1 = this.board[x1][y1]; + if (!capt) { + return new Move({ + appear: [ new PiPo({ x: x2, y: y2, c: cp1[0], p: cp1[1] }) ], + vanish: [ new PiPo({ x: x1, y: y1, c: cp1[0], p: cp1[1] }) ] + }); + } + // Compute resulting types based on jumped + jumping pieces + const color = this.getColor(x1, y1); + const firstCodes = (color == 'w' ? [65, 97] : [97, 65]); + const cpCapt = this.board[capt[0]][capt[1]]; + let count1 = [cp1.charCodeAt(0) - firstCodes[0], -1]; + if (cp1[1] != '@') count1[1] = cp1.charCodeAt(1) - firstCodes[0]; + let countC = [cpCapt.charCodeAt(0) - firstCodes[1], -1]; + if (cpCapt[1] != '@') countC[1] = cpCapt.charCodeAt(1) - firstCodes[1]; + count1[1]++; + countC[0]--; + let colorChange = false, + captVanish = false; + if (countC[0] < 0) { + if (countC[1] >= 0) { + colorChange = true; + countC = [countC[1], -1]; + } + else captVanish = true; + } + const incPrisoners = String.fromCharCode(firstCodes[0] + count1[1]); + let mv = new Move({ + appear: [ + new PiPo({ + x: x2, + y: y2, + c: cp1[0], + p: incPrisoners + }) + ], + vanish: [ + new PiPo({ x: x1, y: y1, c: cp1[0], p: cp1[1] }), + new PiPo({ x: capt[0], y: capt[1], c: cpCapt[0], p: cpCapt[1] }) + ] + }); + if (!captVanish) { + mv.appear.push( + new PiPo({ + x: capt[0], + y: capt[1], + c: String.fromCharCode( + firstCodes[(colorChange ? 0 : 1)] + countC[0]), + p: (colorChange ? '@' : cpCapt[1]), + }) + ); + } + return mv; + } + + getReserveMoves(x) { + const color = this.turn; + if (!this.reserve[color] || this.atLeastOneCapture(color)) return []; + let moves = []; + const shadowPiece = + this.reserve[V.GetOppCol(color)] == null + ? this.reserve[color][V.PAWN] - 1 + : 0; + const appearColor = String.fromCharCode( + (color == 'w' ? 'A' : 'a').charCodeAt(0) + shadowPiece); + const addMove = ([i, j]) => { + moves.push( + new Move({ + appear: [ new PiPo({ x: i, y: j, c: appearColor, p: '@' }) ], + vanish: [], + start: { x: V.size.x + (color == 'w' ? 0 : 1), y: 0 } + }) + ); + }; + const oppCol = V.GetOppCol(color); + const opponentCanCapture = this.atLeastOneCapture(oppCol); + for (let i = 0; i < V.size.x; i++) { + for (let j = i % 2; j < V.size.y; j += 2) { + if ( + this.board[i][j] == V.EMPTY && + // prevent playing on central square at move 1: + (this.movesCount >= 1 || i != 4 || j != 4) + ) { + if (opponentCanCapture) addMove([i, j]); + else { + let canAddMove = true; + for (let s of V.steps[V.BISHOP]) { + if ( + V.OnBoard(i + s[0], j + s[1]) && + V.OnBoard(i - s[0], j - s[1]) && + this.board[i + s[0]][j + s[1]] != V.EMPTY && + this.board[i - s[0]][j - s[1]] == V.EMPTY && + this.getColor(i + s[0], j + s[1]) == oppCol + ) { + canAddMove = false; + break; + } + } + if (canAddMove) addMove([i, j]); + } + } + } + } + return moves; + } + + getPotentialMovesFrom([x, y], longestCaptures) { + if (x >= V.size.x) { + if (longestCaptures.length == 0) return this.getReserveMoves(x); + return []; + } + const color = this.turn; + if (!!this.reserve[color] && !this.atLeastOneCapture(color)) return []; + const L0 = this.captures.length; + const captures = this.captures[L0 - 1]; + const L = captures.length; + let moves = []; + if (longestCaptures.length > 0) { + if ( + L > 0 && + (x != captures[L-1].square[0] || y != captures[L-1].square[1]) + ) { + return []; + } + longestCaptures.forEach(lc => { + if (lc.square[0] == x && lc.square[1] == y) { + const s = lc.step; + const [i, j] = [x + s[0], y + s[1]]; + moves.push(this.getBasicMove([x, y], [i + s[0], j + s[1]], [i, j])); + } + }); + return moves; + } + // Just search simple moves: + for (let s of V.steps[V.BISHOP]) { + const [i, j] = [x + s[0], y + s[1]]; + if (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) + moves.push(this.getBasicMove([x, y], [i, j])); + } + return moves; + } + + getAllValidMoves() { + const color = this.turn; + const longestCaptures = this.getAllLongestCaptures(color); + let potentialMoves = []; + for (let i = 0; i < V.size.x; i++) { + for (let j = 0; j < V.size.y; j++) { + if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { + Array.prototype.push.apply( + potentialMoves, + this.getPotentialMovesFrom([i, j], longestCaptures) + ); + } + } + } + // Add reserve moves + potentialMoves = potentialMoves.concat( + this.getReserveMoves(V.size.x + (color == "w" ? 0 : 1)) + ); + return potentialMoves; + } + + getPossibleMovesFrom([x, y]) { + const longestCaptures = this.getAllLongestCaptures(this.getColor(x, y)); + return this.getPotentialMovesFrom([x, y], longestCaptures); + } + + filterValid(moves) { + return moves; + } + + getCheckSquares() { + return []; + } + + play(move) { + const color = this.turn; + move.turn = color; //for undo + V.PlayOnBoard(this.board, move); + if (move.vanish.length == 2) { + const L0 = this.captures.length; + let captures = this.captures[L0 - 1]; + captures.push({ + square: [move.end.x, move.end.y], + step: [(move.end.x - move.start.x)/2, (move.end.y - move.start.y)/2] + }); + if (this.atLeastOneCapture(color)) + // There could be other captures (mandatory) + move.notTheEnd = true; + } + else if (move.vanish == 0) { + const firstCode = (color == 'w' ? 65 : 97); + // Generally, reserveCount == 1 (except for shadow piece) + const reserveCount = move.appear[0].c.charCodeAt() - firstCode + 1; + this.reserve[color][V.PAWN] -= reserveCount; + if (this.reserve[color][V.PAWN] == 0) this.reserve[color] = null; + } + if (!move.notTheEnd) { + this.turn = V.GetOppCol(color); + this.movesCount++; + this.captures.push([]); + } + } + + undo(move) { + V.UndoOnBoard(this.board, move); + if (!move.notTheEnd) { + this.turn = move.turn; + this.movesCount--; + this.captures.pop(); + } + if (move.vanish.length == 0) { + const color = (move.appear[0].c == 'A' ? 'w' : 'b'); + const firstCode = (color == 'w' ? 65 : 97); + const reserveCount = move.appear[0].c.charCodeAt() - firstCode + 1; + if (!this.reserve[color]) this.reserve[color] = { [V.PAWN]: 0 }; + this.reserve[color][V.PAWN] += reserveCount; + } + else if (move.vanish.length == 2) { + const L0 = this.captures.length; + let captures = this.captures[L0 - 1]; + captures.pop(); + } + } + + atLeastOneMove() { + const color = this.turn; + if (this.atLeastOneCapture(color)) return true; + for (let i = 0; i < V.size.x; i++) { + for (let j = 0; j < V.size.y; j++) { + if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { + const moves = this.getPotentialMovesFrom([i, j], []); + if (moves.length > 0) return true; + } + } + } + const reserveMoves = + this.getReserveMoves(V.size.x + (this.turn == "w" ? 0 : 1)); + return (reserveMoves.length > 0); + } + + getCurrentScore() { + const color = this.turn; + // If no pieces on board + reserve, I lose + if (!!this.reserve[color]) return "*"; + let atLeastOnePiece = false; + outerLoop: for (let i=0; i < V.size.x; i++) { + for (let j=0; j < V.size.y; j++) { + if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { + atLeastOnePiece = true; + break outerLoop; + } + } + } + if (!atLeastOnePiece) return (color == 'w' ? "0-1" : "1-0"); + if (!this.atLeastOneMove()) return "1/2"; + return "*"; + } + + getComputerMove() { + // Random mover for now (TODO) + const color = this.turn; + let mvArray = []; + let mv = null; + while (this.turn == color) { + const moves = this.getAllValidMoves(); + mv = moves[randInt(moves.length)]; + mvArray.push(mv); + this.play(mv); + } + for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]); + return (mvArray.length > 1 ? mvArray : mvArray[0]); + } + + getNotation(move) { + if (move.vanish.length == 0) return "@" + V.CoordsToSquare(move.end); + const L0 = this.captures.length; + if (this.captures[L0 - 1].length > 0) return V.CoordsToSquare(move.end); + return V.CoordsToSquare(move.start) + V.CoordsToSquare(move.end); + } + +}; diff --git a/variants/Empire/class.js b/variants/Empire/class.js new file mode 100644 index 0000000..d04e4a8 --- /dev/null +++ b/variants/Empire/class.js @@ -0,0 +1,432 @@ +import { ChessRules } from "@/base_rules"; + +export class EmpireRules extends ChessRules { + + static get PawnSpecs() { + return Object.assign( + {}, + ChessRules.PawnSpecs, + { promotions: [V.QUEEN] } + ); + } + + static get LoseOnRepetition() { + return true; + } + + static IsGoodFlags(flags) { + // Only black can castle + return !!flags.match(/^[a-z]{2,2}$/); + } + + getPpath(b) { + return (b[0] == 'w' ? "Empire/" : "") + b; + } + + static GenRandInitFen(options) { + if (options.randomness == 0) + return "rnbqkbnr/pppppppp/8/8/8/PPPSSPPP/8/TECDKCET w 0 ah -"; + + // Mapping kingdom --> empire: + const piecesMap = { + 'R': 'T', + 'N': 'E', + 'B': 'C', + 'Q': 'D', + 'K': 'K' + }; + + const baseFen = ChessRules.GenRandInitFen(options); + return ( + baseFen.substr(0, 24) + "PPPSSPPP/8/" + + baseFen.substr(35, 8).split('').map(p => piecesMap[p]).join('') + + baseFen.substr(43, 5) + baseFen.substr(50) + ); + } + + getFlagsFen() { + return this.castleFlags['b'].map(V.CoordToColumn).join(""); + } + + setFlags(fenflags) { + this.castleFlags = { 'b': [-1, -1] }; + for (let i = 0; i < 2; i++) + this.castleFlags['b'][i] = V.ColumnToCoord(fenflags.charAt(i)); + } + + static get TOWER() { + return 't'; + } + static get EAGLE() { + return 'e'; + } + static get CARDINAL() { + return 'c'; + } + static get DUKE() { + return 'd'; + } + static get SOLDIER() { + return 's'; + } + // Kaiser is technically a King, so let's keep things simple. + + static get PIECES() { + return ChessRules.PIECES.concat( + [V.TOWER, V.EAGLE, V.CARDINAL, V.DUKE, V.SOLDIER]); + } + + getPotentialMovesFrom(sq) { + let moves = []; + const piece = this.getPiece(sq[0], sq[1]); + switch (piece) { + case V.TOWER: + moves = this.getPotentialTowerMoves(sq); + break; + case V.EAGLE: + moves = this.getPotentialEagleMoves(sq); + break; + case V.CARDINAL: + moves = this.getPotentialCardinalMoves(sq); + break; + case V.DUKE: + moves = this.getPotentialDukeMoves(sq); + break; + case V.SOLDIER: + moves = this.getPotentialSoldierMoves(sq); + break; + default: + moves = super.getPotentialMovesFrom(sq); + } + if ( + piece != V.KING && + this.kingPos['w'][0] != this.kingPos['b'][0] && + this.kingPos['w'][1] != this.kingPos['b'][1] + ) { + return moves; + } + // TODO: factor two next "if" into one (rank/column...) + if (this.kingPos['w'][1] == this.kingPos['b'][1]) { + const colKing = this.kingPos['w'][1]; + let intercept = 0; //count intercepting pieces + let [kingPos1, kingPos2] = [this.kingPos['w'][0], this.kingPos['b'][0]]; + if (kingPos1 > kingPos2) [kingPos1, kingPos2] = [kingPos2, kingPos1]; + for (let i = kingPos1 + 1; i < kingPos2; i++) { + if (this.board[i][colKing] != V.EMPTY) intercept++; + } + if (intercept >= 2) return moves; + // intercept == 1 (0 is impossible): + // Any move not removing intercept is OK + return moves.filter(m => { + return ( + // From another column? + m.start.y != colKing || + // From behind a king? (including kings themselves!) + m.start.x <= kingPos1 || + m.start.x >= kingPos2 || + // Intercept piece moving: must remain in-between + ( + m.end.y == colKing && + m.end.x > kingPos1 && + m.end.x < kingPos2 + ) + ); + }); + } + if (this.kingPos['w'][0] == this.kingPos['b'][0]) { + const rowKing = this.kingPos['w'][0]; + let intercept = 0; //count intercepting pieces + let [kingPos1, kingPos2] = [this.kingPos['w'][1], this.kingPos['b'][1]]; + if (kingPos1 > kingPos2) [kingPos1, kingPos2] = [kingPos2, kingPos1]; + for (let i = kingPos1 + 1; i < kingPos2; i++) { + if (this.board[rowKing][i] != V.EMPTY) intercept++; + } + if (intercept >= 2) return moves; + // intercept == 1 (0 is impossible): + // Any move not removing intercept is OK + return moves.filter(m => { + return ( + // From another row? + m.start.x != rowKing || + // From "behind" a king? (including kings themselves!) + m.start.y <= kingPos1 || + m.start.y >= kingPos2 || + // Intercept piece moving: must remain in-between + ( + m.end.x == rowKing && + m.end.y > kingPos1 && + m.end.y < kingPos2 + ) + ); + }); + } + // piece == king: check only if move.end.y == enemy king column, + // or if move.end.x == enemy king rank. + const color = this.getColor(sq[0], sq[1]); + const oppCol = V.GetOppCol(color); + return moves.filter(m => { + if ( + m.end.y != this.kingPos[oppCol][1] && + m.end.x != this.kingPos[oppCol][0] + ) { + return true; + } + // check == -1 if (row, or col) unchecked, 1 if checked and occupied, + // 0 if checked and clear + let check = [-1, -1]; + // TODO: factor two next "if"... + if (m.end.x == this.kingPos[oppCol][0]) { + if (check[0] < 0) { + // Do the check: + check[0] = 0; + let [kingPos1, kingPos2] = [m.end.y, this.kingPos[oppCol][1]]; + if (kingPos1 > kingPos2) [kingPos1, kingPos2] = [kingPos2, kingPos1]; + for (let i = kingPos1 + 1; i < kingPos2; i++) { + if (this.board[m.end.x][i] != V.EMPTY) { + check[0]++; + break; + } + } + return check[0] == 1; + } + // Check already done: + return check[0] == 1; + } + //if (m.end.y == this.kingPos[oppCol][1]) //true... + if (check[1] < 0) { + // Do the check: + check[1] = 0; + let [kingPos1, kingPos2] = [m.end.x, this.kingPos[oppCol][0]]; + if (kingPos1 > kingPos2) [kingPos1, kingPos2] = [kingPos2, kingPos1]; + for (let i = kingPos1 + 1; i < kingPos2; i++) { + if (this.board[i][m.end.y] != V.EMPTY) { + check[1]++; + break; + } + } + return check[1] == 1; + } + // Check already done: + return check[1] == 1; + }); + } + + // TODO: some merging to do with Orda method (and into base_rules.js) + getSlideNJumpMoves_([x, y], steps, oneStep) { + let moves = []; + outerLoop: for (let step of steps) { + const s = step.s; + let i = x + s[0]; + let j = y + s[1]; + while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + if (!step.onlyTake) moves.push(this.getBasicMove([x, y], [i, j])); + // NOTE: (bad) HACK here, since onlyTake is true only for Eagle + // capturing moves, which are oneStep... + if (oneStep || step.onlyTake) continue outerLoop; + i += s[0]; + j += s[1]; + } + if (V.OnBoard(i, j) && this.canTake([x, y], [i, j]) && !step.onlyMove) + moves.push(this.getBasicMove([x, y], [i, j])); + } + return moves; + } + + static get steps() { + return ( + Object.assign( + { + t: [ + { s: [-1, 0] }, + { s: [1, 0] }, + { s: [0, -1] }, + { s: [0, 1] }, + { s: [-1, -1], onlyMove: true }, + { s: [-1, 1], onlyMove: true }, + { s: [1, -1], onlyMove: true }, + { s: [1, 1], onlyMove: true } + ], + c: [ + { s: [-1, 0], onlyMove: true }, + { s: [1, 0], onlyMove: true }, + { s: [0, -1], onlyMove: true }, + { s: [0, 1], onlyMove: true }, + { s: [-1, -1] }, + { s: [-1, 1] }, + { s: [1, -1] }, + { s: [1, 1] } + ], + e: [ + { s: [-1, 0], onlyMove: true }, + { s: [1, 0], onlyMove: true }, + { s: [0, -1], onlyMove: true }, + { s: [0, 1], onlyMove: true }, + { s: [-1, -1], onlyMove: true }, + { s: [-1, 1], onlyMove: true }, + { s: [1, -1], onlyMove: true }, + { s: [1, 1], onlyMove: true }, + { s: [-2, -1], onlyTake: true }, + { s: [-2, 1], onlyTake: true }, + { s: [-1, -2], onlyTake: true }, + { s: [-1, 2], onlyTake: true }, + { s: [1, -2], onlyTake: true }, + { s: [1, 2], onlyTake: true }, + { s: [2, -1], onlyTake: true }, + { s: [2, 1], onlyTake: true } + ] + }, + ChessRules.steps + ) + ); + } + + getPotentialTowerMoves(sq) { + return this.getSlideNJumpMoves_(sq, V.steps[V.TOWER]); + } + + getPotentialCardinalMoves(sq) { + return this.getSlideNJumpMoves_(sq, V.steps[V.CARDINAL]); + } + + getPotentialEagleMoves(sq) { + return this.getSlideNJumpMoves_(sq, V.steps[V.EAGLE]); + } + + getPotentialDukeMoves([x, y]) { + // Anything to capture around? mark other steps to explore after + let steps = []; + const oppCol = V.GetOppCol(this.getColor(x, y)); + let moves = []; + for (let s of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { + const [i, j] = [x + s[0], y + s[1]]; + if ( + V.OnBoard(i, j) && + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == oppCol + ) { + moves.push(super.getBasicMove([x, y], [i, j])); + } + else steps.push({ s: s, onlyMove: true }); + } + if (steps.length > 0) { + const noncapturingMoves = this.getSlideNJumpMoves_([x, y], steps); + Array.prototype.push.apply(moves, noncapturingMoves); + } + return moves; + } + + getPotentialKingMoves([x, y]) { + if (this.getColor(x, y) == 'b') return super.getPotentialKingMoves([x, y]); + // Empire doesn't castle: + return super.getSlideNJumpMoves( + [x, y], V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1); + } + + getPotentialSoldierMoves([x, y]) { + const c = this.getColor(x, y); + const shiftX = (c == 'w' ? -1 : 1); + const lastRank = (c == 'w' && x == 0 || c == 'b' && x == 9); + let steps = []; + if (!lastRank) steps.push([shiftX, 0]); + if (y > 0) steps.push([0, -1]); + if (y < 9) steps.push([0, 1]); + return super.getSlideNJumpMoves([x, y], steps, 1); + } + + isAttacked(sq, color) { + if (color == 'b') return super.isAttacked(sq, color); + // Empire: only pawn and king (+ queen if promotion) in common: + return ( + super.isAttackedByPawn(sq, color) || + this.isAttackedByTower(sq, color) || + this.isAttackedByEagle(sq, color) || + this.isAttackedByCardinal(sq, color) || + this.isAttackedByDuke(sq, color) || + this.isAttackedBySoldier(sq, color) || + super.isAttackedByKing(sq, color) || + super.isAttackedByQueen(sq, color) + ); + } + + isAttackedByTower(sq, color) { + return super.isAttackedBySlideNJump(sq, color, V.TOWER, V.steps[V.ROOK]); + } + + isAttackedByEagle(sq, color) { + return super.isAttackedBySlideNJump( + sq, color, V.EAGLE, V.steps[V.KNIGHT], 1); + } + + isAttackedByCardinal(sq, color) { + return super.isAttackedBySlideNJump( + sq, color, V.CARDINAL, V.steps[V.BISHOP]); + } + + isAttackedByDuke(sq, color) { + return ( + super.isAttackedBySlideNJump( + sq, color, V.DUKE, + V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1 + ) + ); + } + + isAttackedBySoldier([x, y], color) { + const shiftX = (color == 'w' ? 1 : -1); //shift from king + return super.isAttackedBySlideNJump( + [x, y], color, V.SOLDIER, [[shiftX, 0], [0, 1], [0, -1]], 1); + } + + updateCastleFlags(move, piece) { + // Only black can castle: + const firstRank = 0; + if (piece == V.KING && move.appear[0].c == 'b') + this.castleFlags['b'] = [8, 8]; + else if ( + move.start.x == firstRank && + this.castleFlags['b'].includes(move.start.y) + ) { + const flagIdx = (move.start.y == this.castleFlags['b'][0] ? 0 : 1); + this.castleFlags['b'][flagIdx] = 8; + } + else if ( + move.end.x == firstRank && + this.castleFlags['b'].includes(move.end.y) + ) { + const flagIdx = (move.end.y == this.castleFlags['b'][0] ? 0 : 1); + this.castleFlags['b'][flagIdx] = 8; + } + } + + getCurrentScore() { + // Turn has changed: + const color = V.GetOppCol(this.turn); + const lastRank = (color == 'w' ? 0 : 7); + if (this.kingPos[color][0] == lastRank) + // The opposing edge is reached! + return color == "w" ? "1-0" : "0-1"; + if (this.atLeastOneMove()) return "*"; + // Game over + const oppCol = this.turn; + return (oppCol == "w" ? "0-1" : "1-0"); + } + + static get VALUES() { + return Object.assign( + {}, + ChessRules.VALUES, + { + t: 7, + e: 7, + c: 4, + d: 4, + s: 2 + } + ); + } + + static get SEARCH_DEPTH() { + return 2; + } + +}; diff --git a/variants/Enpassant/class.js b/variants/Enpassant/class.js new file mode 100644 index 0000000..cb21830 --- /dev/null +++ b/variants/Enpassant/class.js @@ -0,0 +1,209 @@ +import { ChessRules, PiPo, Move } from "@/base_rules"; + +export class EnpassantRules extends ChessRules { + + static IsGoodEnpassant(enpassant) { + if (enpassant != "-") { + const squares = enpassant.split(","); + if (squares.length > 2) return false; + for (let sq of squares) { + const ep = V.SquareToCoords(sq); + if (isNaN(ep.x) || !V.OnBoard(ep)) return false; + } + } + return true; + } + + getPpath(b) { + return (b[1] == V.KNIGHT ? "Enpassant/" : "") + b; + } + + getEpSquare(moveOrSquare) { + if (!moveOrSquare) return undefined; + if (typeof moveOrSquare === "string") { + const square = moveOrSquare; + if (square == "-") return undefined; + // Expand init + dest squares into a full path: + const init = V.SquareToCoords(square.substr(0, 2)); + let newPath = [init]; + if (square.length == 2) return newPath; + const dest = V.SquareToCoords(square.substr(2)); + const delta = ['x', 'y'].map(i => Math.abs(dest[i] - init[i])); + // Check if it's a knight(rider) movement: + let step = [0, 0]; + if (delta[0] > 0 && delta[1] > 0 && delta[0] != delta[1]) { + // Knightrider + const minShift = Math.min(delta[0], delta[1]); + step[0] = (dest.x - init.x) / minShift; + step[1] = (dest.y - init.y) / minShift; + } else { + // "Sliders" + step = ['x', 'y'].map((i, idx) => { + return (dest[i] - init[i]) / delta[idx] || 0 + }); + } + let x = init.x + step[0], + y = init.y + step[1]; + while (x != dest.x || y != dest.y) { + newPath.push({ x: x, y: y }); + x += step[0]; + y += step[1]; + } + newPath.push(dest); + return newPath; + } + // Argument is a move: all intermediate squares are en-passant candidates, + // except if the moving piece is a king. + const move = moveOrSquare; + const piece = move.appear[0].p; + if (piece == V.KING || + ( + Math.abs(move.end.x-move.start.x) <= 1 && + Math.abs(move.end.y-move.start.y) <= 1 + ) + ) { + return undefined; + } + const delta = [move.end.x-move.start.x, move.end.y-move.start.y]; + let step = undefined; + if (piece == V.KNIGHT) { + const divisor = Math.min(Math.abs(delta[0]), Math.abs(delta[1])); + step = [delta[0]/divisor || 0, delta[1]/divisor || 0]; + } else { + step = [ + delta[0]/Math.abs(delta[0]) || 0, + delta[1]/Math.abs(delta[1]) || 0 + ]; + } + let res = []; + for ( + let [x,y] = [move.start.x+step[0],move.start.y+step[1]]; + x != move.end.x || y != move.end.y; + x += step[0], y += step[1] + ) { + res.push({ x: x, y: y }); + } + // Add final square to know which piece is taken en passant: + res.push(move.end); + return res; + } + + getEnpassantFen() { + const L = this.epSquares.length; + if (!this.epSquares[L - 1]) return "-"; //no en-passant + const epsq = this.epSquares[L - 1]; + if (epsq.length <= 2) return epsq.map(V.CoordsToSquare).join(""); + // Condensate path: just need initial and final squares: + return V.CoordsToSquare(epsq[0]) + V.CoordsToSquare(epsq[epsq.length - 1]); + } + + getPotentialMovesFrom([x, y]) { + let moves = super.getPotentialMovesFrom([x,y]); + // Add en-passant captures from this square: + const L = this.epSquares.length; + if (!this.epSquares[L - 1]) return moves; + const squares = this.epSquares[L - 1]; + const S = squares.length; + // Object describing the removed opponent's piece: + const pipoV = new PiPo({ + x: squares[S-1].x, + y: squares[S-1].y, + c: V.GetOppCol(this.turn), + p: this.getPiece(squares[S-1].x, squares[S-1].y) + }); + // Check if existing non-capturing moves could also capture en passant + moves.forEach(m => { + if ( + m.appear[0].p != V.PAWN && //special pawn case is handled elsewhere + m.vanish.length <= 1 && + [...Array(S-1).keys()].some(i => { + return m.end.x == squares[i].x && m.end.y == squares[i].y; + }) + ) { + m.vanish.push(pipoV); + } + }); + // Special case of the king knight's movement: + if (this.getPiece(x, y) == V.KING) { + V.steps[V.KNIGHT].forEach(step => { + const endX = x + step[0]; + const endY = y + step[1]; + if ( + V.OnBoard(endX, endY) && + [...Array(S-1).keys()].some(i => { + return endX == squares[i].x && endY == squares[i].y; + }) + ) { + let enpassantMove = this.getBasicMove([x, y], [endX, endY]); + enpassantMove.vanish.push(pipoV); + moves.push(enpassantMove); + } + }); + } + return moves; + } + + getEnpassantCaptures([x, y], shiftX) { + const Lep = this.epSquares.length; + const squares = this.epSquares[Lep - 1]; + let moves = []; + if (!!squares) { + const S = squares.length; + const taken = squares[S-1]; + const pipoV = new PiPo({ + x: taken.x, + y: taken.y, + p: this.getPiece(taken.x, taken.y), + c: this.getColor(taken.x, taken.y) + }); + [...Array(S-1).keys()].forEach(i => { + const sq = squares[i]; + if (sq.x == x + shiftX && Math.abs(sq.y - y) == 1) { + let enpassantMove = this.getBasicMove([x, y], [sq.x, sq.y]); + enpassantMove.vanish.push(pipoV); + moves.push(enpassantMove); + } + }); + } + return moves; + } + + // Remove the "onestep" condition: knight promote to knightrider: + getPotentialKnightMoves(sq) { + return this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT]); + } + + filterValid(moves) { + const filteredMoves = super.filterValid(moves); + // If at least one full move made, everything is allowed: + if (this.movesCount >= 2) + return filteredMoves; + // Else, forbid captures: + return filteredMoves.filter(m => m.vanish.length == 1); + } + + isAttackedByKnight(sq, color) { + return this.isAttackedBySlideNJump( + sq, + color, + V.KNIGHT, + V.steps[V.KNIGHT] + ); + } + + static get SEARCH_DEPTH() { + return 2; + } + + static get VALUES() { + return { + p: 1, + r: 5, + n: 4, + b: 3, + q: 9, + k: 1000 + }; + } + +}; diff --git a/variants/Evolution/class.js b/variants/Evolution/class.js new file mode 100644 index 0000000..5250089 --- /dev/null +++ b/variants/Evolution/class.js @@ -0,0 +1,34 @@ +import { ChessRules } from "@/base_rules"; + +export class EvolutionRules extends ChessRules { + + getPotentialMovesFrom([x, y]) { + let moves = super.getPotentialMovesFrom([x, y]); + const c = this.getColor(x, y); + const piece = this.getPiece(x, y); + if ( + [V.BISHOP, V.ROOK, V.QUEEN].includes(piece) && + (c == 'w' && x == 7) || (c == 'b' && x == 0) + ) { + // Move from first rank + const forward = (c == 'w' ? -1 : 1); + for (let shift of [-2, 0, 2]) { + if ( + (piece == V.ROOK && shift != 0) || + (piece == V.BISHOP && shift == 0) + ) { + continue; + } + if ( + V.OnBoard(x+2*forward, y+shift) && + this.board[x+forward][y+shift/2] != V.EMPTY && + this.getColor(x+2*forward, y+shift) != c + ) { + moves.push(this.getBasicMove([x,y], [x+2*forward,y+shift])); + } + } + } + return moves; + } + +}; diff --git a/variants/Extinction/class.js b/variants/Extinction/class.js new file mode 100644 index 0000000..e42e294 --- /dev/null +++ b/variants/Extinction/class.js @@ -0,0 +1,118 @@ +import { ChessRules } from "@/base_rules"; + +export class ExtinctionRules extends ChessRules { + + static get PawnSpecs() { + return Object.assign( + {}, + ChessRules.PawnSpecs, + { promotions: ChessRules.PawnSpecs.promotions.concat([V.KING]) } + ); + } + + static IsGoodPosition(position) { + if (!ChessRules.IsGoodPosition(position)) return false; + // Also check that each piece type is present + const rows = position.split("/"); + let pieces = {}; + for (let row of rows) { + for (let i = 0; i < row.length; i++) { + if (isNaN(parseInt(row[i], 10)) && !pieces[row[i]]) + pieces[row[i]] = true; + } + } + if (Object.keys(pieces).length != 12) return false; + return true; + } + + setOtherVariables(fen) { + super.setOtherVariables(fen); + const pos = V.ParseFen(fen).position; + // NOTE: no need for safety "|| []", because each piece type is present + // (otherwise game is already over!) + this.material = { + w: { + [V.KING]: pos.match(/K/g).length, + [V.QUEEN]: pos.match(/Q/g).length, + [V.ROOK]: pos.match(/R/g).length, + [V.KNIGHT]: pos.match(/N/g).length, + [V.BISHOP]: pos.match(/B/g).length, + [V.PAWN]: pos.match(/P/g).length + }, + b: { + [V.KING]: pos.match(/k/g).length, + [V.QUEEN]: pos.match(/q/g).length, + [V.ROOK]: pos.match(/r/g).length, + [V.KNIGHT]: pos.match(/n/g).length, + [V.BISHOP]: pos.match(/b/g).length, + [V.PAWN]: pos.match(/p/g).length + } + }; + } + + // TODO: verify this assertion + atLeastOneMove() { + return true; //always at least one possible move + } + + filterValid(moves) { + return moves; //there is no check + } + + getCheckSquares() { + return []; + } + + postPlay(move) { + super.postPlay(move); + // Treat the promotion case: (not the capture part) + if (move.appear[0].p != move.vanish[0].p) { + this.material[move.appear[0].c][move.appear[0].p]++; + this.material[move.appear[0].c][V.PAWN]--; + } + if (move.vanish.length == 2 && move.appear.length == 1) + //capture + this.material[move.vanish[1].c][move.vanish[1].p]--; + } + + postUndo(move) { + super.postUndo(move); + if (move.appear[0].p != move.vanish[0].p) { + this.material[move.appear[0].c][move.appear[0].p]--; + this.material[move.appear[0].c][V.PAWN]++; + } + if (move.vanish.length == 2 && move.appear.length == 1) + this.material[move.vanish[1].c][move.vanish[1].p]++; + } + + getCurrentScore() { + if (this.atLeastOneMove()) { + // Game not over? + const color = this.turn; + if ( + Object.keys(this.material[color]).some(p => { + return this.material[color][p] == 0; + }) + ) { + return this.turn == "w" ? "0-1" : "1-0"; + } + return "*"; + } + return this.turn == "w" ? "0-1" : "1-0"; //NOTE: currently unreachable... + } + + evalPosition() { + const color = this.turn; + if ( + Object.keys(this.material[color]).some(p => { + return this.material[color][p] == 0; + }) + ) { + // Very negative (resp. positive) + // if white (reps. black) pieces set is incomplete + return (color == "w" ? -1 : 1) * V.INFINITY; + } + return super.evalPosition(); + } + +}; diff --git a/variants/Fanorona/class.js b/variants/Fanorona/class.js new file mode 100644 index 0000000..e1e653a --- /dev/null +++ b/variants/Fanorona/class.js @@ -0,0 +1,342 @@ +import { ChessRules, Move, PiPo } from "@/base_rules"; +import { randInt } from "@/utils/alea"; + +export class FanoronaRules extends ChessRules { + + static get Options() { + return null; + } + + static get HasFlags() { + return false; + } + + static get HasEnpassant() { + return false; + } + + static get Monochrome() { + return true; + } + + static get Lines() { + let lines = []; + // Draw all inter-squares lines, shifted: + for (let i = 0; i < V.size.x; i++) + lines.push([[i+0.5, 0.5], [i+0.5, V.size.y-0.5]]); + for (let j = 0; j < V.size.y; j++) + lines.push([[0.5, j+0.5], [V.size.x-0.5, j+0.5]]); + const columnDiags = [ + [[0.5, 0.5], [2.5, 2.5]], + [[0.5, 2.5], [2.5, 0.5]], + [[2.5, 0.5], [4.5, 2.5]], + [[4.5, 0.5], [2.5, 2.5]] + ]; + for (let j of [0, 2, 4, 6]) { + lines = lines.concat( + columnDiags.map(L => [[L[0][0], L[0][1] + j], [L[1][0], L[1][1] + j]]) + ); + } + return lines; + } + + static get Notoodark() { + return true; + } + + static GenRandInitFen() { + return "ppppppppp/ppppppppp/pPpP1pPpP/PPPPPPPPP/PPPPPPPPP w 0"; + } + + setOtherVariables(fen) { + // Local stack of captures during a turn (squares + directions) + this.captures = [ [] ]; + } + + static get size() { + return { x: 5, y: 9 }; + } + + getPiece() { + return V.PAWN; + } + + static IsGoodPosition(position) { + if (position.length == 0) return false; + const rows = position.split("/"); + if (rows.length != V.size.x) return false; + for (let row of rows) { + let sumElts = 0; + for (let i = 0; i < row.length; i++) { + if (row[i].toLowerCase() == V.PAWN) sumElts++; + else { + const num = parseInt(row[i], 10); + if (isNaN(num) || num <= 0) return false; + sumElts += num; + } + } + if (sumElts != V.size.y) return false; + } + return true; + } + + getPpath(b) { + return "Fanorona/" + b; + } + + getPPpath(m, orientation) { + // m.vanish.length >= 2, first capture gives direction + const ref = (Math.abs(m.vanish[1].x - m.start.x) == 1 ? m.start : m.end); + const step = [m.vanish[1].x - ref.x, m.vanish[1].y - ref.y]; + const multStep = (orientation == 'w' ? 1 : -1); + const normalizedStep = [ + multStep * step[0] / Math.abs(step[0]), + multStep * step[1] / Math.abs(step[1]) + ]; + return ( + "Fanorona/arrow_" + + (normalizedStep[0] || 0) + "_" + (normalizedStep[1] || 0) + ); + } + + // After moving, add stones captured in "step" direction from new location + // [x, y] to mv.vanish (if any captured stone!) + addCapture([x, y], step, move) { + let [i, j] = [x + step[0], y + step[1]]; + const oppCol = V.GetOppCol(move.vanish[0].c); + while ( + V.OnBoard(i, j) && + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == oppCol + ) { + move.vanish.push(new PiPo({ x: i, y: j, c: oppCol, p: V.PAWN })); + i += step[0]; + j += step[1]; + } + return (move.vanish.length >= 2); + } + + getPotentialMovesFrom([x, y]) { + const L0 = this.captures.length; + const captures = this.captures[L0 - 1]; + const L = captures.length; + if (L > 0) { + var c = captures[L-1]; + if (x != c.square.x + c.step[0] || y != c.square.y + c.step[1]) + return []; + } + const oppCol = V.GetOppCol(this.turn); + let steps = V.steps[V.ROOK]; + if ((x + y) % 2 == 0) steps = steps.concat(V.steps[V.BISHOP]); + let moves = []; + for (let s of steps) { + if (L > 0 && c.step[0] == s[0] && c.step[1] == s[1]) { + // Add a move to say "I'm done capturing" + moves.push( + new Move({ + appear: [], + vanish: [], + start: { x: x, y: y }, + end: { x: x - s[0], y: y - s[1] } + }) + ); + continue; + } + let [i, j] = [x + s[0], y + s[1]]; + if (captures.some(c => c.square.x == i && c.square.y == j)) continue; + if (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { + // The move is potentially allowed. Might lead to 2 different captures + let mv = super.getBasicMove([x, y], [i, j]); + const capt = this.addCapture([i, j], s, mv); + if (capt) { + moves.push(mv); + mv = super.getBasicMove([x, y], [i, j]); + } + const capt_bw = this.addCapture([x, y], [-s[0], -s[1]], mv); + if (capt_bw) moves.push(mv); + // Captures take priority (if available) + if (!capt && !capt_bw && L == 0) moves.push(mv); + } + } + return moves; + } + + atLeastOneCapture() { + const color = this.turn; + const oppCol = V.GetOppCol(color); + const L0 = this.captures.length; + const captures = this.captures[L0 - 1]; + const L = captures.length; + if (L > 0) { + // If some adjacent enemy stone, with free space to capture it, + // toward a square not already visited, through a different step + // from last one: then yes. + const c = captures[L-1]; + const [x, y] = [c.square.x + c.step[0], c.square.y + c.step[1]]; + let steps = V.steps[V.ROOK]; + if ((x + y) % 2 == 0) steps = steps.concat(V.steps[V.BISHOP]); + // TODO: half of the steps explored are redundant + for (let s of steps) { + if (s[0] == c.step[0] && s[1] == c.step[1]) continue; + const [i, j] = [x + s[0], y + s[1]]; + if ( + !V.OnBoard(i, j) || + this.board[i][j] != V.EMPTY || + captures.some(c => c.square.x == i && c.square.y == j) + ) { + continue; + } + if ( + V.OnBoard(i + s[0], j + s[1]) && + this.board[i + s[0]][j + s[1]] != V.EMPTY && + this.getColor(i + s[0], j + s[1]) == oppCol + ) { + return true; + } + if ( + V.OnBoard(x - s[0], y - s[1]) && + this.board[x - s[0]][y - s[1]] != V.EMPTY && + this.getColor(x - s[0], y - s[1]) == oppCol + ) { + return true; + } + } + return false; + } + for (let i = 0; i < V.size.x; i++) { + for (let j = 0; j < V.size.y; j++) { + if ( + this.board[i][j] != V.EMPTY && + this.getColor(i, j) == color && + // TODO: this could be more efficient + this.getPotentialMovesFrom([i, j]).some(m => m.vanish.length >= 2) + ) { + return true; + } + } + } + return false; + } + + static KeepCaptures(moves) { + return moves.filter(m => m.vanish.length >= 2); + } + + getPossibleMovesFrom(sq) { + let moves = this.getPotentialMovesFrom(sq); + const L0 = this.captures.length; + const captures = this.captures[L0 - 1]; + if (captures.length > 0) return this.getPotentialMovesFrom(sq); + const captureMoves = V.KeepCaptures(moves); + if (captureMoves.length > 0) return captureMoves; + if (this.atLeastOneCapture()) return []; + return moves; + } + + getAllValidMoves() { + const moves = super.getAllValidMoves(); + if (moves.some(m => m.vanish.length >= 2)) return V.KeepCaptures(moves); + return moves; + } + + filterValid(moves) { + return moves; + } + + getCheckSquares() { + return []; + } + + play(move) { + const color = this.turn; + move.turn = color; //for undo + V.PlayOnBoard(this.board, move); + if (move.vanish.length >= 2) { + const L0 = this.captures.length; + let captures = this.captures[L0 - 1]; + captures.push({ + square: move.start, + step: [move.end.x - move.start.x, move.end.y - move.start.y] + }); + if (this.atLeastOneCapture()) + // There could be other captures (optional) + move.notTheEnd = true; + } + if (!move.notTheEnd) { + this.turn = V.GetOppCol(color); + this.movesCount++; + this.captures.push([]); + } + } + + undo(move) { + V.UndoOnBoard(this.board, move); + if (!move.notTheEnd) { + this.turn = move.turn; + this.movesCount--; + this.captures.pop(); + } + if (move.vanish.length >= 2) { + const L0 = this.captures.length; + let captures = this.captures[L0 - 1]; + captures.pop(); + } + } + + getCurrentScore() { + const color = this.turn; + // If no stones on board, I lose + if ( + this.board.every(b => { + return b.every(cell => { + return (cell == "" || cell[0] != color); + }); + }) + ) { + return (color == 'w' ? "0-1" : "1-0"); + } + return "*"; + } + + getComputerMove() { + const moves = this.getAllValidMoves(); + if (moves.length == 0) return null; + const color = this.turn; + // Capture available? If yes, play it + let captures = moves.filter(m => m.vanish.length >= 2); + let mvArray = []; + while (captures.length >= 1) { + // Then just pick random captures (trying to maximize) + let candidates = captures.filter(c => !!c.notTheEnd); + let mv = null; + if (candidates.length >= 1) mv = candidates[randInt(candidates.length)]; + else mv = captures[randInt(captures.length)]; + this.play(mv); + mvArray.push(mv); + captures = (this.turn == color ? this.getAllValidMoves() : []); + } + if (mvArray.length >= 1) { + for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]); + return mvArray; + } + // Just play a random move, which if possible does not let a capture + let candidates = []; + for (let m of moves) { + this.play(m); + if (!this.atLeastOneCapture()) candidates.push(m); + this.undo(m); + } + if (candidates.length >= 1) return candidates[randInt(candidates.length)]; + return moves[randInt(moves.length)]; + } + + getNotation(move) { + if (move.appear.length == 0) return "stop"; + return ( + V.CoordsToSquare(move.start) + + V.CoordsToSquare(move.end) + + (move.vanish.length >= 2 ? "X" : "") + ); + } + +}; diff --git a/variants/Sleepy/class.js b/variants/Sleepy/class.js new file mode 100644 index 0000000..e58ceae --- /dev/null +++ b/variants/Sleepy/class.js @@ -0,0 +1,110 @@ +import ChessRules from "/base_rules.js"; +import PiPo from "/utils/PiPo.js"; +import Move from "/utils/Move.js"; + +export default class SleepyRules extends ChessRules { + + static get Options() { + return { + select: C.Options.select, + input: {}, + styles: ["cylinder"] //TODO + }; + } + + setOtherVariables(fenParsed) { + super.setOtherVariables(fenParsed); + // Stack of "last move" only for intermediate chaining + this.lastMoveEnd = []; + } + + getBasicMove([sx, sy], [ex, ey], tr) { + const L = this.lastMoveEnd.length; + const piece = (L >= 1 ? this.lastMoveEnd[L-1].p : null); + if ( + this.board[ex][ey] == "" || + this.getColor(ex, ey) == C.GetOppTurn(this.turn) + ) { + if (piece && !tr) + tr = {c: this.turn, p: piece}; + let mv = super.getBasicMove([sx, sy], [ex, ey], tr); + if (piece) + mv.vanish.pop(); //end of a chain: initial piece remains + return mv; + } + // (Self)Capture: initial, or inside a chain + const initPiece = (piece || this.getPiece(sx, sy)), + destPiece = this.getPiece(ex, ey); + let mv = new Move({ + start: {x: sx, y: sy}, + end: {x: ex, y: ey}, + appear: [ + new PiPo({ + x: ex, + y: ey, + c: this.turn, + p: (!!tr ? tr.p : initPiece) + }) + ], + vanish: [ + new PiPo({ + x: ex, + y: ey, + c: this.turn, + p: destPiece + }) + ] + }); + if (!piece) { + // Initial capture + mv.vanish.unshift( + new PiPo({ + x: sx, + y: sy, + c: this.turn, + p: initPiece + }) + ); + } + mv.chained = destPiece; //easier (no need to detect it) +// mv.drag = {c: this.turn, p: initPiece}; //TODO: doesn't work + return mv; + } + + getPiece(x, y) { + const L = this.lastMoveEnd.length; + if (L >= 1 && this.lastMoveEnd[L-1].x == x && this.lastMoveEnd[L-1].y == y) + return this.lastMoveEnd[L-1].p; + return super.getPiece(x, y); + } + + getPotentialMovesFrom([x, y], color) { + const L = this.lastMoveEnd.length; + if ( + L >= 1 && + (x != this.lastMoveEnd[L-1].x || y != this.lastMoveEnd[L-1].y) + ) { + // A self-capture was played: wrong square + return []; + } + return super.getPotentialMovesFrom([x, y], color); + } + + isLastMove(move) { + return !move.chained; + } + + postPlay(move) { + super.postPlay(move); + if (!!move.chained) { + this.lastMoveEnd.push({ + x: move.end.x, + y: move.end.y, + p: move.chained + }); + } + else + this.lastMoveEnd = []; + } + +}; diff --git a/variants/Sleepy/rules.html b/variants/Sleepy/rules.html new file mode 100644 index 0000000..f27cd96 --- /dev/null +++ b/variants/Sleepy/rules.html @@ -0,0 +1,7 @@ +

    + You can "capture" your own pieces, and then move them from the capturing + square in the same turn, with potential chaining if the captured unit + makes a self-capture too. +

    + +

    Benjamin Auder (2021).

    diff --git a/variants/Sleepy/style.css b/variants/Sleepy/style.css new file mode 100644 index 0000000..a3550bc --- /dev/null +++ b/variants/Sleepy/style.css @@ -0,0 +1 @@ +@import url("/base_pieces.css"); -- 2.48.1 From ab2ca6784b154f0fd6183b908df124063a45f876 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Thu, 30 Jan 2025 18:26:19 +0100 Subject: [PATCH 16/16] update --- base_rules.js | 3 +-- variants/Dynamo/class.js | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/base_rules.js b/base_rules.js index 3d8e463..888fdcf 100644 --- a/base_rules.js +++ b/base_rules.js @@ -2235,8 +2235,7 @@ export default class ChessRules { // 'color' arg because some variants (e.g. Refusal) check opponent moves filterValid(moves, color) { - if (!color) - color = this.turn; + color = color || this.turn; const oppCols = this.getOppCols(color); let kingPos = this.searchKingPos(color); return moves.filter(m => { diff --git a/variants/Dynamo/class.js b/variants/Dynamo/class.js index 7b835e0..d726522 100644 --- a/variants/Dynamo/class.js +++ b/variants/Dynamo/class.js @@ -181,8 +181,8 @@ export default class DynamoRules extends ChessRules { // NOTE: for pushes, play the pushed piece first. // for pulls: play the piece doing the action first // NOTE: to push a piece out of the board, make it slide until its king - getPotentialMovesFrom([x, y]) { - const color = this.turn; + getPotentialMovesFrom([x, y], color) { + const color = color || this.turn; const sqCol = this.getColor(x, y); const pawnShift = (color == 'w' ? -1 : 1); const pawnStartRank = (color == 'w' ? 6 : 1); @@ -486,6 +486,10 @@ export default class DynamoRules extends ChessRules { return []; } + getAllPotentialMoves(color) { + + } + getSlideNJumpMoves([x, y], steps, oneStep) { let moves = []; const c = this.getColor(x, y); -- 2.48.1