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
{
7 static get PawnSpecs() {
15 static get HasCastle() {
19 static get ANTIKING() {
24 return ChessRules
.PIECES
.concat([V
.ANTIKING
]);
28 return b
[1] == "a" ? "Antiking/" + b : b
;
31 static IsGoodPosition(position
) {
32 if (!ChessRules
.IsGoodPosition(position
)) return false;
33 const rows
= position
.split("/");
34 // Check that exactly one antiking of each color is there:
35 let antikings
= { 'a': 0, 'A': 0 };
36 for (let row
of rows
) {
37 for (let i
= 0; i
< row
.length
; i
++)
38 if (['A','a'].includes(row
[i
])) antikings
[row
[i
]]++;
40 if (Object
.values(antikings
).some(v
=> v
!= 1)) return false;
44 setOtherVariables(fen
) {
45 super.setOtherVariables(fen
);
46 this.antikingPos
= { w: [-1, -1], b: [-1, -1] };
47 const rows
= V
.ParseFen(fen
).position
.split("/");
48 for (let i
= 0; i
< rows
.length
; i
++) {
50 for (let j
= 0; j
< rows
[i
].length
; j
++) {
51 switch (rows
[i
].charAt(j
)) {
53 this.antikingPos
["b"] = [i
, k
];
56 this.antikingPos
["w"] = [i
, k
];
59 const num
= parseInt(rows
[i
].charAt(j
));
60 if (!isNaN(num
)) k
+= num
- 1;
68 // (Anti)King flags at 1 (true) if they can knight-jump
72 w: [...Array(2).fill(false)],
73 b: [...Array(2).fill(false)]
75 for (let c
of ["w", "b"]) {
76 for (let i
= 0; i
< 2; i
++)
77 this.kingFlags
[c
][i
] = fenflags
.charAt((c
== "w" ? 0 : 2) + i
) == "1";
82 return this.kingFlags
;
85 disaggregateFlags(flags
) {
86 this.kingFlags
= flags
;
92 for (let c
of ["w", "b"]) {
93 for (let i
= 0; i
< 2; i
++) flags
+= this.kingFlags
[c
][i
] ? "1" : "0";
98 canTake([x1
, y1
], [x2
, y2
]) {
99 const piece1
= this.getPiece(x1
, y1
);
100 const piece2
= this.getPiece(x2
, y2
);
101 const color1
= this.getColor(x1
, y1
);
102 const color2
= this.getColor(x2
, y2
);
105 ((piece1
!= "a" && color1
!= color2
) ||
106 (piece1
== "a" && color1
== color2
))
110 getPotentialMovesFrom([x
, y
]) {
112 let addKnightJumps
= false;
113 const piece
= this.getPiece(x
, y
);
114 const color
= this.getColor(x
, y
);
115 if (piece
== V
.ANTIKING
) {
116 moves
= this.getPotentialAntikingMoves([x
, y
]);
117 addKnightJumps
= this.kingFlags
[color
][1];
119 moves
= super.getPotentialMovesFrom([x
, y
]);
120 if (piece
== V
.KING
) addKnightJumps
= this.kingFlags
[color
][0];
122 if (addKnightJumps
) {
123 // Add potential knight jump to (anti)kings
124 const knightJumps
= super.getPotentialKnightMoves([x
, y
]);
125 // Remove captures (TODO: could be done more efficiently...)
126 moves
= moves
.concat(knightJumps
.filter(m
=> m
.vanish
.length
== 1));
131 getPotentialAntikingMoves(sq
) {
132 // The antiking moves like a king (only captured colors differ)
133 return this.getSlideNJumpMoves(
135 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
140 isAttacked(sq
, color
) {
142 super.isAttacked(sq
, color
) ||
143 this.isAttackedByAntiking(sq
, color
)
147 isAttackedByKing([x
, y
], color
) {
148 // Antiking is not attacked by king:
149 if (this.getPiece(x
, y
) == V
.ANTIKING
) return false;
150 return this.isAttackedBySlideNJump(
154 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
159 isAttackedByAntiking([x
, y
], color
) {
160 // (Anti)King is not attacked by antiking
161 if ([V
.KING
, V
.ANTIKING
].includes(this.getPiece(x
, y
))) return false;
162 return this.isAttackedBySlideNJump(
166 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
172 const oppCol
= V
.GetOppCol(color
);
174 this.isAttacked(this.kingPos
[color
], oppCol
) ||
175 !this.isAttacked(this.antikingPos
[color
], oppCol
);
179 getCheckSquares(color
) {
181 const oppCol
= V
.GetOppCol(color
);
182 if (this.isAttacked(this.kingPos
[color
], oppCol
))
183 res
.push(JSON
.parse(JSON
.stringify(this.kingPos
[color
])));
184 if (!this.isAttacked(this.antikingPos
[color
], oppCol
))
185 res
.push(JSON
.parse(JSON
.stringify(this.antikingPos
[color
])));
190 super.postPlay(move);
191 const piece
= move.vanish
[0].p
;
192 const c
= move.vanish
[0].c
;
193 // Update antiking position, and kings flags
194 if (piece
== V
.ANTIKING
) {
195 this.antikingPos
[c
][0] = move.appear
[0].x
;
196 this.antikingPos
[c
][1] = move.appear
[0].y
;
197 this.kingFlags
[c
][1] = false;
198 } else if (piece
== V
.KING
) this.kingFlags
[c
][0] = false;
202 super.postUndo(move);
203 const c
= move.vanish
[0].c
;
204 if (move.vanish
[0].p
== V
.ANTIKING
)
205 this.antikingPos
[c
] = [move.start
.x
, move.start
.y
];
208 static get VALUES() {
209 return Object
.assign(
215 static GenRandInitFen() {
216 // Always deterministic setup
217 return "2prbkqA/2p1nnbr/2pppppp/8/8/PPPPPP2/RBNN1P2/aQKBRP2 w 0 1111";
220 static get SEARCH_DEPTH() {