229c7b35b30512b564104215d43bcb87bf6e38e6
[vchess.git] / client / src / variants / Switching.js
1 import { ChessRules, Move, PiPo } from "@/base_rules";
2
3 export class SwitchingRules extends ChessRules {
4 // Build switch move between squares x1,y1 and x2,y2
5 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);
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 });
19 // Move completion: promote switched pawns (as in Magnetic)
20 const lastRank = (c == "w" ? 0 : V.size.x - 1);
21 let moves = [];
22 if ((p1 == V.PAWN && x2 == lastRank) || (p2 == V.PAWN && x1 == lastRank)) {
23 const idx = (p1 == V.PAWN ? 0 : 1);
24 move.appear[idx].p = V.ROOK;
25 moves.push(move);
26 for (let piece of [V.KNIGHT, V.BISHOP, V.QUEEN]) {
27 let cmove = JSON.parse(JSON.stringify(move));
28 cmove.appear[idx].p = piece;
29 moves.push(cmove);
30 }
31 if (idx == 1) {
32 // Swap moves[i].appear[0] and [1] for moves presentation [TODO...]
33 moves.forEach(m => {
34 let tmp = m.appear[0];
35 m.appear[0] = m.appear[1];
36 m.appear[1] = tmp;
37 });
38 }
39 }
40 else
41 // Other cases
42 moves.push(move);
43 return moves;
44 }
45
46 getPotentialMovesFrom([x,y]) {
47 let moves = super.getPotentialMovesFrom([x,y]);
48 const piece = this.getPiece(x,y);
49 const color = this.turn;
50 const oppCol = V.GetOppCol(color);
51 const kp = this.kingPos[color];
52 // Add switches (if not under check, from anything but the king)
53 if (piece != V.KING && !this.isAttacked(kp, oppCol)) {
54 const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
55 for (let step of steps) {
56 const [i, j] = [x+step[0], y+step[1]];
57 if (
58 V.OnBoard(i, j) &&
59 this.board[i][j] != V.EMPTY &&
60 this.getColor(i,j) == color &&
61 this.getPiece(i,j) != piece
62 ) {
63 const switchMove_s = this.getSwitchMove_s([x,y], [i,j]);
64 Array.prototype.push.apply(moves, switchMove_s);
65 }
66 }
67 }
68 return moves;
69 }
70
71 postPlay(move) {
72 // Did some king move?
73 move.appear.forEach(a => {
74 if (a.p == V.KING) {
75 this.kingPos[a.c] = [a.x, a.y];
76 this.castleFlags[a.c] = [V.size.y, V.size.y];
77 }
78 });
79 for (let coords of [move.start, move.end]) {
80 if (
81 Object.keys(firstRank).includes(coords.x) &&
82 this.castleFlags[firstRank[coords.x]].includes(coords.y)
83 ) {
84 const c = firstRank[coords.x];
85 const flagIdx = (coords.y == this.castleFlags[c][0] ? 0 : 1);
86 this.castleFlags[c][flagIdx] = V.size.y;
87 }
88 }
89 }
90
91 postUndo(move) {
92 // Did some king move?
93 move.vanish.forEach(v => {
94 if (v.p == V.KING) this.kingPos[v.c] = [v.x, v.y];
95 });
96 }
97
98 static get SEARCH_DEPTH() {
99 // Branching factor is quite high
100 return 2;
101 }
102
103 getAllPotentialMoves() {
104 // Since this function is used only for computer play,
105 // remove duplicate switches:
106 return super.getAllPotentialMoves().filter(m => {
107 return (
108 m.appear.length == 1 ||
109 (move.appear[0].p == V.KING && move.appear[1].p == V.ROOK) ||
110 (m.appear[1].x <= m.vanish[1].x && m.appear[1].y <= m.vanish[1].y)
111 );
112 });
113 }
114
115 getNotation(move) {
116 if (move.appear.length == 1)
117 // Normal move
118 return super.getNotation(move);
119 if (move.appear[0].p == V.KING && move.appear[1].p == V.ROOK)
120 // Castle
121 return (move.end.y < move.start.y ? "0-0-0" : "0-0");
122 // Switch
123 return "S" + V.CoordsToSquare(move.start) + V.CoordsToSquare(move.end);
124 }
125 }