087dce35c6e155caf2a3c16dde47828fe5a35121
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 setOtherVariables(fen
) {
19 super.setOtherVariables(fen
);
20 this.antikingPos
= { w: [-1, -1], b: [-1, -1] };
21 const rows
= V
.ParseFen(fen
).position
.split("/");
22 for (let i
= 0; i
< rows
.length
; i
++) {
24 for (let j
= 0; j
< rows
[i
].length
; j
++) {
25 switch (rows
[i
].charAt(j
)) {
27 this.antikingPos
["b"] = [i
, k
];
30 this.antikingPos
["w"] = [i
, k
];
33 const num
= parseInt(rows
[i
].charAt(j
));
34 if (!isNaN(num
)) k
+= num
- 1;
42 canTake([x1
, y1
], [x2
, y2
]) {
43 const piece1
= this.getPiece(x1
, y1
);
44 const piece2
= this.getPiece(x2
, y2
);
45 const color1
= this.getColor(x1
, y1
);
46 const color2
= this.getColor(x2
, y2
);
50 (piece1
!= "a" && color1
!= color2
) ||
51 (piece1
== "a" && color1
== color2
)
56 getPotentialMovesFrom([x
, y
]) {
57 switch (this.getPiece(x
, y
)) {
59 return this.getPotentialAntikingMoves([x
, y
]);
61 return super.getPotentialMovesFrom([x
, y
]);
65 getPotentialAntikingMoves(sq
) {
66 // The antiking moves like a king (only captured colors differ)
67 return this.getSlideNJumpMoves(
69 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
74 isAttacked(sq
, color
) {
76 super.isAttacked(sq
, color
) ||
77 this.isAttackedByAntiking(sq
, color
)
81 isAttackedByKing([x
, y
], color
) {
82 // Antiking is not attacked by king:
83 if (this.getPiece(x
, y
) == V
.ANTIKING
) return false;
84 return super.isAttackedByKing([x
, y
], color
);
87 isAttackedByAntiking([x
, y
], color
) {
88 // (Anti)King is not attacked by antiking
89 if ([V
.KING
, V
.ANTIKING
].includes(this.getPiece(x
, y
))) return false;
90 return this.isAttackedBySlideNJump(
94 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
100 const oppCol
= V
.GetOppCol(color
);
102 this.isAttacked(this.kingPos
[color
], oppCol
) ||
103 !this.isAttacked(this.antikingPos
[color
], oppCol
);
107 getCheckSquares(color
) {
109 const oppCol
= V
.GetOppCol(color
);
110 if (this.isAttacked(this.kingPos
[color
], oppCol
))
111 res
.push(JSON
.parse(JSON
.stringify(this.kingPos
[color
])));
112 if (!this.isAttacked(this.antikingPos
[color
], oppCol
))
113 res
.push(JSON
.parse(JSON
.stringify(this.antikingPos
[color
])));
118 super.postPlay(move);
119 const piece
= move.vanish
[0].p
;
120 const c
= move.vanish
[0].c
;
121 // Update antiking position
122 if (piece
== V
.ANTIKING
) {
123 this.antikingPos
[c
][0] = move.appear
[0].x
;
124 this.antikingPos
[c
][1] = move.appear
[0].y
;
129 super.postUndo(move);
130 const c
= move.vanish
[0].c
;
131 if (move.vanish
[0].p
== V
.ANTIKING
)
132 this.antikingPos
[c
] = [move.start
.x
, move.start
.y
];
135 static get VALUES() {
136 return Object
.assign(
142 static GenRandInitFen(randomness
) {
144 return "rnbqkbnr/pppppppp/3A4/8/8/3a4/PPPPPPPP/RNBQKBNR w 0 ahah -";
146 let pieces
= { w: new Array(8), b: new Array(8) };
148 let antikingPos
= { w: -1, b: -1 };
149 for (let c
of ["w", "b"]) {
150 if (c
== 'b' && randomness
== 1) {
151 pieces
['b'] = pieces
['w'];
152 antikingPos
['b'] = antikingPos
['w'];
157 let positions
= ArrayFun
.range(8);
159 // Get random squares for bishops, but avoid corners; because,
160 // if an antiking blocks a cornered bishop, it can never be checkmated
161 let randIndex
= 2 * randInt(1, 4);
162 const bishop1Pos
= positions
[randIndex
];
163 let randIndex_tmp
= 2 * randInt(3) + 1;
164 const bishop2Pos
= positions
[randIndex_tmp
];
165 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
166 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
168 randIndex
= randInt(6);
169 const knight1Pos
= positions
[randIndex
];
170 positions
.splice(randIndex
, 1);
171 randIndex
= randInt(5);
172 const knight2Pos
= positions
[randIndex
];
173 positions
.splice(randIndex
, 1);
175 randIndex
= randInt(4);
176 const queenPos
= positions
[randIndex
];
177 positions
.splice(randIndex
, 1);
179 const rook1Pos
= positions
[0];
180 const kingPos
= positions
[1];
181 const rook2Pos
= positions
[2];
183 // Random squares for antikings
184 antikingPos
[c
] = randInt(8);
186 pieces
[c
][rook1Pos
] = "r";
187 pieces
[c
][knight1Pos
] = "n";
188 pieces
[c
][bishop1Pos
] = "b";
189 pieces
[c
][queenPos
] = "q";
190 pieces
[c
][kingPos
] = "k";
191 pieces
[c
][bishop2Pos
] = "b";
192 pieces
[c
][knight2Pos
] = "n";
193 pieces
[c
][rook2Pos
] = "r";
194 flags
+= V
.CoordToColumn(rook1Pos
) + V
.CoordToColumn(rook2Pos
);
196 const ranks23_black
=
198 (antikingPos
["w"] > 0 ? antikingPos
["w"] : "") +
200 (antikingPos
["w"] < 7 ? 7 - antikingPos
["w"] : "");
201 const ranks23_white
=
202 (antikingPos
["b"] > 0 ? antikingPos
["b"] : "") +
204 (antikingPos
["b"] < 7 ? 7 - antikingPos
["b"] : "") +
207 pieces
["b"].join("") +
213 pieces
["w"].join("").toUpperCase() +
214 " w 0 " + flags
+ " -"
218 static get SEARCH_DEPTH() {