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
21 // En-passant after 2-sq or 3-sq jumps
24 const [sx
,sy
,ex
] = [move.start
.x
,move.start
.y
,move.end
.x
];
25 if (this.getPiece(sx
,sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2)
27 const step
= (ex
-sx
) / Math
.abs(ex
-sx
);
32 if (sx
+ 2*step
!= ex
) //3-squares move
41 return undefined; //default
44 getPotentialMovesFrom([x
,y
])
46 switch (this.getPiece(x
,y
))
49 return this.getPotentialMarshallMoves([x
,y
]);
51 return this.getPotentialCardinalMoves([x
,y
]);
53 return super.getPotentialMovesFrom([x
,y
])
57 // Special pawn rules: promotions to captured friendly pieces,
58 // optional on ranks 8-9 and mandatory on rank 10.
59 getPotentialPawnMoves([x
,y
])
61 const color
= this.turn
;
63 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
64 const shift
= (color
== "w" ? -1 : 1);
65 const startRanks
= (color
== "w" ? [sizeX
-2,sizeX
-3] : [1,2]);
66 const lastRanks
= (color
== "w" ? [0,1,2] : [sizeX
-1,sizeX
-2,sizeX
-3]);
68 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRanks
[0])
71 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
73 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
74 if (startRanks
.includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
77 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
78 if (x
== startRanks
[0] && this.board
[x
+3*shift
][y
] == V
.EMPTY
)
81 moves
.push(this.getBasicMove([x
,y
], [x
+3*shift
,y
]));
86 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
87 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
89 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1]));
91 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
92 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
94 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1]));
98 if (lastRanks
.includes(x
+shift
))
101 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
,V
.MARSHALL
,V
.CARDINAL
];
102 promotionPieces
.forEach(p
=> {
103 if (!this.captures
[color
][p
] || this.captures
[color
][p
]==0)
106 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
107 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:p
}));
109 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
110 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
112 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:p
}));
114 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
115 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
117 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:p
}));
123 const Lep
= this.epSquares
.length
;
124 const epSquare
= Lep
>0 ? this.epSquares
[Lep
-1] : undefined;
127 for (let epsq
of epSquare
)
129 // TODO: some redundant checks
130 if (epsq
.x
== x
+shift
&& Math
.abs(epsq
.y
- y
) == 1)
132 let epStep
= epsq
.y
- y
;
133 var enpassantMove
= this.getBasicMove([x
,y
], [x
+shift
,y
+epStep
]);
134 enpassantMove
.vanish
.push({
138 c: this.getColor(x
,y
+epStep
)
140 moves
.push(enpassantMove
);
148 // TODO: different castle?
150 getPotentialMarshallMoves(sq
)
152 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
153 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"));
156 getPotentialCardinalMoves(sq
)
158 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
159 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"));
162 isAttacked(sq
, colors
)
164 return super.isAttacked(sq
, colors
)
165 || this.isAttackedByMarshall(sq
, colors
)
166 || this.isAttackedByCardinal(sq
, colors
);
169 isAttackedByMarshall(sq
, colors
)
171 return this.isAttackedBySlideNJump(sq
, colors
, V
.MARSHALL
, V
.steps
[V
.ROOK
])
172 || this.isAttackedBySlideNJump(
173 sq
, colors
, V
.MARSHALL
, V
.steps
[V
.KNIGHT
], "oneStep");
176 isAttackedByCardinal(sq
, colors
)
178 return this.isAttackedBySlideNJump(sq
, colors
, V
.CARDINAL
, V
.steps
[V
.BISHOP
])
179 || this.isAttackedBySlideNJump(
180 sq
, colors
, V
.CARDINAL
, V
.steps
[V
.KNIGHT
], "oneStep");
183 updateVariables(move)
185 super.updateVariables(move);
186 if (move.vanish
.length
==2 && move.appear
.length
==1 && move.vanish
[1].p
!= V
.PAWN
)
188 // Capture: update this.captures
189 if (!this.captures
[move.vanish
[1].c
][move.vanish
[1].p
])
190 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
] = 1;
192 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
]++;
196 unupdateVariables(move)
198 super.unupdateVariables(move);
199 if (move.vanish
.length
==2 && move.appear
.length
==1 && move.vanish
[1].p
!= V
.PAWN
)
201 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
] =
202 Math
.max(0, this.captures
[move.vanish
[1].c
][move.vanish
[1].p
]-1);
206 static get VALUES() {
207 return Object
.assign(
209 {'c': 5, 'm': 7} //experimental
213 static get SEARCH_DEPTH() { return 2; }
215 // TODO: this function could be generalized and shared better
216 static GenRandInitFen()
218 let pieces
= { "w": new Array(10), "b": new Array(10) };
219 // Shuffle pieces on first and last rank
220 for (let c
of ["w","b"])
222 let positions
= _
.range(10);
224 // Get random squares for bishops
225 let randIndex
= 2 * _
.random(4);
226 let bishop1Pos
= positions
[randIndex
];
227 // The second bishop must be on a square of different color
228 let randIndex_tmp
= 2 * _
.random(4) + 1;
229 let bishop2Pos
= positions
[randIndex_tmp
];
230 // Remove chosen squares
231 positions
.splice(Math
.max(randIndex
,randIndex_tmp
), 1);
232 positions
.splice(Math
.min(randIndex
,randIndex_tmp
), 1);
234 // Get random squares for knights
235 randIndex
= _
.random(7);
236 let knight1Pos
= positions
[randIndex
];
237 positions
.splice(randIndex
, 1);
238 randIndex
= _
.random(6);
239 let knight2Pos
= positions
[randIndex
];
240 positions
.splice(randIndex
, 1);
242 // Get random square for queen
243 randIndex
= _
.random(5);
244 let queenPos
= positions
[randIndex
];
245 positions
.splice(randIndex
, 1);
247 // ...random square for marshall
248 randIndex
= _
.random(4);
249 let marshallPos
= positions
[randIndex
];
250 positions
.splice(randIndex
, 1);
252 // ...random square for cardinal
253 randIndex
= _
.random(3);
254 let cardinalPos
= positions
[randIndex
];
255 positions
.splice(randIndex
, 1);
257 // Rooks and king positions are now fixed, because of the ordering rook-king-rook
258 let rook1Pos
= positions
[0];
259 let kingPos
= positions
[1];
260 let rook2Pos
= positions
[2];
262 // Finally put the shuffled pieces in the board array
263 pieces
[c
][rook1Pos
] = 'r';
264 pieces
[c
][knight1Pos
] = 'n';
265 pieces
[c
][bishop1Pos
] = 'b';
266 pieces
[c
][queenPos
] = 'q';
267 pieces
[c
][marshallPos
] = 'm';
268 pieces
[c
][cardinalPos
] = 'c';
269 pieces
[c
][kingPos
] = 'k';
270 pieces
[c
][bishop2Pos
] = 'b';
271 pieces
[c
][knight2Pos
] = 'n';
272 pieces
[c
][rook2Pos
] = 'r';
274 let fen
= pieces
["b"].join("") +
275 "/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/" +
276 pieces
["w"].join("").toUpperCase() +