8ea3c9ccffe941e7a49fbfbb653653e4f9ff7021
1 class BerolinaRules
extends ChessRules
3 // En-passant after 2-sq jump
4 getEpSquare(moveOrSquare
)
8 if (typeof moveOrSquare
=== "string")
10 const square
= moveOrSquare
;
13 // Enemy pawn initial column must be given too:
15 const epParts
= square
.split(",");
16 res
.push(V
.SquareToCoords(epParts
[0]));
17 res
.push(V
.ColumnToCoord(epParts
[1]));
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)
29 y: (move.end
.y
+ sy
)/2
34 return undefined; //default
37 // Special pawns movements
38 getPotentialPawnMoves([x
,y
])
40 const color
= this.turn
;
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);
48 if (x
+shiftX
>= 0 && x
+shiftX
< sizeX
) //TODO: always true
50 const finalPieces
= x
+ shiftX
== lastRank
51 ? [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
]
53 // One square diagonally
54 for (let shiftY
of [-1,1])
56 if (this.board
[x
+shiftX
][y
+shiftY
] == V
.EMPTY
)
58 for (let piece
of finalPieces
)
60 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
+shiftY
],
63 if (x
== startRank
&& y
+2*shiftY
>=0 && y
+2*shiftY
<sizeY
64 && this.board
[x
+2*shiftX
][y
+2*shiftY
] == V
.EMPTY
)
67 moves
.push(this.getBasicMove([x
,y
], [x
+2*shiftX
,y
+2*shiftY
]));
72 if (this.board
[x
+shiftX
][y
] != V
.EMPTY
73 && this.canTake([x
,y
], [x
+shiftX
,y
]))
75 for (let piece
of finalPieces
)
76 moves
.push(this.getBasicMove([x
,y
], [x
+shiftX
,y
], {c:color
,p:piece
}));
81 const Lep
= this.epSquares
.length
;
82 const epSquare
= this.epSquares
[Lep
-1]; //always at least one element
83 if (!!epSquare
&& epSquare
[0].x
== x
+shiftX
&& epSquare
[0].y
== y
84 && Math
.abs(epSquare
[1] - y
) == 1)
86 let enpassantMove
= this.getBasicMove([x
,y
], [x
+shiftX
,y
]);
87 enpassantMove
.vanish
.push({
91 c: this.getColor(x
,epSquare
[1])
93 moves
.push(enpassantMove
);
99 isAttackedByPawn([x
,y
], colors
)
101 for (let c
of colors
)
103 let pawnShift
= (c
=="w" ? 1 : -1);
104 if (x
+pawnShift
>=0 && x
+pawnShift
<V
.size
.x
)
106 if (this.getPiece(x
+pawnShift
,y
)==V
.PAWN
107 && this.getColor(x
+pawnShift
,y
)==c
)
118 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
122 const finalSquare
= V
.CoordsToSquare(move.end
);
124 if (move.vanish
.length
== 2) //capture
125 notation
= "Px" + finalSquare
;
128 // No capture: indicate the initial square for potential ambiguity
129 const startSquare
= V
.CoordsToSquare(move.start
);
130 notation
= startSquare
+ finalSquare
;
132 if (move.appear
[0].p
!= V
.PAWN
) //promotion
133 notation
+= "=" + move.appear
[0].p
.toUpperCase();
136 return super.getNotation(move); //all other pieces are orthodox
140 const VariantRules
= BerolinaRules
;