f8406b7ebd41ba718c31f7a83ad11c53b1130a8b
[xogo.git] / variants / Antiking1 / class.js
1 import ChessRules from "/base_rules.js";
2 import AbstractAntikingRules from "/variants/AbstractAntiking.js";
3
4 export class Antiking1Rules extends AbstractAntikingRules {
5
6 static get Options() {
7 return {
8 styles: [
9 "atomic",
10 "balance",
11 "cannibal",
12 "capture",
13 "crazyhouse",
14 "doublemove",
15 "madrasi",
16 "progressive",
17 "recycle",
18 "rifle",
19 "teleport",
20 "zen"
21 ]
22 };
23 }
24
25 get hasCastle() {
26 return false;
27 }
28
29 pieces(color, x, y) {
30 const pawnShift = (color == "w" ? -1 : 1);
31 let res = super.pieces(color, x, y);
32 res['p'].moves = [
33 {
34 steps: [[pawnShift, 1], [pawnShift, -1]],
35 range: 1
36 }
37 ];
38 res['p'].attack = [
39 {
40 steps: [[pawnShift, 0]],
41 range: 1
42 }
43 ];
44 return res;
45 }
46
47 genRandInitFen() {
48 // Always deterministic setup
49 return (
50 '2prbkqA/2p1nnbr/2pppppp/8/8/PPPPPP2/RBNN1P2/aQKBRP2 w 0 ' +
51 '{"flags":"KAka"}'
52 );
53 }
54
55 // (Anti)King flags at 1 (true) if they can knight-jump
56 setFlags(fenflags) {
57 this.kingFlags = { w: {}, b: {} };
58 for (let i=0; i<fenFlags.length; i++) {
59 const white = fenFlags.charCodeAt(i) <= 90;
60 const curChar = fenFlags.charCodeAt(i).toLowerCase();
61 this.kingFlags[white ? 'w' : 'b'][curChar] = true;
62 }
63 }
64
65 getFlagsFen() {
66 return (
67 Array.prototype.concat.apply(
68 ['w', 'b'].map(c => Object.keys(this.kingFlags[c]))
69 ).join("")
70 );
71 }
72
73 getPotentialMovesFrom([x, y]) {
74 const color = this.turn;
75 let moves = super.getPotentialMovesFrom([x, y]);
76 if (this.kingFlags[color][piece]) {
77 // Allow knight jump (king or antiking)
78 const knightMoves = super.getPotentialMovesOf('n', [x, y]);
79 // Remove captures (TODO: could be done more efficiently...)
80 moves = moves.concat(knightJumps.filter(m => m.vanish.length == 1));
81 }
82 return moves;
83 }
84
85 prePlay(move) {
86 super.prePlay(move);
87 // Update king+antiking flags
88 const piece = move.vanish[0].p;
89 if (this.isKing(0, 0, piece))
90 delete this.kingFlags[move.vanish[0].c][piece];
91 }
92
93 };