Better pieces values in Knghtrelay variant
[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 : [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 }
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 getNotation(move) {
92 if (move.appear.length == 2 && move.appear[0].p == V.KING)
93 // Castle
94 return move.end.y < move.start.y ? "0-0-0" : "0-0";
95
96 // Translate final and initial square
97 const initSquare = V.CoordsToSquare(move.start);
98 const finalSquare = V.CoordsToSquare(move.end);
99 const piece = this.getPiece(move.start.x, move.start.y);
100
101 // Since pieces and pawns could move like knight, indicate start and end squares
102 let notation =
103 piece.toUpperCase() +
104 initSquare +
105 (move.vanish.length > move.appear.length ? "x" : "") +
106 finalSquare
107
108 if (
109 piece == V.PAWN &&
110 move.appear.length > 0 &&
111 move.appear[0].p != V.PAWN
112 ) {
113 // Promotion
114 notation += "=" + move.appear[0].p.toUpperCase();
115 }
116
117 return notation;
118 }
119 };