Commit | Line | Data |
---|---|---|
375ecdd1 BA |
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; | |
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); | |
47 | ||
48 | if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true | |
49 | { | |
50 | const finalPieces = x + shiftX == lastRank | |
51 | ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN] | |
52 | : [V.PAWN] | |
53 | // One square diagonally | |
54 | for (let shiftY of [-1,1]) | |
55 | { | |
56 | if (this.board[x+shiftX][y+shiftY] == V.EMPTY) | |
57 | { | |
58 | for (let piece of finalPieces) | |
26c1e3bd BA |
59 | { |
60 | moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY], | |
61 | {c:color,p:piece})); | |
62 | } | |
f6dbe8e3 BA |
63 | if (x == startRank && y+2*shiftY>=0 && y+2*shiftY<sizeY |
64 | && this.board[x+2*shiftX][y+2*shiftY] == V.EMPTY) | |
375ecdd1 BA |
65 | { |
66 | // Two squares jump | |
f6dbe8e3 | 67 | moves.push(this.getBasicMove([x,y], [x+2*shiftX,y+2*shiftY])); |
375ecdd1 BA |
68 | } |
69 | } | |
70 | } | |
71 | // Capture | |
72 | if (this.board[x+shiftX][y] != V.EMPTY | |
73 | && this.canTake([x,y], [x+shiftX,y])) | |
74 | { | |
75 | for (let piece of finalPieces) | |
f6dbe8e3 | 76 | moves.push(this.getBasicMove([x,y], [x+shiftX,y], {c:color,p:piece})); |
375ecdd1 BA |
77 | } |
78 | } | |
79 | ||
80 | // En passant | |
81 | const Lep = this.epSquares.length; | |
82 | const epSquare = this.epSquares[Lep-1]; //always at least one element | |
26c1e3bd BA |
83 | if (!!epSquare && epSquare[0].x == x+shiftX && epSquare[0].y == y |
84 | && Math.abs(epSquare[1] - y) == 1) | |
375ecdd1 | 85 | { |
f6dbe8e3 | 86 | let enpassantMove = this.getBasicMove([x,y], [x+shiftX,y]); |
375ecdd1 | 87 | enpassantMove.vanish.push({ |
26c1e3bd BA |
88 | x: x, |
89 | y: epSquare[1], | |
375ecdd1 | 90 | p: 'p', |
26c1e3bd | 91 | c: this.getColor(x,epSquare[1]) |
375ecdd1 BA |
92 | }); |
93 | moves.push(enpassantMove); | |
94 | } | |
95 | ||
96 | return moves; | |
97 | } | |
f6dbe8e3 BA |
98 | |
99 | isAttackedByPawn([x,y], colors) | |
100 | { | |
101 | for (let c of colors) | |
102 | { | |
103 | let pawnShift = (c=="w" ? 1 : -1); | |
104 | if (x+pawnShift>=0 && x+pawnShift<V.size.x) | |
105 | { | |
26c1e3bd BA |
106 | if (this.getPiece(x+pawnShift,y)==V.PAWN |
107 | && this.getColor(x+pawnShift,y)==c) | |
108 | { | |
f6dbe8e3 | 109 | return true; |
26c1e3bd | 110 | } |
f6dbe8e3 BA |
111 | } |
112 | } | |
113 | return false; | |
114 | } | |
26c1e3bd BA |
115 | |
116 | getNotation(move) | |
117 | { | |
118 | const piece = this.getPiece(move.start.x, move.start.y); | |
119 | if (piece == V.PAWN) | |
120 | { | |
121 | // Pawn move | |
122 | let notation = ""; | |
123 | if (move.vanish.length == 2) //capture | |
124 | notation = finalSquare; | |
125 | else | |
126 | { | |
127 | // No capture | |
128 | const startColumn = V.CoordToColumn(move.start.y); | |
129 | notation = startColumn + "x" + finalSquare; | |
130 | } | |
131 | if (move.appear[0].p != V.PAWN) //promotion | |
132 | notation += "=" + move.appear[0].p.toUpperCase(); | |
133 | return notation; | |
134 | } | |
135 | return super.getNotation(move); //all other pieces are orthodox | |
136 | } | |
375ecdd1 BA |
137 | } |
138 | ||
139 | const VariantRules = BerolinaRules; |