9b8af78ecd41905b98d7f8d92da77c33419d6628
[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 genRandInitFen() {
32 // Always deterministic setup
33 return (
34 '2prbkqA/2p1nnbr/2pppppp/8/8/PPPPPP2/RBNN1P2/aQKBRP2 w 0 ' +
35 '{"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 => Object.keys(this.kingFlags[c]))
53 ).join("")
54 );
55 }
56
57 getPotentialMovesFrom([x, y]) {
58 const color = this.turn;
59 let moves = super.getPotentialMovesFrom([x, y]);
60 if (this.kingFlags[color][this.getPiece(x, y)]) {
61 // Allow knight jump (king or antiking)
62 const knightMoves = super.getPotentialMovesOf('n', [x, y]);
63 // Remove captures (TODO: could be done more efficiently...)
64 moves = moves.concat(knightMoves.filter(m => m.vanish.length == 1));
65 }
66 return moves;
67 }
68
69 prePlay(move) {
70 super.prePlay(move);
71 // Update king+antiking flags
72 const piece = move.vanish[0].p;
73 if (this.isKing(0, 0, piece))
74 delete this.kingFlags[move.vanish[0].c][piece];
75 }
76
77 };