1 import { ChessRules
} from "@/base_rules";
2 import { BerolinaRules
} from "@/variants/Berolina";
3 import { ArrayFun
} from "@/utils/array";
4 import { randInt
} from "@/utils/alea";
6 export class Antiking1Rules
extends BerolinaRules
{
8 static get PawnSpecs() {
16 static get HasCastle() {
20 static get ANTIKING() {
25 return ChessRules
.PIECES
.concat([V
.ANTIKING
]);
29 return b
[1] == "a" ? "Antiking/" + b : b
;
32 static IsGoodPosition(position
) {
33 if (!ChessRules
.IsGoodPosition(position
)) return false;
34 const rows
= position
.split("/");
35 // Check that exactly one antiking of each color is there:
36 let antikings
= { 'a': 0, 'A': 0 };
37 for (let row
of rows
) {
38 for (let i
= 0; i
< row
.length
; i
++)
39 if (['A','a'].includes(row
[i
])) antikings
[row
[i
]]++;
41 if (Object
.values(antikings
).some(v
=> v
!= 1)) return false;
45 setOtherVariables(fen
) {
46 super.setOtherVariables(fen
);
47 this.antikingPos
= { w: [-1, -1], b: [-1, -1] };
48 const rows
= V
.ParseFen(fen
).position
.split("/");
49 for (let i
= 0; i
< rows
.length
; i
++) {
51 for (let j
= 0; j
< rows
[i
].length
; j
++) {
52 switch (rows
[i
].charAt(j
)) {
54 this.antikingPos
["b"] = [i
, k
];
57 this.antikingPos
["w"] = [i
, k
];
60 const num
= parseInt(rows
[i
].charAt(j
), 10);
61 if (!isNaN(num
)) k
+= num
- 1;
69 // (Anti)King flags at 1 (true) if they can knight-jump
73 w: [...Array(2).fill(false)],
74 b: [...Array(2).fill(false)]
76 for (let c
of ["w", "b"]) {
77 for (let i
= 0; i
< 2; i
++)
78 this.kingFlags
[c
][i
] = fenflags
.charAt((c
== "w" ? 0 : 2) + i
) == "1";
83 return this.kingFlags
;
86 disaggregateFlags(flags
) {
87 this.kingFlags
= flags
;
93 for (let c
of ["w", "b"]) {
94 for (let i
= 0; i
< 2; i
++) flags
+= this.kingFlags
[c
][i
] ? "1" : "0";
99 canTake([x1
, y1
], [x2
, y2
]) {
100 const piece1
= this.getPiece(x1
, y1
);
101 const piece2
= this.getPiece(x2
, y2
);
102 const color1
= this.getColor(x1
, y1
);
103 const color2
= this.getColor(x2
, y2
);
106 ((piece1
!= "a" && color1
!= color2
) ||
107 (piece1
== "a" && color1
== color2
))
111 getPotentialMovesFrom([x
, y
]) {
113 let addKnightJumps
= false;
114 const piece
= this.getPiece(x
, y
);
115 const color
= this.getColor(x
, y
);
116 if (piece
== V
.ANTIKING
) {
117 moves
= this.getPotentialAntikingMoves([x
, y
]);
118 addKnightJumps
= this.kingFlags
[color
][1];
120 moves
= super.getPotentialMovesFrom([x
, y
]);
121 if (piece
== V
.KING
) addKnightJumps
= this.kingFlags
[color
][0];
123 if (addKnightJumps
) {
124 // Add potential knight jump to (anti)kings
125 const knightJumps
= super.getPotentialKnightMoves([x
, y
]);
126 // Remove captures (TODO: could be done more efficiently...)
127 moves
= moves
.concat(knightJumps
.filter(m
=> m
.vanish
.length
== 1));
132 getPotentialAntikingMoves(sq
) {
133 // The antiking moves like a king (only captured colors differ)
134 return this.getSlideNJumpMoves(
136 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
141 isAttacked(sq
, color
) {
143 super.isAttacked(sq
, color
) ||
144 this.isAttackedByAntiking(sq
, color
)
148 isAttackedByKing([x
, y
], color
) {
149 // Antiking is not attacked by king:
150 if (this.getPiece(x
, y
) == V
.ANTIKING
) return false;
151 return this.isAttackedBySlideNJump(
155 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
160 isAttackedByAntiking([x
, y
], color
) {
161 // (Anti)King is not attacked by antiking
162 if ([V
.KING
, V
.ANTIKING
].includes(this.getPiece(x
, y
))) return false;
163 return this.isAttackedBySlideNJump(
167 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
173 const oppCol
= V
.GetOppCol(color
);
175 this.isAttacked(this.kingPos
[color
], oppCol
) ||
176 !this.isAttacked(this.antikingPos
[color
], oppCol
);
181 const color
= this.turn
;
183 const oppCol
= V
.GetOppCol(color
);
184 if (this.isAttacked(this.kingPos
[color
], oppCol
))
185 res
.push(JSON
.parse(JSON
.stringify(this.kingPos
[color
])));
186 if (!this.isAttacked(this.antikingPos
[color
], oppCol
))
187 res
.push(JSON
.parse(JSON
.stringify(this.antikingPos
[color
])));
192 super.postPlay(move);
193 const piece
= move.vanish
[0].p
;
194 const c
= move.vanish
[0].c
;
195 // Update antiking position, and kings flags
196 if (piece
== V
.ANTIKING
) {
197 this.antikingPos
[c
][0] = move.appear
[0].x
;
198 this.antikingPos
[c
][1] = move.appear
[0].y
;
199 this.kingFlags
[c
][1] = false;
200 } else if (piece
== V
.KING
) this.kingFlags
[c
][0] = false;
204 super.postUndo(move);
205 const c
= move.vanish
[0].c
;
206 if (move.vanish
[0].p
== V
.ANTIKING
)
207 this.antikingPos
[c
] = [move.start
.x
, move.start
.y
];
210 static get VALUES() {
211 return Object
.assign(
217 static GenRandInitFen() {
218 // Always deterministic setup
219 return "2prbkqA/2p1nnbr/2pppppp/8/8/PPPPPP2/RBNN1P2/aQKBRP2 w 0 1111";
222 static get SEARCH_DEPTH() {