cc2febd1e646f64fbdec59e520e9a94fa7ee1243
1 class SwitchingRules
extends ChessRules
3 // Build switch move between squares x1,y1 and x2,y2
4 getSwitchMove_s([x1
,y1
],[x2
,y2
])
7 const c
= this.getColor(x1
,y1
); //same as color at square 2
8 const p1
= this.getPiece(x1
,y1
);
9 const p2
= this.getPiece(x2
,y2
);
12 new PiPo({x:x2
,y:y2
,c:c
,p:p1
}),
13 new PiPo({x:x1
,y:y1
,c:c
,p:p2
})
16 new PiPo({x:x1
,y:y1
,c:c
,p:p1
}),
17 new PiPo({x:x2
,y:y2
,c:c
,p:p2
})
22 // Move completion: promote switched pawns (as in Magnetic)
23 const sizeX
= VariantRules
.size
[0];
24 const lastRank
= (c
== "w" ? 0 : sizeX
-1);
25 const V
= VariantRules
;
27 if (p1
==V
.PAWN
&& x2
==lastRank
) //TODO: also the case p2==V.PAWN and x1==lastRank! see Magnetic chess
29 move.appear
[0].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
[0].p
= piece
;
43 getPotentialMovesFrom([x
,y
])
45 let moves
= super.getPotentialMovesFrom([x
,y
]);
47 const V
= VariantRules
;
48 const color
= this.turn
;
49 const piece
= this.getPiece(x
,y
);
50 const [sizeX
,sizeY
] = V
.size
;
51 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
52 const kp
= this.kingPos
[color
];
53 const oppCol
= this.getOppCol(color
);
54 for (let step
of steps
)
56 let [i
,j
] = [x
+step
[0],y
+step
[1]];
57 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
]!=V
.EMPTY
58 && this.getColor(i
,j
)==color
&& this.getPiece(i
,j
)!=piece
59 // No switching under check (theoretically non-king pieces could, but not)
60 && !this.isAttacked(kp
, [oppCol
]))
62 let switchMove_s
= this.getSwitchMove_s([x
,y
],[i
,j
]);
63 if (switchMove_s
.length
== 1)
64 moves
.push(switchMove_s
[0]);
66 moves
= moves
.concat(switchMove_s
);
72 static get SEARCH_DEPTH() { return 2; } //branching factor is quite high