e8506db8a83c85e6581f623132967c52d0a5bd70
[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 pieces(color, x, y) {
18 let res = super.pieces(color, x, y);
19 res['s'] = {"class": "nv-pawn", moveas: "p"};
20 res['u'] = {"class": "nv-rook", moveas: "r"};
21 res['o'] = {"class": "nv-knight", moveas: "n"};
22 res['c'] = {"class": "nv-bishop", moveas: "b"};
23 res['t'] = {"class": "nv-queen", moveas: "q"};
24 return res;
25 }
26
27 static get V_PIECES() {
28 return ['p', 'r', 'n', 'b', 'q'];
29 }
30 static get NV_PIECES() {
31 return ['s', 'u', 'o', 'c', 't'];
32 }
33 static get ReserveArray() {
34 return V.V_PIECES.concat(V.NV_PIECES);
35 }
36
37 // Forbid non-violent pieces to capture
38 canTake([x1, y1], [x2, y2]) {
39 return (
40 this.getColor(x1, y1) !== this.getColor(x2, y2) &&
41 (['k'].concat(V.V_PIECES)).includes(this.getPiece(x1, y1))
42 );
43 }
44
45 pawnPostProcess(moves, color, oppCols) {
46 let res = super.pawnPostProcess(moves, color, oppCols);
47 if (res.length > 0 && res[0].vanish[0].p == 's') {
48 // Fix promotions of non-violent pawns (if any)
49 res.forEach(m => {
50 if (m.appear[0].p != 's')
51 m.appear[0].p = V.NV_PIECES[V.V_PIECES.indexOf(m.appear[0].p)];
52 });
53 }
54 return res;
55 }
56
57 prePlay(move) {
58 super.prePlay(move);
59 // NOTE: drop moves already taken into account in base prePlay()
60 if (move.vanish.length == 2 && move.appear.length == 1) {
61 const normal = V.V_PIECES.includes(move.vanish[1].p);
62 const pIdx =
63 (normal ? V.V_PIECES : V.NV_PIECES).indexOf(move.vanish[1].p);
64 const resPiece = (normal ? V.NV_PIECES : V.V_PIECES)[pIdx];
65 super.updateReserve(C.GetOppTurn(this.turn), resPiece,
66 this.reserve[C.GetOppTurn(this.turn)][resPiece] + 1);
67 }
68 }
69
70 };