1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { SuicideRules
} from "@/variants/Suicide";
4 export class SuctionRules
extends ChessRules
{
5 static get PawnSpecs() {
10 { promotions: [V
.PAWN
] }
14 static get HasFlags() {
18 setOtherVariables(fen
) {
19 super.setOtherVariables(fen
);
20 // Local stack of "captures"
22 const cmove
= V
.ParseFen(fen
).cmove
;
23 if (cmove
== "-") this.cmoves
.push(null);
26 start: ChessRules
.SquareToCoords(cmove
.substr(0, 2)),
27 end: ChessRules
.SquareToCoords(cmove
.substr(2))
32 static ParseFen(fen
) {
34 ChessRules
.ParseFen(fen
),
35 { cmove: fen
.split(" ")[4] }
39 static IsGoodFen(fen
) {
40 if (!ChessRules
.IsGoodFen(fen
)) return false;
41 const fenParts
= fen
.split(" ");
42 if (fenParts
.length
!= 5) return false;
43 if (fenParts
[4] != "-" && !fenParts
[4].match(/^([a-h][1-8]){2}$/))
49 if (move.vanish
.length
== 2)
50 return { start: move.start
, end: move.end
};
54 getBasicMove([sx
, sy
], [ex
, ey
]) {
55 const startColor
= this.getColor(sx
, sy
);
56 const startPiece
= this.getPiece(sx
, sy
);
76 if (this.board
[ex
][ey
] != V
.EMPTY
) {
77 const endColor
= this.getColor(ex
, ey
);
78 const endPiece
= this.getPiece(ex
, ey
);
99 getEnpassantCaptures([x
, y
], shiftX
) {
101 const Lep
= this.epSquares
.length
;
102 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
105 epSquare
.x
== x
+ shiftX
&&
106 Math
.abs(epSquare
.y
- y
) == 1
108 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
109 const oppCol
= V
.GetOppCol(this.turn
);
110 enpassantMove
.vanish
.push({
116 enpassantMove
.appear
.push({
122 moves
.push(enpassantMove
);
127 getPotentialKingMoves() {
131 // Does m2 un-do m1 ? (to disallow undoing captures)
132 oppositeMoves(m1
, m2
) {
135 m2
.vanish
.length
== 2 &&
136 m1
.start
.x
== m2
.start
.x
&&
137 m1
.end
.x
== m2
.end
.x
&&
138 m1
.start
.y
== m2
.start
.y
&&
144 if (moves
.length
== 0) return [];
145 return moves
.filter(m
=> {
146 const L
= this.cmoves
.length
; //at least 1: init from FEN
147 return !this.oppositeMoves(this.cmoves
[L
- 1], m
);
151 static GenRandInitFen(randomness
) {
153 return SuicideRules
.GenRandInitFen(randomness
) + " -";
157 const L
= this.cmoves
.length
;
161 : ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].start
) +
162 ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].end
)
167 return super.getFen() + " " + this.getCmoveFen();
171 return super.getFenForRepeat() + "_" + this.getCmoveFen();
175 super.postPlay(move);
176 if (move.vanish
.length
== 2) {
177 // Was opponent king swapped?
178 if (move.vanish
[1].p
== V
.KING
)
179 this.kingPos
[this.turn
] = [move.appear
[1].x
, move.appear
[1].y
];
181 this.cmoves
.push(this.getCmove(move));
185 super.postUndo(move);
186 if (move.appear
.length
== 2) {
187 if (move.appear
[1].p
== V
.KING
)
188 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
202 const color
= this.turn
;
203 const kp
= this.kingPos
[color
];
204 if (color
== "w" && kp
[0] == 0) return "0-1";
205 if (color
== "b" && kp
[0] == V
.size
.x
- 1) return "1-0";
206 // King is not on the opposite edge: game not over
211 // Very simple criterion for now: kings position
212 return this.kingPos
["w"][0] + this.kingPos
["b"][0];
216 // Translate final square
217 const finalSquare
= V
.CoordsToSquare(move.end
);
219 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
220 if (piece
== V
.PAWN
) {
223 if (move.vanish
.length
== 2) {
225 const startColumn
= V
.CoordToColumn(move.start
.y
);
226 notation
= startColumn
+ "x" + finalSquare
;
228 else notation
= finalSquare
;
233 piece
.toUpperCase() +
234 (move.vanish
.length
== 2 ? "x" : "") +