1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class SuctionRules
extends ChessRules
{
4 static get HasFlags() {
8 setOtherVariables(fen
) {
9 super.setOtherVariables(fen
);
10 // Local stack of "captures"
12 const cmove
= V
.ParseFen(fen
).cmove
;
13 if (cmove
== "-") this.cmoves
.push(null);
16 start: ChessRules
.SquareToCoords(cmove
.substr(0, 2)),
17 end: ChessRules
.SquareToCoords(cmove
.substr(2))
22 static ParseFen(fen
) {
24 ChessRules
.ParseFen(fen
),
25 { cmove: fen
.split(" ")[4] }
29 static IsGoodFen(fen
) {
30 if (!ChessRules
.IsGoodFen(fen
)) return false;
31 const fenParts
= fen
.split(" ");
32 if (fenParts
.length
!= 6) return false;
33 if (fenParts
[5] != "-" && !fenParts
[5].match(/^([a-h][1-8]){2}$/))
39 if (move.vanish
.length
== 2)
40 return { start: move.start
, end: move.end
};
44 getBasicMove([sx
, sy
], [ex
, ey
]) {
45 const startColor
= this.getColor(sx
, sy
);
46 const startPiece
= this.getPiece(sx
, sy
);
66 if (this.board
[ex
][ey
] != V
.EMPTY
) {
67 const endColor
= this.getColor(ex
, ey
);
68 const endPiece
= this.getPiece(ex
, ey
);
89 getEnpassantCaptures([x
, y
], shiftX
) {
91 const Lep
= this.epSquares
.length
;
92 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
95 epSquare
.x
== x
+ shiftX
&&
96 Math
.abs(epSquare
.y
- y
) == 1
98 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
99 const oppCol
= V
.GetOppCol(this.turn
);
100 enpassantMove
.vanish
.push({
106 enpassantMove
.appear
.push({
112 moves
.push(enpassantMove
);
117 getPotentialKingMoves() {
121 // Does m2 un-do m1 ? (to disallow undoing captures)
122 oppositeMoves(m1
, m2
) {
125 m2
.vanish
.length
== 2 &&
126 m1
.start
.x
== m2
.start
.x
&&
127 m1
.end
.x
== m2
.end
.x
&&
128 m1
.start
.y
== m2
.start
.y
&&
134 if (moves
.length
== 0) return [];
135 const color
= this.turn
;
136 return moves
.filter(m
=> {
137 const L
= this.cmoves
.length
; //at least 1: init from FEN
138 return !this.oppositeMoves(this.cmoves
[L
- 1], m
);
142 static GenRandInitFen(randomness
) {
144 return ChessRules
.GenRandInitFen(randomness
).slice(0, -6) + "- -";
148 const L
= this.cmoves
.length
;
149 const cmoveFen
= !this.cmoves
[L
- 1]
151 : ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].start
) +
152 ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].end
);
153 return super.getFen() + " " + cmoveFen
;
157 super.postPlay(move);
158 if (move.vanish
.length
== 2) {
159 // Was opponent king swapped?
160 if (move.vanish
[1].p
== V
.KING
)
161 this.kingPos
[this.turn
] = [move.appear
[1].x
, move.appear
[1].y
];
163 this.cmoves
.push(this.getCmove(move));
167 super.postUndo(move);
168 if (move.appear
.length
== 2) {
169 if (move.appear
[1].p
== V
.KING
)
170 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
184 const color
= this.turn
;
185 const kp
= this.kingPos
[color
];
186 if (color
== "w" && kp
[0] == 0) return "0-1";
187 if (color
== "b" && kp
[0] == V
.size
.x
- 1) return "1-0";
188 // King is not on the opposite edge: game not over
193 // Very simple criterion for now: kings position
194 return this.kingPos
["w"][0] + this.kingPos
["b"][0];
198 // Translate final square
199 const finalSquare
= V
.CoordsToSquare(move.end
);
201 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
202 if (piece
== V
.PAWN
) {
205 if (move.vanish
.length
== 2) {
207 const startColumn
= V
.CoordToColumn(move.start
.y
);
208 notation
= startColumn
+ "x" + finalSquare
;
210 else notation
= finalSquare
;
215 piece
.toUpperCase() +
216 (move.vanish
.length
== 2 ? "x" : "") +