2bf3ecf762afe449b23e94d7e42c2339d504aeab
1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { SuicideRules
} from "@/variants/Suicide";
4 export class SuctionRules
extends ChessRules
{
6 static get PawnSpecs() {
11 { promotions: [V
.PAWN
] }
15 static get HasFlags() {
19 setOtherVariables(fen
) {
20 super.setOtherVariables(fen
);
21 // Local stack of "captures"
23 const cmove
= V
.ParseFen(fen
).cmove
;
24 if (cmove
== "-") this.cmoves
.push(null);
27 start: ChessRules
.SquareToCoords(cmove
.substr(0, 2)),
28 end: ChessRules
.SquareToCoords(cmove
.substr(2))
33 static ParseFen(fen
) {
35 ChessRules
.ParseFen(fen
),
36 { cmove: fen
.split(" ")[4] }
40 static IsGoodFen(fen
) {
41 if (!ChessRules
.IsGoodFen(fen
)) return false;
42 const fenParts
= fen
.split(" ");
43 if (fenParts
.length
!= 5) return false;
44 if (fenParts
[4] != "-" && !fenParts
[4].match(/^([a-h][1-8]){2}$/))
50 if (move.vanish
.length
== 2)
51 return { start: move.start
, end: move.end
};
55 getBasicMove([sx
, sy
], [ex
, ey
]) {
56 const startColor
= this.getColor(sx
, sy
);
57 const startPiece
= this.getPiece(sx
, sy
);
77 if (this.board
[ex
][ey
] != V
.EMPTY
) {
78 const endColor
= this.getColor(ex
, ey
);
79 const endPiece
= this.getPiece(ex
, ey
);
100 getEnpassantCaptures([x
, y
], shiftX
) {
102 const Lep
= this.epSquares
.length
;
103 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
106 epSquare
.x
== x
+ shiftX
&&
107 Math
.abs(epSquare
.y
- y
) == 1
109 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
110 const oppCol
= V
.GetOppCol(this.turn
);
111 enpassantMove
.vanish
.push({
117 enpassantMove
.appear
.push({
123 moves
.push(enpassantMove
);
128 getPotentialKingMoves() {
132 // Does m2 un-do m1 ? (to disallow undoing captures)
133 oppositeMoves(m1
, m2
) {
136 m2
.vanish
.length
== 2 &&
137 m1
.start
.x
== m2
.start
.x
&&
138 m1
.end
.x
== m2
.end
.x
&&
139 m1
.start
.y
== m2
.start
.y
&&
145 if (moves
.length
== 0) return [];
146 return moves
.filter(m
=> {
147 const L
= this.cmoves
.length
; //at least 1: init from FEN
148 return !this.oppositeMoves(this.cmoves
[L
- 1], m
);
152 static GenRandInitFen(randomness
) {
154 return SuicideRules
.GenRandInitFen(randomness
) + " -";
158 const L
= this.cmoves
.length
;
162 : ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].start
) +
163 ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].end
)
168 return super.getFen() + " " + this.getCmoveFen();
172 return super.getFenForRepeat() + "_" + this.getCmoveFen();
176 super.postPlay(move);
177 // Was opponent king swapped?
178 if (move.vanish
.length
== 2 && move.vanish
[1].p
== V
.KING
)
179 this.kingPos
[this.turn
] = [move.appear
[1].x
, move.appear
[1].y
];
180 this.cmoves
.push(this.getCmove(move));
184 super.postUndo(move);
185 if (move.appear
.length
== 2 && move.appear
[1].p
== V
.KING
)
186 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
199 const color
= this.turn
;
200 const kp
= this.kingPos
[color
];
201 if (color
== "w" && kp
[0] == 0) return "0-1";
202 if (color
== "b" && kp
[0] == V
.size
.x
- 1) return "1-0";
203 // King is not on the opposite edge: game not over
208 // Very simple criterion for now: kings position
209 return this.kingPos
["w"][0] + this.kingPos
["b"][0];
213 // Translate final square
214 const finalSquare
= V
.CoordsToSquare(move.end
);
216 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
217 if (piece
== V
.PAWN
) {
220 if (move.vanish
.length
== 2) {
222 const startColumn
= V
.CoordToColumn(move.start
.y
);
223 notation
= startColumn
+ "x" + finalSquare
;
225 else notation
= finalSquare
;
230 piece
.toUpperCase() +
231 (move.vanish
.length
== 2 ? "x" : "") +