d2e352881dc84f92ca521a578c155ccc46d68f5b
1 import { ChessRules
, PiPo
} from "@/base_rules";
3 export class SchessRules
extends ChessRules
{
4 static get PawnSpecs() {
10 ChessRules
.PawnSpecs
.promotions
.concat([V
.HAWK
, V
.ELEPHANT
])
19 static get ELEPHANT() {
23 static get NOTHING() {
28 return ChessRules
.PIECES
.concat([V
.HAWK
, V
.ELEPHANT
]);
32 if ([V
.HAWK
, V
.ELEPHANT
, V
.NOTHING
].includes(b
[1])) return "Schess/" + b
;
36 // TODO: maybe changes could be done to this method to show "empty"
37 // instead of a piece to not use a pocket piece...
40 static IsGoodFen(fen
) {
41 if (!ChessRules
.IsGoodFen(fen
)) return false;
42 const fenParsed
= V
.ParseFen(fen
);
44 if (!fenParsed
.pocket
|| !fenParsed
.pocket
.match(/^[0-1]{4,4}$/))
49 static IsGoodFlags(flags
) {
50 // 4 for castle + 16 for generators
51 return !!flags
.match(/^[a-z]{4,4}[01]{16,16}$/);
55 super.setFlags(fenflags
); //castleFlags
57 w: [...Array(8)], //pawns can move 2 squares?
60 const flags
= fenflags
.substr(4); //skip first 4 letters, for castle
61 for (let c
of ["w", "b"]) {
62 for (let i
= 0; i
< 8; i
++)
63 this.pieceFlags
[c
][i
] = flags
.charAt((c
== "w" ? 0 : 8) + i
) == "1";
68 return [this.castleFlags
, this.pieceFlags
];
71 disaggregateFlags(flags
) {
72 this.castleFlags
= flags
[0];
73 this.pieceFlags
= flags
[1];
76 static ParseFen(fen
) {
77 const fenParts
= fen
.split(" ");
79 ChessRules
.ParseFen(fen
),
80 { pocket: fenParts
[5] }
84 static GenRandInitFen(randomness
) {
86 ChessRules
.GenRandInitFen(randomness
).slice(0, -2) +
87 // Add pieceFlags + pocket
88 "1111111111111111 - 1111"
94 super.getFen() + " " +
101 super.getFenForRepeat() + "_" +
107 let fen
= super.getFlagsFen();
109 for (let c
of ["w", "b"])
110 for (let i
= 0; i
< 8; i
++) fen
+= (this.pieceFlags
[c
][i
] ? "1" : "0");
116 for (let c
of ["w", "b"])
117 res
+= this.pocket
[c
][V
.HAWK
] + this.pocket
[c
][V
.ELEPHANT
];
121 setOtherVariables(fen
) {
122 super.setOtherVariables(fen
);
123 const fenParsed
= V
.ParseFen(fen
);
126 h: parseInt(fenParsed
.pocket
[0]),
127 e: parseInt(fenParsed
.pocket
[1])
130 h: parseInt(fenParsed
.pocket
[2]),
131 e: parseInt(fenParsed
.pocket
[3])
136 getPotentialMovesFrom([x
, y
]) {
137 let moves
= undefined;
138 switch (this.getPiece(x
, y
)) {
140 moves
= this.getPotentialHawkMoves([x
, y
]);
143 moves
= this.getPotentialElephantMoves([x
, y
]);
146 moves
= super.getPotentialMovesFrom([x
, y
]);
148 // For moves presentation when choices:
149 const unshiftNothing
= (m
) => {
150 const a
= m
.appear
[0];
151 m
.appear
.unshift(new PiPo({
158 // Post-processing: add choices for hawk and elephant,
159 // except for moves letting the king under check.
160 const color
= this.turn
;
161 if (Object
.values(this.pocket
[color
]).some(v
=> v
> 0)) {
162 const firstRank
= (color
== "w" ? 7 : 0);
165 let inCheckAfter
= false;
167 if (this.underCheck(color
)) inCheckAfter
= true;
170 for (let pp
of ['h', 'e']) {
171 if (this.pocket
[color
][pp
] > 0) {
172 let shift
= (m
.appear
[0].p
== V
.NOTHING
? 1 : 0);
174 m
.start
.x
== firstRank
&&
175 this.pieceFlags
[color
][m
.start
.y
] &&
177 m
.appear
.length
== shift
+1 ||
178 // Special castle case: is initial king square free?
179 ![m
.appear
[shift
].y
, m
.appear
[shift
+1].y
].includes(m
.vanish
[0].y
)
182 let pMove
= JSON
.parse(JSON
.stringify(m
));
183 if (shift
== 1) pMove
.appear
.shift();
184 // NOTE: unshift instead of push, for choices presentation
185 pMove
.appear
.unshift(new PiPo({
191 validMoves
.push(pMove
);
192 if (shift
== 0) unshiftNothing(m
);
194 shift
= (m
.appear
[0].p
== V
.NOTHING
? 1 : 0);
196 m
.appear
.length
>= 2 &&
197 m
.vanish
.length
== 2 &&
198 ![m
.appear
[shift
].y
, m
.appear
[shift
+1].y
].includes(m
.vanish
[1].y
)
200 // Special castle case: rook flag was necessarily on
201 let pMove
= JSON
.parse(JSON
.stringify(m
));
202 if (shift
== 1) pMove
.appear
.shift();
203 pMove
.appear
.unshift(new PiPo({
209 validMoves
.push(pMove
);
210 if (shift
== 0) unshiftNothing(m
);
214 // Unshift, to show the empty square on the left:
215 validMoves
.unshift(m
);
223 getPotentialHawkMoves(sq
) {
224 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
225 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
229 getPotentialElephantMoves(sq
) {
230 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
231 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
235 isAttacked(sq
, color
) {
237 super.isAttacked(sq
, color
) ||
238 this.isAttackedByHawk(sq
, color
) ||
239 this.isAttackedByElephant(sq
, color
)
243 isAttackedByHawk(sq
, color
) {
245 this.isAttackedBySlideNJump(sq
, color
, V
.HAWK
, V
.steps
[V
.BISHOP
]) ||
246 this.isAttackedBySlideNJump(
256 isAttackedByElephant(sq
, color
) {
258 this.isAttackedBySlideNJump(sq
, color
, V
.ELEPHANT
, V
.steps
[V
.ROOK
]) ||
259 this.isAttackedBySlideNJump(
270 if (Object
.values(this.pocket
[this.turn
]).some(v
=> v
> 0))
271 // Undercheck tests done in getPotentialMovesFrom()
273 return super.filterValid(moves
);
278 if (move.appear
.length
>= 2) {
279 if ([V
.HAWK
, V
.ELEPHANT
].includes(move.appear
[0].p
)) {
280 // A pocket piece is used
281 const color
= this.turn
;
282 this.pocket
[color
][move.appear
[0].p
] = 0;
288 const color
= move.vanish
[0].c
;
289 const piece
= move.vanish
[0].p
;
290 // Update king position + flags
291 if (piece
== V
.KING
) {
293 ([V
.HAWK
, V
.ELEPHANT
, V
.NOTHING
].includes(move.appear
[0].p
) ? 1 : 0);
294 this.kingPos
[color
][0] = move.appear
[shift
].x
;
295 this.kingPos
[color
][1] = move.appear
[shift
].y
;
298 this.updateCastleFlags(move, piece
);
300 const oppCol
= V
.GetOppCol(color
);
301 const firstRank
= (color
== 'w' ? 7 : 0);
302 const oppFirstRank
= 7 - firstRank
;
303 // Does this move turn off a piece init square flag?
304 if (move.start
.x
== firstRank
) {
305 if (this.pieceFlags
[color
][move.start
.y
])
306 this.pieceFlags
[color
][move.start
.y
] = false;
307 // Special castle case:
308 if (move.appear
.length
>= 2 && move.vanish
.length
== 2) {
309 const L
= move.appear
.length
;
310 if (move.appear
[L
-1].p
== V
.ROOK
)
311 this.pieceFlags
[color
][move.vanish
[1].y
] = false;
314 if (move.end
.x
== oppFirstRank
&& this.pieceFlags
[oppCol
][move.end
.y
])
315 this.pieceFlags
[oppCol
][move.end
.y
] = false;
319 super.postUndo(move);
320 if (move.appear
.length
>= 2) {
321 if ([V
.HAWK
, V
.ELEPHANT
].includes(move.appear
[0].p
)) {
322 // A pocket piece was used
323 const color
= this.turn
;
324 this.pocket
[color
][move.appear
[0].p
] = 1;
329 static get SEARCH_DEPTH() {
333 static get VALUES() {
334 return Object
.assign(
342 if (move.appear
.length
>= 2) {
343 const pPieceAppear
= [V
.HAWK
, V
.ELEPHANT
].includes(move.appear
[0].p
);
344 const nothingAppear
= (move.appear
[0].p
== V
.NOTHING
);
345 if (pPieceAppear
|| nothingAppear
) {
347 if (pPieceAppear
) suffix
= "/" + move.appear
[0].p
.toUpperCase();
348 let cmove
= JSON
.parse(JSON
.stringify(move));
349 cmove
.appear
.shift();
350 return super.getNotation(cmove
) + suffix
;
353 return super.getNotation(move);