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
) {
23 return Object
.assign({}, ChessRules
.ParseFen(fen
), {
24 cmove: fen
.split(" ")[4]
28 static IsGoodFen(fen
) {
29 if (!ChessRules
.IsGoodFen(fen
)) return false;
30 const fenParts
= fen
.split(" ");
31 if (fenParts
.length
!= 6) return false;
32 if (fenParts
[5] != "-" && !fenParts
[5].match(/^([a-h][1-8]){2}$/))
38 if (move.vanish
.length
== 2)
39 return { start: move.start
, end: move.end
};
43 getBasicMove([sx
, sy
], [ex
, ey
]) {
44 const startColor
= this.getColor(sx
, sy
);
45 const startPiece
= this.getPiece(sx
, sy
);
65 if (this.board
[ex
][ey
] != V
.EMPTY
) {
66 const endColor
= this.getColor(ex
, ey
);
67 const endPiece
= this.getPiece(ex
, ey
);
88 getEnpassantCaptures([x
, y
], shiftX
) {
90 const Lep
= this.epSquares
.length
;
91 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
94 epSquare
.x
== x
+ shiftX
&&
95 Math
.abs(epSquare
.y
- y
) == 1
97 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
98 const oppCol
= V
.GetOppCol(color
);
99 enpassantMove
.vanish
.push({
105 enpassantMove
.appear
.push({
111 moves
.push(enpassantMove
);
116 getPotentialKingMoves() {
120 // Does m2 un-do m1 ? (to disallow undoing captures)
121 oppositeMoves(m1
, m2
) {
124 m2
.vanish
.length
== 2 &&
125 m1
.start
.x
== m2
.start
.x
&&
126 m1
.end
.x
== m2
.end
.x
&&
127 m1
.start
.y
== m2
.start
.y
&&
133 if (moves
.length
== 0) return [];
134 const color
= this.turn
;
135 return moves
.filter(m
=> {
136 const L
= this.cmoves
.length
; //at least 1: init from FEN
137 return !this.oppositeMoves(this.cmoves
[L
- 1], m
);
141 static GenRandInitFen(randomness
) {
143 return ChessRules
.GenRandInitFen(randomness
).slice(0, -6) + "- -";
147 const L
= this.cmoves
.length
;
148 const cmoveFen
= !this.cmoves
[L
- 1]
150 : ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].start
) +
151 ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].end
);
152 return super.getFen() + " " + cmoveFen
;
156 super.postPlay(move);
157 if (move.vanish
.length
== 2) {
158 // Was opponent king swapped?
159 if (move.vanish
[1].p
== V
.KING
)
160 this.kingPos
[this.turn
] = [move.appear
[1].x
, move.appear
[1].y
];
162 this.cmoves
.push(this.getCmove(move));
166 super.postUndo(move);
167 if (move.appear
.length
== 2) {
168 if (move.appear
[1].p
== V
.KING
)
169 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
183 const color
= this.turn
;
184 const kp
= this.kingPos
[color
];
185 if (color
== "w" && kp
[0] == 0) return "0-1";
186 if (color
== "b" && kp
[0] == V
.size
.x
- 1) return "1-0";
187 // King is not on the opposite edge: game not over
192 // Very simple criterion for now: kings position
193 return this.kingPos
["w"][0] + this.kingPos
["b"][0];
197 // Translate final square
198 const finalSquare
= V
.CoordsToSquare(move.end
);
200 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
201 if (piece
== V
.PAWN
) {
204 if (move.vanish
.length
== 2) {
206 const startColumn
= V
.CoordToColumn(move.start
.y
);
207 notation
= startColumn
+ "x" + finalSquare
;
209 else notation
= finalSquare
;
214 piece
.toUpperCase() +
215 (move.vanish
.length
== 2 ? "x" : "") +