1 class MagneticRules
extends ChessRules
5 return undefined; //no en-passant
8 getPotentialMovesFrom([x
,y
])
10 let standardMoves
= super.getPotentialMovesFrom([x
,y
]);
12 standardMoves
.forEach(m
=> {
13 let newMove_s
= this.applyMagneticLaws(m
);
14 if (newMove_s
.length
== 1)
15 moves
.push(newMove_s
[0]);
17 moves
= moves
.concat(newMove_s
);
22 // Complete a move with magnetic actions
23 // TODO: job is done multiple times for (normal) promotions.
24 applyMagneticLaws(move)
26 const V
= VariantRules
;
27 if (move.appear
[0].p
== V
.KING
&& move.appear
.length
==1)
28 return [move]; //kings are not charged
29 const aIdx
= (move.appear
[0].p
!= V
.KING
? 0 : 1); //if castling, rook is charged
30 const [x
,y
] = [move.appear
[aIdx
].x
, move.appear
[aIdx
].y
];
31 const color
= this.turn
;
32 const lastRank
= (color
=="w" ? 0 : 7);
33 const standardMove
= JSON
.parse(JSON
.stringify(move));
34 this.play(standardMove
);
35 const [sizeX
,sizeY
] = V
.size
;
36 for (let step
of [[-1,0],[1,0],[0,-1],[0,1]])
38 let [i
,j
] = [x
+step
[0],y
+step
[1]];
39 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
)
41 if (this.board
[i
][j
] != V
.EMPTY
)
43 // Found something. Same color or not?
44 if (this.getColor(i
,j
) != color
)
47 if ((Math
.abs(i
-x
)>=2 || Math
.abs(j
-y
)>=2) && this.getPiece(i
,j
) != V
.KING
)
70 if (this.getPiece(i
,j
) != V
.KING
)
72 // Push it until we meet an obstacle or edge of the board
73 let [ii
,jj
] = [i
+step
[0],j
+step
[1]];
74 while (ii
>=0 && ii
<sizeX
&& jj
>=0 && jj
<sizeY
)
76 if (this.board
[ii
][jj
] != V
.EMPTY
)
83 if (Math
.abs(ii
-i
)>=1 || Math
.abs(jj
-j
)>=1)
110 this.undo(standardMove
);
112 // Scan move for pawn (max 1) on 8th rank
113 for (let i
=1; i
<move.appear
.length
; i
++)
115 if (move.appear
[i
].p
==V
.PAWN
&& move.appear
[i
].c
==color
116 && move.appear
[i
].x
==lastRank
)
118 move.appear
[i
].p
= V
.ROOK
;
120 for (let piece
of [V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
])
122 let cmove
= JSON
.parse(JSON
.stringify(move));
123 cmove
.appear
[i
].p
= piece
;
126 // Swap appear[i] and appear[0] for moves presentation (TODO: this is awkward)
128 let tmp
= m
.appear
[0];
129 m
.appear
[0] = m
.appear
[i
];
135 if (moves
.length
== 0) //no pawn on 8th rank
140 // TODO: verify this assertion
143 return true; //always at least one possible move
148 return false; //there is no check
151 getCheckSquares(move)
153 const c
= this.getOppCol(this.turn
); //opponent
154 const saveKingPos
= this.kingPos
[c
]; //king might be taken
156 // The only way to be "under check" is to have lost the king (thus game over)
157 let res
= this.kingPos
[c
][0] < 0
158 ? [ JSON
.parse(JSON
.stringify(saveKingPos
)) ]
164 updateVariables(move)
166 super.updateVariables(move);
167 const c
= this.getColor(move.start
.x
,move.start
.y
);
168 if (c
!= this.getColor(move.end
.x
,move.end
.y
)
169 && this.board
[move.end
.x
][move.end
.y
] != VariantRules
.EMPTY
170 && this.getPiece(move.end
.x
,move.end
.y
) == VariantRules
.KING
)
172 // We took opponent king !
173 const oppCol
= this.getOppCol(c
);
174 this.kingPos
[oppCol
] = [-1,-1];
175 this.castleFlags
[oppCol
] = [false,false];
177 // Did we magnetically move our (init) rooks or opponents' ones ?
178 const firstRank
= (c
== "w" ? 7 : 0);
179 const oppFirstRank
= 7 - firstRank
;
180 const oppCol
= this.getOppCol(c
);
181 move.vanish
.forEach(psq
=> {
182 if (psq
.x
== firstRank
&& this.INIT_COL_ROOK
[c
].includes(psq
.y
))
183 this.castleFlags
[c
][psq
.y
==this.INIT_COL_ROOK
[c
][0] ? 0 : 1] = false;
184 else if (psq
.x
== oppFirstRank
&& this.INIT_COL_ROOK
[oppCol
].includes(psq
.y
))
185 this.castleFlags
[oppCol
][psq
.y
==this.INIT_COL_ROOK
[oppCol
][0] ? 0 : 1] = false;
189 unupdateVariables(move)
191 super.unupdateVariables(move);
192 const c
= this.getColor(move.start
.x
,move.start
.y
);
193 const oppCol
= this.getOppCol(c
);
194 if (this.kingPos
[oppCol
][0] < 0)
196 // Last move took opponent's king
197 for (let psq
of move.vanish
)
201 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
210 if (this.checkRepetition())
213 const color
= this.turn
;
214 // TODO: do we need "atLeastOneMove()"?
215 if (this.atLeastOneMove() && this.kingPos
[color
][0] >= 0)
218 return this.checkGameEnd();
223 // No valid move: our king disappeared
224 return this.turn
== "w" ? "0-1" : "1-0";
227 static get THRESHOLD_MATE() {
228 return 500; //checkmates evals may be slightly below 1000