f8cc6d9fdb0b3f3cc698a1bb167dcbd7841b3940
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
82 static GenRandInitFen(options
) {
83 const baseFen
= ChessRules
.GenRandInitFen(options
.randomness
);
84 let blackLine
= baseFen
.substr(0, 8), blackPawns
= "pppppppp";
85 if (options
.army2
!= 'F') {
86 blackLine
= blackLine
.split('')
87 .map(p
=> V
.PiecesMap
[options
.army2
][p
]).join('');
88 blackPawns
= V
.PiecesMap
[options
.army2
]['p'].repeat(8);
90 let whiteLine
= baseFen
.substr(35, 8), whitePawns
= "PPPPPPPP";
91 if (options
.army1
!= 'F') {
92 whiteLine
= whiteLine
.split('')
93 .map(p
=> V
.PiecesMap
[options
.army1
][p
.toLowerCase()])
94 .join('').toUpperCase();
95 whitePawns
= V
.PiecesMap
[options
.army1
]['p'].toUpperCase().repeat(8);
98 blackLine
+ "/" + blackPawns
+
99 baseFen
.substring(17, 26) +
100 whitePawns
+ "/" + whiteLine
+
101 baseFen
.substr(43) + " " + options
.army1
+ options
.army2
105 setOtherVariables(fen
) {
106 super.setOtherVariables(fen
);
107 const armies
= V
.ParseFen(fen
).armies
;
108 this.army1
= armies
.charAt(0);
109 this.army2
= armies
.charAt(1);
113 this.kingPos
= { w: [-1, -1], b: [-1, -1] };
114 const fenRows
= V
.ParseFen(fen
).position
.split("/");
115 for (let i
= 0; i
< fenRows
.length
; i
++) {
117 for (let j
= 0; j
< fenRows
[i
].length
; j
++) {
118 const newChar
= fenRows
[i
].charAt(j
);
119 if (['a', 'e', 'k'].includes(newChar
)) this.kingPos
["b"] = [i
, k
];
120 else if (['A', 'E', 'K'].includes(newChar
)) this.kingPos
["w"] = [i
, k
];
122 const num
= parseInt(fenRows
[i
].charAt(j
), 10);
123 if (!isNaN(num
)) k
+= num
- 1;
130 static ParseFen(fen
) {
131 return Object
.assign(
132 { armies: fen
.split(" ")[5] },
133 ChessRules
.ParseFen(fen
)
137 static IsGoodFen(fen
) {
138 if (!ChessRules
.IsGoodFen(fen
)) return false;
139 const armies
= V
.ParseFen(fen
).armies
;
140 return (!!armies
&& armies
.match(/^[CNRF]{2,2}$/));
144 return super.getFen() + " " + this.army1
+ this.army2
;
147 static get C_ROOK() {
150 static get C_KNIGHT() {
153 static get C_BISHOP() {
156 static get C_QUEEN() {
159 static get N_ROOK() {
162 static get N_KNIGHT() {
165 static get N_BISHOP() {
168 static get N_QUEEN() {
171 static get N_KING() {
174 static get N_PAWN() {
177 static get R_ROOK() {
180 static get R_KNIGHT() {
183 static get R_BISHOP() {
186 static get R_QUEEN() {
189 static get R_KING() {
192 static get R_PAWN() {
197 const p
= this.board
[x
][y
][1];
198 if (['u', 'v'].includes(p
)) return 'p';
199 if (['a', 'e'].includes(p
)) return 'k';
203 static get PIECES() {
204 return ChessRules
.PIECES
.concat(
206 V
.C_ROOK
, V
.C_KNIGHT
, V
.C_BISHOP
, V
.C_QUEEN
,
207 V
.N_ROOK
, V
.N_KNIGHT
, V
.N_BISHOP
, V
.N_QUEEN
, V
.N_KING
, V
.N_PAWN
,
208 V
.R_ROOK
, V
.R_KNIGHT
, V
.R_BISHOP
, V
.R_QUEEN
, V
.R_KING
, V
.R_PAWN
213 getEpSquare(moveOrSquare
) {
214 if (!moveOrSquare
) return undefined; //TODO: necessary line?!
215 if (typeof moveOrSquare
=== "string") {
216 const square
= moveOrSquare
;
217 if (square
== "-") return undefined;
218 return V
.SquareToCoords(square
);
220 // Argument is a move:
221 const move = moveOrSquare
;
222 const s
= move.start
,
226 Math
.abs(s
.x
- e
.x
) == 2 &&
227 ['p', 'u', 'v'].includes(move.appear
[0].p
)
234 return undefined; //default
237 getPotentialMovesFrom(sq
) {
238 switch (this.getPiece(sq
[0], sq
[1])) {
239 case V
.C_ROOK: return this.getPotentialC_rookMoves(sq
);
240 case V
.C_KNIGHT: return this.getPotentialC_knightMoves(sq
);
241 case V
.C_BISHOP: return this.getPotentialC_bishopMoves(sq
);
242 case V
.C_QUEEN: return this.getPotentialC_queenMoves(sq
);
243 case V
.N_ROOK: return this.getPotentialN_rookMoves(sq
);
244 case V
.N_KNIGHT: return this.getPotentialN_knightMoves(sq
);
245 case V
.N_BISHOP: return this.getPotentialN_bishopMoves(sq
);
246 case V
.N_QUEEN: return this.getPotentialN_queenMoves(sq
);
247 case V
.R_ROOK: return this.getPotentialR_rookMoves(sq
);
248 case V
.R_KNIGHT: return this.getPotentialR_knightMoves(sq
);
249 case V
.R_BISHOP: return this.getPotentialR_bishopMoves(sq
);
250 case V
.R_QUEEN: return this.getPotentialR_queenMoves(sq
);
252 // Can promote in anything from the two current armies
254 for (let army
of ["army1", "army2"]) {
255 if (army
== "army2" && this.army2
== this.army1
) break;
256 switch (this[army
]) {
258 Array
.prototype.push
.apply(promotions
,
259 [V
.C_ROOK
, V
.C_KNIGHT
, V
.C_BISHOP
, V
.C_QUEEN
]);
263 Array
.prototype.push
.apply(promotions
,
264 [V
.N_ROOK
, V
.N_KNIGHT
, V
.N_BISHOP
, V
.N_QUEEN
]);
268 Array
.prototype.push
.apply(promotions
,
269 [V
.R_ROOK
, V
.R_KNIGHT
, V
.R_BISHOP
, V
.R_QUEEN
]);
273 Array
.prototype.push
.apply(promotions
,
274 [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]);
279 return super.getPotentialPawnMoves(sq
, promotions
);
281 default: return super.getPotentialMovesFrom(sq
);
287 return Object
.assign(
336 getPotentialC_rookMoves(sq
) {
338 this.getSlideNJumpMoves(sq
, V
.steps
.b
).concat(
339 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1))
343 getPotentialC_knightMoves(sq
) {
345 this.getSlideNJumpMoves(sq
, V
.steps
.a
, 1).concat(
346 this.getSlideNJumpMoves(sq
, V
.steps
.r
, 1))
350 getPotentialC_bishopMoves(sq
) {
352 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
353 this.getSlideNJumpMoves(sq
, V
.steps
.a
, 1)).concat(
354 this.getSlideNJumpMoves(sq
, V
.steps
.b
, 1))
358 getPotentialC_queenMoves(sq
) {
360 this.getSlideNJumpMoves(sq
, V
.steps
.b
).concat(
361 this.getSlideNJumpMoves(sq
, V
.steps
.n
, 1))
365 getPotentialN_rookMoves(sq
) {
367 const rookSteps
= [ [0, -1], [0, 1], [c
== 'w' ? -1 : 1, 0] ];
368 const backward
= (c
== 'w' ? 1 : -1);
369 const kingSteps
= [ [backward
, -1], [backward
, 0], [backward
, 1] ];
371 this.getSlideNJumpMoves(sq
, rookSteps
).concat(
372 this.getSlideNJumpMoves(sq
, kingSteps
, 1))
376 getPotentialN_knightMoves(sq
) {
378 this.getSlideNJumpMoves(sq
, V
.steps
.$n
, 1).concat(
379 this.getSlideNJumpMoves(sq
, V
.steps
.f
, 1))
383 getPotentialN_bishopMoves(sq
) {
384 const backward
= (this.turn
== 'w' ? 1 : -1);
386 [0, -1], [0, 1], [backward
, -1], [backward
, 0], [backward
, 1]
388 const forward
= -backward
;
389 const knightSteps
= [
390 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
393 this.getSlideNJumpMoves(sq
, knightSteps
, 1).concat(
394 this.getSlideNJumpMoves(sq
, kingSteps
, 1))
398 getPotentialN_queenMoves(sq
) {
399 const backward
= (this.turn
== 'w' ? 1 : -1);
400 const forward
= -backward
;
402 [forward
, -1], [forward
, 1],
403 [backward
, -1], [backward
, 0], [backward
, 1]
405 const knightSteps
= [
406 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
408 const rookSteps
= [ [0, -1], [0, 1], [forward
, 0] ];
410 this.getSlideNJumpMoves(sq
, rookSteps
).concat(
411 this.getSlideNJumpMoves(sq
, kingSteps
, 1)).concat(
412 this.getSlideNJumpMoves(sq
, knightSteps
, 1))
416 getPotentialR_rookMoves(sq
) {
417 return this.getSlideNJumpMoves(sq
, V
.steps
.r
, 4);
420 getPotentialR_knightMoves(sq
) {
422 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
423 this.getSlideNJumpMoves(sq
, V
.steps
.w
, 1))
427 getPotentialR_bishopMoves(sq
) {
429 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
430 this.getSlideNJumpMoves(sq
, V
.steps
.f
, 1)).concat(
431 this.getSlideNJumpMoves(sq
, V
.steps
.$3, 1))
435 getPotentialR_queenMoves(sq
) {
437 this.getSlideNJumpMoves(sq
, V
.steps
.r
).concat(
438 this.getSlideNJumpMoves(sq
, V
.steps
.n
, 1))
442 getCastleMoves([x
, y
]) {
443 const color
= this.getColor(x
, y
);
444 let finalSquares
= [ [2, 3], [V
.size
.y
- 2, V
.size
.y
- 3] ];
446 (color
== 'w' && this.army1
== 'C') ||
447 (color
== 'b' && this.army2
== 'C')
449 // Colorbound castle long in an unusual way:
450 finalSquares
[0] = [1, 2];
452 return super.getCastleMoves([x
, y
], finalSquares
);
455 isAttacked(sq
, color
) {
456 if (super.isAttackedByPawn(sq
, color
) || super.isAttackedByKing(sq
, color
))
458 for (let army
of ['C', 'N', 'R', 'F']) {
460 [this.army1
, this.army2
].includes(army
) &&
462 this["isAttackedBy" + army
+ "_rook"](sq
, color
) ||
463 this["isAttackedBy" + army
+ "_knight"](sq
, color
) ||
464 this["isAttackedBy" + army
+ "_bishop"](sq
, color
) ||
465 this["isAttackedBy" + army
+ "_queen"](sq
, color
)
474 isAttackedByC_rook(sq
, color
) {
476 this.isAttackedBySlideNJump(sq
, color
, V
.C_ROOK
, V
.steps
.b
) ||
477 this.isAttackedBySlideNJump(sq
, color
, V
.C_ROOK
, V
.steps
.d
, 1)
481 isAttackedByC_knight(sq
, color
) {
483 this.isAttackedBySlideNJump(sq
, color
, V
.C_KNIGHT
, V
.steps
.r
, 1) ||
484 this.isAttackedBySlideNJump(sq
, color
, V
.C_KNIGHT
, V
.steps
.a
, 1)
488 isAttackedByC_bishop(sq
, color
) {
490 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.d
, 1) ||
491 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.a
, 1) ||
492 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.f
, 1)
496 isAttackedByC_queen(sq
, color
) {
498 this.isAttackedBySlideNJump(sq
, color
, V
.C_QUEEN
, V
.steps
.b
) ||
499 this.isAttackedBySlideNJump(sq
, color
, V
.C_QUEEN
, V
.steps
.n
, 1)
503 isAttackedByN_rook(sq
, color
) {
504 const rookSteps
= [ [0, -1], [0, 1], [color
== 'w' ? 1 : -1, 0] ];
505 const backward
= (color
== 'w' ? -1 : 1);
506 const kingSteps
= [ [backward
, -1], [backward
, 0], [backward
, 1] ];
508 this.isAttackedBySlideNJump(sq
, color
, V
.N_ROOK
, rookSteps
) ||
509 this.isAttackedBySlideNJump(sq
, color
, V
.N_ROOK
, kingSteps
, 1)
513 isAttackedByN_knight(sq
, color
) {
515 this.isAttackedBySlideNJump(sq
, color
, V
.N_KNIGHT
, V
.steps
.$n
, 1) ||
516 this.isAttackedBySlideNJump(sq
, color
, V
.N_KNIGHT
, V
.steps
.f
, 1)
520 isAttackedByN_bishop(sq
, color
) {
521 const backward
= (color
== 'w' ? -1 : 1);
523 [0, -1], [0, 1], [backward
, -1], [backward
, 0], [backward
, 1]
525 const forward
= -backward
;
526 const knightSteps
= [
527 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
530 this.isAttackedBySlideNJump(sq
, color
, V
.N_BISHOP
, knightSteps
, 1) ||
531 this.isAttackedBySlideNJump(sq
, color
, V
.N_BISHOP
, kingSteps
, 1)
535 isAttackedByN_queen(sq
, color
) {
536 const backward
= (color
== 'w' ? -1 : 1);
537 const forward
= -backward
;
539 [forward
, -1], [forward
, 1],
540 [backward
, -1], [backward
, 0], [backward
, 1]
542 const knightSteps
= [
543 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
545 const rookSteps
= [ [0, -1], [0, 1], [forward
, 0] ];
547 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, knightSteps
, 1) ||
548 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, kingSteps
, 1) ||
549 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, rookSteps
)
553 isAttackedByR_rook(sq
, color
) {
554 return this.isAttackedBySlideNJump(sq
, color
, V
.R_ROOK
, V
.steps
.r
, 4);
557 isAttackedByR_knight(sq
, color
) {
559 this.isAttackedBySlideNJump(sq
, color
, V
.R_KNIGHT
, V
.steps
.d
, 1) ||
560 this.isAttackedBySlideNJump(sq
, color
, V
.R_KNIGHT
, V
.steps
.w
, 1)
564 isAttackedByR_bishop(sq
, color
) {
566 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.d
, 1) ||
567 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.f
, 1) ||
568 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.$3, 1)
572 isAttackedByR_queen(sq
, color
) {
574 this.isAttackedBySlideNJump(sq
, color
, V
.R_QUEEN
, V
.steps
.r
) ||
575 this.isAttackedBySlideNJump(sq
, color
, V
.R_QUEEN
, V
.steps
.n
, 1)
579 // [HACK] So that the function above works also on Fide army:
580 isAttackedByF_rook(sq
, color
) {
581 return super.isAttackedByRook(sq
, color
);
583 isAttackedByF_knight(sq
, color
) {
584 return super.isAttackedByKnight(sq
, color
);
586 isAttackedByF_bishop(sq
, color
) {
587 return super.isAttackedByBishop(sq
, color
);
589 isAttackedByF_queen(sq
, color
) {
590 return super.isAttackedByQueen(sq
, color
);
594 const c
= V
.GetOppCol(this.turn
);
595 const piece
= move.appear
[0].p
;
596 // Update king position + flags
597 if (['k', 'a', 'e'].includes(piece
)) {
598 this.kingPos
[c
][0] = move.appear
[0].x
;
599 this.kingPos
[c
][1] = move.appear
[0].y
;
600 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
602 // Next call is still required because the king may eat an opponent's rook
603 // TODO: castleFlags will be turned off twice then.
604 super.updateCastleFlags(move, piece
);
608 // (Potentially) Reset king position
609 const c
= this.getColor(move.start
.x
, move.start
.y
);
610 const piece
= move.appear
[0].p
;
611 if (['k', 'a', 'e'].includes(piece
))
612 this.kingPos
[c
] = [move.start
.x
, move.start
.y
];
615 static get VALUES() {
616 return Object
.assign(
635 static get SEARCH_DEPTH() {
640 let notation
= super.getNotation(move);
641 if (['u', 'v'].includes(move.appear
[0].p
))
642 notation
= notation
.slice(0, -2);