From aea1443ebf56afb2c507c2830ac6b67b509778bc Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Wed, 21 Nov 2018 16:00:26 +0100
Subject: [PATCH] Complete Magnetic rules

---
 public/javascripts/base_rules.js         |  6 +--
 public/javascripts/components/game.js    |  3 +-
 public/javascripts/variants/Checkered.js |  8 ++--
 public/javascripts/variants/Magnetic.js  | 51 +++++++++++++++++-------
 4 files changed, 45 insertions(+), 23 deletions(-)

diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js
index f07aaff2..5b0d5d1e 100644
--- a/public/javascripts/base_rules.js
+++ b/public/javascripts/base_rules.js
@@ -682,7 +682,7 @@ class ChessRules
 			this.castleFlags[c][flagIdx] = false;
 		}
 		else if (move.end.x == oppFirstRank //we took opponent rook?
-			&& this.INIT_COL_ROOK[c].includes(move.end.y))
+			&& this.INIT_COL_ROOK[oppCol].includes(move.end.y))
 		{
 			const flagIdx = (move.end.y == this.INIT_COL_ROOK[oppCol][0] ? 0 : 1);
 			this.castleFlags[oppCol][flagIdx] = false;
@@ -699,7 +699,6 @@ class ChessRules
 
 	play(move, ingame)
 	{
-		console.log("AVANT " + this.getNotation(move) + " "  + this.board[1][5]);
 		if (!!ingame)
 			move.notation = this.getNotation(move);
 
@@ -717,7 +716,6 @@ class ChessRules
 		this.moves.pop();
 		this.unupdateVariables(move);
 		this.parseFlags(JSON.parse(move.flags));
-		console.log("APRES " + this.getNotation(move) + " "  + this.board[1][5]);
 	}
 
 	//////////////
@@ -1003,7 +1001,7 @@ class ChessRules
 	// Context: just before move is played, turn hasn't changed
 	getNotation(move)
 	{
-		if (move.appear.length == 2)
+		if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING)
 		{
 			// Castle
 			if (move.end.y < move.start.y)
diff --git a/public/javascripts/components/game.js b/public/javascripts/components/game.js
index 170aeac9..74092b56 100644
--- a/public/javascripts/components/game.js
+++ b/public/javascripts/components/game.js
@@ -412,7 +412,6 @@ Vue.component('my-game', {
 		};
 		const socketMessageListener = msg => {
 			const data = JSON.parse(msg.data);
-			console.log("Receive message: " + data.code);
 			switch (data.code)
 			{
 				case "newgame": //opponent found
@@ -575,7 +574,7 @@ Vue.component('my-game', {
 			this.newGame("computer");
 		},
 		newGame: function(mode, fenInit, color, oppId, moves, continuation) {
-			const fen = "brnbnkrq/pppppppp/8/8/8/8/PPPPPPPP/BNNRKBQR 11111111111111111111";//fenInit || VariantRules.GenRandInitFen();
+			const fen = fenInit || VariantRules.GenRandInitFen();
 			console.log(fen); //DEBUG
 			this.score = "*";
 			if (mode=="human" && !oppId)
diff --git a/public/javascripts/variants/Checkered.js b/public/javascripts/variants/Checkered.js
index 4f523edb..56fa15a3 100644
--- a/public/javascripts/variants/Checkered.js
+++ b/public/javascripts/variants/Checkered.js
@@ -72,6 +72,7 @@ class CheckeredRules extends ChessRules
 	getPotentialMovesFrom([x,y])
 	{
 		let standardMoves = super.getPotentialMovesFrom([x,y]);
+		const lastRank = this.turn == "w" ? 0 : 7;
 		if (this.getPiece(x,y) == VariantRules.KING)
 			return standardMoves; //king has to be treated differently (for castles)
 		let moves = [];
@@ -87,12 +88,13 @@ class CheckeredRules extends ChessRules
 			{
 				// A capture occured (m.vanish.length == 2)
 				m.appear[0].c = "c";
-				moves.push(JSON.parse(JSON.stringify(m)));
-				if (m.appear[0].p != m.vanish[1].p)
+				moves.push(m);
+				if (m.appear[0].p != m.vanish[1].p //avoid promotions:
+					&& (m.vanish[0].p != VariantRules.PAWN || m.end.x != lastRank))
 				{
 					// Add transformation into captured piece
 					let m2 = JSON.parse(JSON.stringify(m));
-					m2.vanish[1].p = m.appear[0].p;
+					m2.appear[0].p = m.vanish[1].p;
 					moves.push(m2);
 				}
 			}
diff --git a/public/javascripts/variants/Magnetic.js b/public/javascripts/variants/Magnetic.js
index d5731a63..d6cf918c 100644
--- a/public/javascripts/variants/Magnetic.js
+++ b/public/javascripts/variants/Magnetic.js
@@ -7,7 +7,7 @@ class MagneticRules extends ChessRules
 
 	getPotentialMovesFrom([x,y])
 	{
-		const standardMoves = super.getPotentialMovesFrom([x,y]);
+		let standardMoves = super.getPotentialMovesFrom([x,y]);
 		let moves = [];
 		standardMoves.forEach(m => {
 			let newMove_s = this.applyMagneticLaws(m);
@@ -16,29 +16,35 @@ class MagneticRules extends ChessRules
 			else //promotion
 				moves = moves.concat(moves, newMove_s);
 		});
+		return moves;
 	}
 
 	// Complete a move with magnetic actions
-	applyMagneticLaws(standardMove)
+	applyMagneticLaws(move)
 	{
-		const [x,y] = [moves.start.x, move.start.y];
-		let move = JSON.parse(JSON.stringify(standardMove));
+		const V = VariantRules;
+		if (move.appear[0].p == V.KING && move.appear.length==1)
+			return [move]; //kings are not charged
+		const aIdx = (move.appear[0].p != V.KING ? 0 : 1); //if castling, rook is charged
+		const [x,y] = [move.appear[aIdx].x, move.appear[aIdx].y];
+		const color = this.turn;
+		const lastRank = (color=="w" ? 0 : 7);
+		const standardMove = JSON.parse(JSON.stringify(move));
 		this.play(standardMove);
-		const color = this.getColor(x,y);
-		const [sizeX,sizeY] = VariantRules.size;
+		const [sizeX,sizeY] = V.size;
 		for (let step of [[-1,0],[1,0],[0,-1],[0,1]])
 		{
 			let [i,j] = [x+step[0],y+step[1]];
 			while (i>=0 && i<sizeX && j>=0 && j<sizeY)
 			{
-				if (this.board[i][j] != VariantRules.EMPTY)
+				if (this.board[i][j] != V.EMPTY)
 				{
 					// Found something. Same color or not?
 					if (this.getColor(i,j) != color)
 					{
 						// Attraction
 						if ((Math.abs(i-x)>=2 || Math.abs(j-y)>=2)
-							&& this.getPiece(i,j) != VariantRules.KING)
+							&& this.getPiece(i,j) != V.KING)
 						{
 							move.vanish.push(
 								new PiPo({
@@ -61,13 +67,13 @@ class MagneticRules extends ChessRules
 					else
 					{
 						// Repulsion
-						if (this.getPiece(i,j) != VariantRules.KING)
+						if (this.getPiece(i,j) != V.KING)
 						{
 							// Push it until we meet an obstacle or edge of the board
 							let [ii,jj] = [i+step[0],j+step[1]];
 							while (ii>=0 && ii<sizeX && jj>=0 && jj<sizeY)
 							{
-								if (this.board[ii][jj] != VariantRules.EMPTY)
+								if (this.board[ii][jj] != V.EMPTY)
 									break;
 								ii += step[0];
 								jj += step[1];
@@ -103,12 +109,29 @@ class MagneticRules extends ChessRules
 		}
 		this.undo(standardMove);
 		let moves = [];
-		if (..condition pawn promote)
+		// Scan move for pawn (max 1) on 8th rank
+		for (let i=1; i<move.appear.length; i++)
 		{
-			move. ... = ... //loop
-			moves.push(...);
+			if (move.appear[i].p==V.PAWN && move.appear[i].x==lastRank)
+			{
+				move.appear[i].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[i].p = piece;
+					moves.push(cmove);
+				}
+				// Swap appear[i] and appear[0] for moves presentation (TODO: this is awkward)
+				moves.forEach(m => {
+					let tmp = m.appear[0];
+					m.appear[0] = m.appear[i];
+					m.appear[i] = tmp;
+				});
+				break;
+			}
 		}
-		else
+		if (moves.length == 0) //no pawn on 8th rank
 			moves.push(move);
 		return moves;
 	}
-- 
2.44.0