1 import { ChessRules
} from "@/base_rules";
3 export const VariantRules
= class CheckeredRules
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 : b
;
43 setOtherVariables(fen
) {
44 super.setOtherVariables(fen
);
45 // Local stack of non-capturing checkered moves:
47 const cmove
= fen
.split(" ")[5];
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(/^[01]{20,20}$/);
72 super.setFlags(fenflags
); //castleFlags
74 w: [...Array(8).fill(true)], //pawns can move 2 squares?
75 b: [...Array(8).fill(true)]
77 const flags
= fenflags
.substr(4); //skip first 4 digits, 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];
94 if (move.appear
[0].c
== "c" && move.vanish
.length
== 1)
95 return { start: move.start
, end: move.end
};
99 canTake([x1
, y1
], [x2
, y2
]) {
100 const color1
= this.getColor(x1
, y1
);
101 const color2
= this.getColor(x2
, y2
);
102 // Checkered aren't captured
106 (color1
!= "c" || color2
!= this.turn
)
110 // Post-processing: apply "checkerization" of standard moves
111 getPotentialMovesFrom([x
, y
]) {
112 let standardMoves
= super.getPotentialMovesFrom([x
, y
]);
113 const lastRank
= this.turn
== "w" ? 0 : 7;
114 // King has to be treated differently (for castles)
115 if (this.getPiece(x
, y
) == V
.KING
) return standardMoves
;
117 standardMoves
.forEach(m
=> {
118 if (m
.vanish
[0].p
== V
.PAWN
) {
120 Math
.abs(m
.end
.x
- m
.start
.x
) == 2 &&
121 !this.pawnFlags
[this.turn
][m
.start
.y
]
123 return; //skip forbidden 2-squares jumps
125 this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
&&
126 m
.vanish
.length
== 2 &&
127 this.getColor(m
.start
.x
, m
.start
.y
) == "c"
129 return; //checkered pawns cannot take en-passant
132 if (m
.vanish
.length
== 1) moves
.push(m
);
135 // A capture occured (m.vanish.length == 2)
139 m
.appear
[0].p
!= m
.vanish
[1].p
&& //avoid promotions (already treated):
140 (m
.vanish
[0].p
!= V
.PAWN
|| m
.end
.x
!= lastRank
)
142 // Add transformation into captured piece
143 let m2
= JSON
.parse(JSON
.stringify(m
));
144 m2
.appear
[0].p
= m
.vanish
[1].p
;
152 canIplay(side
, [x
, y
]) {
153 return side
== this.turn
&& [side
, "c"].includes(this.getColor(x
, y
));
156 // Does m2 un-do m1 ? (to disallow undoing checkered moves)
157 oppositeMoves(m1
, m2
) {
160 m2
.appear
[0].c
== "c" &&
161 m2
.appear
.length
== 1 &&
162 m2
.vanish
.length
== 1 &&
163 m1
.start
.x
== m2
.end
.x
&&
164 m1
.end
.x
== m2
.start
.x
&&
165 m1
.start
.y
== m2
.end
.y
&&
166 m1
.end
.y
== m2
.start
.y
171 if (moves
.length
== 0) return [];
172 const color
= this.turn
;
173 const L
= this.cmoves
.length
; //at least 1: init from FEN
174 return moves
.filter(m
=> {
175 if (this.oppositeMoves(this.cmoves
[L
- 1], m
)) return false;
177 const res
= !this.underCheck(color
);
183 isAttackedByPawn([x
, y
], colors
) {
184 for (let c
of colors
) {
185 const color
= c
== "c" ? this.turn : c
;
186 let pawnShift
= color
== "w" ? 1 : -1;
187 if (x
+ pawnShift
>= 0 && x
+ pawnShift
< 8) {
188 for (let i
of [-1, 1]) {
192 this.getPiece(x
+ pawnShift
, y
+ i
) == V
.PAWN
&&
193 this.getColor(x
+ pawnShift
, y
+ i
) == c
204 return this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
), "c"]);
207 getCheckSquares(color
) {
208 // Artifically change turn, for checkered pawns
209 this.turn
= V
.GetOppCol(color
);
210 const kingAttacked
= this.isAttacked(this.kingPos
[color
], [
214 let res
= kingAttacked
215 ? [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))] //need to duplicate!
221 updateVariables(move) {
222 super.updateVariables(move);
223 // Does this move turn off a 2-squares pawn flag?
224 const secondRank
= [1, 6];
225 if (secondRank
.includes(move.start
.x
) && move.vanish
[0].p
== V
.PAWN
)
226 this.pawnFlags
[move.start
.x
== 6 ? "w" : "b"][move.start
.y
] = false;
230 if (this.atLeastOneMove())
234 const color
= this.turn
;
235 // Artifically change turn, for checkered pawns
236 this.turn
= V
.GetOppCol(this.turn
);
237 const res
= this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
), "c"])
242 this.turn
= V
.GetOppCol(this.turn
);
248 //Just count material for now, considering checkered neutral (...)
249 for (let i
= 0; i
< V
.size
.x
; i
++) {
250 for (let j
= 0; j
< V
.size
.y
; j
++) {
251 if (this.board
[i
][j
] != V
.EMPTY
) {
252 const sqColor
= this.getColor(i
, j
);
253 const sign
= sqColor
== "w" ? 1 : sqColor
== "b" ? -1 : 0;
254 evaluation
+= sign
* V
.VALUES
[this.getPiece(i
, j
)];
261 static GenRandInitFen() {
262 const randFen
= ChessRules
.GenRandInitFen();
263 // Add 16 pawns flags + empty cmove:
264 return randFen
.replace(" w 0 1111", " w 0 11111111111111111111 -");
267 static ParseFen(fen
) {
268 return Object
.assign({}, ChessRules
.ParseFen(fen
), {
269 cmove: fen
.split(" ")[5]
274 const L
= this.cmoves
.length
;
275 const cmoveFen
= !this.cmoves
[L
- 1]
277 : ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].start
) +
278 ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].end
);
279 return super.getFen() + " " + cmoveFen
;
283 let fen
= super.getFlagsFen();
285 for (let c
of ["w", "b"]) {
286 for (let i
= 0; i
< 8; i
++) fen
+= this.pawnFlags
[c
][i
] ? "1" : "0";
291 // TODO (design): this cmove update here or in (un)updateVariables ?
293 this.cmoves
.push(this.getCmove(move));
303 if (move.appear
.length
== 2) {
305 if (move.end
.y
< move.start
.y
) return "0-0-0";
309 // Translate final square
310 const finalSquare
= V
.CoordsToSquare(move.end
);
312 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
313 if (piece
== V
.PAWN
) {
316 if (move.vanish
.length
> 1) {
318 const startColumn
= V
.CoordToColumn(move.start
.y
);
324 move.appear
[0].p
.toUpperCase();
327 notation
= finalSquare
;
328 if (move.appear
.length
> 0 && piece
!= move.appear
[0].p
)
330 notation
+= "=" + move.appear
[0].p
.toUpperCase();
336 piece
.toUpperCase() +
337 (move.vanish
.length
> 1 ? "x" : "") +
339 (move.vanish
.length
> 1 ? "=" + move.appear
[0].p
.toUpperCase() : "")