485bbd63a8cab30eef0209181bcdb0ba4d2f8110
[xogo.git] / variants / Clorange / class.js
1 import ChessRules from "/base_rules.js";
2
3 export default class ClorangeRules extends ChessRules {
4
5 static get Options() {
6 return {
7 select: C.Options.select,
8 styles:
9 C.Options.styles.filter(s => !["crazyhouse","recycle"].includes(s))
10 };
11 }
12
13 get hasReserve() {
14 return true;
15 }
16
17 getReserveFen(o) {
18 if (o.init)
19 return "00000000000000000000";
20 return (
21 ["w","b"].map(c => Object.values(this.reserve[c]).join("")).join("")
22 );
23 }
24
25 pieces(color, x, y) {
26 let res = super.pieces(color, x, y);
27 res['s'] = {"class": "nv-pawn", moveas: "p"};
28 res['u'] = {"class": "nv-rook", moveas: "r"};
29 res['o'] = {"class": "nv-knight", moveas: "n"};
30 res['c'] = {"class": "nv-bishop", moveas: "b"};
31 res['t'] = {"class": "nv-queen", moveas: "q"};
32 return res;
33 }
34
35 static get V_PIECES() {
36 return ['p', 'r', 'n', 'b', 'q'];
37 }
38 static get NV_PIECES() {
39 return ['s', 'u', 'o', 'c', 't'];
40 }
41
42 setOtherVariables(fen) {
43 super.setOtherVariables(fen, V.V_PIECES.concat(V.NV_PIECES));
44 }
45
46 // Forbid non-violent pieces to capture
47 canTake([x1, y1], [x2, y2]) {
48 return (
49 this.getColor(x1, y1) !== this.getColor(x2, y2) &&
50 (['k'].concat(V.V_PIECES)).includes(this.getPiece(x1, y1))
51 );
52 }
53
54 pawnPostProcess(moves, color, oppCols) {
55 let res = super.pawnPostProcess(moves, color, oppCols);
56 if (res.length > 0 && res[0].vanish[0].p == 's') {
57 // Fix promotions of non-violent pawns (if any)
58 res.forEach(m => {
59 if (m.appear[0].p != 's')
60 m.appear[0].p = V.NV_PIECES[V.V_PIECES.indexOf(m.appear[0].p)];
61 });
62 }
63 return res;
64 }
65
66 prePlay(move) {
67 super.prePlay(move);
68 // NOTE: drop moves already taken into account in base prePlay()
69 if (move.vanish.length == 2 && move.appear.length == 1) {
70 const normal = V.V_PIECES.includes(move.vanish[1].p);
71 const pIdx =
72 (normal ? V.V_PIECES : V.NV_PIECES).indexOf(move.vanish[1].p);
73 const resPiece = (normal ? V.NV_PIECES : V.V_PIECES)[pIdx];
74 super.updateReserve(C.GetOppTurn(this.turn), resPiece,
75 this.reserve[C.GetOppTurn(this.turn)][resPiece] + 1);
76 }
77 }
78
79 };