1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export const VariantRules
= 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 getPotentialPawnMoves([x
, y
]) {
89 const color
= this.turn
;
91 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
92 const shiftX
= color
== "w" ? -1 : 1;
93 const startRank
= color
== "w" ? sizeX
- 2 : 1;
94 const firstRank
= color
== "w" ? sizeX
- 1 : 0;
96 if (x
+ shiftX
>= 0 && x
+ shiftX
< sizeX
) {
98 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
100 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
106 [startRank
,firstRank
].includes(x
) &&
107 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
110 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
114 for (let shiftY
of [-1, 1]) {
117 y
+ shiftY
< sizeY
&&
118 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
119 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
122 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
132 const Lep
= this.epSquares
.length
;
133 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
136 epSquare
.x
== x
+ shiftX
&&
137 Math
.abs(epSquare
.y
- y
) == 1
139 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
140 const oppCol
= V
.GetOppCol(color
);
141 enpassantMove
.vanish
.push({
147 enpassantMove
.appear
.push({
153 moves
.push(enpassantMove
);
159 getPotentialKingMoves() {
163 // Does m2 un-do m1 ? (to disallow undoing captures)
164 oppositeMoves(m1
, m2
) {
167 m2
.vanish
.length
== 2 &&
168 m1
.start
.x
== m2
.start
.x
&&
169 m1
.end
.x
== m2
.end
.x
&&
170 m1
.start
.y
== m2
.start
.y
&&
176 if (moves
.length
== 0) return [];
177 const color
= this.turn
;
178 return moves
.filter(m
=> {
179 const L
= this.cmoves
.length
; //at least 1: init from FEN
180 return !this.oppositeMoves(this.cmoves
[L
- 1], m
);
184 static GenRandInitFen(randomness
) {
186 return ChessRules
.GenRandInitFen(randomness
).slice(0, -6) + "- -";
190 const L
= this.cmoves
.length
;
191 const cmoveFen
= !this.cmoves
[L
- 1]
193 : ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].start
) +
194 ChessRules
.CoordsToSquare(this.cmoves
[L
- 1].end
);
195 return super.getFen() + " " + cmoveFen
;
199 super.postPlay(move);
200 if (move.vanish
.length
== 2) {
201 // Was opponent king swapped?
202 if (move.vanish
[1].p
== V
.KING
)
203 this.kingPos
[this.turn
] = [move.appear
[1].x
, move.appear
[1].y
];
205 this.cmoves
.push(this.getCmove(move));
209 super.postUndo(move);
210 if (move.appear
.length
== 2) {
211 if (move.appear
[1].p
== V
.KING
)
212 this.kingPos
[move.vanish
[1].c
] = [move.vanish
[1].x
, move.vanish
[1].y
];
226 const color
= this.turn
;
227 const kp
= this.kingPos
[color
];
228 if (color
== "w" && kp
[0] == 0)
230 if (color
== "b" && kp
[0] == V
.size
.x
- 1)
232 // King is not on the opposite edge: game not over
237 // Very simple criterion for now: kings position
238 return this.kingPos
["w"][0] + this.kingPos
["b"][0];
242 // Translate final square
243 const finalSquare
= V
.CoordsToSquare(move.end
);
245 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
246 if (piece
== V
.PAWN
) {
249 if (move.vanish
.length
== 2) {
251 const startColumn
= V
.CoordToColumn(move.start
.y
);
252 notation
= startColumn
+ "x" + finalSquare
;
254 else notation
= finalSquare
;
259 piece
.toUpperCase() +
260 (move.vanish
.length
== 2 ? "x" : "") +