Bugs fixing, finalization of rules in french+english
[vchess.git] / public / javascripts / variants / Berolina.js
CommitLineData
375ecdd1
BA
1class 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;
26c1e3bd
BA
13 // Enemy pawn initial column must be given too:
14 let res = [];
15 const epParts = square.split(",");
16 res.push(V.SquareToCoords(epParts[0]));
17 res.push(V.ColumnToCoord(epParts[1]));
18 return res;
375ecdd1
BA
19 }
20 // Argument is a move:
21 const move = moveOrSquare;
22 const [sx,ex,sy] = [move.start.x,move.end.x,move.start.y];
23 if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) == 2)
24 {
26c1e3bd
BA
25 return
26 [
27 {
28 x: (ex + sx)/2,
29 y: (move.end.y + sy)/2
30 },
31 move.end.y
32 ];
375ecdd1
BA
33 }
34 return undefined; //default
35 }
36
f6dbe8e3 37 // Special pawns movements
375ecdd1
BA
38 getPotentialPawnMoves([x,y])
39 {
40 const color = this.turn;
41 let moves = [];
42 const [sizeX,sizeY] = [V.size.x,V.size.y];
43 const shiftX = (color == "w" ? -1 : 1);
44 const firstRank = (color == 'w' ? sizeX-1 : 0);
45 const startRank = (color == "w" ? sizeX-2 : 1);
46 const lastRank = (color == "w" ? 0 : sizeX-1);
69f3d801
BA
47 const finalPieces = x + shiftX == lastRank
48 ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]
49 : [V.PAWN];
375ecdd1 50
69f3d801
BA
51 // One square diagonally
52 for (let shiftY of [-1,1])
375ecdd1 53 {
69f3d801 54 if (this.board[x+shiftX][y+shiftY] == V.EMPTY)
375ecdd1 55 {
69f3d801 56 for (let piece of finalPieces)
375ecdd1 57 {
69f3d801
BA
58 moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY],
59 {c:color,p:piece}));
60 }
61 if (x == startRank && y+2*shiftY>=0 && y+2*shiftY<sizeY
62 && this.board[x+2*shiftX][y+2*shiftY] == V.EMPTY)
63 {
64 // Two squares jump
65 moves.push(this.getBasicMove([x,y], [x+2*shiftX,y+2*shiftY]));
375ecdd1
BA
66 }
67 }
69f3d801
BA
68 }
69 // Capture
70 if (this.board[x+shiftX][y] != V.EMPTY
71 && this.canTake([x,y], [x+shiftX,y]))
72 {
73 for (let piece of finalPieces)
74 moves.push(this.getBasicMove([x,y], [x+shiftX,y], {c:color,p:piece}));
375ecdd1
BA
75 }
76
77 // En passant
78 const Lep = this.epSquares.length;
79 const epSquare = this.epSquares[Lep-1]; //always at least one element
26c1e3bd
BA
80 if (!!epSquare && epSquare[0].x == x+shiftX && epSquare[0].y == y
81 && Math.abs(epSquare[1] - y) == 1)
375ecdd1 82 {
f6dbe8e3 83 let enpassantMove = this.getBasicMove([x,y], [x+shiftX,y]);
375ecdd1 84 enpassantMove.vanish.push({
26c1e3bd
BA
85 x: x,
86 y: epSquare[1],
375ecdd1 87 p: 'p',
26c1e3bd 88 c: this.getColor(x,epSquare[1])
375ecdd1
BA
89 });
90 moves.push(enpassantMove);
91 }
92
93 return moves;
94 }
f6dbe8e3
BA
95
96 isAttackedByPawn([x,y], colors)
97 {
98 for (let c of colors)
99 {
100 let pawnShift = (c=="w" ? 1 : -1);
101 if (x+pawnShift>=0 && x+pawnShift<V.size.x)
102 {
26c1e3bd
BA
103 if (this.getPiece(x+pawnShift,y)==V.PAWN
104 && this.getColor(x+pawnShift,y)==c)
105 {
f6dbe8e3 106 return true;
26c1e3bd 107 }
f6dbe8e3
BA
108 }
109 }
110 return false;
111 }
26c1e3bd
BA
112
113 getNotation(move)
114 {
115 const piece = this.getPiece(move.start.x, move.start.y);
116 if (piece == V.PAWN)
117 {
118 // Pawn move
1b61a94d 119 const finalSquare = V.CoordsToSquare(move.end);
26c1e3bd
BA
120 let notation = "";
121 if (move.vanish.length == 2) //capture
1b61a94d 122 notation = "Px" + finalSquare;
26c1e3bd
BA
123 else
124 {
1b61a94d
BA
125 // No capture: indicate the initial square for potential ambiguity
126 const startSquare = V.CoordsToSquare(move.start);
127 notation = startSquare + finalSquare;
26c1e3bd
BA
128 }
129 if (move.appear[0].p != V.PAWN) //promotion
130 notation += "=" + move.appear[0].p.toUpperCase();
131 return notation;
132 }
133 return super.getNotation(move); //all other pieces are orthodox
134 }
375ecdd1
BA
135}
136
137const VariantRules = BerolinaRules;