From 9f18af3bf306f89ae67c5cb5ba4ab3f581787256 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Wed, 28 Nov 2018 14:35:57 +0100
Subject: [PATCH] Fix switching chess

---
 public/javascripts/variants/Switching.js | 40 +++++++++++++++++++++---
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git a/public/javascripts/variants/Switching.js b/public/javascripts/variants/Switching.js
index cc2febd1..3ddf5032 100644
--- a/public/javascripts/variants/Switching.js
+++ b/public/javascripts/variants/Switching.js
@@ -3,7 +3,6 @@ class SwitchingRules extends ChessRules
 	// Build switch move between squares x1,y1 and x2,y2
 	getSwitchMove_s([x1,y1],[x2,y2])
 	{
-
 		const c = this.getColor(x1,y1); //same as color at square 2
 		const p1 = this.getPiece(x1,y1);
 		const p2 = this.getPiece(x2,y2);
@@ -24,16 +23,26 @@ class SwitchingRules extends ChessRules
 		const lastRank = (c == "w" ? 0 : sizeX-1);
 		const V = VariantRules;
 		let moves = [];
-		if (p1==V.PAWN && x2==lastRank) //TODO: also the case p2==V.PAWN and x1==lastRank! see Magnetic chess
+		if ((p1==V.PAWN && x2==lastRank) || (p2==V.PAWN && x1==lastRank))
 		{
-			move.appear[0].p = V.ROOK;
+			const idx = (p1==V.PAWN ? 0 : 1);
+			move.appear[idx].p = V.ROOK;
 			moves.push(move);
 			for (let piece of [V.KNIGHT, V.BISHOP, V.QUEEN])
 			{
 				let cmove = JSON.parse(JSON.stringify(move));
-				cmove.appear[0].p = piece;
+				cmove.appear[idx].p = piece;
 				moves.push(cmove);
 			}
+			if (idx == 1)
+			{
+				// Swap moves[i].appear[0] and [1] for moves presentation [TODO...]
+				moves.forEach(m => {
+					let tmp = m.appear[0];
+					m.appear[0] = m.appear[1];
+					m.appear[1] = tmp;
+				});
+			}
 		}
 		else //other cases
 			moves.push(move);
@@ -69,5 +78,28 @@ class SwitchingRules extends ChessRules
 		return moves;
 	}
 
+	updateVariables(move)
+	{
+		super.updateVariables(move);
+		if (move.appear.length == 2 && move.vanish.length == 2
+			&& move.appear[1].p == VariantRules.KING)
+		{
+			// Switch with the king; not castle, and not handled by main class
+			const color = this.getColor(move.start.x, move.start.y);
+			this.kingPos[color] = [move.appear[1].x, move.appear[1].y];
+		}
+	}
+
+	unupdateVariables(move)
+	{
+		super.unupdateVariables(move);
+		if (move.appear.length == 2 && move.vanish.length == 2
+			&& move.appear[1].p == VariantRules.KING)
+		{
+			const color = this.getColor(move.start.x, move.start.y);
+			this.kingPos[color] = [move.appear[0].x, move.appear[0].y];
+		}
+	}
+
 	static get SEARCH_DEPTH() { return 2; } //branching factor is quite high
 }
-- 
2.44.0