From f5cd0fb81ec9bd790d812c9ad02587e50427fcdf Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 25 Jan 2021 00:49:16 +0100
Subject: [PATCH] Fix Makruk/Makpong

---
 client/src/translations/rules/Makpong/en.pug |  6 +--
 client/src/translations/rules/Makpong/es.pug |  6 +--
 client/src/translations/rules/Makpong/fr.pug |  6 +--
 client/src/variants/Makpong.js               | 46 +++++++++++++++++---
 client/src/variants/Makruk.js                | 16 +++++++
 5 files changed, 66 insertions(+), 14 deletions(-)

diff --git a/client/src/translations/rules/Makpong/en.pug b/client/src/translations/rules/Makpong/en.pug
index 522cb58b..561485e8 100644
--- a/client/src/translations/rules/Makpong/en.pug
+++ b/client/src/translations/rules/Makpong/en.pug
@@ -2,10 +2,10 @@ p.boxed
   | Makruk, with kings immobilized by checks.
 
 p
-  | Everything goes as in the variant 
+  | Everything goes as in 
   a(href="/#/variants/Makruk") Makruk
-  | , but the king isn't allowed to move when he's under check:
-  | one has to capture the attacker, or intercept the attack if possible.
+  | , but the king isn't allowed to move when he's under check
+  | except to capture the attacker.
 
 figure.diagram-container
   .diagram
diff --git a/client/src/translations/rules/Makpong/es.pug b/client/src/translations/rules/Makpong/es.pug
index d3e2cf7a..11df52e8 100644
--- a/client/src/translations/rules/Makpong/es.pug
+++ b/client/src/translations/rules/Makpong/es.pug
@@ -2,10 +2,10 @@ p.boxed
   | Makruk, con reyes inmovilizados por los jaques.
 
 p
-  | Todo va como en la variante 
+  | Todo va como en 
   a(href="/#/variants/Makruk") Makruk
-  | , pero el rey no puede moverse cuando está en jaque:
-  | debemos capturar al agresor, o interponer una pieza si es posible.
+  | , pero el rey no puede moverse cuando está en jaque
+  | excepto por capturar al agresor.
 
 figure.diagram-container
   .diagram
diff --git a/client/src/translations/rules/Makpong/fr.pug b/client/src/translations/rules/Makpong/fr.pug
index dd24d4af..d90c63f9 100644
--- a/client/src/translations/rules/Makpong/fr.pug
+++ b/client/src/translations/rules/Makpong/fr.pug
@@ -2,10 +2,10 @@ p.boxed
   | Makruk, avec rois immobilisés par les échecs.
 
 p
-  | Tout se déroule comme dans la variante
+  | Tout se déroule comme au 
   a(href="/#/variants/Makruk") Makruk
-  | , mais le roi n'a pas le droit de bouger lorsqu'il est en échec :
-  | il faut capturer l'assaillant, ou interposer une pièce si possible.
+  | , mais le roi n'a pas le droit de bouger lorsqu'il est en échec
+  | sauf pour capturer l'attaquant.
 
 figure.diagram-container
   .diagram
diff --git a/client/src/variants/Makpong.js b/client/src/variants/Makpong.js
index 05766651..885de3ef 100644
--- a/client/src/variants/Makpong.js
+++ b/client/src/variants/Makpong.js
@@ -4,11 +4,47 @@ export class MakpongRules extends MakrukRules {
 
   filterValid(moves) {
     const color = this.turn;
-    if (this.underCheck(color)) {
-      // Filter out all moves involving king
-      return super.filterValid(moves.filter(m => m.vanish[0].p != V.KING));
-    }
-    return super.filterValid(moves);
+    if (!this.underCheck(color)) return super.filterValid(moves);
+    // Filter out all moves involving king, except for capturing a
+    // potential attacker.
+    const pawnAttack = (color == 'w' ? -1 : 1);
+    return super.filterValid(moves.filter(m => {
+      const p = (m.vanish.length == 2 ? m.vanish[1].p : null);
+      return (
+        m.vanish[0].p != V.KING ||
+        (
+          m.vanish.length == 2 &&
+          (
+            (
+              p == V.PAWN &&
+              m.end.x - m.start.x == pawnAttack &&
+              Math.abs(m.end.y - m.start.y) == 1
+            )
+            ||
+            (
+              p == V.ROOK &&
+              (m.end.x == m.start.x || m.end.y == m.start.y)
+            )
+            ||
+            (
+              [V.PROMOTED, V.QUEEN].includes(p) &&
+              m.end.x != m.start.x && m.end.y != m.start.y
+            )
+            ||
+            (
+              p == V.BISHOP &&
+              (
+                m.end.x - m.start.x == pawnAttack ||
+                (
+                  m.end.x - m.start.x == -pawnAttack &&
+                  Math.abs(m.end.y - m.start.y) == 1
+                )
+              )
+            )
+          )
+        )
+      );
+    }));
   }
 
 };
diff --git a/client/src/variants/Makruk.js b/client/src/variants/Makruk.js
index 55617025..75fb1da2 100644
--- a/client/src/variants/Makruk.js
+++ b/client/src/variants/Makruk.js
@@ -109,6 +109,12 @@ export class MakrukRules extends ChessRules {
     );
   }
 
+  isAttacked(sq, color) {
+    return (
+      super.isAttacked(sq, color) || this.isAttackedByPromoted(sq, color)
+    );
+  }
+
   isAttackedByBishop(sq, color) {
     const forward = (color == 'w' ? 1 : -1);
     return this.isAttackedBySlideNJump(
@@ -130,6 +136,16 @@ export class MakrukRules extends ChessRules {
     );
   }
 
+  isAttackedByPromoted(sq, color) {
+    return super.isAttackedBySlideNJump(
+      sq,
+      color,
+      V.PROMOTED,
+      V.steps[V.BISHOP],
+      "oneStep"
+    );
+  }
+
   static get VALUES() {
     return {
       p: 1,
-- 
2.44.0