From aaa0d7d5e1342d85f758b55deb5a8599eb19f35d Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Fri, 5 Jun 2020 22:28:57 +0200 Subject: [PATCH] Monochrome: remove duplicate capturing moves --- client/src/variants/Monochrome.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/client/src/variants/Monochrome.js b/client/src/variants/Monochrome.js index ba56c69a..50ca04b1 100644 --- a/client/src/variants/Monochrome.js +++ b/client/src/variants/Monochrome.js @@ -105,7 +105,21 @@ export class MonochromeRules extends ChessRules { } getPotentialMovesFrom(sq) { - return super.getPotentialMovesFrom(sq).concat(this.findCaptures(sq)); + const moves = super.getPotentialMovesFrom(sq); + const zenCaptures = this.findCaptures(sq); + // Remove duplicate captures in a lazy way (TODO: more efficient...) + let hashMoves = {}; + moves.forEach(m => { + if (m.vanish.length == 2) { + const hash = + m.start.x + "." + m.start.y + "." + m.end.x + "." + m.end.y; + hashMoves[hash] = true; + } + } + return moves.concat(zenCaptures.filter(m => { + const hash = m.start.x + "." + m.start.y + "." + m.end.x + "." + m.end.y; + return !hashMoves[hash]; + }); } getAllPotentialMoves() { -- 2.44.0