Prepare Coregal variant
[xogo.git] / variants / Coregal / class.js
CommitLineData
5c1c7bce
BA
1import { ChessRules, Move, PiPo } from "@/base_rules";
2import { ArrayFun } from "@/utils/array";
3import { randInt, sample } from "@/utils/alea";
4
5export class CoregalRules extends ChessRules {
6
7//TODO: CSS royal queen symbol
8
9 genRandInitBaseFen() {
10 const s = FenUtil.setupPieces(
11 ['r', 'n', 'b', 'l', 'k', 'b', 'n', 'r'],
12 {
13 randomness: this.options["randomness"],
14 between: [{p1: 'k', p2: 'r'}, {p1: 'l', p2: 'r'}],
15 diffCol: ['b'],
16 flags: ['r', 'k', 'l'] //TODO: add 'k' to all 'flags' calls ??!
17 }
18 );
19 return {
20 fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
21 s.w.join("").toUpperCase(),
22 o: {flags: s.flags}
23 };
24 }
25
26 pieces() {
27 let res = super.pieces();
28 res['l'] = JSON.parse(JSON.stringify(res['q']));
29 res['l']["class"] = "royal_queen";
30 return res;
31 }
32
33 setFlags(fenflags) {
34 // white pieces positions, then black pieces positions
35 this.castleFlags = { w: [...Array(4)], b: [...Array(4)] };
36 for (let i = 0; i < 8; i++) {
37 this.castleFlags[i < 4 ? "w" : "b"][i % 4] =
38 parseInt(fenflags.charAt(i), 10);
39 }
40 }
41
42 isKing(x, y, p) {
43 if (!p)
44 p = this.getPiece(x, y);
45 ['k', 'l'].includes(p); //no cannibal mode
46 }
47
48 getCastleMoves([x, y]) {
49 // Relative position of the selected piece: left or right ?
50 // If left: small castle left, large castle right.
51 // If right: usual situation.
52 const c = this.getColor(x, y);
53 const relPos = (this.castleFlags[c][1] == y ? "left" : "right");
54
55 const finalSquares = [
56 relPos == "left" ? [1, 2] : [2, 3],
57 relPos == "right" ? [6, 5] : [5, 4]
58 ];
59 const saveFlags = JSON.stringify(this.castleFlags[c]);
60 // Alter flags to follow base_rules semantic
61 this.castleFlags[c] = [0, 3].map(i => this.castleFlags[c][i]);
62 const moves = super.getCastleMoves([x, y], finalSquares);
63 this.castleFlags[c] = JSON.parse(saveFlags);
64 return moves;
65 }
66
67};