1 import { ChessRules
} from "@/base_rules";
3 export class CwdaRules
extends ChessRules
{
7 select: ChessRules
.Options
.select
.concat([
13 { label: "Colorbound Clobberers", value: 'C' },
14 { label: "Nutty Knights", value: 'N' },
15 { label: "Remarkable Rookies", value: 'R' },
16 { label: "Fide", value: 'F' }
24 { label: "Colorbound Clobberers", value: 'C' },
25 { label: "Nutty Knights", value: 'N' },
26 { label: "Remarkable Rookies", value: 'R' },
27 { label: "Fide", value: 'F' }
34 static AbbreviateOptions(opts
) {
35 return opts
["army1"] + opts
["army2"];
38 static IsValidOptions(opts
) {
39 // Both armies filled, avoid Fide vs Fide
41 opts
.army1
&& opts
.army2
&&
42 (opts
.army1
!= 'F' || opts
.army2
!= 'F')
47 return (ChessRules
.PIECES
.includes(b
[1]) ? "" : "Cwda/") + b
;
50 static get PiecesMap() {
52 // Colorbound Clobberers
79 static GenRandInitFen(options
) {
80 const baseFen
= ChessRules
.GenRandInitFen(options
.randomness
);
81 let blackLine
= baseFen
.substr(0, 8);
82 if (options
.army2
!= 'F') {
83 blackLine
= blackLine
.split('')
84 .map(p
=> V
.PiecesMap
[options
.army2
][p
]).join('');
86 let whiteLine
= baseFen
.substr(35, 8);
87 if (options
.army1
!= 'F') {
88 whiteLine
= whiteLine
.split('')
89 .map(p
=> V
.PiecesMap
[options
.army1
][p
.toLowerCase()])
90 .join('').toUpperCase();
93 blackLine
+ baseFen
.substring(8, 35) + whiteLine
+
94 baseFen
.substr(43) + " " + options
.army1
+ options
.army2
98 setOtherVariables(fen
) {
99 super.setOtherVariables(fen
);
100 const armies
= V
.ParseFen(fen
).armies
;
101 this.army1
= armies
.charAt(0);
102 this.army2
= armies
.charAt(1);
105 static ParseFen(fen
) {
106 return Object
.assign(
107 { armies: fen
.split(" ")[5] },
108 ChessRules
.ParseFen(fen
)
112 static IsGoodFen(fen
) {
113 if (!ChessRules
.IsGoodFen(fen
)) return false;
114 const armies
= V
.ParseFen(fen
).armies
;
115 return (!!armies
&& armies
.match(/^[CNRF]{2,2}$/));
119 return super.getFen() + " " + this.army1
+ this.army2
;
122 static get C_ROOK() {
125 static get C_KNIGHT() {
128 static get C_BISHOP() {
131 static get C_QUEEN() {
134 static get N_ROOK() {
137 static get N_KNIGHT() {
140 static get N_BISHOP() {
143 static get N_QUEEN() {
146 static get R_ROOK() {
149 static get R_KNIGHT() {
152 static get R_BISHOP() {
155 static get R_QUEEN() {
159 static get PIECES() {
160 return ChessRules
.PIECES
.concat(
161 [V
.C_ROOK
, V
.C_KNIGHT
, V
.C_BISHOP
, V
.C_QUEEN
]).concat(
162 [V
.N_ROOK
, V
.N_KNIGHT
, V
.N_BISHOP
, V
.N_QUEEN
]).concat(
163 [V
.R_ROOK
, V
.R_KNIGHT
, V
.R_BISHOP
, V
.R_QUEEN
]);
166 getPotentialMovesFrom(sq
) {
167 switch (this.getPiece(sq
[0], sq
[1])) {
168 case V
.C_ROOK: return this.getPotentialC_rookMoves(sq
);
169 case V
.C_KNIGHT: return this.getPotentialC_knightMoves(sq
);
170 case V
.C_BISHOP: return this.getPotentialC_bishopMoves(sq
);
171 case V
.C_QUEEN: return this.getPotentialC_queenMoves(sq
);
172 case V
.N_ROOK: return this.getPotentialN_rookMoves(sq
);
173 case V
.N_KNIGHT: return this.getPotentialN_knightMoves(sq
);
174 case V
.N_BISHOP: return this.getPotentialN_bishopMoves(sq
);
175 case V
.N_QUEEN: return this.getPotentialN_queenMoves(sq
);
176 case V
.R_ROOK: return this.getPotentialR_rookMoves(sq
);
177 case V
.R_KNIGHT: return this.getPotentialR_knightMoves(sq
);
178 case V
.R_BISHOP: return this.getPotentialR_bishopMoves(sq
);
179 case V
.R_QUEEN: return this.getPotentialR_queenMoves(sq
);
181 // Can promote in anything from the two current armies
183 for (let army
of ["army1", "army2"]) {
184 if (army
== "army2" && this.army2
== this.army1
) break;
185 switch (this[army
]) {
187 Array
.prototype.push
.apply(promotions
,
188 [V
.C_ROOK
, V
.C_KNIGHT
, V
.C_BISHOP
, V
.C_QUEEN
]);
192 Array
.prototype.push
.apply(promotions
,
193 [V
.N_ROOK
, V
.N_KNIGHT
, V
.N_BISHOP
, V
.N_QUEEN
]);
197 Array
.prototype.push
.apply(promotions
,
198 [V
.R_ROOK
, V
.R_KNIGHT
, V
.R_BISHOP
, V
.R_QUEEN
]);
202 Array
.prototype.push
.apply(promotions
,
203 [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]);
208 return super.getPotentialPawnMoves(sq
, promotions
);
210 default: return super.getPotentialMovesFrom(sq
);
216 return Object
.assign(
265 getPotentialC_rookMoves(sq
) {
267 this.getSlideNJumpMoves(sq
, V
.steps
.b
).concat(
268 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1))
272 getPotentialC_knightMoves(sq
) {
274 this.getSlideNJumpMoves(sq
, V
.steps
.a
, 1).concat(
275 this.getSlideNJumpMoves(sq
, V
.steps
.r
, 1))
279 getPotentialC_bishopMoves(sq
) {
281 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
282 this.getSlideNJumpMoves(sq
, V
.steps
.a
, 1)).concat(
283 this.getSlideNJumpMoves(sq
, V
.steps
.b
, 1))
287 getPotentialC_queenMoves(sq
) {
289 this.getSlideNJumpMoves(sq
, V
.steps
.b
).concat(
290 this.getSlideNJumpMoves(sq
, V
.steps
.n
, 1))
294 getPotentialN_rookMoves(sq
) {
296 const rookSteps
= [ [0, -1], [0, 1], [c
== 'w' ? -1 : 1, 0] ];
297 const backward
= (c
== 'w' ? 1 : -1);
298 const kingSteps
= [ [backward
, -1], [backward
, 0], [backward
, 1] ];
300 this.getSlideNJumpMoves(sq
, rookSteps
).concat(
301 this.getSlideNJumpMoves(sq
, kingSteps
, 1))
305 getPotentialN_knightMoves(sq
) {
307 this.getSlideNJumpMoves(sq
, V
.steps
.$n
, 1).concat(
308 this.getSlideNJumpMoves(sq
, V
.steps
.f
, 1))
312 getPotentialN_bishopMoves(sq
) {
313 const backward
= (this.turn
== 'w' ? 1 : -1);
315 [0, -1], [0, 1], [backward
, -1], [backward
, 0], [backward
, 1]
317 const forward
= -backward
;
318 const knightSteps
= [
319 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
322 this.getSlideNJumpMoves(sq
, knightSteps
, 1).concat(
323 this.getSlideNJumpMoves(sq
, kingSteps
, 1))
327 getPotentialN_queenMoves(sq
) {
328 const backward
= (this.turn
== 'w' ? 1 : -1);
329 const forward
= -backward
;
331 [forward
, -1], [forward
, 1],
332 [backward
, -1], [backward
, 0], [backward
, 1]
334 const knightSteps
= [
335 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
337 const rookSteps
= [ [0, -1], [0, 1], [forward
, 0] ];
339 this.getSlideNJumpMoves(sq
, rookSteps
).concat(
340 this.getSlideNJumpMoves(sq
, kingSteps
, 1)).concat(
341 this.getSlideNJumpMoves(sq
, knightSteps
, 1))
345 getPotentialR_rookMoves(sq
) {
346 return this.getSlideNJumpMoves(sq
, V
.steps
.r
, 4);
349 getPotentialR_knightMoves(sq
) {
351 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
352 this.getSlideNJumpMoves(sq
, V
.steps
.w
, 1))
356 getPotentialR_bishopMoves(sq
) {
358 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
359 this.getSlideNJumpMoves(sq
, V
.steps
.f
, 1)).concat(
360 this.getSlideNJumpMoves(sq
, V
.steps
.$3, 1))
364 getPotentialR_queenMoves(sq
) {
366 this.getSlideNJumpMoves(sq
, V
.steps
.r
).concat(
367 this.getSlideNJumpMoves(sq
, V
.steps
.n
, 1))
371 getCastleMoves([x
, y
]) {
372 const color
= this.getColor(x
, y
);
373 let finalSquares
= [ [2, 3], [V
.size
.y
- 2, V
.size
.y
- 3] ];
375 (color
== 'w' && this.army1
== 'C') ||
376 (color
== 'b' && this.army2
== 'C')
378 // Colorbound castle long in an unusual way:
379 finalSquares
[0] = [1, 2];
381 return super.getCastleMoves([x
, y
], finalSquares
);
384 isAttacked(sq
, color
) {
385 if (super.isAttackedByPawn(sq
, color
) || super.isAttackedByKing(sq
, color
))
387 for (let army
of ['C', 'N', 'R', 'F']) {
389 [this.army1
, this.army2
].includes(army
) &&
391 this["isAttackedBy" + army
+ "_rook"](sq
, color
) ||
392 this["isAttackedBy" + army
+ "_knight"](sq
, color
) ||
393 this["isAttackedBy" + army
+ "_bishop"](sq
, color
) ||
394 this["isAttackedBy" + army
+ "_queen"](sq
, color
)
403 isAttackedByC_rook(sq
, color
) {
405 this.isAttackedBySlideNJump(sq
, color
, V
.C_ROOK
, V
.steps
.b
) ||
406 this.isAttackedBySlideNJump(sq
, color
, V
.C_ROOK
, V
.steps
.d
, 1)
410 isAttackedByC_knight(sq
, color
) {
412 this.isAttackedBySlideNJump(sq
, color
, V
.C_KNIGHT
, V
.steps
.r
, 1) ||
413 this.isAttackedBySlideNJump(sq
, color
, V
.C_KNIGHT
, V
.steps
.a
, 1)
417 isAttackedByC_bishop(sq
, color
) {
419 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.d
, 1) ||
420 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.a
, 1) ||
421 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.f
, 1)
425 isAttackedByC_queen(sq
, color
) {
427 this.isAttackedBySlideNJump(sq
, color
, V
.C_QUEEN
, V
.steps
.b
) ||
428 this.isAttackedBySlideNJump(sq
, color
, V
.C_QUEEN
, V
.steps
.n
, 1)
432 isAttackedByN_rook(sq
, color
) {
433 const rookSteps
= [ [0, -1], [0, 1], [color
== 'w' ? 1 : -1, 0] ];
434 const backward
= (color
== 'w' ? -1 : 1);
435 const kingSteps
= [ [backward
, -1], [backward
, 0], [backward
, 1] ];
437 this.isAttackedBySlideNJump(sq
, color
, V
.N_ROOK
, rookSteps
) ||
438 this.isAttackedBySlideNJump(sq
, color
, V
.N_ROOK
, kingSteps
, 1)
442 isAttackedByN_knight(sq
, color
) {
444 this.isAttackedBySlideNJump(sq
, color
, V
.N_KNIGHT
, V
.steps
.$n
, 1) ||
445 this.isAttackedBySlideNJump(sq
, color
, V
.N_KNIGHT
, V
.steps
.f
, 1)
449 isAttackedByN_bishop(sq
, color
) {
450 const backward
= (color
== 'w' ? -1 : 1);
452 [0, -1], [0, 1], [backward
, -1], [backward
, 0], [backward
, 1]
454 const forward
= -backward
;
455 const knightSteps
= [
456 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
459 this.isAttackedBySlideNJump(sq
, color
, V
.N_BISHOP
, knightSteps
, 1) ||
460 this.isAttackedBySlideNJump(sq
, color
, V
.N_BISHOP
, kingSteps
, 1)
464 isAttackedByN_queen(sq
, color
) {
465 const backward
= (color
== 'w' ? -1 : 1);
466 const forward
= -backward
;
468 [forward
, -1], [forward
, 1],
469 [backward
, -1], [backward
, 0], [backward
, 1]
471 const knightSteps
= [
472 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
474 const rookSteps
= [ [0, -1], [0, 1], [forward
, 0] ];
476 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, knightSteps
, 1) ||
477 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, kingSteps
, 1) ||
478 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, rookSteps
)
482 isAttackedByR_rook(sq
, color
) {
483 return this.isAttackedBySlideNJump(sq
, color
, V
.R_ROOK
, V
.steps
.r
, 4);
486 isAttackedByR_knight(sq
, color
) {
488 this.isAttackedBySlideNJump(sq
, color
, V
.R_KNIGHT
, V
.steps
.d
, 1) ||
489 this.isAttackedBySlideNJump(sq
, color
, V
.R_KNIGHT
, V
.steps
.w
, 1)
493 isAttackedByR_bishop(sq
, color
) {
495 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.d
, 1) ||
496 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.f
, 1) ||
497 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.$3, 1)
501 isAttackedByR_queen(sq
, color
) {
503 this.isAttackedBySlideNJump(sq
, color
, V
.R_QUEEN
, V
.steps
.r
) ||
504 this.isAttackedBySlideNJump(sq
, color
, V
.R_QUEEN
, V
.steps
.n
, 1)
508 // [HACK] So that the function above works also on Fide army:
509 isAttackedByF_rook(sq
, color
) {
510 return super.isAttackedByRook(sq
, color
);
512 isAttackedByF_knight(sq
, color
) {
513 return super.isAttackedByKnight(sq
, color
);
515 isAttackedByF_bishop(sq
, color
) {
516 return super.isAttackedByBishop(sq
, color
);
518 isAttackedByF_queen(sq
, color
) {
519 return super.isAttackedByQueen(sq
, color
);
522 static get VALUES() {
523 return Object
.assign(
542 static get SEARCH_DEPTH() {