update
[xogo.git] / variants / Coregal / class.js
1 import ChessRules from "/base_rules.js";
2
3 export default class CoregalRules extends ChessRules {
4
5 genRandInitBaseFen() {
6 const s = FenUtil.setupPieces(
7 ['r', 'n', 'b', 'l', 'k', 'b', 'n', 'r'],
8 {
9 randomness: this.options["randomness"],
10 between: [{p1: 'k', p2: 'r'}, {p1: 'l', p2: 'r'}],
11 diffCol: ['b'],
12 // 'k' and 'l' useful only to get relative position
13 flags: ['r', 'k', 'l']
14 }
15 );
16 return {
17 fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
18 s.w.join("").toUpperCase(),
19 // TODO: re-arrange flags, use another init variable "relPos" (in o)
20 // (maybe after FEN parsing, easier?)
21 o: {flags: s.flags + s.flags} //second set for royal queen
22 };
23 }
24
25 pieces() {
26 let res = super.pieces();
27 res['l'] = JSON.parse(JSON.stringify(res['q']));
28 // TODO: CSS royal queen symbol (with cross?)
29 res['l']["class"] = "royal_queen";
30 return res;
31 }
32
33 // TODO: something like that indeed (+ flags to FEN)
34 setFlags(fenflags) {
35 this.castleFlags = {
36 'k': { 'w': [...Array(4)], b: [...Array(4)] },
37 'l': { 'w': [...Array(4)], b: [...Array(4)] }
38 };
39 for (let i = 0; i < 8; i++) {
40 this.castleFlags[i < 4 ? "k" : "l"][i % 4 < 2 ? "w" : "b"] =
41 parseInt(fenflags.charAt(i), 10);
42 }
43 }
44
45 isKing(x, y, p) {
46 if (!p)
47 p = this.getPiece(x, y);
48 ['k', 'l'].includes(p); //no cannibal mode
49 }
50
51 getCastleMoves([x, y]) {
52 const c = this.getColor(x, y),
53 p = this.getPiece(x, y);
54 // Relative position of the selected piece: left or right ?
55 // If left: small castle left, large castle right.
56 // If right: usual situation.
57 const finalSquares = [
58 this.relPos[c][p] == "left" ? [1, 2] : [2, 3],
59 this.relPos[c][p] == "right" ? [6, 5] : [5, 4]
60 ];
61 const moves =
62 super.getCastleMoves([x, y], finalSquares, null, this.castleFlags[p]);
63 return moves;
64 }
65
66 // TODO: updateFlags (just pass castleFlags arg)
67
68 };