Prepare some more variants (unfinished)
[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,
22 y: (move.end.y + sy)/2
23 };
24 }
25 return undefined; //default
26 }
27
28 // Special pawn rules: promotions to captured friendly pieces,
29 // optional on ranks 8-9 and mandatory on rank 10.
30 getPotentialPawnMoves([x,y])
31 {
32 const color = this.turn;
33 let moves = [];
34 const [sizeX,sizeY] = [V.size.x,V.size.y];
35 const shiftX = (color == "w" ? -1 : 1);
36 const firstRank = (color == 'w' ? sizeX-1 : 0);
37 const startRank = (color == "w" ? sizeX-2 : 1);
38 const lastRank = (color == "w" ? 0 : sizeX-1);
39
40 if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true
41 {
42 const finalPieces = x + shiftX == lastRank
43 ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
44 : [V.PAWN]
45 // One square diagonally
46 for (let shiftY of [-1,1])
47 {
48 if (this.board[x+shiftX][y+shiftY] == V.EMPTY)
49 {
50 for (let piece of finalPieces)
51 {
52 moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
53 {c:pawnColor,p:piece}));
54 }
55 if (x == startRank && this.board[x+2*shiftX][y] == V.EMPTY)
56 {
57 // Two squares jump
58 moves.push(this.getBasicMove([x,y], [x+2*shiftX,y+2*shiftY]);
59 }
60 }
61 }
62 // Capture
63 if (this.board[x+shiftX][y] != V.EMPTY
64 && this.canTake([x,y], [x+shiftX,y]))
65 {
66 for (let piece of finalPieces)
67 {
68 moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
69 {c:pawnColor,p:piece}));
70 }
71 }
72 }
73
74 // En passant
75 const Lep = this.epSquares.length;
76 const epSquare = this.epSquares[Lep-1]; //always at least one element
77 if (!!epSquare && epSquare.x == x+shiftX && epSquare.y == y)
78 {
79 let enpassantMove = this.getBasicMove([x,y], [x+shift,y]);
80 enpassantMove.vanish.push({
81 x: epSquare.x,
82 y: y,
83 p: 'p',
84 c: this.getColor(epSquare.x,y)
85 });
86 moves.push(enpassantMove);
87 }
88
89 return moves;
90 }
91 }
92
93 const VariantRules = BerolinaRules;