1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class RecycleRules
extends ChessRules
{
5 static get PawnSpecs() {
9 { promotions: [V
.PAWN
] } //in fact, none
13 static IsGoodFen(fen
) {
14 if (!ChessRules
.IsGoodFen(fen
)) return false;
15 const fenParsed
= V
.ParseFen(fen
);
17 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{10,10}$/))
22 static ParseFen(fen
) {
23 const fenParts
= fen
.split(" ");
25 ChessRules
.ParseFen(fen
),
26 { reserve: fenParts
[5] }
30 getEpSquare(moveOrSquare
) {
31 if (typeof moveOrSquare
!== "object" || moveOrSquare
.vanish
.length
> 0)
32 return super.getEpSquare(moveOrSquare
);
33 // Landing move: no en-passant
37 static GenRandInitFen(randomness
) {
38 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000";
42 return super.getFen() + " " + this.getReserveFen();
46 return super.getFenForRepeat() + "_" + this.getReserveFen();
50 let counts
= new Array(10);
53 i
< V
.PIECES
.length
- 1;
54 i
++ //-1: no king reserve
56 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
57 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
59 return counts
.join("");
62 setOtherVariables(fen
) {
63 super.setOtherVariables(fen
);
64 const fenParsed
= V
.ParseFen(fen
);
65 // Also init reserves (used by the interface to show landable pieces)
68 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
69 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
70 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
71 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
72 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
75 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
76 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
77 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
78 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
79 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
85 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
86 return this.board
[i
][j
].charAt(0);
90 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
91 return this.board
[i
][j
].charAt(1);
94 // Used by the interface:
95 getReservePpath(index
, color
) {
96 return color
+ V
.RESERVE_PIECES
[index
];
99 // Ordering on reserve pieces
100 static get RESERVE_PIECES() {
101 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
104 getReserveMoves([x
, y
]) {
105 const color
= this.turn
;
106 const p
= V
.RESERVE_PIECES
[y
];
107 if (this.reserve
[color
][p
] == 0) return [];
109 const pawnShift
= p
== V
.PAWN
? 1 : 0;
110 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
111 for (let j
= 0; j
< V
.size
.y
; j
++) {
112 if (this.board
[i
][j
] == V
.EMPTY
) {
123 start: { x: x
, y: y
}, //a bit artificial...
133 getPotentialMovesFrom([x
, y
]) {
135 // Reserves, outside of board: x == sizeX(+1)
136 return this.getReserveMoves([x
, y
]);
139 return super.getPotentialMovesFrom([x
, y
]);
142 getPotentialPawnMoves([x
, y
]) {
143 let moves
= super.getPotentialPawnMoves([x
, y
]);
144 // Remove pawns on 8th rank ("fallen"):
145 const color
= this.turn
;
146 const lastRank
= (color
== "w" ? 0 : V
.size
.x
- 1);
148 if (m
.appear
[0].x
== lastRank
) m
.appear
.pop();
154 let moves
= super.getAllValidMoves();
155 const color
= this.turn
;
156 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
157 moves
= moves
.concat(
158 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
160 return this.filterValid(moves
);
164 if (!super.atLeastOneMove()) {
165 // Search one reserve move
166 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
167 let moves
= this.filterValid(
168 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
170 if (moves
.length
> 0) return true;
177 canTake([x1
, y1
], [x2
, y2
]) {
178 // Self-captures allowed, except for the king:
179 return this.getPiece(x2
, y2
) != V
.KING
;
184 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
185 const color
= this.turn
;
186 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
187 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
189 this.reserve
[color
][move.vanish
[1].p
]++;
193 super.postUndo(move);
194 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
195 const color
= this.turn
;
196 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
197 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
198 this.reserve
[color
][move.vanish
[1].p
]--;
201 static get SEARCH_DEPTH() {
206 let evaluation
= super.evalPosition();
208 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
209 const p
= V
.RESERVE_PIECES
[i
];
210 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
211 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
217 const finalSquare
= V
.CoordsToSquare(move.end
);
218 if (move.vanish
.length
> 0) {
219 if (move.appear
.length
> 0) {
221 return super.getNotation(move);
223 // Pawn fallen: capturing or not
225 if (move.vanish
.length
== 2)
226 res
+= V
.CoordToColumn(move.start
.y
) + "x";
227 return res
+ finalSquare
;
232 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
233 return piece
+ "@" + V
.CoordsToSquare(move.end
);