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