1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class RecycleRules
extends ChessRules
{
6 static get PawnSpecs() {
10 { promotions: [V
.PAWN
] } //in fact, none
14 static IsGoodFen(fen
) {
15 if (!ChessRules
.IsGoodFen(fen
)) return false;
16 const fenParsed
= V
.ParseFen(fen
);
18 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{10,10}$/))
23 static ParseFen(fen
) {
24 const fenParts
= fen
.split(" ");
26 ChessRules
.ParseFen(fen
),
27 { reserve: fenParts
[5] }
31 static GenRandInitFen(options
) {
32 return ChessRules
.GenRandInitFen(options
) + " 0000000000";
36 return super.getFen() + " " + this.getReserveFen();
40 return super.getFenForRepeat() + "_" + this.getReserveFen();
44 let counts
= new Array(10);
47 i
< V
.PIECES
.length
- 1;
48 i
++ //-1: no king reserve
50 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
51 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
53 return counts
.join("");
56 setOtherVariables(fen
) {
57 super.setOtherVariables(fen
);
58 // Also init reserves (used by the interface to show landable pieces)
60 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
65 [V
.KNIGHT
]: reserve
[2],
66 [V
.BISHOP
]: reserve
[3],
72 [V
.KNIGHT
]: reserve
[7],
73 [V
.BISHOP
]: reserve
[8],
80 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
81 return this.board
[i
][j
].charAt(0);
85 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
86 return this.board
[i
][j
].charAt(1);
90 if (m
.vanish
.length
== 2 && m
.appear
.length
== 2) {
91 // Castle: show castle symbol
92 return "Coregal/castle";
94 return super.getPPpath(m
);
97 // Used by the interface:
98 getReservePpath(index
, color
) {
99 return color
+ V
.RESERVE_PIECES
[index
];
102 // Ordering on reserve pieces
103 static get RESERVE_PIECES() {
104 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
107 getReserveMoves([x
, y
]) {
108 const color
= this.turn
;
109 const p
= V
.RESERVE_PIECES
[y
];
110 if (this.reserve
[color
][p
] == 0) return [];
112 const pawnShift
= p
== V
.PAWN
? 1 : 0;
113 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
114 for (let j
= 0; j
< V
.size
.y
; j
++) {
115 if (this.board
[i
][j
] == V
.EMPTY
) {
126 start: { x: x
, y: y
}, //a bit artificial...
136 getPotentialMovesFrom([x
, y
]) {
138 // Reserves, outside of board: x == sizeX(+1)
139 return this.getReserveMoves([x
, y
]);
141 return super.getPotentialMovesFrom([x
, y
]);
144 getPotentialPawnMoves([x
, y
]) {
145 let moves
= super.getPotentialPawnMoves([x
, y
]);
146 // Remove pawns on 8th rank ("fallen"):
147 const color
= this.turn
;
148 const lastRank
= (color
== "w" ? 0 : V
.size
.x
- 1);
150 if (m
.appear
[0].x
== lastRank
) m
.appear
.pop();
156 let moves
= super.getAllPotentialMoves();
157 const color
= this.turn
;
158 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
159 moves
= moves
.concat(
160 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
163 return this.filterValid(moves
);
167 if (!super.atLeastOneMove()) {
168 // Search one reserve move
169 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
170 let moves
= this.filterValid(
171 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
173 if (moves
.length
> 0) return true;
180 canTake([x1
, y1
], [x2
, y2
]) {
181 // Self-captures allowed, except for the king:
182 return this.getPiece(x2
, y2
) != V
.KING
;
188 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
189 const color
= this.turn
;
190 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
191 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
193 this.reserve
[color
][move.vanish
[1].p
]++;
197 super.postUndo(move);
198 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
199 const color
= this.turn
;
200 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
201 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
202 this.reserve
[color
][move.vanish
[1].p
]--;
205 static get SEARCH_DEPTH() {
210 let evaluation
= super.evalPosition();
212 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
213 const p
= V
.RESERVE_PIECES
[i
];
214 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
215 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
221 const finalSquare
= V
.CoordsToSquare(move.end
);
222 if (move.vanish
.length
> 0) {
223 if (move.appear
.length
> 0) {
225 return super.getNotation(move);
227 // Pawn fallen: capturing or not
229 if (move.vanish
.length
== 2)
230 res
+= V
.CoordToColumn(move.start
.y
) + "x";
231 return res
+ finalSquare
;
236 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
237 return piece
+ "@" + V
.CoordsToSquare(move.end
);