Some code cleaning + clarifying (TODO: work on variables names)
[vchess.git] / public / javascripts / variants / Zen.js
1 class ZenRules extends ChessRules
2 {
3 // NOTE: enPassant, if enabled, would need to redefine carefully getEpSquare
4 getEpSquare(move)
5 {
6 return undefined;
7 }
8
9 // TODO(?): some duplicated code in 2 next functions
10 getSlideNJumpMoves([x,y], steps, oneStep)
11 {
12 const color = this.getColor(x,y);
13 let moves = [];
14 const [sizeX,sizeY] = VariantRules.size;
15 outerLoop:
16 for (let loop=0; loop<steps.length; loop++)
17 {
18 const step = steps[loop];
19 let i = x + step[0];
20 let j = y + step[1];
21 while (i>=0 && i<sizeX && j>=0 && j<sizeY
22 && this.board[i][j] == VariantRules.EMPTY)
23 {
24 moves.push(this.getBasicMove([x,y], [i,j]));
25 if (!!oneStep)
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!
37 findCaptures_aux([x,y], asA)
38 {
39 const color = this.getColor(x,y);
40 var moves = [];
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])
44 : color=='w' ? [[-1,-1],[-1,1]] : [[1,-1],[1,1]];
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];
49 outerLoop:
50 for (let loop=0; loop<steps.length; loop++)
51 {
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)
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 => {
70 moves.push(this.getBasicMove([x,y], [i,j], {c:color,p:p}));
71 });
72 }
73 else
74 {
75 // All other cases
76 moves.push(this.getBasicMove([x,y], [i,j]));
77 }
78 }
79 }
80 return moves;
81 }
82
83 // Find possible captures from a square: look in every direction!
84 findCaptures(sq)
85 {
86 let moves = [];
87
88 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.PAWN));
89 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.ROOK));
90 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.KNIGHT));
91 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.BISHOP));
92 Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.QUEEN));
93
94 return moves;
95 }
96
97 getPotentialPawnMoves([x,y])
98 {
99 const color = this.getColor(x,y);
100 var moves = [];
101 var V = VariantRules;
102 let [sizeX,sizeY] = VariantRules.size;
103 let shift = (color == 'w' ? -1 : 1);
104 let startRank = (color == 'w' ? sizeY-2 : 1);
105 let firstRank = (color == 'w' ? sizeY-1 : 0);
106 let lastRank = (color == "w" ? 0 : sizeY-1);
107
108 if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank)
109 {
110 // Normal moves
111 if (this.board[x+shift][y] == V.EMPTY)
112 {
113 moves.push(this.getBasicMove([x,y], [x+shift,y]));
114 if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY)
115 {
116 //two squares jump
117 moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
118 }
119 }
120 }
121
122 if (x+shift == lastRank)
123 {
124 // Promotion
125 let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
126 promotionPieces.forEach(p => {
127 // Normal move
128 if (this.board[x+shift][y] == V.EMPTY)
129 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
130 });
131 }
132
133 // No en passant here
134
135 // Add "zen" captures
136 Array.prototype.push.apply(moves, this.findCaptures([x,y]));
137
138 return moves;
139 }
140
141 getPotentialRookMoves(sq)
142 {
143 let noCaptures = this.getSlideNJumpMoves(
144 sq, VariantRules.steps[VariantRules.ROOK]);
145 let captures = this.findCaptures(sq);
146 return noCaptures.concat(captures);
147 }
148
149 getPotentialKnightMoves(sq)
150 {
151 let noCaptures = this.getSlideNJumpMoves(
152 sq, VariantRules.steps[VariantRules.KNIGHT], "oneStep");
153 let captures = this.findCaptures(sq);
154 return noCaptures.concat(captures);
155 }
156
157 getPotentialBishopMoves(sq)
158 {
159 let noCaptures = this.getSlideNJumpMoves(
160 sq, VariantRules.steps[VariantRules.BISHOP]);
161 let captures = this.findCaptures(sq);
162 return noCaptures.concat(captures);
163 }
164
165 getPotentialQueenMoves(sq)
166 {
167 const V = VariantRules;
168 let noCaptures = this.getSlideNJumpMoves(
169 sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]));
170 let captures = this.findCaptures(sq);
171 return noCaptures.concat(captures);
172 }
173
174 getPotentialKingMoves(sq)
175 {
176 const V = VariantRules;
177 // Initialize with normal moves
178 let noCaptures = this.getSlideNJumpMoves(sq,
179 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
180 let captures = this.findCaptures(sq);
181 return noCaptures.concat(captures).concat(this.getCastleMoves(sq));
182 }
183
184 getNotation(move)
185 {
186 // Recognize special moves first
187 if (move.appear.length == 2)
188 {
189 // castle
190 if (move.end.y < move.start.y)
191 return "0-0-0";
192 else
193 return "0-0";
194 }
195
196 // Translate initial square (because pieces may fly unusually in this variant!)
197 let initialSquare =
198 String.fromCharCode(97 + move.start.y) + (VariantRules.size[0]-move.start.x);
199
200 // Translate final square
201 let finalSquare =
202 String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
203
204 let notation = "";
205 let piece = this.getPiece(move.start.x, move.start.y);
206 if (piece == VariantRules.PAWN)
207 {
208 // pawn move (TODO: enPassant indication)
209 if (move.vanish.length > 1)
210 {
211 // capture
212 notation = initialSquare + "x" + finalSquare;
213 }
214 else //no capture
215 notation = finalSquare;
216 if (piece != move.appear[0].p) //promotion
217 notation += "=" + move.appear[0].p.toUpperCase();
218 }
219
220 else
221 {
222 // Piece movement
223 notation = piece.toUpperCase();
224 if (move.vanish.length > 1)
225 notation += initialSquare + "x";
226 notation += finalSquare;
227 }
228 return notation;
229 }
230
231 static get VALUES() { //TODO: experimental
232 return {
233 'p': 1,
234 'r': 3,
235 'n': 2,
236 'b': 2,
237 'q': 5,
238 'k': 1000
239 }
240 }
241 }