dc9e25740f622be88c720a4ec53c4a6cc1eff395
[vchess.git] / public / javascripts / variants / Berolina.js
1 class BerolinaRules extends ChessRules
2 {
3 // En-passant after 2-sq jump
4 getEpSquare(moveOrSquare)
5 {
6 if (!moveOrSquare)
7 return undefined;
8 if (typeof moveOrSquare === "string")
9 {
10 const square = moveOrSquare;
11 if (square == "-")
12 return undefined;
13 return V.SquareToCoords(square);
14 }
15 // Argument is a move:
16 const move = moveOrSquare;
17 const [sx,ex,sy] = [move.start.x,move.end.x,move.start.y];
18 if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) == 2)
19 {
20 return {
21 x: (ex + sx)/2,
22 y: (move.end.y + sy)/2
23 };
24 }
25 return undefined; //default
26 }
27
28 // Special pawns movements
29 getPotentialPawnMoves([x,y])
30 {
31 const color = this.turn;
32 let moves = [];
33 const [sizeX,sizeY] = [V.size.x,V.size.y];
34 const shiftX = (color == "w" ? -1 : 1);
35 const firstRank = (color == 'w' ? sizeX-1 : 0);
36 const startRank = (color == "w" ? sizeX-2 : 1);
37 const lastRank = (color == "w" ? 0 : sizeX-1);
38
39 if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true
40 {
41 const finalPieces = x + shiftX == lastRank
42 ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
43 : [V.PAWN]
44 // One square diagonally
45 for (let shiftY of [-1,1])
46 {
47 if (this.board[x+shiftX][y+shiftY] == V.EMPTY)
48 {
49 for (let piece of finalPieces)
50 moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY], {c:color,p:piece}));
51 if (x == startRank && y+2*shiftY>=0 && y+2*shiftY<sizeY
52 && this.board[x+2*shiftX][y+2*shiftY] == V.EMPTY)
53 {
54 // Two squares jump
55 moves.push(this.getBasicMove([x,y], [x+2*shiftX,y+2*shiftY]));
56 }
57 }
58 }
59 // Capture
60 if (this.board[x+shiftX][y] != V.EMPTY
61 && this.canTake([x,y], [x+shiftX,y]))
62 {
63 for (let piece of finalPieces)
64 moves.push(this.getBasicMove([x,y], [x+shiftX,y], {c:color,p:piece}));
65 }
66 }
67
68 // En passant
69 const Lep = this.epSquares.length;
70 const epSquare = this.epSquares[Lep-1]; //always at least one element
71 if (!!epSquare && epSquare.x == x+shiftX && epSquare.y == y)
72 {
73 let enpassantMove = this.getBasicMove([x,y], [x+shiftX,y]);
74 enpassantMove.vanish.push({
75 x: epSquare.x,
76 y: epSquare.y,
77 p: 'p',
78 c: this.getColor(epSquare.x,epSquare.y)
79 });
80 moves.push(enpassantMove);
81 }
82
83 return moves;
84 }
85
86 isAttackedByPawn([x,y], colors)
87 {
88 for (let c of colors)
89 {
90 let pawnShift = (c=="w" ? 1 : -1);
91 if (x+pawnShift>=0 && x+pawnShift<V.size.x)
92 {
93 if (this.getPiece(x+pawnShift,y)==V.PAWN && this.getColor(x+pawnShift,y)==c)
94 return true;
95 }
96 }
97 return false;
98 }
99 }
100
101 const VariantRules = BerolinaRules;