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 const fenParsed
= V
.ParseFen(fen
);
83 // Also init reserves (used by the interface to show landable pieces)
86 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
87 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
88 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
89 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
90 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
93 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
94 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
95 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
96 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
97 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
100 this.promoted
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
101 if (fenParsed
.promoted
!= "-") {
102 for (let square
of fenParsed
.promoted
.split(",")) {
103 const coords
= V
.SquareToCoords(square
);
104 this.promoted
[coords
.x
][coords
.y
] = true;
110 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
111 return this.board
[i
][j
].charAt(0);
115 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
116 return this.board
[i
][j
].charAt(1);
119 // Used by the interface:
120 getReservePpath(index
, color
) {
121 return color
+ V
.RESERVE_PIECES
[index
];
123 // // Version if some day I have pieces with numbers printed on it:
124 // getReservePpath(index, color) {
127 // color + V.RESERVE_PIECES[index] +
128 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
132 // Ordering on reserve pieces
133 static get RESERVE_PIECES() {
134 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
137 getReserveMoves([x
, y
]) {
138 const color
= this.turn
;
139 const p
= V
.RESERVE_PIECES
[y
];
140 if (this.reserve
[color
][p
] == 0) return [];
142 const pawnShift
= p
== V
.PAWN
? 1 : 0;
143 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
144 for (let j
= 0; j
< V
.size
.y
; j
++) {
145 if (this.board
[i
][j
] == V
.EMPTY
) {
156 start: { x: x
, y: y
}, //a bit artificial...
166 getPotentialMovesFrom([x
, y
]) {
168 // Reserves, outside of board: x == sizeX(+1)
169 return this.getReserveMoves([x
, y
]);
172 return super.getPotentialMovesFrom([x
, y
]);
176 let moves
= super.getAllValidMoves();
177 const color
= this.turn
;
178 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
179 moves
= moves
.concat(
180 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
182 return this.filterValid(moves
);
186 if (!super.atLeastOneMove()) {
187 // Search one reserve move
188 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
189 let moves
= this.filterValid(
190 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
192 if (moves
.length
> 0) return true;
200 super.postPlay(move);
201 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
202 const color
= move.appear
[0].c
;
203 if (move.vanish
.length
== 0) {
204 this.reserve
[color
][move.appear
[0].p
]--;
207 move.movePromoted
= this.promoted
[move.start
.x
][move.start
.y
];
208 move.capturePromoted
= this.promoted
[move.end
.x
][move.end
.y
];
209 this.promoted
[move.start
.x
][move.start
.y
] = false;
210 this.promoted
[move.end
.x
][move.end
.y
] =
212 (move.vanish
[0].p
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
);
213 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]++;
214 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]++;
218 super.postUndo(move);
219 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
220 const color
= this.turn
;
221 if (move.vanish
.length
== 0) {
222 this.reserve
[color
][move.appear
[0].p
]++;
225 if (move.movePromoted
) this.promoted
[move.start
.x
][move.start
.y
] = true;
226 this.promoted
[move.end
.x
][move.end
.y
] = move.capturePromoted
;
227 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]--;
228 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]--;
231 static get SEARCH_DEPTH() {
236 let evaluation
= super.evalPosition();
238 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
239 const p
= V
.RESERVE_PIECES
[i
];
240 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
241 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
247 if (move.vanish
.length
> 0) return super.getNotation(move);
250 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
251 return piece
+ "@" + V
.CoordsToSquare(move.end
);