ba5730e6fdbc731202dcd11e7599312ffb86942b
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 static IsGoodFen(fen
) {
37 if (!ChessRules
.IsGoodFen(fen
)) return false;
38 const fenParsed
= V
.ParseFen(fen
);
40 if (!fenParsed
.pocket
|| !fenParsed
.pocket
.match(/^[0-1]{4,4}$/))
45 static IsGoodFlags(flags
) {
46 // 4 for castle + 16 for generators
47 return !!flags
.match(/^[a-z]{4,4}[01]{16,16}$/);
51 super.setFlags(fenflags
); //castleFlags
53 w: [...Array(8)], //pawns can move 2 squares?
56 const flags
= fenflags
.substr(4); //skip first 4 letters, for castle
57 for (let c
of ["w", "b"]) {
58 for (let i
= 0; i
< 8; i
++)
59 this.pieceFlags
[c
][i
] = flags
.charAt((c
== "w" ? 0 : 8) + i
) == "1";
64 return [this.castleFlags
, this.pieceFlags
];
67 disaggregateFlags(flags
) {
68 this.castleFlags
= flags
[0];
69 this.pieceFlags
= flags
[1];
72 static ParseFen(fen
) {
73 const fenParts
= fen
.split(" ");
75 ChessRules
.ParseFen(fen
),
76 { pocket: fenParts
[5] }
80 static GenRandInitFen(randomness
) {
82 ChessRules
.GenRandInitFen(randomness
).slice(0, -2) +
83 // Add pieceFlags + pocket
84 "1111111111111111 - 1111"
90 super.getFen() + " " +
97 super.getFenForRepeat() + "_" +
103 let fen
= super.getFlagsFen();
105 for (let c
of ["w", "b"])
106 for (let i
= 0; i
< 8; i
++) fen
+= (this.pieceFlags
[c
][i
] ? "1" : "0");
112 for (let c
of ["w", "b"])
113 res
+= this.pocket
[c
][V
.HAWK
] + this.pocket
[c
][V
.ELEPHANT
];
117 setOtherVariables(fen
) {
118 super.setOtherVariables(fen
);
119 const fenParsed
= V
.ParseFen(fen
);
122 h: parseInt(fenParsed
.pocket
[0]),
123 e: parseInt(fenParsed
.pocket
[1])
126 h: parseInt(fenParsed
.pocket
[2]),
127 e: parseInt(fenParsed
.pocket
[3])
132 getPotentialMovesFrom([x
, y
]) {
133 let moves
= undefined;
134 switch (this.getPiece(x
, y
)) {
136 moves
= this.getPotentialHawkMoves([x
, y
]);
139 moves
= this.getPotentialElephantMoves([x
, y
]);
142 moves
= super.getPotentialMovesFrom([x
, y
]);
144 // For moves presentation when choices:
145 const unshiftNothing
= (m
) => {
146 const a
= m
.appear
[0];
147 m
.appear
.unshift(new PiPo({
154 // Post-processing: add choices for hawk and elephant,
155 // except for moves letting the king under check.
156 const color
= this.turn
;
157 if (Object
.values(this.pocket
[color
]).some(v
=> v
> 0)) {
158 const firstRank
= (color
== "w" ? 7 : 0);
161 let inCheckAfter
= false;
163 if (this.underCheck(color
)) inCheckAfter
= true;
166 for (let pp
of ['h', 'e']) {
167 if (this.pocket
[color
][pp
] > 0) {
168 let shift
= (m
.appear
[0].p
== V
.NOTHING
? 1 : 0);
170 m
.start
.x
== firstRank
&&
171 this.pieceFlags
[color
][m
.start
.y
] &&
173 m
.appear
.length
== shift
+1 ||
174 // Special castle case: is initial king square free?
175 ![m
.appear
[shift
].y
, m
.appear
[shift
+1].y
].includes(m
.vanish
[0].y
)
178 let pMove
= JSON
.parse(JSON
.stringify(m
));
179 if (shift
== 1) pMove
.appear
.shift();
180 // NOTE: unshift instead of push, for choices presentation
181 pMove
.appear
.unshift(new PiPo({
187 validMoves
.push(pMove
);
188 if (shift
== 0) unshiftNothing(m
);
190 shift
= (m
.appear
[0].p
== V
.NOTHING
? 1 : 0);
192 m
.appear
.length
>= 2 + shift
&&
193 m
.vanish
.length
== 2 &&
194 ![m
.appear
[shift
].y
, m
.appear
[shift
+1].y
].includes(m
.vanish
[1].y
)
196 // Special castle case: rook flag was necessarily on
197 let pMove
= JSON
.parse(JSON
.stringify(m
));
198 if (shift
== 1) pMove
.appear
.shift();
199 pMove
.appear
.unshift(new PiPo({
205 validMoves
.push(pMove
);
206 if (shift
== 0) unshiftNothing(m
);
210 // Unshift, to show the empty square on the left:
211 validMoves
.unshift(m
);
219 getPotentialHawkMoves(sq
) {
220 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]).concat(
221 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
225 getPotentialElephantMoves(sq
) {
226 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]).concat(
227 this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep")
231 isAttacked(sq
, color
) {
233 super.isAttacked(sq
, color
) ||
234 this.isAttackedByHawk(sq
, color
) ||
235 this.isAttackedByElephant(sq
, color
)
239 isAttackedByHawk(sq
, color
) {
241 this.isAttackedBySlideNJump(sq
, color
, V
.HAWK
, V
.steps
[V
.BISHOP
]) ||
242 this.isAttackedBySlideNJump(
252 isAttackedByElephant(sq
, color
) {
254 this.isAttackedBySlideNJump(sq
, color
, V
.ELEPHANT
, V
.steps
[V
.ROOK
]) ||
255 this.isAttackedBySlideNJump(
266 if (Object
.values(this.pocket
[this.turn
]).some(v
=> v
> 0))
267 // Undercheck tests done in getPotentialMovesFrom()
269 return super.filterValid(moves
);
274 if (move.appear
.length
>= 2) {
275 if ([V
.HAWK
, V
.ELEPHANT
].includes(move.appear
[0].p
)) {
276 // A pocket piece is used
277 const color
= this.turn
;
278 this.pocket
[color
][move.appear
[0].p
] = 0;
284 const color
= move.vanish
[0].c
;
285 const piece
= move.vanish
[0].p
;
286 // Update king position + flags
287 if (piece
== V
.KING
) {
289 ([V
.HAWK
, V
.ELEPHANT
, V
.NOTHING
].includes(move.appear
[0].p
) ? 1 : 0);
290 this.kingPos
[color
][0] = move.appear
[shift
].x
;
291 this.kingPos
[color
][1] = move.appear
[shift
].y
;
294 this.updateCastleFlags(move, piece
);
296 const oppCol
= V
.GetOppCol(color
);
297 const firstRank
= (color
== 'w' ? 7 : 0);
298 const oppFirstRank
= 7 - firstRank
;
299 // Does this move turn off a piece init square flag?
300 if (move.start
.x
== firstRank
) {
301 if (this.pieceFlags
[color
][move.start
.y
])
302 this.pieceFlags
[color
][move.start
.y
] = false;
303 // Special castle case:
304 if (move.appear
.length
>= 2 && move.vanish
.length
== 2) {
305 const L
= move.appear
.length
;
306 if (move.appear
[L
-1].p
== V
.ROOK
)
307 this.pieceFlags
[color
][move.vanish
[1].y
] = false;
310 if (move.end
.x
== oppFirstRank
&& this.pieceFlags
[oppCol
][move.end
.y
])
311 this.pieceFlags
[oppCol
][move.end
.y
] = false;
315 super.postUndo(move);
316 if (move.appear
.length
>= 2) {
317 if ([V
.HAWK
, V
.ELEPHANT
].includes(move.appear
[0].p
)) {
318 // A pocket piece was used
319 const color
= this.turn
;
320 this.pocket
[color
][move.appear
[0].p
] = 1;
325 static get SEARCH_DEPTH() {
329 static get VALUES() {
330 return Object
.assign(
338 if (move.appear
.length
>= 2) {
339 const pPieceAppear
= [V
.HAWK
, V
.ELEPHANT
].includes(move.appear
[0].p
);
340 const nothingAppear
= (move.appear
[0].p
== V
.NOTHING
);
341 if (pPieceAppear
|| nothingAppear
) {
343 if (pPieceAppear
) suffix
= "/" + move.appear
[0].p
.toUpperCase();
344 let cmove
= JSON
.parse(JSON
.stringify(move));
345 cmove
.appear
.shift();
346 return super.getNotation(cmove
) + suffix
;
349 return super.getNotation(move);