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