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 super.initVariables(fen
);
13 this.captures
= { "w": {}, "b": {} }; //for promotions
16 static get size() { return {x:10,y:10}; }
18 static get MARSHALL() { return 'm'; } //rook+knight
19 static get CARDINAL() { return 'c'; } //bishop+knight
22 return ChessRules
.PIECES
.concat([V
.MARSHALL
,V
.CARDINAL
]);
25 // En-passant after 2-sq or 3-sq jumps
28 const [sx
,sy
,ex
] = [move.start
.x
,move.start
.y
,move.end
.x
];
29 if (this.getPiece(sx
,sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2)
31 const step
= (ex
-sx
) / Math
.abs(ex
-sx
);
36 if (sx
+ 2*step
!= ex
) //3-squares move
45 return undefined; //default
48 getPotentialMovesFrom([x
,y
])
50 switch (this.getPiece(x
,y
))
53 return this.getPotentialMarshallMoves([x
,y
]);
55 return this.getPotentialCardinalMoves([x
,y
]);
57 return super.getPotentialMovesFrom([x
,y
])
61 // Special pawn rules: promotions to captured friendly pieces,
62 // optional on ranks 8-9 and mandatory on rank 10.
63 getPotentialPawnMoves([x
,y
])
65 const color
= this.turn
;
67 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
68 const shift
= (color
== "w" ? -1 : 1);
69 const startRanks
= (color
== "w" ? [sizeX
-2,sizeX
-3] : [1,2]);
70 const lastRanks
= (color
== "w" ? [0,1,2] : [sizeX
-1,sizeX
-2,sizeX
-3]);
72 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRanks
[0])
75 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
77 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
78 if (startRanks
.includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
81 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
82 if (x
== startRanks
[0] && this.board
[x
+3*shift
][y
] == V
.EMPTY
)
85 moves
.push(this.getBasicMove([x
,y
], [x
+3*shift
,y
]));
90 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
91 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
93 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1]));
95 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
96 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
98 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1]));
102 if (lastRanks
.includes(x
+shift
))
105 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
,V
.MARSHALL
,V
.CARDINAL
];
106 promotionPieces
.forEach(p
=> {
107 if (!this.captures
[color
][p
] || this.captures
[color
][p
]==0)
110 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
111 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:p
}));
113 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
114 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
116 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:p
}));
118 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
119 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
121 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:p
}));
127 const Lep
= this.epSquares
.length
;
128 const epSquare
= Lep
>0 ? this.epSquares
[Lep
-1] : undefined;
131 for (let epsq
of epSquare
)
133 // TODO: some redundant checks
134 if (epsq
.x
== x
+shift
&& Math
.abs(epsq
.y
- y
) == 1)
136 let epStep
= epsq
.y
- y
;
137 var enpassantMove
= this.getBasicMove([x
,y
], [x
+shift
,y
+epStep
]);
138 enpassantMove
.vanish
.push({
142 c: this.getColor(x
,y
+epStep
)
144 moves
.push(enpassantMove
);
152 // TODO: different castle?
154 getPotentialMarshallMoves(sq
)
156 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
157 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"));
160 getPotentialCardinalMoves(sq
)
162 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
163 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"));
166 isAttacked(sq
, colors
)
168 return super.isAttacked(sq
, colors
)
169 || this.isAttackedByMarshall(sq
, colors
)
170 || this.isAttackedByCardinal(sq
, colors
);
173 isAttackedByMarshall(sq
, colors
)
175 return this.isAttackedBySlideNJump(sq
, colors
, V
.MARSHALL
, V
.steps
[V
.ROOK
])
176 || this.isAttackedBySlideNJump(
177 sq
, colors
, V
.MARSHALL
, V
.steps
[V
.KNIGHT
], "oneStep");
180 isAttackedByCardinal(sq
, colors
)
182 return this.isAttackedBySlideNJump(sq
, colors
, V
.CARDINAL
, V
.steps
[V
.BISHOP
])
183 || this.isAttackedBySlideNJump(
184 sq
, colors
, V
.CARDINAL
, V
.steps
[V
.KNIGHT
], "oneStep");
187 updateVariables(move)
189 super.updateVariables(move);
190 if (move.vanish
.length
==2 && move.appear
.length
==1 && move.vanish
[1].p
!= V
.PAWN
)
192 // Capture: update this.captures
193 if (!this.captures
[move.vanish
[1].c
][move.vanish
[1].p
])
194 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
] = 1;
196 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
]++;
200 unupdateVariables(move)
202 super.unupdateVariables(move);
203 if (move.vanish
.length
==2 && move.appear
.length
==1 && move.vanish
[1].p
!= V
.PAWN
)
205 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
] =
206 Math
.max(0, this.captures
[move.vanish
[1].c
][move.vanish
[1].p
]-1);
210 static get VALUES() {
211 return Object
.assign(
213 {'c': 5, 'm': 7} //experimental
217 static get SEARCH_DEPTH() { return 2; }
219 // TODO: this function could be generalized and shared better
220 static GenRandInitFen()
222 let pieces
= { "w": new Array(10), "b": new Array(10) };
223 // Shuffle pieces on first and last rank
224 for (let c
of ["w","b"])
226 let positions
= _
.range(10);
228 // Get random squares for bishops
229 let randIndex
= 2 * _
.random(4);
230 let bishop1Pos
= positions
[randIndex
];
231 // The second bishop must be on a square of different color
232 let randIndex_tmp
= 2 * _
.random(4) + 1;
233 let bishop2Pos
= positions
[randIndex_tmp
];
234 // Remove chosen squares
235 positions
.splice(Math
.max(randIndex
,randIndex_tmp
), 1);
236 positions
.splice(Math
.min(randIndex
,randIndex_tmp
), 1);
238 // Get random squares for knights
239 randIndex
= _
.random(7);
240 let knight1Pos
= positions
[randIndex
];
241 positions
.splice(randIndex
, 1);
242 randIndex
= _
.random(6);
243 let knight2Pos
= positions
[randIndex
];
244 positions
.splice(randIndex
, 1);
246 // Get random square for queen
247 randIndex
= _
.random(5);
248 let queenPos
= positions
[randIndex
];
249 positions
.splice(randIndex
, 1);
251 // ...random square for marshall
252 randIndex
= _
.random(4);
253 let marshallPos
= positions
[randIndex
];
254 positions
.splice(randIndex
, 1);
256 // ...random square for cardinal
257 randIndex
= _
.random(3);
258 let cardinalPos
= positions
[randIndex
];
259 positions
.splice(randIndex
, 1);
261 // Rooks and king positions are now fixed, because of the ordering rook-king-rook
262 let rook1Pos
= positions
[0];
263 let kingPos
= positions
[1];
264 let rook2Pos
= positions
[2];
266 // Finally put the shuffled pieces in the board array
267 pieces
[c
][rook1Pos
] = 'r';
268 pieces
[c
][knight1Pos
] = 'n';
269 pieces
[c
][bishop1Pos
] = 'b';
270 pieces
[c
][queenPos
] = 'q';
271 pieces
[c
][marshallPos
] = 'm';
272 pieces
[c
][cardinalPos
] = 'c';
273 pieces
[c
][kingPos
] = 'k';
274 pieces
[c
][bishop2Pos
] = 'b';
275 pieces
[c
][knight2Pos
] = 'n';
276 pieces
[c
][rook2Pos
] = 'r';
278 let fen
= pieces
["b"].join("") +
279 "/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/" +
280 pieces
["w"].join("").toUpperCase() +