1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
3 export class Checkered2Rules
extends ChessRules
{
5 const checkered_codes
= {
12 if (b
[0] == "c") return checkered_codes
[b
[1]];
13 return ChessRules
.board2fen(b
);
17 // Tolerate upper-case versions of checkered pieces (why not?)
18 const checkered_pieces
= {
30 if (Object
.keys(checkered_pieces
).includes(f
))
31 return "c" + checkered_pieces
[f
];
32 return ChessRules
.fen2board(f
);
36 return ChessRules
.PIECES
.concat(["s", "t", "u", "c", "o"]);
40 return (b
[0] == "c" ? "Checkered/" : "") + b
;
43 setOtherVariables(fen
) {
44 super.setOtherVariables(fen
);
45 // Local stack of non-capturing checkered moves:
47 const cmove
= V
.ParseFen(fen
).cmove
;
48 if (cmove
== "-") this.cmoves
.push(null);
51 start: ChessRules
.SquareToCoords(cmove
.substr(0, 2)),
52 end: ChessRules
.SquareToCoords(cmove
.substr(2))
57 static IsGoodFen(fen
) {
58 if (!ChessRules
.IsGoodFen(fen
)) return false;
59 const fenParts
= fen
.split(" ");
60 if (fenParts
.length
!= 6) return false;
61 if (fenParts
[5] != "-" && !fenParts
[5].match(/^([a-h][1-8]){2}$/))
66 static IsGoodFlags(flags
) {
67 // 4 for castle + 16 for pawns
68 return !!flags
.match(/^[a-z]{4,4}[01]{16,16}$/);
72 super.setFlags(fenflags
); //castleFlags
74 w: [...Array(8)], //pawns can move 2 squares?
77 const flags
= fenflags
.substr(4); //skip first 4 letters, for castle
78 for (let c
of ["w", "b"]) {
79 for (let i
= 0; i
< 8; i
++)
80 this.pawnFlags
[c
][i
] = flags
.charAt((c
== "w" ? 0 : 8) + i
) == "1";
85 return [this.castleFlags
, this.pawnFlags
];
88 disaggregateFlags(flags
) {
89 this.castleFlags
= flags
[0];
90 this.pawnFlags
= flags
[1];
93 getEpSquare(moveOrSquare
) {
94 if (typeof moveOrSquare
!== "object" || moveOrSquare
.appear
[0].c
!= 'c')
95 return super.getEpSquare(moveOrSquare
);
96 // Checkered move: no en-passant
101 if (move.appear
[0].c
== "c" && move.vanish
.length
== 1)
102 return { start: move.start
, end: move.end
};
106 canTake([x1
, y1
], [x2
, y2
]) {
107 const color1
= this.getColor(x1
, y1
);
108 const color2
= this.getColor(x2
, y2
);
109 // Checkered aren't captured
113 (color1
!= "c" || color2
!= this.turn
)
117 // Post-processing: apply "checkerization" of standard moves
118 getPotentialMovesFrom([x
, y
]) {
119 let standardMoves
= super.getPotentialMovesFrom([x
, y
]);
120 const lastRank
= this.turn
== "w" ? 0 : 7;
121 // King is treated differently: it never turn checkered
122 if (this.getPiece(x
, y
) == V
.KING
) return standardMoves
;
124 standardMoves
.forEach(m
=> {
125 if (m
.vanish
[0].p
== V
.PAWN
) {
127 Math
.abs(m
.end
.x
- m
.start
.x
) == 2 &&
128 !this.pawnFlags
[this.turn
][m
.start
.y
]
130 return; //skip forbidden 2-squares jumps
133 this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
&&
134 m
.vanish
.length
== 2 &&
135 this.getColor(m
.start
.x
, m
.start
.y
) == "c"
137 return; //checkered pawns cannot take en-passant
140 if (m
.vanish
.length
== 1)
144 // A capture occured (m.vanish.length == 2)
148 // Avoid promotions (already treated):
149 m
.appear
[0].p
!= m
.vanish
[1].p
&&
150 (m
.vanish
[0].p
!= V
.PAWN
|| m
.end
.x
!= lastRank
)
152 // Add transformation into captured piece
153 let m2
= JSON
.parse(JSON
.stringify(m
));
154 m2
.appear
[0].p
= m
.vanish
[1].p
;
162 getPotentialPawnMoves([x
, y
]) {
163 let moves
= super.getPotentialPawnMoves([x
, y
]);
164 // Post-process: set right color for checkered moves
165 if (this.getColor(x
, y
) == 'c') {
167 m
.appear
[0].c
= 'c'; //may be done twice if capture
174 canIplay(side
, [x
, y
]) {
175 return side
== this.turn
&& [side
, "c"].includes(this.getColor(x
, y
));
178 // Does m2 un-do m1 ? (to disallow undoing checkered moves)
179 oppositeMoves(m1
, m2
) {
182 m2
.appear
[0].c
== "c" &&
183 m2
.appear
.length
== 1 &&
184 m2
.vanish
.length
== 1 &&
185 m1
.start
.x
== m2
.end
.x
&&
186 m1
.end
.x
== m2
.start
.x
&&
187 m1
.start
.y
== m2
.end
.y
&&
188 m1
.end
.y
== m2
.start
.y
193 if (moves
.length
== 0) return [];
194 const color
= this.turn
;
195 const L
= this.cmoves
.length
; //at least 1: init from FEN
196 return moves
.filter(m
=> {
197 if (this.oppositeMoves(this.cmoves
[L
- 1], m
)) return false;
199 const res
= !this.underCheck(color
);
206 const oppCol
= V
.GetOppCol(this.turn
);
207 let potentialMoves
= [];
208 for (let i
= 0; i
< V
.size
.x
; i
++) {
209 for (let j
= 0; j
< V
.size
.y
; j
++) {
210 // NOTE: just testing == color isn't enough because of checkered pieces
211 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) != oppCol
) {
212 Array
.prototype.push
.apply(
214 this.getPotentialMovesFrom([i
, j
])
219 return this.filterValid(potentialMoves
);
223 const oppCol
= V
.GetOppCol(this.turn
);
224 for (let i
= 0; i
< V
.size
.x
; i
++) {
225 for (let j
= 0; j
< V
.size
.y
; j
++) {
226 // NOTE: just testing == color isn't enough because of checkered pieces
227 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) != oppCol
) {
228 const moves
= this.getPotentialMovesFrom([i
, j
]);
229 if (moves
.length
> 0) {
230 for (let k
= 0; k
< moves
.length
; k
++) {
231 if (this.filterValid([moves
[k
]]).length
> 0) return true;
240 // colors: array, generally 'w' and 'c' or 'b' and 'c'
241 isAttacked(sq
, colors
) {
242 if (!Array
.isArray(colors
)) colors
= [colors
];
244 this.isAttackedByPawn(sq
, colors
) ||
245 this.isAttackedByRook(sq
, colors
) ||
246 this.isAttackedByKnight(sq
, colors
) ||
247 this.isAttackedByBishop(sq
, colors
) ||
248 this.isAttackedByQueen(sq
, colors
) ||
249 this.isAttackedByKing(sq
, colors
)
253 isAttackedByPawn([x
, y
], colors
) {
254 for (let c
of colors
) {
255 const color
= (c
== "c" ? this.turn : c
);
256 let pawnShift
= color
== "w" ? 1 : -1;
257 if (x
+ pawnShift
>= 0 && x
+ pawnShift
< 8) {
258 for (let i
of [-1, 1]) {
262 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
263 this.getColor(x
+ pawnShift
, y
+ i
) == c
273 isAttackedBySlideNJump([x
, y
], colors
, piece
, steps
, oneStep
) {
274 for (let step
of steps
) {
275 let rx
= x
+ step
[0],
277 while (V
.OnBoard(rx
, ry
) && this.board
[rx
][ry
] == V
.EMPTY
&& !oneStep
) {
283 this.getPiece(rx
, ry
) === piece
&&
284 colors
.includes(this.getColor(rx
, ry
))
292 isAttackedByRook(sq
, colors
) {
293 return this.isAttackedBySlideNJump(sq
, colors
, V
.ROOK
, V
.steps
[V
.ROOK
]);
296 isAttackedByKnight(sq
, colors
) {
297 return this.isAttackedBySlideNJump(
306 isAttackedByBishop(sq
, colors
) {
307 return this.isAttackedBySlideNJump(
308 sq
, colors
, V
.BISHOP
, V
.steps
[V
.BISHOP
]);
311 isAttackedByQueen(sq
, colors
) {
312 return this.isAttackedBySlideNJump(
316 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
320 isAttackedByKing(sq
, colors
) {
321 return this.isAttackedBySlideNJump(
325 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
331 return this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
), "c"]);
335 const color
= this.turn
;
336 // Artifically change turn, for checkered pawns
337 this.turn
= V
.GetOppCol(color
);
343 let res
= kingAttacked
344 ? [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))]
351 super.postPlay(move);
352 // Does this move turn off a 2-squares pawn flag?
353 if ([1, 6].includes(move.start
.x
) && move.vanish
[0].p
== V
.PAWN
)
354 this.pawnFlags
[move.start
.x
== 6 ? "w" : "b"][move.start
.y
] = false;
355 this.cmoves
.push(this.getCmove(move));
359 super.postUndo(move);
364 if (this.atLeastOneMove()) return "*";
365 const color
= this.turn
;
366 // Artifically change turn, for checkered pawns
367 this.turn
= V
.GetOppCol(this.turn
);
368 const res
= this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
), "c"])
373 this.turn
= V
.GetOppCol(this.turn
);
379 // Just count material for now, considering checkered neutral (...)
380 for (let i
= 0; i
< V
.size
.x
; i
++) {
381 for (let j
= 0; j
< V
.size
.y
; j
++) {
382 if (this.board
[i
][j
] != V
.EMPTY
) {
383 const sqColor
= this.getColor(i
, j
);
384 if (["w","b"].includes(sqColor
)) {
385 const sign
= sqColor
== "w" ? 1 : -1;
386 evaluation
+= sign
* V
.VALUES
[this.getPiece(i
, j
)];
394 static GenRandInitFen(randomness
) {
395 // Add 16 pawns flags + empty cmove:
396 return ChessRules
.GenRandInitFen(randomness
)
397 .slice(0, -2) + "1111111111111111 - -";
400 static ParseFen(fen
) {
401 return Object
.assign(
402 ChessRules
.ParseFen(fen
),
403 { cmove: fen
.split(" ")[5] }
408 const L
= this.cmoves
.length
;
412 : ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].start
) +
413 ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].end
)
418 return super.getFen() + " " + this.getCmoveFen();
422 return super.getFenForRepeat() + "_" + this.getCmoveFen();
426 let fen
= super.getFlagsFen();
428 for (let c
of ["w", "b"])
429 for (let i
= 0; i
< 8; i
++) fen
+= (this.pawnFlags
[c
][i
] ? "1" : "0");
433 static get SEARCH_DEPTH() {
438 if (move.appear
.length
== 2) {
440 if (move.end
.y
< move.start
.y
) return "0-0-0";
444 const finalSquare
= V
.CoordsToSquare(move.end
);
445 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
447 if (piece
== V
.PAWN
) {
449 if (move.vanish
.length
> 1) {
451 const startColumn
= V
.CoordToColumn(move.start
.y
);
452 notation
= startColumn
+ "x" + finalSquare
;
453 } else notation
= finalSquare
;
457 piece
.toUpperCase() +
458 (move.vanish
.length
> 1 ? "x" : "") +
461 if (move.appear
[0].p
!= move.vanish
[0].p
)
462 notation
+= "=" + move.appear
[0].p
.toUpperCase();