40127208b7a22b2fcc00fce2bf8124d0f27f2d37
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);
112 static ParseFen(fen
) {
113 return Object
.assign(
114 { armies: fen
.split(" ")[5] },
115 ChessRules
.ParseFen(fen
)
119 static IsGoodFen(fen
) {
120 if (!ChessRules
.IsGoodFen(fen
)) return false;
121 const armies
= V
.ParseFen(fen
).armies
;
122 return (!!armies
&& armies
.match(/^[CNRF]{2,2}$/));
126 return super.getFen() + " " + this.army1
+ this.army2
;
129 static get C_ROOK() {
132 static get C_KNIGHT() {
135 static get C_BISHOP() {
138 static get C_QUEEN() {
141 static get N_ROOK() {
144 static get N_KNIGHT() {
147 static get N_BISHOP() {
150 static get N_QUEEN() {
153 static get N_KING() {
156 static get N_PAWN() {
159 static get R_ROOK() {
162 static get R_KNIGHT() {
165 static get R_BISHOP() {
168 static get R_QUEEN() {
171 static get R_KING() {
174 static get R_PAWN() {
179 const p
= this.board
[x
][y
][1];
180 if (['u', 'v'].includes(p
)) return 'p';
181 if (['a', 'e'].includes(p
)) return 'k';
185 static get PIECES() {
186 return ChessRules
.PIECES
.concat(
188 V
.C_ROOK
, V
.C_KNIGHT
, V
.C_BISHOP
, V
.C_QUEEN
,
189 V
.N_ROOK
, V
.N_KNIGHT
, V
.N_BISHOP
, V
.N_QUEEN
, V
.N_KING
, V
.N_PAWN
,
190 V
.R_ROOK
, V
.R_KNIGHT
, V
.R_BISHOP
, V
.R_QUEEN
, V
.R_KING
, V
.R_PAWN
195 getEpSquare(moveOrSquare
) {
196 if (!moveOrSquare
) return undefined; //TODO: necessary line?!
197 if (typeof moveOrSquare
=== "string") {
198 const square
= moveOrSquare
;
199 if (square
== "-") return undefined;
200 return V
.SquareToCoords(square
);
202 // Argument is a move:
203 const move = moveOrSquare
;
204 const s
= move.start
,
208 Math
.abs(s
.x
- e
.x
) == 2 &&
209 ['p', 'u', 'v'].includes(move.appear
[0].p
)
216 return undefined; //default
219 getPotentialMovesFrom(sq
) {
220 switch (this.getPiece(sq
[0], sq
[1])) {
221 case V
.C_ROOK: return this.getPotentialC_rookMoves(sq
);
222 case V
.C_KNIGHT: return this.getPotentialC_knightMoves(sq
);
223 case V
.C_BISHOP: return this.getPotentialC_bishopMoves(sq
);
224 case V
.C_QUEEN: return this.getPotentialC_queenMoves(sq
);
225 case V
.N_ROOK: return this.getPotentialN_rookMoves(sq
);
226 case V
.N_KNIGHT: return this.getPotentialN_knightMoves(sq
);
227 case V
.N_BISHOP: return this.getPotentialN_bishopMoves(sq
);
228 case V
.N_QUEEN: return this.getPotentialN_queenMoves(sq
);
229 case V
.R_ROOK: return this.getPotentialR_rookMoves(sq
);
230 case V
.R_KNIGHT: return this.getPotentialR_knightMoves(sq
);
231 case V
.R_BISHOP: return this.getPotentialR_bishopMoves(sq
);
232 case V
.R_QUEEN: return this.getPotentialR_queenMoves(sq
);
234 // Can promote in anything from the two current armies
236 for (let army
of ["army1", "army2"]) {
237 if (army
== "army2" && this.army2
== this.army1
) break;
238 switch (this[army
]) {
240 Array
.prototype.push
.apply(promotions
,
241 [V
.C_ROOK
, V
.C_KNIGHT
, V
.C_BISHOP
, V
.C_QUEEN
]);
245 Array
.prototype.push
.apply(promotions
,
246 [V
.N_ROOK
, V
.N_KNIGHT
, V
.N_BISHOP
, V
.N_QUEEN
]);
250 Array
.prototype.push
.apply(promotions
,
251 [V
.R_ROOK
, V
.R_KNIGHT
, V
.R_BISHOP
, V
.R_QUEEN
]);
255 Array
.prototype.push
.apply(promotions
,
256 [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]);
261 return super.getPotentialPawnMoves(sq
, promotions
);
263 default: return super.getPotentialMovesFrom(sq
);
269 return Object
.assign(
318 getPotentialC_rookMoves(sq
) {
320 this.getSlideNJumpMoves(sq
, V
.steps
.b
).concat(
321 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1))
325 getPotentialC_knightMoves(sq
) {
327 this.getSlideNJumpMoves(sq
, V
.steps
.a
, 1).concat(
328 this.getSlideNJumpMoves(sq
, V
.steps
.r
, 1))
332 getPotentialC_bishopMoves(sq
) {
334 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
335 this.getSlideNJumpMoves(sq
, V
.steps
.a
, 1)).concat(
336 this.getSlideNJumpMoves(sq
, V
.steps
.b
, 1))
340 getPotentialC_queenMoves(sq
) {
342 this.getSlideNJumpMoves(sq
, V
.steps
.b
).concat(
343 this.getSlideNJumpMoves(sq
, V
.steps
.n
, 1))
347 getPotentialN_rookMoves(sq
) {
349 const rookSteps
= [ [0, -1], [0, 1], [c
== 'w' ? -1 : 1, 0] ];
350 const backward
= (c
== 'w' ? 1 : -1);
351 const kingSteps
= [ [backward
, -1], [backward
, 0], [backward
, 1] ];
353 this.getSlideNJumpMoves(sq
, rookSteps
).concat(
354 this.getSlideNJumpMoves(sq
, kingSteps
, 1))
358 getPotentialN_knightMoves(sq
) {
360 this.getSlideNJumpMoves(sq
, V
.steps
.$n
, 1).concat(
361 this.getSlideNJumpMoves(sq
, V
.steps
.f
, 1))
365 getPotentialN_bishopMoves(sq
) {
366 const backward
= (this.turn
== 'w' ? 1 : -1);
368 [0, -1], [0, 1], [backward
, -1], [backward
, 0], [backward
, 1]
370 const forward
= -backward
;
371 const knightSteps
= [
372 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
375 this.getSlideNJumpMoves(sq
, knightSteps
, 1).concat(
376 this.getSlideNJumpMoves(sq
, kingSteps
, 1))
380 getPotentialN_queenMoves(sq
) {
381 const backward
= (this.turn
== 'w' ? 1 : -1);
382 const forward
= -backward
;
384 [forward
, -1], [forward
, 1],
385 [backward
, -1], [backward
, 0], [backward
, 1]
387 const knightSteps
= [
388 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
390 const rookSteps
= [ [0, -1], [0, 1], [forward
, 0] ];
392 this.getSlideNJumpMoves(sq
, rookSteps
).concat(
393 this.getSlideNJumpMoves(sq
, kingSteps
, 1)).concat(
394 this.getSlideNJumpMoves(sq
, knightSteps
, 1))
398 getPotentialR_rookMoves(sq
) {
399 return this.getSlideNJumpMoves(sq
, V
.steps
.r
, 4);
402 getPotentialR_knightMoves(sq
) {
404 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
405 this.getSlideNJumpMoves(sq
, V
.steps
.w
, 1))
409 getPotentialR_bishopMoves(sq
) {
411 this.getSlideNJumpMoves(sq
, V
.steps
.d
, 1).concat(
412 this.getSlideNJumpMoves(sq
, V
.steps
.f
, 1)).concat(
413 this.getSlideNJumpMoves(sq
, V
.steps
.$3, 1))
417 getPotentialR_queenMoves(sq
) {
419 this.getSlideNJumpMoves(sq
, V
.steps
.r
).concat(
420 this.getSlideNJumpMoves(sq
, V
.steps
.n
, 1))
424 getCastleMoves([x
, y
]) {
425 const color
= this.getColor(x
, y
);
426 let finalSquares
= [ [2, 3], [V
.size
.y
- 2, V
.size
.y
- 3] ];
428 (color
== 'w' && this.army1
== 'C') ||
429 (color
== 'b' && this.army2
== 'C')
431 // Colorbound castle long in an unusual way:
432 finalSquares
[0] = [1, 2];
434 return super.getCastleMoves([x
, y
], finalSquares
);
437 isAttacked(sq
, color
) {
438 if (super.isAttackedByPawn(sq
, color
) || super.isAttackedByKing(sq
, color
))
440 for (let army
of ['C', 'N', 'R', 'F']) {
442 [this.army1
, this.army2
].includes(army
) &&
444 this["isAttackedBy" + army
+ "_rook"](sq
, color
) ||
445 this["isAttackedBy" + army
+ "_knight"](sq
, color
) ||
446 this["isAttackedBy" + army
+ "_bishop"](sq
, color
) ||
447 this["isAttackedBy" + army
+ "_queen"](sq
, color
)
456 isAttackedByC_rook(sq
, color
) {
458 this.isAttackedBySlideNJump(sq
, color
, V
.C_ROOK
, V
.steps
.b
) ||
459 this.isAttackedBySlideNJump(sq
, color
, V
.C_ROOK
, V
.steps
.d
, 1)
463 isAttackedByC_knight(sq
, color
) {
465 this.isAttackedBySlideNJump(sq
, color
, V
.C_KNIGHT
, V
.steps
.r
, 1) ||
466 this.isAttackedBySlideNJump(sq
, color
, V
.C_KNIGHT
, V
.steps
.a
, 1)
470 isAttackedByC_bishop(sq
, color
) {
472 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.d
, 1) ||
473 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.a
, 1) ||
474 this.isAttackedBySlideNJump(sq
, color
, V
.C_BISHOP
, V
.steps
.f
, 1)
478 isAttackedByC_queen(sq
, color
) {
480 this.isAttackedBySlideNJump(sq
, color
, V
.C_QUEEN
, V
.steps
.b
) ||
481 this.isAttackedBySlideNJump(sq
, color
, V
.C_QUEEN
, V
.steps
.n
, 1)
485 isAttackedByN_rook(sq
, color
) {
486 const rookSteps
= [ [0, -1], [0, 1], [color
== 'w' ? 1 : -1, 0] ];
487 const backward
= (color
== 'w' ? -1 : 1);
488 const kingSteps
= [ [backward
, -1], [backward
, 0], [backward
, 1] ];
490 this.isAttackedBySlideNJump(sq
, color
, V
.N_ROOK
, rookSteps
) ||
491 this.isAttackedBySlideNJump(sq
, color
, V
.N_ROOK
, kingSteps
, 1)
495 isAttackedByN_knight(sq
, color
) {
497 this.isAttackedBySlideNJump(sq
, color
, V
.N_KNIGHT
, V
.steps
.$n
, 1) ||
498 this.isAttackedBySlideNJump(sq
, color
, V
.N_KNIGHT
, V
.steps
.f
, 1)
502 isAttackedByN_bishop(sq
, color
) {
503 const backward
= (color
== 'w' ? -1 : 1);
505 [0, -1], [0, 1], [backward
, -1], [backward
, 0], [backward
, 1]
507 const forward
= -backward
;
508 const knightSteps
= [
509 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
512 this.isAttackedBySlideNJump(sq
, color
, V
.N_BISHOP
, knightSteps
, 1) ||
513 this.isAttackedBySlideNJump(sq
, color
, V
.N_BISHOP
, kingSteps
, 1)
517 isAttackedByN_queen(sq
, color
) {
518 const backward
= (color
== 'w' ? -1 : 1);
519 const forward
= -backward
;
521 [forward
, -1], [forward
, 1],
522 [backward
, -1], [backward
, 0], [backward
, 1]
524 const knightSteps
= [
525 [2*forward
, -1], [2*forward
, 1], [forward
, -2], [forward
, 2]
527 const rookSteps
= [ [0, -1], [0, 1], [forward
, 0] ];
529 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, knightSteps
, 1) ||
530 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, kingSteps
, 1) ||
531 this.isAttackedBySlideNJump(sq
, color
, V
.N_QUEEN
, rookSteps
)
535 isAttackedByR_rook(sq
, color
) {
536 return this.isAttackedBySlideNJump(sq
, color
, V
.R_ROOK
, V
.steps
.r
, 4);
539 isAttackedByR_knight(sq
, color
) {
541 this.isAttackedBySlideNJump(sq
, color
, V
.R_KNIGHT
, V
.steps
.d
, 1) ||
542 this.isAttackedBySlideNJump(sq
, color
, V
.R_KNIGHT
, V
.steps
.w
, 1)
546 isAttackedByR_bishop(sq
, color
) {
548 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.d
, 1) ||
549 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.f
, 1) ||
550 this.isAttackedBySlideNJump(sq
, color
, V
.R_BISHOP
, V
.steps
.$3, 1)
554 isAttackedByR_queen(sq
, color
) {
556 this.isAttackedBySlideNJump(sq
, color
, V
.R_QUEEN
, V
.steps
.r
) ||
557 this.isAttackedBySlideNJump(sq
, color
, V
.R_QUEEN
, V
.steps
.n
, 1)
561 // [HACK] So that the function above works also on Fide army:
562 isAttackedByF_rook(sq
, color
) {
563 return super.isAttackedByRook(sq
, color
);
565 isAttackedByF_knight(sq
, color
) {
566 return super.isAttackedByKnight(sq
, color
);
568 isAttackedByF_bishop(sq
, color
) {
569 return super.isAttackedByBishop(sq
, color
);
571 isAttackedByF_queen(sq
, color
) {
572 return super.isAttackedByQueen(sq
, color
);
575 static get VALUES() {
576 return Object
.assign(
595 static get SEARCH_DEPTH() {
600 let notation
= super.getNotation(move);
601 if (['u', 'v'].includes(move.appear
[0].p
))
602 notation
= notation
.slice(0, -2);