1 // NOTE: initial setup differs from the original; see
2 // https://www.chessvariants.com/large.dir/freeling.html
3 class GrandRules
extends ChessRules
7 return ([V
.MARSHALL
,V
.CARDINAL
].includes(b
[1]) ? "Grand/" : "") + b
;
12 if (!ChessRules
.IsGoodFen(fen
))
14 const fenParsed
= V
.ParseFen(fen
);
16 if (!fenParsed
.captured
|| !fenParsed
.captured
.match(/^[0-9]{10,10}$/))
23 const fenParts
= fen
.split(" ");
25 ChessRules
.ParseFen(fen
),
26 { captured: fenParts
[4] }
32 return super.getFen() + " " + this.getCapturedFen();
37 let counts
= _
.map(_
.range(10), 0);
38 for (let i
=0; i
<V
.PIECES
.length
; i
++)
40 counts
[i
] = this.captured
["w"][V
.PIECES
[i
]];
41 counts
[5+i
] = this.captured
["b"][V
.PIECES
[i
]];
43 return counts
.join("");
46 setOtherVariables(fen
)
48 super.setOtherVariables(fen
);
49 const fenParsed
= V
.ParseFen(fen
);
50 // Initialize captured pieces' counts from FEN
55 [V
.PAWN
]: parseInt(fenParsed
.captured
[0]),
56 [V
.ROOK
]: parseInt(fenParsed
.captured
[1]),
57 [V
.KNIGHT
]: parseInt(fenParsed
.captured
[2]),
58 [V
.BISHOP
]: parseInt(fenParsed
.captured
[3]),
59 [V
.QUEEN
]: parseInt(fenParsed
.captured
[4]),
63 [V
.PAWN
]: parseInt(fenParsed
.captured
[5]),
64 [V
.ROOK
]: parseInt(fenParsed
.captured
[6]),
65 [V
.KNIGHT
]: parseInt(fenParsed
.captured
[7]),
66 [V
.BISHOP
]: parseInt(fenParsed
.captured
[8]),
67 [V
.QUEEN
]: parseInt(fenParsed
.captured
[9]),
72 static get size() { return {x:10,y:10}; }
74 static get MARSHALL() { return 'm'; } //rook+knight
75 static get CARDINAL() { return 'c'; } //bishop+knight
79 return ChessRules
.PIECES
.concat([V
.MARSHALL
,V
.CARDINAL
]);
82 // There may be 2 enPassant squares (if pawn jump 3 squares)
85 const L
= this.epSquares
.length
;
86 if (!this.epSquares
[L
-1])
87 return "-"; //no en-passant
89 this.epSquares
[L
-1].forEach(sq
=> {
90 res
+= V
.CoordsToSquare(sq
) + ",";
92 return res
.slice(0,-1); //remove last comma
95 // En-passant after 2-sq or 3-sq jumps
96 getEpSquare(moveOrSquare
)
100 if (typeof moveOrSquare
=== "string")
102 const square
= moveOrSquare
;
106 square
.split(",").forEach(sq
=> {
107 res
.push(V
.SquareToCoords(sq
));
111 // Argument is a move:
112 const move = moveOrSquare
;
113 const [sx
,sy
,ex
] = [move.start
.x
,move.start
.y
,move.end
.x
];
114 if (this.getPiece(sx
,sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2)
116 const step
= (ex
-sx
) / Math
.abs(ex
-sx
);
121 if (sx
+ 2*step
!= ex
) //3-squares move
130 return undefined; //default
133 getPotentialMovesFrom([x
,y
])
135 switch (this.getPiece(x
,y
))
138 return this.getPotentialMarshallMoves([x
,y
]);
140 return this.getPotentialCardinalMoves([x
,y
]);
142 return super.getPotentialMovesFrom([x
,y
])
146 // Special pawn rules: promotions to captured friendly pieces,
147 // optional on ranks 8-9 and mandatory on rank 10.
148 getPotentialPawnMoves([x
,y
])
150 const color
= this.turn
;
152 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
153 const shift
= (color
== "w" ? -1 : 1);
154 const startRanks
= (color
== "w" ? [sizeX
-2,sizeX
-3] : [1,2]);
155 const lastRanks
= (color
== "w" ? [0,1,2] : [sizeX
-1,sizeX
-2,sizeX
-3]);
157 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRanks
[0])
160 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
162 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
163 if (startRanks
.includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
166 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
167 if (x
== startRanks
[0] && this.board
[x
+3*shift
][y
] == V
.EMPTY
)
170 moves
.push(this.getBasicMove([x
,y
], [x
+3*shift
,y
]));
175 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
176 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
178 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1]));
180 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
181 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
183 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1]));
187 if (lastRanks
.includes(x
+shift
))
190 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
,V
.MARSHALL
,V
.CARDINAL
];
191 promotionPieces
.forEach(p
=> {
192 if (this.captured
[color
][p
]==0)
195 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
196 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:p
}));
198 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
199 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
201 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:p
}));
203 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
204 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
206 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:p
}));
212 const Lep
= this.epSquares
.length
;
213 const epSquare
= Lep
>0 ? this.epSquares
[Lep
-1] : undefined;
216 for (let epsq
of epSquare
)
218 // TODO: some redundant checks
219 if (epsq
.x
== x
+shift
&& Math
.abs(epsq
.y
- y
) == 1)
221 let epStep
= epsq
.y
- y
;
222 var enpassantMove
= this.getBasicMove([x
,y
], [x
+shift
,y
+epStep
]);
223 enpassantMove
.vanish
.push({
227 c: this.getColor(x
,y
+epStep
)
229 moves
.push(enpassantMove
);
237 // TODO: different castle?
239 getPotentialMarshallMoves(sq
)
241 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
242 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"));
245 getPotentialCardinalMoves(sq
)
247 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
248 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"));
251 isAttacked(sq
, colors
)
253 return super.isAttacked(sq
, colors
)
254 || this.isAttackedByMarshall(sq
, colors
)
255 || this.isAttackedByCardinal(sq
, colors
);
258 isAttackedByMarshall(sq
, colors
)
260 return this.isAttackedBySlideNJump(sq
, colors
, V
.MARSHALL
, V
.steps
[V
.ROOK
])
261 || this.isAttackedBySlideNJump(
262 sq
, colors
, V
.MARSHALL
, V
.steps
[V
.KNIGHT
], "oneStep");
265 isAttackedByCardinal(sq
, colors
)
267 return this.isAttackedBySlideNJump(sq
, colors
, V
.CARDINAL
, V
.steps
[V
.BISHOP
])
268 || this.isAttackedBySlideNJump(
269 sq
, colors
, V
.CARDINAL
, V
.steps
[V
.KNIGHT
], "oneStep");
272 updateVariables(move)
274 super.updateVariables(move);
275 if (move.vanish
.length
==2 && move.appear
.length
==1 && move.vanish
[1].p
!= V
.PAWN
)
277 // Capture: update this.captured
278 this.captured
[move.vanish
[1].c
][move.vanish
[1].p
]++;
282 unupdateVariables(move)
284 super.unupdateVariables(move);
285 if (move.vanish
.length
==2 && move.appear
.length
==1 && move.vanish
[1].p
!= V
.PAWN
)
286 this.captured
[move.vanish
[1].c
][move.vanish
[1].p
]--;
291 return Object
.assign(
293 {'c': 5, 'm': 7} //experimental
297 static get SEARCH_DEPTH() { return 2; }
299 // TODO: this function could be generalized and shared better (how ?!...)
300 static GenRandInitFen()
302 let pieces
= { "w": new Array(10), "b": new Array(10) };
303 // Shuffle pieces on first and last rank
304 for (let c
of ["w","b"])
306 let positions
= _
.range(10);
308 // Get random squares for bishops
309 let randIndex
= 2 * _
.random(4);
310 let bishop1Pos
= positions
[randIndex
];
311 // The second bishop must be on a square of different color
312 let randIndex_tmp
= 2 * _
.random(4) + 1;
313 let bishop2Pos
= positions
[randIndex_tmp
];
314 // Remove chosen squares
315 positions
.splice(Math
.max(randIndex
,randIndex_tmp
), 1);
316 positions
.splice(Math
.min(randIndex
,randIndex_tmp
), 1);
318 // Get random squares for knights
319 randIndex
= _
.random(7);
320 let knight1Pos
= positions
[randIndex
];
321 positions
.splice(randIndex
, 1);
322 randIndex
= _
.random(6);
323 let knight2Pos
= positions
[randIndex
];
324 positions
.splice(randIndex
, 1);
326 // Get random square for queen
327 randIndex
= _
.random(5);
328 let queenPos
= positions
[randIndex
];
329 positions
.splice(randIndex
, 1);
331 // ...random square for marshall
332 randIndex
= _
.random(4);
333 let marshallPos
= positions
[randIndex
];
334 positions
.splice(randIndex
, 1);
336 // ...random square for cardinal
337 randIndex
= _
.random(3);
338 let cardinalPos
= positions
[randIndex
];
339 positions
.splice(randIndex
, 1);
341 // Rooks and king positions are now fixed, because of the ordering rook-king-rook
342 let rook1Pos
= positions
[0];
343 let kingPos
= positions
[1];
344 let rook2Pos
= positions
[2];
346 // Finally put the shuffled pieces in the board array
347 pieces
[c
][rook1Pos
] = 'r';
348 pieces
[c
][knight1Pos
] = 'n';
349 pieces
[c
][bishop1Pos
] = 'b';
350 pieces
[c
][queenPos
] = 'q';
351 pieces
[c
][marshallPos
] = 'm';
352 pieces
[c
][cardinalPos
] = 'c';
353 pieces
[c
][kingPos
] = 'k';
354 pieces
[c
][bishop2Pos
] = 'b';
355 pieces
[c
][knight2Pos
] = 'n';
356 pieces
[c
][rook2Pos
] = 'r';
358 return pieces
["b"].join("") +
359 "/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/" +
360 pieces
["w"].join("").toUpperCase() +
361 " w 1111 - 0000000000";
365 const VariantRules
= GrandRules
;