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