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