1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export const VariantRules
= class RecycleRules
extends ChessRules
{
5 static IsGoodFen(fen
) {
6 if (!ChessRules
.IsGoodFen(fen
)) return false;
7 const fenParsed
= V
.ParseFen(fen
);
9 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{10,10}$/))
14 static ParseFen(fen
) {
15 const fenParts
= fen
.split(" ");
16 return Object
.assign(ChessRules
.ParseFen(fen
), {
21 getEpSquare(moveOrSquare
) {
22 if (typeof moveOrSquare
!== "object" || move.vanish
.length
> 0)
23 return super.getEpSquare(moveOrSquare
);
24 // Landing move: no en-passant
28 static GenRandInitFen(randomness
) {
29 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000";
34 super.getFen() + " " + this.getReserveFen()
40 this.getBaseFen() + "_" +
41 this.getTurnFen() + "_" +
42 this.getFlagsFen() + "_" +
43 this.getEnpassantFen() + "_" +
49 let counts
= new Array(10);
52 i
< V
.PIECES
.length
- 1;
53 i
++ //-1: no king reserve
55 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
56 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
58 return counts
.join("");
61 setOtherVariables(fen
) {
62 super.setOtherVariables(fen
);
63 const fenParsed
= V
.ParseFen(fen
);
64 // Also init reserves (used by the interface to show landable pieces)
67 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
68 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
69 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
70 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
71 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
74 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
75 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
76 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
77 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
78 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
84 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
85 return this.board
[i
][j
].charAt(0);
89 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
90 return this.board
[i
][j
].charAt(1);
93 // Used by the interface:
94 getReservePpath(index
, color
) {
95 return color
+ V
.RESERVE_PIECES
[index
];
98 // Ordering on reserve pieces
99 static get RESERVE_PIECES() {
100 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
103 getReserveMoves([x
, y
]) {
104 const color
= this.turn
;
105 const p
= V
.RESERVE_PIECES
[y
];
106 if (this.reserve
[color
][p
] == 0) return [];
108 const pawnShift
= p
== V
.PAWN
? 1 : 0;
109 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
110 for (let j
= 0; j
< V
.size
.y
; j
++) {
111 if (this.board
[i
][j
] == V
.EMPTY
) {
122 start: { x: x
, y: y
}, //a bit artificial...
132 getPotentialMovesFrom([x
, y
]) {
134 // Reserves, outside of board: x == sizeX(+1)
135 return this.getReserveMoves([x
, y
]);
138 return super.getPotentialMovesFrom([x
, y
]);
141 getPotentialPawnMoves([x
, y
]) {
142 const color
= this.turn
;
144 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
145 const shiftX
= color
== "w" ? -1 : 1;
146 const startRank
= color
== "w" ? sizeX
- 2 : 1;
147 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
149 // One square forward
150 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
152 this.getBasicMove([x
, y
], [x
+ shiftX
, y
])
154 // Next condition because pawns on 1st rank can generally jump
157 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
160 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
164 for (let shiftY
of [-1, 1]) {
167 y
+ shiftY
< sizeY
&&
168 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
169 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
172 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
])
178 const Lep
= this.epSquares
.length
;
179 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
182 epSquare
.x
== x
+ shiftX
&&
183 Math
.abs(epSquare
.y
- y
) == 1
185 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
186 enpassantMove
.vanish
.push({
190 c: this.getColor(x
, epSquare
.y
)
192 moves
.push(enpassantMove
);
195 // Post-processing: remove falling pawns
196 if (x
+ shiftX
== lastRank
) {
206 let moves
= super.getAllValidMoves();
207 const color
= this.turn
;
208 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
209 moves
= moves
.concat(
210 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
212 return this.filterValid(moves
);
216 if (!super.atLeastOneMove()) {
217 // Search one reserve move
218 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
219 let moves
= this.filterValid(
220 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
222 if (moves
.length
> 0) return true;
229 canTake([x1
, y1
], [x2
, y2
]) {
230 // Self-captures allowed, except for the king:
231 return this.getPiece(x2
, y2
) != V
.KING
;
234 updateVariables(move) {
235 super.updateVariables(move);
236 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
237 const color
= V
.GetOppCol(this.turn
);
238 if (move.vanish
.length
== 0) {
239 this.reserve
[color
][move.appear
[0].p
]--;
242 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
) {
244 this.reserve
[color
][move.vanish
[1].p
]++;
248 unupdateVariables(move) {
249 super.unupdateVariables(move);
250 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
251 const color
= this.turn
;
252 if (move.vanish
.length
== 0) {
253 this.reserve
[color
][move.appear
[0].p
]++;
256 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
) {
257 this.reserve
[color
][move.vanish
[1].p
]--;
261 static get SEARCH_DEPTH() {
266 let evaluation
= super.evalPosition();
268 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
269 const p
= V
.RESERVE_PIECES
[i
];
270 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
271 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
277 const finalSquare
= V
.CoordsToSquare(move.end
);
278 if (move.vanish
.length
> 0) {
279 if (move.appear
.length
> 0) {
281 return super.getNotation(move);
283 // Pawn fallen: capturing or not
285 if (move.vanish
.length
== 2)
286 res
+= V
.CoordToColumn(move.start
.y
) + "x";
287 return res
+ finalSquare
;
292 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
293 return piece
+ "@" + V
.CoordsToSquare(move.end
);