Add TODOs
[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);
9 let move = new Move({
10 appear: [
11 new PiPo({x:x2,y:y2,c:c,p:p1}),
12 new PiPo({x:x1,y:y1,c:c,p:p2})
13 ],
14 vanish: [
15 new PiPo({x:x1,y:y1,c:c,p:p1}),
16 new PiPo({x:x2,y:y2,c:c,p:p2})
17 ],
18 start: {x:x1,y:y1},
19 end: {x:x2,y:y2}
20 });
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;
25 let moves = [];
9f18af3b 26 if ((p1==V.PAWN && x2==lastRank) || (p2==V.PAWN && x1==lastRank))
1221ac47 27 {
9f18af3b
BA
28 const idx = (p1==V.PAWN ? 0 : 1);
29 move.appear[idx].p = V.ROOK;
1221ac47
BA
30 moves.push(move);
31 for (let piece of [V.KNIGHT, V.BISHOP, V.QUEEN])
32 {
33 let cmove = JSON.parse(JSON.stringify(move));
9f18af3b 34 cmove.appear[idx].p = piece;
1221ac47
BA
35 moves.push(cmove);
36 }
9f18af3b
BA
37 if (idx == 1)
38 {
39 // Swap moves[i].appear[0] and [1] for moves presentation [TODO...]
40 moves.forEach(m => {
41 let tmp = m.appear[0];
42 m.appear[0] = m.appear[1];
43 m.appear[1] = tmp;
44 });
45 }
1221ac47
BA
46 }
47 else //other cases
48 moves.push(move);
49 return moves;
50 }
a6abf094 51
1221ac47
BA
52 getPotentialMovesFrom([x,y])
53 {
54 let moves = super.getPotentialMovesFrom([x,y]);
55 // Add switches:
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)
64 {
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]))
70 {
71 let switchMove_s = this.getSwitchMove_s([x,y],[i,j]);
72 if (switchMove_s.length == 1)
73 moves.push(switchMove_s[0]);
74 else //promotion
75 moves = moves.concat(switchMove_s);
76 }
77 }
78 return moves;
79 }
80
9f18af3b
BA
81 updateVariables(move)
82 {
83 super.updateVariables(move);
84 if (move.appear.length == 2 && move.vanish.length == 2
85 && move.appear[1].p == VariantRules.KING)
86 {
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];
90 }
91 }
92
93 unupdateVariables(move)
94 {
95 super.unupdateVariables(move);
96 if (move.appear.length == 2 && move.vanish.length == 2
97 && move.appear[1].p == VariantRules.KING)
98 {
99 const color = this.getColor(move.start.x, move.start.y);
100 this.kingPos[color] = [move.appear[0].x, move.appear[0].y];
101 }
102 }
103
1221ac47 104 static get SEARCH_DEPTH() { return 2; } //branching factor is quite high
a6abf094 105}