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)
152 updateVariables(move)
154 super.updateVariables(move);
155 const c
= move.vanish
[0].c
;
156 if (move.vanish
.length
>= 2 && move.vanish
[1].p
== V
.KING
)
158 // We took opponent king !
159 const oppCol
= this.getOppCol(c
);
160 this.kingPos
[oppCol
] = [-1,-1];
161 this.castleFlags
[oppCol
] = [false,false];
163 // Did we magnetically move our (init) rooks or opponents' ones ?
164 const firstRank
= (c
== "w" ? 7 : 0);
165 const oppFirstRank
= 7 - firstRank
;
166 const oppCol
= this.getOppCol(c
);
167 move.vanish
.forEach(psq
=> {
168 if (psq
.x
== firstRank
&& this.INIT_COL_ROOK
[c
].includes(psq
.y
))
169 this.castleFlags
[c
][psq
.y
==this.INIT_COL_ROOK
[c
][0] ? 0 : 1] = false;
170 else if (psq
.x
== oppFirstRank
&& this.INIT_COL_ROOK
[oppCol
].includes(psq
.y
))
171 this.castleFlags
[oppCol
][psq
.y
==this.INIT_COL_ROOK
[oppCol
][0] ? 0 : 1] = false;
175 unupdateVariables(move)
177 super.unupdateVariables(move);
178 const c
= move.vanish
[0].c
;
179 const oppCol
= this.getOppCol(c
);
180 if (this.kingPos
[oppCol
][0] < 0)
182 // Last move took opponent's king
183 for (let psq
of move.vanish
)
187 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
196 // No valid move: our king disappeared
197 return this.turn
== "w" ? "0-1" : "1-0";
200 static get THRESHOLD_MATE()
202 return 500; //checkmates evals may be slightly below 1000
206 const VariantRules
= MagneticRules
;