Bug fixes
[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 const piece = this.getPiece(x,y);
9 if (piece != V.KNIGHT) {
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) {
23 const lastRank = color == "w" ? 0 : V.size.x - 1;
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 ) {
29 // Potential promotions:
30 const finalPieces = piece == V.PAWN && x + step[0] == lastRank
31 ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
32 : [piece];
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 }
41 }
42 }
43 }
44 }
45
46 return moves;
47 }
48
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
80 static get VALUES() {
81 return {
82 p: 1,
83 r: 5,
84 n: 7, //the knight is valuable
85 b: 3,
86 q: 9,
87 k: 1000
88 };
89 }
90
91 static get SEARCH_DEPTH() {
92 return 2;
93 }
94
95 getNotation(move) {
96 if (move.appear.length == 2 && move.appear[0].p == V.KING)
97 // Castle
98 return move.end.y < move.start.y ? "0-0-0" : "0-0";
99
100 // Translate final and initial square
101 const initSquare = V.CoordsToSquare(move.start);
102 const finalSquare = V.CoordsToSquare(move.end);
103 const piece = this.getPiece(move.start.x, move.start.y);
104
105 // Since pieces and pawns could move like knight, indicate start and end squares
106 let notation =
107 piece.toUpperCase() +
108 initSquare +
109 (move.vanish.length > move.appear.length ? "x" : "") +
110 finalSquare
111
112 if (
113 piece == V.PAWN &&
114 move.appear.length > 0 &&
115 move.appear[0].p != V.PAWN
116 ) {
117 // Promotion
118 notation += "=" + move.appear[0].p.toUpperCase();
119 }
120
121 return notation;
122 }
123 };