Commit | Line | Data |
---|---|---|
1d184b4c BA |
1 | class ZenRules extends ChessRules |
2 | { | |
33ee1916 BA |
3 | // NOTE: enPassant, if enabled, would need to redefine carefully getEpSquare |
4 | getEpSquare(move) | |
5 | { | |
6 | return undefined; | |
7 | } | |
8 | ||
1d184b4c | 9 | // TODO: some duplicated code in 2 next functions |
46302e64 | 10 | getSlideNJumpMoves([x,y], steps, oneStep) |
1d184b4c | 11 | { |
46302e64 | 12 | const color = this.getColor(x,y); |
1221ac47 BA |
13 | let moves = []; |
14 | const [sizeX,sizeY] = VariantRules.size; | |
1d184b4c | 15 | outerLoop: |
1221ac47 | 16 | for (let loop=0; loop<steps.length; loop++) |
1d184b4c | 17 | { |
1221ac47 BA |
18 | const step = steps[loop]; |
19 | let i = x + step[0]; | |
20 | let j = y + step[1]; | |
1d184b4c BA |
21 | while (i>=0 && i<sizeX && j>=0 && j<sizeY |
22 | && this.board[i][j] == VariantRules.EMPTY) | |
23 | { | |
46302e64 BA |
24 | moves.push(this.getBasicMove([x,y], [i,j])); |
25 | if (!!oneStep) | |
1d184b4c BA |
26 | continue outerLoop; |
27 | i += step[0]; | |
28 | j += step[1]; | |
29 | } | |
30 | // No capture check: handled elsewhere (next method) | |
31 | } | |
32 | return moves; | |
33 | } | |
34 | ||
35 | // follow steps from x,y until something is met. | |
36 | // if met piece is opponent and same movement (asA): eat it! | |
46302e64 | 37 | findCaptures_aux([x,y], asA) |
1d184b4c | 38 | { |
46302e64 | 39 | const color = this.getColor(x,y); |
1d184b4c | 40 | var moves = []; |
1221ac47 BA |
41 | const V = VariantRules; |
42 | const steps = asA != V.PAWN | |
43 | ? (asA==V.QUEEN ? V.steps[V.ROOK].concat(V.steps[V.BISHOP]) : V.steps[asA]) | |
1d184b4c | 44 | : color=='w' ? [[-1,-1],[-1,1]] : [[1,-1],[1,1]]; |
1221ac47 BA |
45 | const oneStep = (asA==V.KNIGHT || asA==V.PAWN); //we don't capture king |
46 | const [sizeX,sizeY] = V.size; | |
47 | const lastRank = (color == 'w' ? 0 : sizeY-1); | |
48 | const promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]; | |
1d184b4c | 49 | outerLoop: |
1221ac47 | 50 | for (let loop=0; loop<steps.length; loop++) |
1d184b4c | 51 | { |
1221ac47 BA |
52 | const step = steps[loop]; |
53 | let i = x + step[0]; | |
54 | let j = y + step[1]; | |
55 | while (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j] == V.EMPTY) | |
1d184b4c BA |
56 | { |
57 | if (oneStep) | |
58 | continue outerLoop; | |
59 | i += step[0]; | |
60 | j += step[1]; | |
61 | } | |
62 | if (i>=0 && i<sizeX && j>=0 && j<sizeY && | |
63 | this.getColor(i,j) == this.getOppCol(color) && this.getPiece(i,j) == asA) | |
64 | { | |
65 | // eat! | |
66 | if (this.getPiece(x,y) == V.PAWN && i == lastRank) | |
67 | { | |
68 | // Special case of promotion: | |
69 | promotionPieces.forEach(p => { | |
46302e64 | 70 | moves.push(this.getBasicMove([x,y], [i,j], {c:color,p:p})); |
1d184b4c BA |
71 | }); |
72 | } | |
73 | else | |
74 | { | |
75 | // All other cases | |
46302e64 | 76 | moves.push(this.getBasicMove([x,y], [i,j])); |
1d184b4c BA |
77 | } |
78 | } | |
79 | } | |
80 | return moves; | |
81 | } | |
82 | ||
83 | // Find possible captures from a square: look in every direction! | |
46302e64 | 84 | findCaptures(sq) |
1d184b4c BA |
85 | { |
86 | var moves = []; | |
87 | ||
88 | // PAWN | |
46302e64 | 89 | Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.PAWN)); |
1d184b4c BA |
90 | |
91 | // ROOK | |
46302e64 | 92 | Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.ROOK)); |
1d184b4c BA |
93 | |
94 | // KNIGHT | |
46302e64 | 95 | Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.KNIGHT)); |
1d184b4c BA |
96 | |
97 | // BISHOP | |
46302e64 | 98 | Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.BISHOP)); |
1d184b4c BA |
99 | |
100 | // QUEEN | |
46302e64 | 101 | Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.QUEEN)); |
1d184b4c BA |
102 | |
103 | return moves; | |
104 | } | |
105 | ||
46302e64 | 106 | getPotentialPawnMoves([x,y]) |
1d184b4c | 107 | { |
46302e64 | 108 | const color = this.getColor(x,y); |
1d184b4c BA |
109 | var moves = []; |
110 | var V = VariantRules; | |
111 | let [sizeX,sizeY] = VariantRules.size; | |
112 | let shift = (color == 'w' ? -1 : 1); | |
113 | let startRank = (color == 'w' ? sizeY-2 : 1); | |
01ca2adc | 114 | let firstRank = (color == 'w' ? sizeY-1 : 0); |
1d184b4c BA |
115 | let lastRank = (color == "w" ? 0 : sizeY-1); |
116 | ||
117 | if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank) | |
118 | { | |
119 | // Normal moves | |
120 | if (this.board[x+shift][y] == V.EMPTY) | |
121 | { | |
46302e64 | 122 | moves.push(this.getBasicMove([x,y], [x+shift,y])); |
01ca2adc | 123 | if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY) |
1d184b4c BA |
124 | { |
125 | //two squares jump | |
46302e64 | 126 | moves.push(this.getBasicMove([x,y], [x+2*shift,y])); |
1d184b4c BA |
127 | } |
128 | } | |
129 | } | |
130 | ||
131 | if (x+shift == lastRank) | |
132 | { | |
133 | // Promotion | |
134 | let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]; | |
135 | promotionPieces.forEach(p => { | |
136 | // Normal move | |
137 | if (this.board[x+shift][y] == V.EMPTY) | |
46302e64 | 138 | moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p})); |
1d184b4c BA |
139 | }); |
140 | } | |
141 | ||
33ee1916 | 142 | // No en passant here |
1d184b4c BA |
143 | |
144 | // Add "zen" captures | |
46302e64 | 145 | Array.prototype.push.apply(moves, this.findCaptures([x,y])); |
1d184b4c BA |
146 | |
147 | return moves; | |
148 | } | |
149 | ||
46302e64 | 150 | getPotentialRookMoves(sq) |
1d184b4c | 151 | { |
46302e64 BA |
152 | let noCaptures = this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.ROOK]); |
153 | let captures = this.findCaptures(sq); | |
1d184b4c BA |
154 | return noCaptures.concat(captures); |
155 | } | |
156 | ||
46302e64 | 157 | getPotentialKnightMoves(sq) |
1d184b4c | 158 | { |
a37076f1 BA |
159 | let noCaptures = this.getSlideNJumpMoves(sq, |
160 | VariantRules.steps[VariantRules.KNIGHT], "oneStep"); | |
46302e64 | 161 | let captures = this.findCaptures(sq); |
1d184b4c BA |
162 | return noCaptures.concat(captures); |
163 | } | |
164 | ||
46302e64 | 165 | getPotentialBishopMoves(sq) |
1d184b4c | 166 | { |
46302e64 BA |
167 | let noCaptures = this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.BISHOP]); |
168 | let captures = this.findCaptures(sq); | |
1d184b4c BA |
169 | return noCaptures.concat(captures); |
170 | } | |
171 | ||
46302e64 | 172 | getPotentialQueenMoves(sq) |
1d184b4c | 173 | { |
a37076f1 | 174 | const V = VariantRules; |
1221ac47 BA |
175 | let noCaptures = |
176 | this.getSlideNJumpMoves(sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP])); | |
46302e64 | 177 | let captures = this.findCaptures(sq); |
1d184b4c BA |
178 | return noCaptures.concat(captures); |
179 | } | |
180 | ||
46302e64 | 181 | getPotentialKingMoves(sq) |
1d184b4c | 182 | { |
a37076f1 | 183 | const V = VariantRules; |
1d184b4c | 184 | // Initialize with normal moves |
a37076f1 BA |
185 | let noCaptures = this.getSlideNJumpMoves(sq, |
186 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep"); | |
46302e64 BA |
187 | let captures = this.findCaptures(sq); |
188 | return noCaptures.concat(captures).concat(this.getCastleMoves(sq)); | |
1d184b4c BA |
189 | } |
190 | ||
191 | getNotation(move) | |
192 | { | |
193 | // Recognize special moves first | |
194 | if (move.appear.length == 2) | |
195 | { | |
196 | // castle | |
197 | if (move.end.y < move.start.y) | |
198 | return "0-0-0"; | |
199 | else | |
200 | return "0-0"; | |
201 | } | |
202 | ||
203 | // Translate initial square (because pieces may fly unusually in this variant!) | |
204 | let initialSquare = | |
205 | String.fromCharCode(97 + move.start.y) + (VariantRules.size[0]-move.start.x); | |
206 | ||
207 | // Translate final square | |
208 | let finalSquare = | |
209 | String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x); | |
210 | ||
211 | let notation = ""; | |
52b1e238 | 212 | let piece = this.getPiece(move.start.x, move.start.y); |
1d184b4c BA |
213 | if (piece == VariantRules.PAWN) |
214 | { | |
215 | // pawn move (TODO: enPassant indication) | |
216 | if (move.vanish.length > 1) | |
217 | { | |
218 | // capture | |
219 | notation = initialSquare + "x" + finalSquare; | |
220 | } | |
221 | else //no capture | |
222 | notation = finalSquare; | |
223 | if (piece != move.appear[0].p) //promotion | |
224 | notation += "=" + move.appear[0].p.toUpperCase(); | |
225 | } | |
226 | ||
227 | else | |
228 | { | |
229 | // Piece movement | |
230 | notation = piece.toUpperCase(); | |
231 | if (move.vanish.length > 1) | |
232 | notation += initialSquare + "x"; | |
233 | notation += finalSquare; | |
234 | } | |
235 | return notation; | |
236 | } | |
237 | ||
238 | static get VALUES() { //TODO: experimental | |
239 | return { | |
240 | 'p': 1, | |
241 | 'r': 3, | |
242 | 'n': 2, | |
243 | 'b': 2, | |
244 | 'q': 5, | |
245 | 'k': 1000 | |
246 | } | |
247 | } | |
248 | } |