1 class MagneticRules
extends ChessRules
3 static get HasEnpassant() { return false; }
5 getPotentialMovesFrom([x
,y
])
7 let standardMoves
= super.getPotentialMovesFrom([x
,y
]);
9 standardMoves
.forEach(m
=> {
10 let newMove_s
= this.applyMagneticLaws(m
);
11 if (newMove_s
.length
== 1)
12 moves
.push(newMove_s
[0]);
14 moves
= moves
.concat(newMove_s
);
19 // Complete a move with magnetic actions
20 // TODO: job is done multiple times for (normal) promotions.
21 applyMagneticLaws(move)
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]])
33 let [i
,j
] = [x
+step
[0],y
+step
[1]];
34 while (V
.OnBoard(i
,j
))
36 if (this.board
[i
][j
] != V
.EMPTY
)
38 // Found something. Same color or not?
39 if (this.getColor(i
,j
) != color
)
42 if ((Math
.abs(i
-x
)>=2 || Math
.abs(j
-y
)>=2) && this.getPiece(i
,j
) != V
.KING
)
65 if (this.getPiece(i
,j
) != V
.KING
)
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
))
71 if (this.board
[ii
][jj
] != V
.EMPTY
)
78 if (Math
.abs(ii
-i
)>=1 || Math
.abs(jj
-j
)>=1)
105 this.undo(standardMove
);
107 // Scan move for pawn (max 1) on 8th rank
108 for (let i
=1; i
<move.appear
.length
; i
++)
110 if (move.appear
[i
].p
==V
.PAWN
&& move.appear
[i
].c
==color
111 && move.appear
[i
].x
==lastRank
)
113 move.appear
[i
].p
= V
.ROOK
;
115 for (let piece
of [V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
])
117 let cmove
= JSON
.parse(JSON
.stringify(move));
118 cmove
.appear
[i
].p
= piece
;
121 // Swap appear[i] and appear[0] for moves presentation (TODO: this is awkward)
123 let tmp
= m
.appear
[0];
124 m
.appear
[0] = m
.appear
[i
];
130 if (moves
.length
== 0) //no pawn on 8th rank
137 if (this.kingPos
[this.turn
][0] < 0)
139 return true; //TODO: is it right?
144 return false; //there is no check
147 getCheckSquares(move)
149 const c
= this.getOppCol(this.turn
); //opponent
150 const saveKingPos
= this.kingPos
[c
]; //king might be taken
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
))]
160 updateVariables(move)
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
)
168 // We took opponent king !
169 const oppCol
= this.getOppCol(c
);
170 this.kingPos
[oppCol
] = [-1,-1];
171 this.castleFlags
[oppCol
] = [false,false];
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;
185 unupdateVariables(move)
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)
192 // Last move took opponent's king
193 for (let psq
of move.vanish
)
197 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
206 // No valid move: our king disappeared
207 return this.turn
== "w" ? "0-1" : "1-0";
210 static get THRESHOLD_MATE()
212 return 500; //checkmates evals may be slightly below 1000
216 const VariantRules
= MagneticRules
;