Fix games ordering in MyGames, fix en-passant mistake in Rifle variant
[vchess.git] / client / src / variants / Rifle.js
1 import { ChessRules, PiPo, Move } from "@/base_rules";
2
3 export const VariantRules = class RifleRules extends ChessRules {
4 getEpSquare(moveOrSquare) {
5 if (typeof moveOrSquare !== "object" || move.appear.length > 0)
6 return super.getEpSquare(moveOrSquare);
7 // Capturing move: no en-passant
8 return undefined;
9 }
10
11 getBasicMove([sx, sy], [ex, ey], tr) {
12 let mv = new Move({
13 appear: [],
14 vanish: [],
15 start: {x:sx, y:sy},
16 end: {x:ex, y:ey}
17 });
18 if (this.board[ex][ey] != V.EMPTY) {
19 // No movement: just vanishing enemy piece
20 mv.vanish = [
21 new PiPo({
22 x: ex,
23 y: ey,
24 c: this.getColor(ex, ey),
25 p: this.getPiece(ex, ey)
26 })
27 ];
28 }
29 else {
30 // Normal move
31 mv.appear = [
32 new PiPo({
33 x: ex,
34 y: ey,
35 c: tr ? tr.c : this.getColor(sx, sy),
36 p: tr ? tr.p : this.getPiece(sx, sy)
37 })
38 ];
39 mv.vanish = [
40 new PiPo({
41 x: sx,
42 y: sy,
43 c: this.getColor(sx, sy),
44 p: this.getPiece(sx, sy)
45 })
46 ];
47 }
48
49 return mv;
50 }
51
52 getPotentialPawnMoves([x, y]) {
53 const color = this.turn;
54 let moves = [];
55 const [sizeX, sizeY] = [V.size.x, V.size.y];
56 const shiftX = color == "w" ? -1 : 1;
57 const startRank = color == "w" ? sizeX - 2 : 1;
58 const lastRank = color == "w" ? 0 : sizeX - 1;
59
60 const finalPieces =
61 x + shiftX == lastRank
62 ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
63 : [V.PAWN];
64 if (this.board[x + shiftX][y] == V.EMPTY) {
65 for (let piece of finalPieces) {
66 moves.push(
67 this.getBasicMove([x, y], [x + shiftX, y], {
68 c: color,
69 p: piece
70 })
71 );
72 }
73 if (
74 x == startRank &&
75 this.board[x + 2 * shiftX][y] == V.EMPTY
76 ) {
77 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
78 }
79 }
80 // Captures
81 for (let shiftY of [-1, 1]) {
82 if (
83 y + shiftY >= 0 &&
84 y + shiftY < sizeY &&
85 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
86 this.canTake([x, y], [x + shiftX, y + shiftY])
87 ) {
88 for (let piece of finalPieces) {
89 moves.push(
90 this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
91 c: color,
92 p: piece
93 })
94 );
95 }
96 }
97 }
98
99 // En passant
100 const Lep = this.epSquares.length;
101 const epSquare = this.epSquares[Lep - 1]; //always at least one element
102 if (
103 !!epSquare &&
104 epSquare.x == x + shiftX &&
105 Math.abs(epSquare.y - y) == 1
106 ) {
107 let enpassantMove = new Move({
108 appear: [],
109 vanish: [],
110 start: {x:x, y:y},
111 end: {x:x+shiftX, y:epSquare.y}
112 });
113 enpassantMove.vanish.push({
114 x: x,
115 y: epSquare.y,
116 p: "p",
117 c: this.getColor(x, epSquare.y)
118 });
119 moves.push(enpassantMove);
120 }
121
122 return moves;
123 }
124 };