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