c3277e9c34ee78b1a7c676f7b9b83caa10bb7625
1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class SuctionRules
extends ChessRules
{
4 static get PawnSpecs() {
9 { promotions: [V
.PAWN
] }
13 static get HasFlags() {
17 setOtherVariables(fen
) {
18 super.setOtherVariables(fen
);
19 // Local stack of "captures"
21 const cmove
= V
.ParseFen(fen
).cmove
;
22 if (cmove
== "-") this.cmoves
.push(null);
25 start: ChessRules
.SquareToCoords(cmove
.substr(0, 2)),
26 end: ChessRules
.SquareToCoords(cmove
.substr(2))
31 static ParseFen(fen
) {
33 ChessRules
.ParseFen(fen
),
34 { cmove: fen
.split(" ")[4] }
38 static IsGoodFen(fen
) {
39 if (!ChessRules
.IsGoodFen(fen
)) return false;
40 const fenParts
= fen
.split(" ");
41 if (fenParts
.length
!= 5) return false;
42 if (fenParts
[4] != "-" && !fenParts
[4].match(/^([a-h][1-8]){2}$/))
48 if (move.vanish
.length
== 2)
49 return { start: move.start
, end: move.end
};
53 getBasicMove([sx
, sy
], [ex
, ey
]) {
54 const startColor
= this.getColor(sx
, sy
);
55 const startPiece
= this.getPiece(sx
, sy
);
75 if (this.board
[ex
][ey
] != V
.EMPTY
) {
76 const endColor
= this.getColor(ex
, ey
);
77 const endPiece
= this.getPiece(ex
, ey
);
98 getEnpassantCaptures([x
, y
], shiftX
) {
100 const Lep
= this.epSquares
.length
;
101 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
104 epSquare
.x
== x
+ shiftX
&&
105 Math
.abs(epSquare
.y
- y
) == 1
107 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
108 const oppCol
= V
.GetOppCol(this.turn
);
109 enpassantMove
.vanish
.push({
115 enpassantMove
.appear
.push({
121 moves
.push(enpassantMove
);
126 getPotentialKingMoves() {
130 // Does m2 un-do m1 ? (to disallow undoing captures)
131 oppositeMoves(m1
, m2
) {
134 m2
.vanish
.length
== 2 &&
135 m1
.start
.x
== m2
.start
.x
&&
136 m1
.end
.x
== m2
.end
.x
&&
137 m1
.start
.y
== m2
.start
.y
&&
143 if (moves
.length
== 0) return [];
144 const color
= this.turn
;
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 ChessRules
.GenRandInitFen(randomness
).slice(0, -6) + "- -";
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" : "") +