1 import { ChessRules
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
3 import { randInt
} from "@/utils/alea";
5 export class Antiking2Rules
extends ChessRules
{
6 static get ANTIKING() {
11 return ChessRules
.PIECES
.concat([V
.ANTIKING
]);
15 return b
[1] == "a" ? "Antiking/" + b : b
;
18 static IsGoodPosition(position
) {
19 if (!ChessRules
.IsGoodPosition(position
)) return false;
20 const rows
= position
.split("/");
21 // Check that exactly one antiking of each color is there:
22 let antikings
= { 'a': 0, 'A': 0 };
23 for (let row
of rows
) {
24 for (let i
= 0; i
< row
.length
; i
++)
25 if (['A','a'].includes(row
[i
])) antikings
[row
[i
]]++;
27 if (Object
.values(antikings
).some(v
=> v
!= 1)) return false;
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 canTake([x1
, y1
], [x2
, y2
]) {
56 const piece1
= this.getPiece(x1
, y1
);
57 const piece2
= this.getPiece(x2
, y2
);
58 const color1
= this.getColor(x1
, y1
);
59 const color2
= this.getColor(x2
, y2
);
63 (piece1
!= "a" && color1
!= color2
) ||
64 (piece1
== "a" && color1
== color2
)
69 getPotentialMovesFrom([x
, y
]) {
70 switch (this.getPiece(x
, y
)) {
72 return this.getPotentialAntikingMoves([x
, y
]);
74 return super.getPotentialMovesFrom([x
, y
]);
78 getPotentialAntikingMoves(sq
) {
79 // The antiking moves like a king (only captured colors differ)
80 return this.getSlideNJumpMoves(
82 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
87 isAttacked(sq
, color
) {
89 super.isAttacked(sq
, color
) ||
90 this.isAttackedByAntiking(sq
, color
)
94 isAttackedByKing([x
, y
], color
) {
95 // Antiking is not attacked by king:
96 if (this.getPiece(x
, y
) == V
.ANTIKING
) return false;
97 return super.isAttackedByKing([x
, y
], color
);
100 isAttackedByAntiking([x
, y
], color
) {
101 // (Anti)King is not attacked by antiking
102 if ([V
.KING
, V
.ANTIKING
].includes(this.getPiece(x
, y
))) return false;
103 return this.isAttackedBySlideNJump(
107 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
113 const oppCol
= V
.GetOppCol(color
);
115 this.isAttacked(this.kingPos
[color
], oppCol
) ||
116 !this.isAttacked(this.antikingPos
[color
], oppCol
);
120 getCheckSquares(color
) {
122 const oppCol
= V
.GetOppCol(color
);
123 if (this.isAttacked(this.kingPos
[color
], oppCol
))
124 res
.push(JSON
.parse(JSON
.stringify(this.kingPos
[color
])));
125 if (!this.isAttacked(this.antikingPos
[color
], oppCol
))
126 res
.push(JSON
.parse(JSON
.stringify(this.antikingPos
[color
])));
131 super.postPlay(move);
132 const piece
= move.vanish
[0].p
;
133 const c
= move.vanish
[0].c
;
134 // Update antiking position
135 if (piece
== V
.ANTIKING
) {
136 this.antikingPos
[c
][0] = move.appear
[0].x
;
137 this.antikingPos
[c
][1] = move.appear
[0].y
;
142 super.postUndo(move);
143 const c
= move.vanish
[0].c
;
144 if (move.vanish
[0].p
== V
.ANTIKING
)
145 this.antikingPos
[c
] = [move.start
.x
, move.start
.y
];
148 static get VALUES() {
149 return Object
.assign(
155 static GenRandInitFen(randomness
) {
157 return "rnbqkbnr/pppppppp/3A4/8/8/3a4/PPPPPPPP/RNBQKBNR w 0 ahah -";
159 let pieces
= { w: new Array(8), b: new Array(8) };
161 let antikingPos
= { w: -1, b: -1 };
162 for (let c
of ["w", "b"]) {
163 if (c
== 'b' && randomness
== 1) {
164 pieces
['b'] = pieces
['w'];
165 antikingPos
['b'] = antikingPos
['w'];
170 let positions
= ArrayFun
.range(8);
172 // Get random squares for bishops, but avoid corners; because,
173 // if an antiking blocks a cornered bishop, it can never be checkmated
174 let randIndex
= 2 * randInt(1, 4);
175 const bishop1Pos
= positions
[randIndex
];
176 let randIndex_tmp
= 2 * randInt(3) + 1;
177 const bishop2Pos
= positions
[randIndex_tmp
];
178 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
179 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
181 randIndex
= randInt(6);
182 const knight1Pos
= positions
[randIndex
];
183 positions
.splice(randIndex
, 1);
184 randIndex
= randInt(5);
185 const knight2Pos
= positions
[randIndex
];
186 positions
.splice(randIndex
, 1);
188 randIndex
= randInt(4);
189 const queenPos
= positions
[randIndex
];
190 positions
.splice(randIndex
, 1);
192 const rook1Pos
= positions
[0];
193 const kingPos
= positions
[1];
194 const rook2Pos
= positions
[2];
196 // Random squares for antikings
197 antikingPos
[c
] = randInt(8);
199 pieces
[c
][rook1Pos
] = "r";
200 pieces
[c
][knight1Pos
] = "n";
201 pieces
[c
][bishop1Pos
] = "b";
202 pieces
[c
][queenPos
] = "q";
203 pieces
[c
][kingPos
] = "k";
204 pieces
[c
][bishop2Pos
] = "b";
205 pieces
[c
][knight2Pos
] = "n";
206 pieces
[c
][rook2Pos
] = "r";
207 flags
+= V
.CoordToColumn(rook1Pos
) + V
.CoordToColumn(rook2Pos
);
209 const ranks23_black
=
211 (antikingPos
["w"] > 0 ? antikingPos
["w"] : "") +
213 (antikingPos
["w"] < 7 ? 7 - antikingPos
["w"] : "");
214 const ranks23_white
=
215 (antikingPos
["b"] > 0 ? antikingPos
["b"] : "") +
217 (antikingPos
["b"] < 7 ? 7 - antikingPos
["b"] : "") +
220 pieces
["b"].join("") +
226 pieces
["w"].join("").toUpperCase() +
227 " w 0 " + flags
+ " -"
231 static get SEARCH_DEPTH() {