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