c4995dd3003d9fd0ac36f7780374e455e189703d
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 getPotentialPawnMoves([x
,y
])
21 const color
= this.turn
;
23 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
24 const shift
= (color
== "w" ? -1 : 1);
25 const firstRank
= (color
== 'w' ? sizeX
-1 : 0);
26 const startRank
= (color
== "w" ? sizeX
-2 : 1);
27 const lastRank
= (color
== "w" ? 0 : sizeX
-1);
29 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRank
)
32 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
34 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
35 // Next condition because variants with pawns on 1st rank allow them to jump
36 if ([startRank
,firstRank
].includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
39 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
43 if (y
>0 && this.board
[x
+shift
][y
-1] != V
.EMPTY
44 && this.canTake([x
,y
], [x
+shift
,y
-1]))
46 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1]));
48 if (y
<sizeY
-1 && this.board
[x
+shift
][y
+1] != V
.EMPTY
49 && this.canTake([x
,y
], [x
+shift
,y
+1]))
51 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1]));
55 if (x
+shift
== lastRank
)
58 const pawnColor
= this.getColor(x
,y
); //can be different for checkered
59 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
60 promotionPieces
.forEach(p
=> {
62 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
63 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:pawnColor
,p:p
}));
65 if (y
>0 && this.board
[x
+shift
][y
-1] != V
.EMPTY
66 && this.canTake([x
,y
], [x
+shift
,y
-1]))
68 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:pawnColor
,p:p
}));
70 if (y
<sizeY
-1 && this.board
[x
+shift
][y
+1] != V
.EMPTY
71 && this.canTake([x
,y
], [x
+shift
,y
+1]))
73 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:pawnColor
,p:p
}));
77 return moves
; //no en-passant
80 // Complete a move with magnetic actions
81 // TODO: job is done multiple times for (normal) promotions.
82 applyMagneticLaws(move)
84 if (move.appear
[0].p
== V
.KING
&& move.appear
.length
==1)
85 return [move]; //kings are not charged
86 const aIdx
= (move.appear
[0].p
!= V
.KING
? 0 : 1); //if castling, rook is charged
87 const [x
,y
] = [move.appear
[aIdx
].x
, move.appear
[aIdx
].y
];
88 const color
= this.turn
;
89 const lastRank
= (color
=="w" ? 0 : 7);
90 const standardMove
= JSON
.parse(JSON
.stringify(move));
91 this.play(standardMove
);
92 for (let step
of [[-1,0],[1,0],[0,-1],[0,1]])
94 let [i
,j
] = [x
+step
[0],y
+step
[1]];
95 while (V
.OnBoard(i
,j
))
97 if (this.board
[i
][j
] != V
.EMPTY
)
99 // Found something. Same color or not?
100 if (this.getColor(i
,j
) != color
)
103 if ((Math
.abs(i
-x
)>=2 || Math
.abs(j
-y
)>=2) && this.getPiece(i
,j
) != V
.KING
)
107 p:this.getPiece(i
,j
),
108 c:this.getColor(i
,j
),
115 p:this.getPiece(i
,j
),
116 c:this.getColor(i
,j
),
126 if (this.getPiece(i
,j
) != V
.KING
)
128 // Push it until we meet an obstacle or edge of the board
129 let [ii
,jj
] = [i
+step
[0],j
+step
[1]];
130 while (V
.OnBoard(ii
,jj
))
132 if (this.board
[ii
][jj
] != V
.EMPTY
)
139 if (Math
.abs(ii
-i
)>=1 || Math
.abs(jj
-j
)>=1)
143 p:this.getPiece(i
,j
),
144 c:this.getColor(i
,j
),
151 p:this.getPiece(i
,j
),
152 c:this.getColor(i
,j
),
166 this.undo(standardMove
);
168 // Scan move for pawn (max 1) on 8th rank
169 for (let i
=1; i
<move.appear
.length
; i
++)
171 if (move.appear
[i
].p
==V
.PAWN
&& move.appear
[i
].c
==color
172 && move.appear
[i
].x
==lastRank
)
174 move.appear
[i
].p
= V
.ROOK
;
176 for (let piece
of [V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
])
178 let cmove
= JSON
.parse(JSON
.stringify(move));
179 cmove
.appear
[i
].p
= piece
;
182 // Swap appear[i] and appear[0] for moves presentation (TODO: this is awkward)
184 let tmp
= m
.appear
[0];
185 m
.appear
[0] = m
.appear
[i
];
191 if (moves
.length
== 0) //no pawn on 8th rank
198 if (this.kingPos
[this.turn
][0] < 0)
200 return true; //TODO: is it right?
205 return false; //there is no check
208 getCheckSquares(move)
210 const c
= this.getOppCol(this.turn
); //opponent
211 const saveKingPos
= this.kingPos
[c
]; //king might be taken
213 // The only way to be "under check" is to have lost the king (thus game over)
214 let res
= this.kingPos
[c
][0] < 0
215 ? [JSON
.parse(JSON
.stringify(saveKingPos
))]
221 updateVariables(move)
223 super.updateVariables(move);
224 const c
= this.getColor(move.start
.x
,move.start
.y
);
225 if (this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
226 && c
!= this.getColor(move.end
.x
,move.end
.y
)
227 && this.getPiece(move.end
.x
,move.end
.y
) == V
.KING
)
229 // We took opponent king !
230 const oppCol
= this.getOppCol(c
);
231 this.kingPos
[oppCol
] = [-1,-1];
232 this.castleFlags
[oppCol
] = [false,false];
234 // Did we magnetically move our (init) rooks or opponents' ones ?
235 const firstRank
= (c
== "w" ? 7 : 0);
236 const oppFirstRank
= 7 - firstRank
;
237 const oppCol
= this.getOppCol(c
);
238 move.vanish
.forEach(psq
=> {
239 if (psq
.x
== firstRank
&& this.INIT_COL_ROOK
[c
].includes(psq
.y
))
240 this.castleFlags
[c
][psq
.y
==this.INIT_COL_ROOK
[c
][0] ? 0 : 1] = false;
241 else if (psq
.x
== oppFirstRank
&& this.INIT_COL_ROOK
[oppCol
].includes(psq
.y
))
242 this.castleFlags
[oppCol
][psq
.y
==this.INIT_COL_ROOK
[oppCol
][0] ? 0 : 1] = false;
246 unupdateVariables(move)
248 super.unupdateVariables(move);
249 const c
= this.getColor(move.start
.x
,move.start
.y
);
250 const oppCol
= this.getOppCol(c
);
251 if (this.kingPos
[oppCol
][0] < 0)
253 // Last move took opponent's king
254 for (let psq
of move.vanish
)
258 this.kingPos
[oppCol
] = [psq
.x
, psq
.y
];
267 // No valid move: our king disappeared
268 return this.turn
== "w" ? "0-1" : "1-0";
271 static get THRESHOLD_MATE()
273 return 500; //checkmates evals may be slightly below 1000
277 const VariantRules
= MagneticRules
;