Fix rand init pos for Antiking (no cornered bishops)
[vchess.git] / public / javascripts / variants / Switching.js
CommitLineData
a6abf094
BA
1class SwitchingRules extends ChessRules
2{
1221ac47
BA
3 // Build switch move between squares x1,y1 and x2,y2
4 getSwitchMove_s([x1,y1],[x2,y2])
5 {
1221ac47
BA
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);
6752407b
BA
9 const V = VariantRules;
10 if (p1 == V.KING && p2 == V.ROOK)
11 return []; //avoid duplicate moves (potential conflict with castle)
1221ac47
BA
12 let move = new Move({
13 appear: [
14 new PiPo({x:x2,y:y2,c:c,p:p1}),
15 new PiPo({x:x1,y:y1,c:c,p:p2})
16 ],
17 vanish: [
18 new PiPo({x:x1,y:y1,c:c,p:p1}),
19 new PiPo({x:x2,y:y2,c:c,p:p2})
20 ],
21 start: {x:x1,y:y1},
22 end: {x:x2,y:y2}
23 });
24 // Move completion: promote switched pawns (as in Magnetic)
25 const sizeX = VariantRules.size[0];
26 const lastRank = (c == "w" ? 0 : sizeX-1);
1221ac47 27 let moves = [];
9f18af3b 28 if ((p1==V.PAWN && x2==lastRank) || (p2==V.PAWN && x1==lastRank))
1221ac47 29 {
9f18af3b
BA
30 const idx = (p1==V.PAWN ? 0 : 1);
31 move.appear[idx].p = V.ROOK;
1221ac47
BA
32 moves.push(move);
33 for (let piece of [V.KNIGHT, V.BISHOP, V.QUEEN])
34 {
35 let cmove = JSON.parse(JSON.stringify(move));
9f18af3b 36 cmove.appear[idx].p = piece;
1221ac47
BA
37 moves.push(cmove);
38 }
9f18af3b
BA
39 if (idx == 1)
40 {
41 // Swap moves[i].appear[0] and [1] for moves presentation [TODO...]
42 moves.forEach(m => {
43 let tmp = m.appear[0];
44 m.appear[0] = m.appear[1];
45 m.appear[1] = tmp;
46 });
47 }
1221ac47
BA
48 }
49 else //other cases
50 moves.push(move);
51 return moves;
52 }
a6abf094 53
1221ac47
BA
54 getPotentialMovesFrom([x,y])
55 {
56 let moves = super.getPotentialMovesFrom([x,y]);
57 // Add switches:
58 const V = VariantRules;
59 const color = this.turn;
60 const piece = this.getPiece(x,y);
61 const [sizeX,sizeY] = V.size;
62 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
63 const kp = this.kingPos[color];
64 const oppCol = this.getOppCol(color);
65 for (let step of steps)
66 {
67 let [i,j] = [x+step[0],y+step[1]];
68 if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j]!=V.EMPTY
69 && this.getColor(i,j)==color && this.getPiece(i,j)!=piece
70 // No switching under check (theoretically non-king pieces could, but not)
71 && !this.isAttacked(kp, [oppCol]))
72 {
73 let switchMove_s = this.getSwitchMove_s([x,y],[i,j]);
74 if (switchMove_s.length == 1)
75 moves.push(switchMove_s[0]);
76 else //promotion
77 moves = moves.concat(switchMove_s);
78 }
79 }
80 return moves;
81 }
82
9f18af3b
BA
83 updateVariables(move)
84 {
85 super.updateVariables(move);
86 if (move.appear.length == 2 && move.vanish.length == 2
87 && move.appear[1].p == VariantRules.KING)
88 {
89 // Switch with the king; not castle, and not handled by main class
90 const color = this.getColor(move.start.x, move.start.y);
91 this.kingPos[color] = [move.appear[1].x, move.appear[1].y];
92 }
93 }
94
95 unupdateVariables(move)
96 {
97 super.unupdateVariables(move);
98 if (move.appear.length == 2 && move.vanish.length == 2
99 && move.appear[1].p == VariantRules.KING)
100 {
101 const color = this.getColor(move.start.x, move.start.y);
102 this.kingPos[color] = [move.appear[0].x, move.appear[0].y];
103 }
104 }
105
1221ac47 106 static get SEARCH_DEPTH() { return 2; } //branching factor is quite high
a6abf094 107}