1 //https://www.chessvariants.com/large.dir/freeling.html
2 class GrandRules
extends ChessRules
6 const V
= VariantRules
;
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 [10,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
) == VariantRules
.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
))
48 case VariantRules
.MARSHALL:
49 return this.getPotentialMarshallMoves([x
,y
]);
50 case VariantRules
.CARDINAL:
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 V
= VariantRules
;
64 const [sizeX
,sizeY
] = VariantRules
.size
;
65 const shift
= (color
== "w" ? -1 : 1);
66 const startRanks
= (color
== "w" ? [sizeY
-2,sizeY
-3] : [1,2]);
67 const lastRanks
= (color
== "w" ? [0,1,2] : [sizeY
-1,sizeY
-2,sizeY
-3]);
69 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRanks
[0])
72 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
74 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
75 if (startRanks
.includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
78 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
79 if (x
== startRanks
[0] && this.board
[x
+3*shift
][y
] == V
.EMPTY
)
82 moves
.push(this.getBasicMove([x
,y
], [x
+3*shift
,y
]));
87 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1]) && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
88 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1]));
89 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1]) && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
90 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1]));
93 if (lastRanks
.includes(x
+shift
))
96 let promotionPieces
= [V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
,V
.MARSHALL
,V
.CARDINAL
];
97 promotionPieces
.forEach(p
=> {
98 if (!this.captures
[color
][p
] || this.captures
[color
][p
]==0)
101 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
102 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:p
}));
104 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1]) && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
105 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:p
}));
106 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1]) && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
107 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:p
}));
112 const Lep
= this.epSquares
.length
;
113 const epSquare
= Lep
>0 ? this.epSquares
[Lep
-1] : undefined;
116 for (let epsq
of epSquare
)
118 // TODO: some redundant checks
119 if (epsq
.x
== x
+shift
&& Math
.abs(epsq
.y
- y
) == 1)
121 let epStep
= epsq
.y
- y
;
122 var enpassantMove
= this.getBasicMove([x
,y
], [x
+shift
,y
+epStep
]);
123 enpassantMove
.vanish
.push({
127 c: this.getColor(x
,y
+epStep
)
129 moves
.push(enpassantMove
);
137 // TODO: different castle?
139 getPotentialMarshallMoves(sq
)
141 const V
= VariantRules
;
142 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
143 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"));
146 getPotentialCardinalMoves(sq
)
148 const V
= VariantRules
;
149 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
150 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep"));
153 isAttacked(sq
, colors
)
155 return super.isAttacked(sq
, colors
)
156 || this.isAttackedByMarshall(sq
, colors
)
157 || this.isAttackedByCardinal(sq
, colors
);
160 isAttackedByMarshall(sq
, colors
)
162 const V
= VariantRules
;
163 return this.isAttackedBySlideNJump(sq
, colors
, V
.MARSHALL
, V
.steps
[V
.ROOK
])
164 || this.isAttackedBySlideNJump(sq
, colors
, V
.MARSHALL
, V
.steps
[V
.KNIGHT
], "oneStep");
167 isAttackedByCardinal(sq
, colors
)
169 const V
= VariantRules
;
170 return this.isAttackedBySlideNJump(sq
, colors
, V
.CARDINAL
, V
.steps
[V
.BISHOP
])
171 || this.isAttackedBySlideNJump(sq
, colors
, V
.CARDINAL
, V
.steps
[V
.KNIGHT
], "oneStep");
176 super.play(move, ingame
);
177 if (move.vanish
.length
==2 && move.appear
.length
==1
178 && move.vanish
[1].p
!= VariantRules
.PAWN
)
180 // Capture: update this.captures
181 if (!this.captures
[move.vanish
[1].c
][move.vanish
[1].p
])
182 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
] = 1;
184 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
]++;
191 if (move.vanish
.length
==2 && move.appear
.length
==1
192 && move.vanish
[1].p
!= VariantRules
.PAWN
)
194 // Capture: update this.captures
195 this.captures
[move.vanish
[1].c
][move.vanish
[1].p
] =
196 Math
.max(0, this.captures
[move.vanish
[1].c
][move.vanish
[1].p
]-1);
200 static get VALUES() {
201 return Object
.assign(
203 {'c': 5, 'm': 7} //experimental
207 static get SEARCH_DEPTH() { return 2; }
209 // TODO: this function could be generalized and shared better
210 static GenRandInitFen()
212 let pieces
= [new Array(10), new Array(10)];
213 // Shuffle pieces on first and last rank
214 for (let c
= 0; c
<= 1; c
++)
216 let positions
= _
.range(10);
218 // Get random squares for bishops
219 let randIndex
= 2 * _
.random(4);
220 let bishop1Pos
= positions
[randIndex
];
221 // The second bishop must be on a square of different color
222 let randIndex_tmp
= 2 * _
.random(4) + 1;
223 let bishop2Pos
= positions
[randIndex_tmp
];
224 // Remove chosen squares
225 positions
.splice(Math
.max(randIndex
,randIndex_tmp
), 1);
226 positions
.splice(Math
.min(randIndex
,randIndex_tmp
), 1);
228 // Get random squares for knights
229 randIndex
= _
.random(7);
230 let knight1Pos
= positions
[randIndex
];
231 positions
.splice(randIndex
, 1);
232 randIndex
= _
.random(6);
233 let knight2Pos
= positions
[randIndex
];
234 positions
.splice(randIndex
, 1);
236 // Get random square for queen
237 randIndex
= _
.random(5);
238 let queenPos
= positions
[randIndex
];
239 positions
.splice(randIndex
, 1);
241 // ...random square for marshall
242 randIndex
= _
.random(4);
243 let marshallPos
= positions
[randIndex
];
244 positions
.splice(randIndex
, 1);
246 // ...random square for cardinal
247 randIndex
= _
.random(3);
248 let cardinalPos
= positions
[randIndex
];
249 positions
.splice(randIndex
, 1);
251 // Rooks and king positions are now fixed, because of the ordering rook-king-rook
252 let rook1Pos
= positions
[0];
253 let kingPos
= positions
[1];
254 let rook2Pos
= positions
[2];
256 // Finally put the shuffled pieces in the board array
257 pieces
[c
][rook1Pos
] = 'r';
258 pieces
[c
][knight1Pos
] = 'n';
259 pieces
[c
][bishop1Pos
] = 'b';
260 pieces
[c
][queenPos
] = 'q';
261 pieces
[c
][marshallPos
] = 'm';
262 pieces
[c
][cardinalPos
] = 'c';
263 pieces
[c
][kingPos
] = 'k';
264 pieces
[c
][bishop2Pos
] = 'b';
265 pieces
[c
][knight2Pos
] = 'n';
266 pieces
[c
][rook2Pos
] = 'r';
268 let fen
= pieces
[0].join("") +
269 "/pppppppppp/10/10/10/10/10/10/PPPPPPPPPP/" +
270 pieces
[1].join("").toUpperCase() +