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 static GenRandInitFen() {
22 return ChessRules
.GenRandInitFen() + " 0000000000";
27 super.getFen() + " " + this.getReserveFen()
32 let counts
= new Array(10);
35 i
< V
.PIECES
.length
- 1;
36 i
++ //-1: no king reserve
38 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
39 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
41 return counts
.join("");
44 setOtherVariables(fen
) {
45 super.setOtherVariables(fen
);
46 const fenParsed
= V
.ParseFen(fen
);
47 // Also init reserves (used by the interface to show landable pieces)
50 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
51 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
52 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
53 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
54 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
57 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
58 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
59 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
60 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
61 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
67 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
68 return this.board
[i
][j
].charAt(0);
72 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
73 return this.board
[i
][j
].charAt(1);
76 // Used by the interface:
77 getReservePpath(color
, index
) {
78 return color
+ V
.RESERVE_PIECES
[index
];
81 // Ordering on reserve pieces
82 static get RESERVE_PIECES() {
83 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
86 getReserveMoves([x
, y
]) {
87 const color
= this.turn
;
88 const p
= V
.RESERVE_PIECES
[y
];
89 if (this.reserve
[color
][p
] == 0) return [];
91 const pawnShift
= p
== V
.PAWN
? 1 : 0;
92 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
93 for (let j
= 0; j
< V
.size
.y
; j
++) {
94 if (this.board
[i
][j
] == V
.EMPTY
) {
105 start: { x: x
, y: y
}, //a bit artificial...
115 getPotentialMovesFrom([x
, y
]) {
117 // Reserves, outside of board: x == sizeX(+1)
118 return this.getReserveMoves([x
, y
]);
121 return super.getPotentialMovesFrom([x
, y
]);
124 getPotentialPawnMoves([x
, y
]) {
125 const color
= this.turn
;
127 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
128 const shiftX
= color
== "w" ? -1 : 1;
129 const startRank
= color
== "w" ? sizeX
- 2 : 1;
130 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
132 // One square forward
133 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
135 this.getBasicMove([x
, y
], [x
+ shiftX
, y
])
137 // Next condition because pawns on 1st rank can generally jump
140 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
143 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
147 for (let shiftY
of [-1, 1]) {
150 y
+ shiftY
< sizeY
&&
151 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
152 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
155 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
])
161 const Lep
= this.epSquares
.length
;
162 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
165 epSquare
.x
== x
+ shiftX
&&
166 Math
.abs(epSquare
.y
- y
) == 1
168 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
169 enpassantMove
.vanish
.push({
173 c: this.getColor(x
, epSquare
.y
)
175 moves
.push(enpassantMove
);
178 // Post-processing: remove falling pawns
179 if (x
+ shiftX
== lastRank
) {
189 let moves
= super.getAllValidMoves();
190 const color
= this.turn
;
191 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
192 moves
= moves
.concat(
193 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
195 return this.filterValid(moves
);
199 if (!super.atLeastOneMove()) {
200 // Search one reserve move
201 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
202 let moves
= this.filterValid(
203 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
205 if (moves
.length
> 0) return true;
212 canTake([x1
, y1
], [x2
, y2
]) {
213 // Self-captures allowed, except for the king:
214 return this.getPiece(x2
, y2
) != V
.KING
;
217 updateVariables(move) {
218 super.updateVariables(move);
219 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
220 const color
= V
.GetOppCol(this.turn
);
221 if (move.vanish
.length
== 0) {
222 this.reserve
[color
][move.appear
[0].p
]--;
225 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
) {
227 this.reserve
[color
][move.vanish
[1].p
]++;
231 unupdateVariables(move) {
232 super.unupdateVariables(move);
233 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
234 const color
= this.turn
;
235 if (move.vanish
.length
== 0) {
236 this.reserve
[color
][move.appear
[0].p
]++;
239 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
) {
240 this.reserve
[color
][move.vanish
[1].p
]--;
244 static get SEARCH_DEPTH() {
249 let evaluation
= super.evalPosition();
251 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
252 const p
= V
.RESERVE_PIECES
[i
];
253 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
254 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
260 const finalSquare
= V
.CoordsToSquare(move.end
);
261 if (move.vanish
.length
> 0) {
262 if (move.appear
.length
> 0) {
264 return super.getNotation(move);
266 // Pawn fallen: capturing or not
268 if (move.vanish
.length
== 2)
269 res
+= V
.CoordToColumn(move.start
.y
) + "x";
270 return res
+ finalSquare
;
275 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
276 return piece
+ "@" + V
.CoordsToSquare(move.end
);