From: Benjamin Auder Date: Sat, 22 Oct 2022 06:48:21 +0000 (+0200) Subject: Fix FenUtil.setupPieces and Antiking variants X-Git-Url: https://git.auder.net/%7B%7B%20asset%28%27mixstore/images/assets/current/common.css?a=commitdiff_plain;h=10c9010b3b04915e132b6b3820f2f19c9ea6dcf0;p=xogo.git Fix FenUtil.setupPieces and Antiking variants --- diff --git a/base_rules.js b/base_rules.js index d609a56..7dc134d 100644 --- a/base_rules.js +++ b/base_rules.js @@ -219,6 +219,7 @@ export default class ChessRules { const s = FenUtil.setupPieces( ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], { + randomness: this.options["randomness"], between: {p1: 'k', p2: 'r'}, diffCol: ['b'], flags: ['r'] @@ -556,7 +557,7 @@ export default class ChessRules { chessboard.style.top = spaceTop + "px"; // Give sizes instead of recomputing them, // because chessboard might not be drawn yet. - this.setupPieces({ + this.setupVisualPieces({ width: cbWidth, height: cbHeight, x: spaceLeft, @@ -596,7 +597,7 @@ export default class ChessRules { return board; } - setupPieces(r) { + setupVisualPieces(r) { let chessboard = document.getElementById(this.containerId).querySelector(".chessboard"); if (!r) diff --git a/utils/setupPieces.js b/utils/setupPieces.js index 11af74a..2c8ccee 100644 --- a/utils/setupPieces.js +++ b/utils/setupPieces.js @@ -15,39 +15,41 @@ export const FenUtil = { flags += i; }); } - if (o.diffCol) { - o.diffCol.forEach(p => { - // Pieces of type p on different colors: - const firstP = res.indexOf(p), - lastP = res.lastIndexOf(p); - if ((firstP - lastP) % 2 != 0) { - const choice1 = Random.randBool() ? firstP : lastP; - let choice2; - do { - choice2 = Random.randInt(arr.length); + if (o.randomness >= 1) { + if (o.diffCol) { + o.diffCol.forEach(p => { + // Pieces of type p on different colors: + const firstP = res.indexOf(p), + lastP = res.lastIndexOf(p); + if ((firstP - lastP) % 2 != 0) { + const choice1 = Random.randBool() ? firstP : lastP; + let choice2; + do { + choice2 = Random.randInt(arr.length); + } + while ( + choice2 == choice1 || + o.diffCol.includes(choice2) || + (choice2 - choice1) % 2 != 0 + ); + res[choice1] = res[choice2]; + res[choice2] = p; } - while ( - choice2 == choice1 || - o.diffCol.includes(choice2) || - (choice2 - choice1) % 2 != 0 - ); - res[choice1] = res[choice2]; - res[choice2] = p; + }); + } + if (o.between) { + // Locate p1. If appearing first, exchange with first p2. + // If appearing last, exchange with last p2. + const p1 = res.indexOf(o.between["p1"]); + const firstP2 = res.indexOf(o.between["p2"]), + lastP2 = res.lastIndexOf(o.between["p2"]); + if (p1 < firstP2 || p1 > lastP2) { + res[p1] = o.between["p2"]; + if (p1 < firstP2) + res[firstP2] = o.between["p1"]; + else //p1 > lastP2 + res[lastP2] = o.between["p1"]; } - }); - } - if (o.between) { - // Locate p1. If appearing first, exchange with first p2. - // If appearing last, exchange with last p2. - const p1 = res.indexOf(o.between["p1"]); - const firstP2 = res.indexOf(o.between["p2"]), - lastP2 = res.lastIndexOf(o.between["p2"]); - if (p1 < firstP2 || p1 > lastP2) { - res[p1] = o.between["p2"]; - if (p1 < firstP2) - res[firstP2] = o.between["p1"]; - else //p1 > lastP2 - res[lastP2] = o.between["p1"]; } } return {fen: res, flags: flags}; diff --git a/variants/Alapo/class.js b/variants/Alapo/class.js index 3707ac8..969c80f 100644 --- a/variants/Alapo/class.js +++ b/variants/Alapo/class.js @@ -30,8 +30,13 @@ export default class AlapoRules extends ChessRules { } genRandInitBaseFen() { - const s = - FenUtil.setupPieces(['r', 'b', 'q', 'q', 'b', 'r'], {diffCol: ['b']}); + const s = FenUtil.setupPieces( + ['r', 'b', 'q', 'q', 'b', 'r'], + { + randomness: this.options["randomness"], + diffCol: ['b'] + } + ); const piece2pawn = { r: 't', q: 's', diff --git a/variants/Ambiguous/class.js b/variants/Ambiguous/class.js index 3043b7b..6a83374 100644 --- a/variants/Ambiguous/class.js +++ b/variants/Ambiguous/class.js @@ -24,7 +24,12 @@ export default class AmbiguousRules extends ChessRules { genRandInitBaseFen() { const s = FenUtil.setupPieces( - ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], {diffCol: ['b']}); + ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], + { + randomness: this.options["randomness"], + diffCol: ['b'] + } + ); return { fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + s.w.join("").toUpperCase(), diff --git a/variants/Balaklava/class.js b/variants/Balaklava/class.js index ad189cb..0e7b72f 100644 --- a/variants/Balaklava/class.js +++ b/variants/Balaklava/class.js @@ -34,7 +34,12 @@ export default class BalaklavaRules extends ChessRules { genRandInitBaseFen() { const s = FenUtil.setupPieces( - ['r', 'm', 'b', 'q', 'k', 'b', 'm', 'r'], {diffCol: ['b']}); + ['r', 'm', 'b', 'q', 'k', 'b', 'm', 'r'], + { + randomness: this.options["randomness"], + diffCol: ['b'] + } + ); return { fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + s.w.join("").toUpperCase(), diff --git a/variants/Baroque/class.js b/variants/Baroque/class.js index f87d768..f2039d5 100644 --- a/variants/Baroque/class.js +++ b/variants/Baroque/class.js @@ -34,7 +34,12 @@ export default class BaroqueRules extends AbstractSpecialCaptureRules { genRandInitBaseFen() { const s = FenUtil.setupPieces( - ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'i'], {diffCol: ['b']}); + ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'i'], + { + randomness: this.options["randomness"], + diffCol: ['b'] + } + ); if (this.options["randomness"] <= 1) { // Fix immobilizers/rooks pattern const toExchange1 = s.w.indexOf('r'), diff --git a/variants/Capablanca/class.js b/variants/Capablanca/class.js index 342429b..2fd1516 100644 --- a/variants/Capablanca/class.js +++ b/variants/Capablanca/class.js @@ -54,6 +54,7 @@ export default class CapablancaRules extends ChessRules { const s = FenUtil.setupPieces( ['r', 'n', 's', 'b', 'q', 'k', 'b', 'e', 'n', 'r'], { + randomness: this.options["randomness"], between: {p1: 'k', p2: 'r'}, diffCol: ['b'], flags: ['r'] diff --git a/variants/Chakart/class.js b/variants/Chakart/class.js index aefbcd0..4c55220 100644 --- a/variants/Chakart/class.js +++ b/variants/Chakart/class.js @@ -132,7 +132,12 @@ export default class ChakartRules extends ChessRules { genRandInitBaseFen() { const s = FenUtil.setupPieces( - ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], {diffCol: ['b']}); + ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], + { + randomness: this.options["randomness"], + diffCol: ['b'] + } + ); return { fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + s.w.join("").toUpperCase(), diff --git a/variants/Giveaway/class.js b/variants/Giveaway/class.js index fd23954..9665835 100644 --- a/variants/Giveaway/class.js +++ b/variants/Giveaway/class.js @@ -38,7 +38,10 @@ export default class GiveawayRules extends ChessRules { } genRandInitBaseFen() { - let setupOpts = {diffCol: ['b']}; + let setupOpts = { + randomness: this.options["randomness"], + diffCol: ['b'] + }; if (this.options["mode"] == "losers") { setupOpts["between"] = ['k', 'r']; setupOpts["flags"] = ['r']; diff --git a/variants/Suction/class.js b/variants/Suction/class.js index 6a66a06..ff307d4 100644 --- a/variants/Suction/class.js +++ b/variants/Suction/class.js @@ -43,7 +43,12 @@ export default class SuctionRules extends ChessRules { genRandInitBaseFen() { const s = FenUtil.setupPieces( - ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], {diffCol: ['b']}); + ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], + { + randomness: this.options["randomness"], + diffCol: ['b'] + } + ); return { fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + s.w.join("").toUpperCase(), diff --git a/variants/_Antiking/class.js b/variants/_Antiking/class.js index 771726c..5838ed6 100644 --- a/variants/_Antiking/class.js +++ b/variants/_Antiking/class.js @@ -37,8 +37,8 @@ export default class AbstractAntikingRules extends ChessRules { return ['k', 'a'].includes(p); } - // NOTE: canTake includes (wrong) captures of antiking, - // to detect attacks on antikings. + // NOTE: canTake includes (wrong) captures of (anti)king, + // to detect attacks on (anti)kings. canTake([x1, y1], [x2, y2]) { const piece1 = this.getPiece(x1, y1); const color1 = this.getColor(x1, y1); @@ -49,10 +49,10 @@ export default class AbstractAntikingRules extends ChessRules { ); } - // Remove captures of antiking (see above) + // Remove captures of (anti)king (see above) getPotentialMovesFrom([x, y]) { return super.getPotentialMovesFrom([x, y]).filter(m => - m.vanish.length == 1 || m.vanish[1].p != 'a'); + m.vanish.length == 1 || !['k', 'a'].includes(m.vanish[1].p)); } underCheck(square_s, color) { diff --git a/variants/_ClickFill/class.js b/variants/_ClickFill/class.js index b16b221..4094cc6 100644 --- a/variants/_ClickFill/class.js +++ b/variants/_ClickFill/class.js @@ -10,7 +10,7 @@ export default class AbstractClickFillRules extends ChessRules { return {} } - setupPieces() { + setupVisualPieces() { for (let i=0; i