1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class CrazyhouseRules
extends ChessRules
{
6 static IsGoodFen(fen
) {
7 if (!ChessRules
.IsGoodFen(fen
)) return false;
8 const fenParsed
= V
.ParseFen(fen
);
10 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{10,10}$/))
12 // 6) Check promoted array
13 if (!fenParsed
.promoted
) return false;
14 if (fenParsed
.promoted
== "-") return true; //no promoted piece on board
15 const squares
= fenParsed
.promoted
.split(",");
16 for (let square
of squares
) {
17 const c
= V
.SquareToCoords(square
);
18 if (c
.y
< 0 || c
.y
> V
.size
.y
|| isNaN(c
.x
) || c
.x
< 0 || c
.x
> V
.size
.x
)
24 static ParseFen(fen
) {
25 const fenParts
= fen
.split(" ");
27 ChessRules
.ParseFen(fen
),
35 static GenRandInitFen(randomness
) {
36 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000 -";
41 super.getFen() + " " +
42 this.getReserveFen() + " " +
49 super.getFenForRepeat() + "_" +
50 this.getReserveFen() + "_" +
56 let counts
= new Array(10);
59 i
< V
.PIECES
.length
- 1;
60 i
++ //-1: no king reserve
62 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
63 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
65 return counts
.join("");
70 for (let i
= 0; i
< V
.size
.x
; i
++) {
71 for (let j
= 0; j
< V
.size
.y
; j
++) {
72 if (this.promoted
[i
][j
]) res
+= V
.CoordsToSquare({ x: i
, y: j
}) + ",";
76 if (res
.length
> 0) res
= res
.slice(0, -1);
81 setOtherVariables(fen
) {
82 super.setOtherVariables(fen
);
83 const fenParsed
= V
.ParseFen(fen
);
84 // Also init reserves (used by the interface to show landable pieces)
85 const reserve
= fenParsed
.reserve
.split("").map(x
=> parseInt(x
, 10));
90 [V
.KNIGHT
]: reserve
[2],
91 [V
.BISHOP
]: reserve
[3],
97 [V
.KNIGHT
]: reserve
[7],
98 [V
.BISHOP
]: reserve
[8],
102 this.promoted
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
103 if (fenParsed
.promoted
!= "-") {
104 for (let square
of fenParsed
.promoted
.split(",")) {
105 const coords
= V
.SquareToCoords(square
);
106 this.promoted
[coords
.x
][coords
.y
] = true;
112 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
113 return this.board
[i
][j
].charAt(0);
117 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
118 return this.board
[i
][j
].charAt(1);
121 // Used by the interface:
122 getReservePpath(index
, color
) {
123 return color
+ V
.RESERVE_PIECES
[index
];
125 // // Version if some day I have pieces with numbers printed on it:
126 // getReservePpath(index, color) {
129 // color + V.RESERVE_PIECES[index] +
130 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
134 // Ordering on reserve pieces
135 static get RESERVE_PIECES() {
136 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
139 getReserveMoves([x
, y
]) {
140 const color
= this.turn
;
141 const p
= V
.RESERVE_PIECES
[y
];
142 if (this.reserve
[color
][p
] == 0) return [];
144 const pawnShift
= p
== V
.PAWN
? 1 : 0;
145 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
146 for (let j
= 0; j
< V
.size
.y
; j
++) {
147 if (this.board
[i
][j
] == V
.EMPTY
) {
158 start: { x: x
, y: y
}, //a bit artificial...
168 getPotentialMovesFrom([x
, y
]) {
170 // Reserves, outside of board: x == sizeX(+1)
171 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()) return true;
189 // Search one reserve move
190 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
191 const moves
= this.filterValid(
192 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
194 if (moves
.length
> 0) return true;
200 super.postPlay(move);
202 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
203 const color
= move.appear
[0].c
;
204 if (move.vanish
.length
== 0) {
205 this.reserve
[color
][move.appear
[0].p
]--;
208 move.movePromoted
= this.promoted
[move.start
.x
][move.start
.y
];
209 move.capturePromoted
= this.promoted
[move.end
.x
][move.end
.y
];
210 this.promoted
[move.start
.x
][move.start
.y
] = false;
211 this.promoted
[move.end
.x
][move.end
.y
] =
213 (move.vanish
[0].p
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
);
214 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]++;
215 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]++;
219 super.postUndo(move);
220 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
221 const color
= this.turn
;
222 if (move.vanish
.length
== 0) {
223 this.reserve
[color
][move.appear
[0].p
]++;
226 if (move.movePromoted
) this.promoted
[move.start
.x
][move.start
.y
] = true;
227 this.promoted
[move.end
.x
][move.end
.y
] = move.capturePromoted
;
228 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]--;
229 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]--;
232 static get SEARCH_DEPTH() {
237 let evaluation
= super.evalPosition();
239 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
240 const p
= V
.RESERVE_PIECES
[i
];
241 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
242 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
248 if (move.vanish
.length
> 0) return super.getNotation(move);
251 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
252 return piece
+ "@" + V
.CoordsToSquare(move.end
);