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(" ");
24 return Object
.assign(ChessRules
.ParseFen(fen
), {
29 getEpSquare(moveOrSquare
) {
30 if (typeof moveOrSquare
!== "object" || moveOrSquare
.vanish
.length
> 0)
31 return super.getEpSquare(moveOrSquare
);
32 // Landing move: no en-passant
36 static GenRandInitFen(randomness
) {
37 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000";
41 return super.getFen() + " " + this.getReserveFen();
45 return super.getFenForRepeat() + "_" + this.getReserveFen();
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 let moves
= super.getPotentialPawnMoves([x
, y
]);
143 // Remove pawns on 8th rank ("fallen"):
144 const color
= this.turn
;
145 const lastRank
= (color
== "w" ? 0 : V
.size
.x
- 1);
147 if (m
.appear
[0].x
== lastRank
) m
.appear
.pop();
153 let moves
= super.getAllValidMoves();
154 const color
= this.turn
;
155 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
156 moves
= moves
.concat(
157 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
159 return this.filterValid(moves
);
163 if (!super.atLeastOneMove()) {
164 // Search one reserve move
165 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
166 let moves
= this.filterValid(
167 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
169 if (moves
.length
> 0) return true;
176 canTake([x1
, y1
], [x2
, y2
]) {
177 // Self-captures allowed, except for the king:
178 return this.getPiece(x2
, y2
) != V
.KING
;
183 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
184 const color
= this.turn
;
185 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
186 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
188 this.reserve
[color
][move.vanish
[1].p
]++;
192 super.postUndo(move);
193 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
194 const color
= this.turn
;
195 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
196 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
197 this.reserve
[color
][move.vanish
[1].p
]--;
200 static get SEARCH_DEPTH() {
205 let evaluation
= super.evalPosition();
207 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
208 const p
= V
.RESERVE_PIECES
[i
];
209 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
210 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
216 const finalSquare
= V
.CoordsToSquare(move.end
);
217 if (move.vanish
.length
> 0) {
218 if (move.appear
.length
> 0) {
220 return super.getNotation(move);
222 // Pawn fallen: capturing or not
224 if (move.vanish
.length
== 2)
225 res
+= V
.CoordToColumn(move.start
.y
) + "x";
226 return res
+ finalSquare
;
231 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
232 return piece
+ "@" + V
.CoordsToSquare(move.end
);