1 class SwitchingRules
extends ChessRules
3 // Build switch move between squares x1,y1 and x2,y2
4 getSwitchMove_s([x1
,y1
],[x2
,y2
])
6 const c
= this.getColor(x1
,y1
); //same as color at square 2
7 const p1
= this.getPiece(x1
,y1
);
8 const p2
= this.getPiece(x2
,y2
);
11 new PiPo({x:x2
,y:y2
,c:c
,p:p1
}),
12 new PiPo({x:x1
,y:y1
,c:c
,p:p2
})
15 new PiPo({x:x1
,y:y1
,c:c
,p:p1
}),
16 new PiPo({x:x2
,y:y2
,c:c
,p:p2
})
21 // Move completion: promote switched pawns (as in Magnetic)
22 const sizeX
= VariantRules
.size
[0];
23 const lastRank
= (c
== "w" ? 0 : sizeX
-1);
24 const V
= VariantRules
;
26 if ((p1
==V
.PAWN
&& x2
==lastRank
) || (p2
==V
.PAWN
&& x1
==lastRank
))
28 const idx
= (p1
==V
.PAWN
? 0 : 1);
29 move.appear
[idx
].p
= V
.ROOK
;
31 for (let piece
of [V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
])
33 let cmove
= JSON
.parse(JSON
.stringify(move));
34 cmove
.appear
[idx
].p
= piece
;
39 // Swap moves[i].appear[0] and [1] for moves presentation [TODO...]
41 let tmp
= m
.appear
[0];
42 m
.appear
[0] = m
.appear
[1];
52 getPotentialMovesFrom([x
,y
])
54 let moves
= super.getPotentialMovesFrom([x
,y
]);
56 const V
= VariantRules
;
57 const color
= this.turn
;
58 const piece
= this.getPiece(x
,y
);
59 const [sizeX
,sizeY
] = V
.size
;
60 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
61 const kp
= this.kingPos
[color
];
62 const oppCol
= this.getOppCol(color
);
63 for (let step
of steps
)
65 let [i
,j
] = [x
+step
[0],y
+step
[1]];
66 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
]!=V
.EMPTY
67 && this.getColor(i
,j
)==color
&& this.getPiece(i
,j
)!=piece
68 // No switching under check (theoretically non-king pieces could, but not)
69 && !this.isAttacked(kp
, [oppCol
]))
71 let switchMove_s
= this.getSwitchMove_s([x
,y
],[i
,j
]);
72 if (switchMove_s
.length
== 1)
73 moves
.push(switchMove_s
[0]);
75 moves
= moves
.concat(switchMove_s
);
83 super.updateVariables(move);
84 if (move.appear
.length
== 2 && move.vanish
.length
== 2
85 && move.appear
[1].p
== VariantRules
.KING
)
87 // Switch with the king; not castle, and not handled by main class
88 const color
= this.getColor(move.start
.x
, move.start
.y
);
89 this.kingPos
[color
] = [move.appear
[1].x
, move.appear
[1].y
];
93 unupdateVariables(move)
95 super.unupdateVariables(move);
96 if (move.appear
.length
== 2 && move.vanish
.length
== 2
97 && move.appear
[1].p
== VariantRules
.KING
)
99 const color
= this.getColor(move.start
.x
, move.start
.y
);
100 this.kingPos
[color
] = [move.appear
[0].x
, move.appear
[0].y
];
104 static get SEARCH_DEPTH() { return 2; } //branching factor is quite high