TODO: debug Knightrelay, use generators + yield for moves generation
[vchess.git] / client / src / variants / Knightrelay.js
1 import { ChessRules } from "@/base_rules";
2
3 export const VariantRules = class KnightrelayRules extends ChessRules {
4 getPotentialMovesFrom([x, y]) {
5 let moves = super.getPotentialMovesFrom([x, y]);
6
7 // Expand possible moves if guarded by a knight:
8 if (this.getPiece(x,y) != V.KNIGHT) {
9 const color = this.turn;
10 let guardedByKnight = false;
11 for (const step of V.steps[V.KNIGHT]) {
12 if (
13 V.OnBoard(x+step[0],y+step[1]) &&
14 this.getPiece(x+step[0],y+step[1]) == V.KNIGHT &&
15 this.getColor(x+step[0],y+step[1]) == color
16 ) {
17 guardedByKnight = true;
18 break;
19 }
20 }
21 if (guardedByKnight) {
22 for (const step of V.steps[V.KNIGHT]) {
23 if (
24 V.OnBoard(x+step[0],y+step[1]) &&
25 this.getColor(x+step[0],y+step[1]) != color
26 ) {
27 let m = this.getBasicMove([x,y], [x+step[0],y+step[1]]);
28 if (!m.appear[0].c || !m.vanish[0].c)
29 debugger;
30 moves.push(m);
31 //moves.push(this.getBasicMove([x,y], [x+step[0],y+step[1]]));
32 }
33 }
34 }
35 }
36
37 return moves;
38 }
39
40 getNotation(move) {
41 if (move.appear.length == 2 && move.appear[0].p == V.KING)
42 // Castle
43 return move.end.y < move.start.y ? "0-0-0" : "0-0";
44
45 // Translate final and initial square
46 const initSquare = V.CoordsToSquare(move.start);
47 const finalSquare = V.CoordsToSquare(move.end);
48 const piece = this.getPiece(move.start.x, move.start.y);
49
50 // Since pieces and pawns could move like knight, indicate start and end squares
51 let notation =
52 piece.toUpperCase() +
53 initSquare +
54 (move.vanish.length > move.appear.length ? "x" : "") +
55 finalSquare
56
57 if (
58 piece == V.PAWN &&
59 move.appear.length > 0 &&
60 move.appear[0].p != V.PAWN
61 ) {
62 // Promotion
63 notation += "=" + move.appear[0].p.toUpperCase();
64 }
65
66 return notation;
67 }
68 };