2d576e1b5eaad331fe668293d9d72cf9cbdafaae
1 class MagneticRules
extends ChessRules
5 return undefined; //no en-passant
8 // Complete a move with magnetic actions
9 applyMagneticLaws([x
,y
], move)
11 const standardMove
= JSON
.parse(JSON
.stringify(move));
12 this.play(standardMove
);
13 const color
= this.getColor(x
,y
);
14 const [sizeX
,sizeY
] = VariantRules
.size
;
15 for (let step
of [[-1,0],[1,0],[0,-1],[0,1]])
17 let [i
,j
] = [x
+step
[0],y
+step
[1]];
18 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
)
20 if (this.board
[i
][j
] != VariantRules
.EMPTY
)
22 // Found something. Same color or not?
23 if (this.getColor(i
,j
) != color
)
26 if ((Math
.abs(i
-x
)>=2 || Math
.abs(j
-y
)>=2)
27 && this.getPiece(i
,j
) != VariantRules
.KING
)
50 if (this.getPiece(i
,j
) != VariantRules
.KING
)
52 // Push it until we meet an obstacle or edge of the board
53 let [ii
,jj
] = [i
+step
[0],j
+step
[1]];
54 while (ii
>=0 && ii
<sizeX
&& jj
>=0 && jj
<sizeY
)
56 if (this.board
[ii
][jj
] != VariantRules
.EMPTY
)
63 if (Math
.abs(ii
-i
)>=1 || Math
.abs(jj
-j
)>=1)
90 this.undo(standardMove
);
93 getBasicMove([sx
,sy
], [ex
,ey
], tr
)
100 c: !!tr
? tr
.c : this.getColor(sx
,sy
),
101 p: !!tr
? tr
.p : this.getPiece(sx
,sy
)
108 c: this.getColor(sx
,sy
),
109 p: this.getPiece(sx
,sy
)
114 if (this.board
[ex
][ey
] != VariantRules
.EMPTY
)
120 c: this.getColor(ex
,ey
),
121 p: this.getPiece(ex
,ey
)
125 this.applyMagneticLaws([ex
,ey
], mv
);
129 getPotentialPawnMoves([x
,y
])
131 const color
= this.getColor(x
,y
);
133 var V
= VariantRules
;
134 const [sizeX
,sizeY
] = VariantRules
.size
;
135 let shift
= (color
== "w" ? -1 : 1);
136 let startRank
= (color
== "w" ? sizeY
-2 : 1);
137 let firstRank
= (color
== 'w' ? sizeY
-1 : 0);
138 let lastRank
= (color
== "w" ? 0 : sizeY
-1);
140 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRank
)
143 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
145 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
146 if ([startRank
,firstRank
].includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
149 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
153 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1]) && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
154 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1]));
155 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1]) && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
156 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1]));
159 if (x
+shift
== lastRank
)
162 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
163 promotionPieces
.forEach(p
=> {
165 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
166 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:p
}));
168 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1]) && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
169 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:p
}));
170 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1]) && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
171 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:p
}));
180 getCastleMoves([x
,y
])
182 const c
= this.getColor(x
,y
);
183 if (x
!= (c
=="w" ? 7 : 0) || y
!= this.INIT_COL_KING
[c
])
184 return []; //x isn't first rank, or king has moved (shortcut)
186 const V
= VariantRules
;
189 const oppCol
= this.getOppCol(c
);
192 const finalSquares
= [ [2,3], [6,5] ]; //king, then rook
194 for (let castleSide
=0; castleSide
< 2; castleSide
++) //large, then small
196 if (!this.flags
[c
][castleSide
])
198 // If this code is reached, rooks and king are on initial position
200 // Nothing on the path of the king (and no checks; OK also if y==finalSquare)?
201 let step
= finalSquares
[castleSide
][0] < y
? -1 : 1;
202 for (i
=y
; i
!=finalSquares
[castleSide
][0]; i
+=step
)
204 if (this.isAttacked([x
,i
], oppCol
) || (this.board
[x
][i
] != V
.EMPTY
&&
205 // NOTE: next check is enough, because of chessboard constraints
206 (this.getColor(x
,i
) != c
|| ![V
.KING
,V
.ROOK
].includes(this.getPiece(x
,i
)))))
208 continue castlingCheck
;
212 // Nothing on the path to the rook?
213 step
= castleSide
== 0 ? -1 : 1;
214 for (i
= y
+ step
; i
!= this.INIT_COL_ROOK
[c
][castleSide
]; i
+= step
)
216 if (this.board
[x
][i
] != V
.EMPTY
)
217 continue castlingCheck
;
219 const rookPos
= this.INIT_COL_ROOK
[c
][castleSide
];
221 // Nothing on final squares, except maybe king and castling rook?
224 if (this.board
[x
][finalSquares
[castleSide
][i
]] != V
.EMPTY
&&
225 this.getPiece(x
,finalSquares
[castleSide
][i
]) != V
.KING
&&
226 finalSquares
[castleSide
][i
] != rookPos
)
228 continue castlingCheck
;
232 // If this code is reached, castle is valid
233 let cmove
= new Move({
235 new PiPo({x:x
,y:finalSquares
[castleSide
][0],p:V
.KING
,c:c
}),
236 new PiPo({x:x
,y:finalSquares
[castleSide
][1],p:V
.ROOK
,c:c
})],
238 new PiPo({x:x
,y:y
,p:V
.KING
,c:c
}),
239 new PiPo({x:x
,y:rookPos
,p:V
.ROOK
,c:c
})],
240 end: Math
.abs(y
- rookPos
) <= 2
242 : {x:x
, y:y
+ 2 * (castleSide
==0 ? -1 : 1)}
244 this.applyMagneticLaws([x
,finalSquares
[castleSide
][1]], cmove
);
251 // TODO: verify this assertion
254 // return true; //always at least one possible move
259 return false; //there is no check
262 getCheckSquares(move)
264 const c
= this.getOppCol(this.turn
); //opponent
265 const saveKingPos
= this.kingPos
[c
]; //king might be taken
267 // The only way to be "under check" is to have lost the king (thus game over)
268 let res
= this.kingPos
[c
][0] < 0
269 ? [ JSON
.parse(JSON
.stringify(saveKingPos
)) ]
275 updateVariables(move)
277 super.updateVariables(move);
278 const c
= this.getColor(move.start
.x
,move.start
.y
);
279 if (c
!= this.getColor(move.end
.x
,move.end
.y
)
280 && this.board
[move.end
.x
][move.end
.y
] != VariantRules
.EMPTY
281 && this.getPiece(move.end
.x
,move.end
.y
) == VariantRules
.KING
)
283 // We took opponent king !
284 const oppCol
= this.getOppCol(c
);
285 this.kingPos
[oppCol
] = [-1,-1];
286 this.flags
[oppCol
] = [false,false];
290 unupdateVariables(move)
292 super.unupdateVariables(move);
293 const c
= this.getColor(move.start
.x
,move.start
.y
);
294 const oppCol
= this.getOppCol(c
);
295 if (this.kingPos
[oppCol
][0] < 0)
297 // Last move took opponent's king
298 for (let psq
of move.vanish
)
302 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
311 if (this.checkRepetition())
314 const color
= this.turn
;
315 // TODO: do we need "atLeastOneMove()"?
316 if (this.atLeastOneMove() && this.kingPos
[color
][0] >= 0)
319 return this.checkGameEnd();
324 // No valid move: our king disappeared
325 return this.turn
== "w" ? "0-1" : "1-0";