Do not show eog modal if navigating and not analyzing
[vchess.git] / client / src / variants / Knightrelay.js
CommitLineData
c322a844
BA
1import { ChessRules } from "@/base_rules";
2
3export 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:
8055eabd
BA
8 const piece = this.getPiece(x,y);
9 if (piece != V.KNIGHT) {
c322a844
BA
10 const color = this.turn;
11 let guardedByKnight = false;
12 for (const step of V.steps[V.KNIGHT]) {
13 if (
14 V.OnBoard(x+step[0],y+step[1]) &&
15 this.getPiece(x+step[0],y+step[1]) == V.KNIGHT &&
16 this.getColor(x+step[0],y+step[1]) == color
17 ) {
18 guardedByKnight = true;
19 break;
20 }
21 }
22 if (guardedByKnight) {
8055eabd 23 const lastRank = color == "w" ? 0 : V.size.x - 1;
c322a844
BA
24 for (const step of V.steps[V.KNIGHT]) {
25 if (
26 V.OnBoard(x+step[0],y+step[1]) &&
27 this.getColor(x+step[0],y+step[1]) != color
28 ) {
8055eabd
BA
29 // Potential promotions:
30 const finalPieces = piece == V.PAWN && x + step[0] == lastRank
31 ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
32 : [V.PAWN];
33 for (let p of finalPieces) {
34 moves.push(
35 this.getBasicMove([x,y], [x+step[0],y+step[1]], {
36 c: color,
37 p: p
38 })
39 );
40 }
c322a844
BA
41 }
42 }
43 }
44 }
45
46 return moves;
47 }
48
1c5bfdf2
BA
49 isAttacked(sq, colors) {
50 if (super.isAttacked(sq, colors))
51 return true;
52
53 // Check if a (non-knight) piece at knight distance
54 // is guarded by a knight (and thus attacking)
55 const x = sq[0],
56 y = sq[1];
57 for (const step of V.steps[V.KNIGHT]) {
58 if (
59 V.OnBoard(x+step[0],y+step[1]) &&
60 colors.includes(this.getColor(x+step[0],y+step[1])) &&
61 this.getPiece(x+step[0],y+step[1]) != V.KNIGHT
62 ) {
63 for (const step2 of V.steps[V.KNIGHT]) {
64 const xx = x+step[0]+step2[0],
65 yy = y+step[1]+step2[1];
66 if (
67 V.OnBoard(xx,yy) &&
68 colors.includes(this.getColor(xx,yy)) &&
69 this.getPiece(xx,yy) == V.KNIGHT
70 ) {
71 return true;
72 }
73 }
74 }
75 }
76
77 return false;
78 }
79
c322a844
BA
80 getNotation(move) {
81 if (move.appear.length == 2 && move.appear[0].p == V.KING)
82 // Castle
83 return move.end.y < move.start.y ? "0-0-0" : "0-0";
84
85 // Translate final and initial square
86 const initSquare = V.CoordsToSquare(move.start);
87 const finalSquare = V.CoordsToSquare(move.end);
88 const piece = this.getPiece(move.start.x, move.start.y);
89
90 // Since pieces and pawns could move like knight, indicate start and end squares
91 let notation =
92 piece.toUpperCase() +
93 initSquare +
94 (move.vanish.length > move.appear.length ? "x" : "") +
95 finalSquare
96
97 if (
98 piece == V.PAWN &&
99 move.appear.length > 0 &&
100 move.appear[0].p != V.PAWN
101 ) {
102 // Promotion
103 notation += "=" + move.appear[0].p.toUpperCase();
104 }
105
106 return notation;
107 }
108};