Prepare some more variants (unfinished)
[vchess.git] / public / javascripts / variants / Magnetic.js
1 class MagneticRules extends ChessRules
2 {
3 static get HasEnpassant() { return false; }
4
5 getPotentialMovesFrom([x,y])
6 {
7 let standardMoves = super.getPotentialMovesFrom([x,y]);
8 let moves = [];
9 standardMoves.forEach(m => {
10 let newMove_s = this.applyMagneticLaws(m);
11 if (newMove_s.length == 1)
12 moves.push(newMove_s[0]);
13 else //promotion
14 moves = moves.concat(newMove_s);
15 });
16 return moves;
17 }
18
19 // Complete a move with magnetic actions
20 // TODO: job is done multiple times for (normal) promotions.
21 applyMagneticLaws(move)
22 {
23 if (move.appear[0].p == V.KING && move.appear.length==1)
24 return [move]; //kings are not charged
25 const aIdx = (move.appear[0].p != V.KING ? 0 : 1); //if castling, rook is charged
26 const [x,y] = [move.appear[aIdx].x, move.appear[aIdx].y];
27 const color = this.turn;
28 const lastRank = (color=="w" ? 0 : 7);
29 const standardMove = JSON.parse(JSON.stringify(move));
30 this.play(standardMove);
31 for (let step of [[-1,0],[1,0],[0,-1],[0,1]])
32 {
33 let [i,j] = [x+step[0],y+step[1]];
34 while (V.OnBoard(i,j))
35 {
36 if (this.board[i][j] != V.EMPTY)
37 {
38 // Found something. Same color or not?
39 if (this.getColor(i,j) != color)
40 {
41 // Attraction
42 if ((Math.abs(i-x)>=2 || Math.abs(j-y)>=2) && this.getPiece(i,j) != V.KING)
43 {
44 move.vanish.push(
45 new PiPo({
46 p:this.getPiece(i,j),
47 c:this.getColor(i,j),
48 x:i,
49 y:j
50 })
51 );
52 move.appear.push(
53 new PiPo({
54 p:this.getPiece(i,j),
55 c:this.getColor(i,j),
56 x:x+step[0],
57 y:y+step[1]
58 })
59 );
60 }
61 }
62 else
63 {
64 // Repulsion
65 if (this.getPiece(i,j) != V.KING)
66 {
67 // Push it until we meet an obstacle or edge of the board
68 let [ii,jj] = [i+step[0],j+step[1]];
69 while (V.OnBoard(ii,jj))
70 {
71 if (this.board[ii][jj] != V.EMPTY)
72 break;
73 ii += step[0];
74 jj += step[1];
75 }
76 ii -= step[0];
77 jj -= step[1];
78 if (Math.abs(ii-i)>=1 || Math.abs(jj-j)>=1)
79 {
80 move.vanish.push(
81 new PiPo({
82 p:this.getPiece(i,j),
83 c:this.getColor(i,j),
84 x:i,
85 y:j
86 })
87 );
88 move.appear.push(
89 new PiPo({
90 p:this.getPiece(i,j),
91 c:this.getColor(i,j),
92 x:ii,
93 y:jj
94 })
95 );
96 }
97 }
98 }
99 break;
100 }
101 i += step[0];
102 j += step[1];
103 }
104 }
105 this.undo(standardMove);
106 let moves = [];
107 // Scan move for pawn (max 1) on 8th rank
108 for (let i=1; i<move.appear.length; i++)
109 {
110 if (move.appear[i].p==V.PAWN && move.appear[i].c==color
111 && move.appear[i].x==lastRank)
112 {
113 move.appear[i].p = V.ROOK;
114 moves.push(move);
115 for (let piece of [V.KNIGHT, V.BISHOP, V.QUEEN])
116 {
117 let cmove = JSON.parse(JSON.stringify(move));
118 cmove.appear[i].p = piece;
119 moves.push(cmove);
120 }
121 // Swap appear[i] and appear[0] for moves presentation (TODO: this is awkward)
122 moves.forEach(m => {
123 let tmp = m.appear[0];
124 m.appear[0] = m.appear[i];
125 m.appear[i] = tmp;
126 });
127 break;
128 }
129 }
130 if (moves.length == 0) //no pawn on 8th rank
131 moves.push(move);
132 return moves;
133 }
134
135 atLeastOneMove()
136 {
137 if (this.kingPos[this.turn][0] < 0)
138 return false;
139 return true; //TODO: is it right?
140 }
141
142 underCheck(move)
143 {
144 return false; //there is no check
145 }
146
147 getCheckSquares(move)
148 {
149 const c = this.getOppCol(this.turn); //opponent
150 const saveKingPos = this.kingPos[c]; //king might be taken
151 this.play(move);
152 // The only way to be "under check" is to have lost the king (thus game over)
153 let res = this.kingPos[c][0] < 0
154 ? [JSON.parse(JSON.stringify(saveKingPos))]
155 : [];
156 this.undo(move);
157 return res;
158 }
159
160 updateVariables(move)
161 {
162 super.updateVariables(move);
163 const c = this.getColor(move.start.x,move.start.y);
164 if (this.board[move.end.x][move.end.y] != V.EMPTY
165 && c != this.getColor(move.end.x,move.end.y)
166 && this.getPiece(move.end.x,move.end.y) == V.KING)
167 {
168 // We took opponent king !
169 const oppCol = this.getOppCol(c);
170 this.kingPos[oppCol] = [-1,-1];
171 this.castleFlags[oppCol] = [false,false];
172 }
173 // Did we magnetically move our (init) rooks or opponents' ones ?
174 const firstRank = (c == "w" ? 7 : 0);
175 const oppFirstRank = 7 - firstRank;
176 const oppCol = this.getOppCol(c);
177 move.vanish.forEach(psq => {
178 if (psq.x == firstRank && this.INIT_COL_ROOK[c].includes(psq.y))
179 this.castleFlags[c][psq.y==this.INIT_COL_ROOK[c][0] ? 0 : 1] = false;
180 else if (psq.x == oppFirstRank && this.INIT_COL_ROOK[oppCol].includes(psq.y))
181 this.castleFlags[oppCol][psq.y==this.INIT_COL_ROOK[oppCol][0] ? 0 : 1] = false;
182 });
183 }
184
185 unupdateVariables(move)
186 {
187 super.unupdateVariables(move);
188 const c = this.getColor(move.start.x,move.start.y);
189 const oppCol = this.getOppCol(c);
190 if (this.kingPos[oppCol][0] < 0)
191 {
192 // Last move took opponent's king
193 for (let psq of move.vanish)
194 {
195 if (psq.p == 'k')
196 {
197 this.kingPos[oppCol] = [psq.x, psq.y];
198 break;
199 }
200 }
201 }
202 }
203
204 checkGameEnd()
205 {
206 // No valid move: our king disappeared
207 return this.turn == "w" ? "0-1" : "1-0";
208 }
209
210 static get THRESHOLD_MATE()
211 {
212 return 500; //checkmates evals may be slightly below 1000
213 }
214 }
215
216 const VariantRules = MagneticRules;