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" || moveOrSquare
.vanish
.length
> 0)
23 return super.getEpSquare(moveOrSquare
);
24 // Landing move: no en-passant
28 static GenRandInitFen(randomness
) {
29 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000";
33 return super.getFen() + " " + this.getReserveFen();
37 return super.getFenForRepeat() + "_" + this.getReserveFen();
41 let counts
= new Array(10);
44 i
< V
.PIECES
.length
- 1;
45 i
++ //-1: no king reserve
47 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
48 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
50 return counts
.join("");
53 setOtherVariables(fen
) {
54 super.setOtherVariables(fen
);
55 const fenParsed
= V
.ParseFen(fen
);
56 // Also init reserves (used by the interface to show landable pieces)
59 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
60 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
61 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
62 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
63 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
66 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
67 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
68 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
69 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
70 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
76 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
77 return this.board
[i
][j
].charAt(0);
81 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
82 return this.board
[i
][j
].charAt(1);
85 // Used by the interface:
86 getReservePpath(index
, color
) {
87 return color
+ V
.RESERVE_PIECES
[index
];
90 // Ordering on reserve pieces
91 static get RESERVE_PIECES() {
92 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
95 getReserveMoves([x
, y
]) {
96 const color
= this.turn
;
97 const p
= V
.RESERVE_PIECES
[y
];
98 if (this.reserve
[color
][p
] == 0) return [];
100 const pawnShift
= p
== V
.PAWN
? 1 : 0;
101 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
102 for (let j
= 0; j
< V
.size
.y
; j
++) {
103 if (this.board
[i
][j
] == V
.EMPTY
) {
114 start: { x: x
, y: y
}, //a bit artificial...
124 getPotentialMovesFrom([x
, y
]) {
126 // Reserves, outside of board: x == sizeX(+1)
127 return this.getReserveMoves([x
, y
]);
130 return super.getPotentialMovesFrom([x
, y
]);
133 getPotentialPawnMoves([x
, y
]) {
134 const color
= this.turn
;
136 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
137 const shiftX
= color
== "w" ? -1 : 1;
138 const startRank
= color
== "w" ? sizeX
- 2 : 1;
139 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
141 // One square forward
142 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
144 this.getBasicMove([x
, y
], [x
+ shiftX
, y
])
146 // Next condition because pawns on 1st rank can generally jump
149 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
152 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
156 for (let shiftY
of [-1, 1]) {
159 y
+ shiftY
< sizeY
&&
160 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
161 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
164 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
])
170 const Lep
= this.epSquares
.length
;
171 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
174 epSquare
.x
== x
+ shiftX
&&
175 Math
.abs(epSquare
.y
- y
) == 1
177 let enpassantMove
= this.getBasicMove([x
, y
], [epSquare
.x
, epSquare
.y
]);
178 enpassantMove
.vanish
.push({
182 c: this.getColor(x
, epSquare
.y
)
184 moves
.push(enpassantMove
);
187 // Post-processing: remove falling pawns
188 if (x
+ shiftX
== lastRank
) {
198 let moves
= super.getAllValidMoves();
199 const color
= this.turn
;
200 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
201 moves
= moves
.concat(
202 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
204 return this.filterValid(moves
);
208 if (!super.atLeastOneMove()) {
209 // Search one reserve move
210 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
211 let moves
= this.filterValid(
212 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
214 if (moves
.length
> 0) return true;
221 canTake([x1
, y1
], [x2
, y2
]) {
222 // Self-captures allowed, except for the king:
223 return this.getPiece(x2
, y2
) != V
.KING
;
227 super.postPlay(move);
228 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
229 const color
= move.appear
[0].c
;
230 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
231 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
233 this.reserve
[color
][move.vanish
[1].p
]++;
237 super.postUndo(move);
238 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
239 const color
= this.turn
;
240 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
241 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
242 this.reserve
[color
][move.vanish
[1].p
]--;
245 static get SEARCH_DEPTH() {
250 let evaluation
= super.evalPosition();
252 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
253 const p
= V
.RESERVE_PIECES
[i
];
254 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
255 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
261 const finalSquare
= V
.CoordsToSquare(move.end
);
262 if (move.vanish
.length
> 0) {
263 if (move.appear
.length
> 0) {
265 return super.getNotation(move);
267 // Pawn fallen: capturing or not
269 if (move.vanish
.length
== 2)
270 res
+= V
.CoordToColumn(move.start
.y
) + "x";
271 return res
+ finalSquare
;
276 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
277 return piece
+ "@" + V
.CoordsToSquare(move.end
);