d5731a63c0d70e4c8a5071575e1ca01c80cbe998
1 class MagneticRules
extends ChessRules
5 return undefined; //no en-passant
8 getPotentialMovesFrom([x
,y
])
10 const 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(moves
, newMove_s
);
21 // Complete a move with magnetic actions
22 applyMagneticLaws(standardMove
)
24 const [x
,y
] = [moves
.start
.x
, move.start
.y
];
25 let move = JSON
.parse(JSON
.stringify(standardMove
));
26 this.play(standardMove
);
27 const color
= this.getColor(x
,y
);
28 const [sizeX
,sizeY
] = VariantRules
.size
;
29 for (let step
of [[-1,0],[1,0],[0,-1],[0,1]])
31 let [i
,j
] = [x
+step
[0],y
+step
[1]];
32 while (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
)
34 if (this.board
[i
][j
] != VariantRules
.EMPTY
)
36 // Found something. Same color or not?
37 if (this.getColor(i
,j
) != color
)
40 if ((Math
.abs(i
-x
)>=2 || Math
.abs(j
-y
)>=2)
41 && this.getPiece(i
,j
) != VariantRules
.KING
)
64 if (this.getPiece(i
,j
) != VariantRules
.KING
)
66 // Push it until we meet an obstacle or edge of the board
67 let [ii
,jj
] = [i
+step
[0],j
+step
[1]];
68 while (ii
>=0 && ii
<sizeX
&& jj
>=0 && jj
<sizeY
)
70 if (this.board
[ii
][jj
] != VariantRules
.EMPTY
)
77 if (Math
.abs(ii
-i
)>=1 || Math
.abs(jj
-j
)>=1)
104 this.undo(standardMove
);
106 if (..condition pawn promote
)
108 move. ... = ... //loop
116 // TODO: verify this assertion
119 return true; //always at least one possible move
124 return false; //there is no check
127 getCheckSquares(move)
129 const c
= this.getOppCol(this.turn
); //opponent
130 const saveKingPos
= this.kingPos
[c
]; //king might be taken
132 // The only way to be "under check" is to have lost the king (thus game over)
133 let res
= this.kingPos
[c
][0] < 0
134 ? [ JSON
.parse(JSON
.stringify(saveKingPos
)) ]
140 updateVariables(move)
142 super.updateVariables(move);
143 const c
= this.getColor(move.start
.x
,move.start
.y
);
144 if (c
!= this.getColor(move.end
.x
,move.end
.y
)
145 && this.board
[move.end
.x
][move.end
.y
] != VariantRules
.EMPTY
146 && this.getPiece(move.end
.x
,move.end
.y
) == VariantRules
.KING
)
148 // We took opponent king !
149 const oppCol
= this.getOppCol(c
);
150 this.kingPos
[oppCol
] = [-1,-1];
151 this.castleFlags
[oppCol
] = [false,false];
155 unupdateVariables(move)
157 super.unupdateVariables(move);
158 const c
= this.getColor(move.start
.x
,move.start
.y
);
159 const oppCol
= this.getOppCol(c
);
160 if (this.kingPos
[oppCol
][0] < 0)
162 // Last move took opponent's king
163 for (let psq
of move.vanish
)
167 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
176 if (this.checkRepetition())
179 const color
= this.turn
;
180 // TODO: do we need "atLeastOneMove()"?
181 if (this.atLeastOneMove() && this.kingPos
[color
][0] >= 0)
184 return this.checkGameEnd();
189 // No valid move: our king disappeared
190 return this.turn
== "w" ? "0-1" : "1-0";