1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class CrazyhouseRules
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}$/))
11 // 6) Check promoted array
12 if (!fenParsed
.promoted
) return false;
13 if (fenParsed
.promoted
== "-") return true; //no promoted piece on board
14 const squares
= fenParsed
.promoted
.split(",");
15 for (let square
of squares
) {
16 const c
= V
.SquareToCoords(square
);
17 if (c
.y
< 0 || c
.y
> V
.size
.y
|| isNaN(c
.x
) || c
.x
< 0 || c
.x
> V
.size
.x
)
23 static ParseFen(fen
) {
24 const fenParts
= fen
.split(" ");
26 ChessRules
.ParseFen(fen
),
34 static GenRandInitFen(randomness
) {
35 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000 -";
40 super.getFen() + " " +
41 this.getReserveFen() + " " +
48 super.getFenForRepeat() + "_" +
49 this.getReserveFen() + "_" +
55 let counts
= new Array(10);
58 i
< V
.PIECES
.length
- 1;
59 i
++ //-1: no king reserve
61 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
62 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
64 return counts
.join("");
69 for (let i
= 0; i
< V
.size
.x
; i
++) {
70 for (let j
= 0; j
< V
.size
.y
; j
++) {
71 if (this.promoted
[i
][j
]) res
+= V
.CoordsToSquare({ x: i
, y: j
}) + ",";
75 if (res
.length
> 0) res
= res
.slice(0, -1);
80 setOtherVariables(fen
) {
81 super.setOtherVariables(fen
);
82 // Also init reserves (used by the interface to show landable pieces)
84 V
.ParseFen(fen
).reserve
.split("").map(x
=> parseInt(x
, 10));
89 [V
.KNIGHT
]: reserve
[2],
90 [V
.BISHOP
]: reserve
[3],
96 [V
.KNIGHT
]: reserve
[7],
97 [V
.BISHOP
]: reserve
[8],
101 this.promoted
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
102 if (fenParsed
.promoted
!= "-") {
103 for (let square
of fenParsed
.promoted
.split(",")) {
104 const coords
= V
.SquareToCoords(square
);
105 this.promoted
[coords
.x
][coords
.y
] = true;
111 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
112 return this.board
[i
][j
].charAt(0);
116 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
117 return this.board
[i
][j
].charAt(1);
120 // Used by the interface:
121 getReservePpath(index
, color
) {
122 return color
+ V
.RESERVE_PIECES
[index
];
124 // // Version if some day I have pieces with numbers printed on it:
125 // getReservePpath(index, color) {
128 // color + V.RESERVE_PIECES[index] +
129 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
133 // Ordering on reserve pieces
134 static get RESERVE_PIECES() {
135 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
138 getReserveMoves([x
, y
]) {
139 const color
= this.turn
;
140 const p
= V
.RESERVE_PIECES
[y
];
141 if (this.reserve
[color
][p
] == 0) return [];
143 const pawnShift
= p
== V
.PAWN
? 1 : 0;
144 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
145 for (let j
= 0; j
< V
.size
.y
; j
++) {
146 if (this.board
[i
][j
] == V
.EMPTY
) {
157 start: { x: x
, y: y
}, //a bit artificial...
167 getPotentialMovesFrom([x
, y
]) {
169 // Reserves, outside of board: x == sizeX(+1)
170 return this.getReserveMoves([x
, y
]);
173 return super.getPotentialMovesFrom([x
, y
]);
177 let moves
= super.getAllPotentialMoves();
178 const color
= this.turn
;
179 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
180 moves
= moves
.concat(
181 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
184 return this.filterValid(moves
);
188 if (!super.atLeastOneMove()) {
189 // Search one reserve move
190 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
191 let moves
= this.filterValid(
192 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
194 if (moves
.length
> 0) return true;
202 super.postPlay(move);
204 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
205 const color
= move.appear
[0].c
;
206 if (move.vanish
.length
== 0) {
207 this.reserve
[color
][move.appear
[0].p
]--;
210 move.movePromoted
= this.promoted
[move.start
.x
][move.start
.y
];
211 move.capturePromoted
= this.promoted
[move.end
.x
][move.end
.y
];
212 this.promoted
[move.start
.x
][move.start
.y
] = false;
213 this.promoted
[move.end
.x
][move.end
.y
] =
215 (move.vanish
[0].p
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
);
216 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]++;
217 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]++;
221 super.postUndo(move);
222 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
223 const color
= this.turn
;
224 if (move.vanish
.length
== 0) {
225 this.reserve
[color
][move.appear
[0].p
]++;
228 if (move.movePromoted
) this.promoted
[move.start
.x
][move.start
.y
] = true;
229 this.promoted
[move.end
.x
][move.end
.y
] = move.capturePromoted
;
230 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]--;
231 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]--;
234 static get SEARCH_DEPTH() {
239 let evaluation
= super.evalPosition();
241 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
242 const p
= V
.RESERVE_PIECES
[i
];
243 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
244 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
250 if (move.vanish
.length
> 0) return super.getNotation(move);
253 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
254 return piece
+ "@" + V
.CoordsToSquare(move.end
);