3aa02d35f5b8095597bf7a3e8e1a585eee677166
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 setOtherVariables(fen
) {
32 super.setOtherVariables(fen
);
33 this.antikingPos
= { w: [-1, -1], b: [-1, -1] };
34 const rows
= V
.ParseFen(fen
).position
.split("/");
35 for (let i
= 0; i
< rows
.length
; i
++) {
37 for (let j
= 0; j
< rows
[i
].length
; j
++) {
38 switch (rows
[i
].charAt(j
)) {
40 this.antikingPos
["b"] = [i
, k
];
43 this.antikingPos
["w"] = [i
, k
];
46 const num
= parseInt(rows
[i
].charAt(j
));
47 if (!isNaN(num
)) k
+= num
- 1;
55 // (Anti)King flags at 1 (true) if they can knight-jump
59 w: [...Array(2).fill(false)],
60 b: [...Array(2).fill(false)]
62 for (let c
of ["w", "b"]) {
63 for (let i
= 0; i
< 2; i
++)
64 this.kingFlags
[c
][i
] = fenflags
.charAt((c
== "w" ? 0 : 2) + i
) == "1";
69 return this.kingFlags
;
72 disaggregateFlags(flags
) {
73 this.kingFlags
= flags
;
79 for (let c
of ["w", "b"]) {
80 for (let i
= 0; i
< 2; i
++) flags
+= this.kingFlags
[c
][i
] ? "1" : "0";
85 canTake([x1
, y1
], [x2
, y2
]) {
86 const piece1
= this.getPiece(x1
, y1
);
87 const piece2
= this.getPiece(x2
, y2
);
88 const color1
= this.getColor(x1
, y1
);
89 const color2
= this.getColor(x2
, y2
);
92 ((piece1
!= "a" && color1
!= color2
) ||
93 (piece1
== "a" && color1
== color2
))
97 getPotentialMovesFrom([x
, y
]) {
99 let addKnightJumps
= false;
100 const piece
= this.getPiece(x
, y
);
101 const color
= this.getColor(x
, y
);
102 if (piece
== V
.ANTIKING
) {
103 moves
= this.getPotentialAntikingMoves([x
, y
]);
104 addKnightJumps
= this.kingFlags
[color
][1];
106 moves
= super.getPotentialMovesFrom([x
, y
]);
107 if (piece
== V
.KING
) addKnightJumps
= this.kingFlags
[color
][0];
109 if (addKnightJumps
) {
110 // Add potential knight jump to (anti)kings
111 const knightJumps
= super.getPotentialKnightMoves([x
, y
]);
112 // Remove captures (TODO: could be done more efficiently...)
113 moves
= moves
.concat(knightJumps
.filter(m
=> m
.vanish
.length
== 1));
118 getPotentialAntikingMoves(sq
) {
119 // The antiking moves like a king (only captured colors differ)
120 return this.getSlideNJumpMoves(
122 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
127 isAttacked(sq
, color
) {
129 super.isAttacked(sq
, color
) ||
130 this.isAttackedByAntiking(sq
, color
)
134 isAttackedByKing([x
, y
], color
) {
135 // Antiking is not attacked by king:
136 if (this.getPiece(x
, y
) == V
.ANTIKING
) return false;
137 return this.isAttackedBySlideNJump(
141 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
146 isAttackedByAntiking([x
, y
], color
) {
147 // (Anti)King is not attacked by antiking
148 if ([V
.KING
, V
.ANTIKING
].includes(this.getPiece(x
, y
))) return false;
149 return this.isAttackedBySlideNJump(
153 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
159 const oppCol
= V
.GetOppCol(color
);
161 this.isAttacked(this.kingPos
[color
], oppCol
) ||
162 !this.isAttacked(this.antikingPos
[color
], oppCol
);
166 getCheckSquares(color
) {
168 const oppCol
= V
.GetOppCol(color
);
169 if (this.isAttacked(this.kingPos
[color
], oppCol
))
170 res
.push(JSON
.parse(JSON
.stringify(this.kingPos
[color
])));
171 if (!this.isAttacked(this.antikingPos
[color
], oppCol
))
172 res
.push(JSON
.parse(JSON
.stringify(this.antikingPos
[color
])));
177 super.postPlay(move);
178 const piece
= move.vanish
[0].p
;
179 const c
= move.vanish
[0].c
;
180 // Update antiking position, and kings flags
181 if (piece
== V
.ANTIKING
) {
182 this.antikingPos
[c
][0] = move.appear
[0].x
;
183 this.antikingPos
[c
][1] = move.appear
[0].y
;
184 this.kingFlags
[c
][1] = false;
185 } else if (piece
== V
.KING
) this.kingFlags
[c
][0] = false;
189 super.postUndo(move);
190 const c
= move.vanish
[0].c
;
191 if (move.vanish
[0].p
== V
.ANTIKING
)
192 this.antikingPos
[c
] = [move.start
.x
, move.start
.y
];
195 static get VALUES() {
196 return Object
.assign(
202 static GenRandInitFen() {
203 // Always deterministic setup
204 return "2prbkqA/2p1nnbr/2pppppp/8/8/PPPPPP2/RBNN1P2/aQKBRP2 w 0 1111";
207 static get SEARCH_DEPTH() {