eb84044e83033cf10b617eb811e8c6048da89d09
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(randomness
) {
32 return ChessRules
.GenRandInitFen(randomness
) + " 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);
89 // Used by the interface:
90 getReservePpath(index
, color
) {
91 return color
+ V
.RESERVE_PIECES
[index
];
94 // Ordering on reserve pieces
95 static get RESERVE_PIECES() {
96 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
99 getReserveMoves([x
, y
]) {
100 const color
= this.turn
;
101 const p
= V
.RESERVE_PIECES
[y
];
102 if (this.reserve
[color
][p
] == 0) return [];
104 const pawnShift
= p
== V
.PAWN
? 1 : 0;
105 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
106 for (let j
= 0; j
< V
.size
.y
; j
++) {
107 if (this.board
[i
][j
] == V
.EMPTY
) {
118 start: { x: x
, y: y
}, //a bit artificial...
128 getPotentialMovesFrom([x
, y
]) {
130 // Reserves, outside of board: x == sizeX(+1)
131 return this.getReserveMoves([x
, y
]);
133 return super.getPotentialMovesFrom([x
, y
]);
136 getPotentialPawnMoves([x
, y
]) {
137 let moves
= super.getPotentialPawnMoves([x
, y
]);
138 // Remove pawns on 8th rank ("fallen"):
139 const color
= this.turn
;
140 const lastRank
= (color
== "w" ? 0 : V
.size
.x
- 1);
142 if (m
.appear
[0].x
== lastRank
) m
.appear
.pop();
148 let moves
= super.getAllPotentialMoves();
149 const color
= this.turn
;
150 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
151 moves
= moves
.concat(
152 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
155 return this.filterValid(moves
);
159 if (!super.atLeastOneMove()) {
160 // Search one reserve move
161 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
162 let moves
= this.filterValid(
163 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
165 if (moves
.length
> 0) return true;
172 canTake([x1
, y1
], [x2
, y2
]) {
173 // Self-captures allowed, except for the king:
174 return this.getPiece(x2
, y2
) != V
.KING
;
180 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
181 const color
= this.turn
;
182 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]--;
183 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
185 this.reserve
[color
][move.vanish
[1].p
]++;
189 super.postUndo(move);
190 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
191 const color
= this.turn
;
192 if (move.vanish
.length
== 0) this.reserve
[color
][move.appear
[0].p
]++;
193 else if (move.vanish
.length
== 2 && move.vanish
[1].c
== color
)
194 this.reserve
[color
][move.vanish
[1].p
]--;
197 static get SEARCH_DEPTH() {
202 let evaluation
= super.evalPosition();
204 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
205 const p
= V
.RESERVE_PIECES
[i
];
206 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
207 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
213 const finalSquare
= V
.CoordsToSquare(move.end
);
214 if (move.vanish
.length
> 0) {
215 if (move.appear
.length
> 0) {
217 return super.getNotation(move);
219 // Pawn fallen: capturing or not
221 if (move.vanish
.length
== 2)
222 res
+= V
.CoordToColumn(move.start
.y
) + "x";
223 return res
+ finalSquare
;
228 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
229 return piece
+ "@" + V
.CoordsToSquare(move.end
);