4ae7208bf5bd0b1646b6f5f8a2dffd84a5587b6a
1 import ChessRules
from "/base_rules.js";
2 import PiPo
from "/utils/PiPo.js";
3 import Move
from "/utils/Move.js";
5 export default class ChainingRules
extends ChessRules
{
9 select: C
.Options
.select
,
10 input: C
.Options
.input
,
11 styles: ["atomic", "capture", "crazyhouse", "cylinder", "dark", "zen"]
15 get hasSelfCaptures() {
20 return true; //self captures induce chaining
23 setOtherVariables(fenParsed
) {
24 super.setOtherVariables(fenParsed
);
25 // Stack of "last move" only for intermediate chaining
26 this.lastMoveEnd
= [];
29 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
30 const L
= this.lastMoveEnd
.length
;
31 const piece
= (L
>= 1 ? this.lastMoveEnd
[L
-1].p : null);
33 this.board
[ex
][ey
] == "" ||
34 this.getColor(ex
, ey
) == C
.GetOppTurn(this.turn
)
37 tr
= {c: this.turn
, p: piece
};
38 let mv
= super.getBasicMove([sx
, sy
], [ex
, ey
], tr
);
40 mv
.vanish
.pop(); //end of a chain: initial piece remains
43 // (Self)Capture: initial, or inside a chain
44 const initPiece
= (piece
|| this.getPiece(sx
, sy
)),
45 destPiece
= this.getPiece(ex
, ey
);
47 start: {x: sx
, y: sy
},
54 p: (!!tr
? tr
.p : initPiece
)
77 mv
.chained
= destPiece
; //easier (no need to detect it)
78 // mv.drag = {c: this.turn, p: initPiece}; //TODO: doesn't work
83 const L
= this.lastMoveEnd
.length
;
84 if (L
>= 1 && this.lastMoveEnd
[L
-1].x
== x
&& this.lastMoveEnd
[L
-1].y
== y
)
85 return this.lastMoveEnd
[L
-1].p
;
86 return super.getPiece(x
, y
);
89 getPotentialMovesFrom([x
, y
], color
) {
90 const L
= this.lastMoveEnd
.length
;
93 (x
!= this.lastMoveEnd
[L
-1].x
|| y
!= this.lastMoveEnd
[L
-1].y
)
95 // A self-capture was played: wrong square
98 return super.getPotentialMovesFrom([x
, y
], color
);
102 return !move.chained
;
106 super.postPlay(move);
107 if (!!move.chained
) {
108 this.lastMoveEnd
.push({
115 this.lastMoveEnd
= [];