1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export const VariantRules
= 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(" ");
25 return Object
.assign(ChessRules
.ParseFen(fen
), {
31 static GenRandInitFen(randomness
) {
32 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000 -";
37 super.getFen() + " " + this.getReserveFen() + " " + this.getPromotedFen()
43 this.getBaseFen() + "_" +
44 this.getTurnFen() + "_" +
45 this.getFlagsFen() + "_" +
46 this.getEnpassantFen() + "_" +
47 this.getReserveFen() + "_" +
53 let counts
= new Array(10);
56 i
< V
.PIECES
.length
- 1;
57 i
++ //-1: no king reserve
59 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
60 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
62 return counts
.join("");
67 for (let i
= 0; i
< V
.size
.x
; i
++) {
68 for (let j
= 0; j
< V
.size
.y
; j
++) {
69 if (this.promoted
[i
][j
]) res
+= V
.CoordsToSquare({ x: i
, y: j
}) + ",";
73 if (res
.length
> 0) res
= res
.slice(0, -1);
78 setOtherVariables(fen
) {
79 super.setOtherVariables(fen
);
80 const fenParsed
= V
.ParseFen(fen
);
81 // Also init reserves (used by the interface to show landable pieces)
84 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
85 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
86 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
87 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
88 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
91 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
92 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
93 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
94 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
95 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
98 this.promoted
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
99 if (fenParsed
.promoted
!= "-") {
100 for (let square
of fenParsed
.promoted
.split(",")) {
101 const coords
= V
.SquareToCoords(square
);
102 this.promoted
[coords
.x
][coords
.y
] = true;
108 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
109 return this.board
[i
][j
].charAt(0);
113 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
114 return this.board
[i
][j
].charAt(1);
117 // Used by the interface:
118 getReservePpath(index
, color
) {
119 return color
+ V
.RESERVE_PIECES
[index
];
121 // // Version if some day I have pieces with numbers printed on it:
122 // getReservePpath(index, color) {
125 // color + V.RESERVE_PIECES[index] +
126 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
130 // Ordering on reserve pieces
131 static get RESERVE_PIECES() {
132 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
135 getReserveMoves([x
, y
]) {
136 const color
= this.turn
;
137 const p
= V
.RESERVE_PIECES
[y
];
138 if (this.reserve
[color
][p
] == 0) return [];
140 const pawnShift
= p
== V
.PAWN
? 1 : 0;
141 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
142 for (let j
= 0; j
< V
.size
.y
; j
++) {
143 if (this.board
[i
][j
] == V
.EMPTY
) {
154 start: { x: x
, y: y
}, //a bit artificial...
164 getPotentialMovesFrom([x
, y
]) {
166 // Reserves, outside of board: x == sizeX(+1)
167 return this.getReserveMoves([x
, y
]);
170 return super.getPotentialMovesFrom([x
, y
]);
174 let moves
= super.getAllValidMoves();
175 const color
= this.turn
;
176 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
177 moves
= moves
.concat(
178 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
180 return this.filterValid(moves
);
184 if (!super.atLeastOneMove()) {
185 // Search one reserve move
186 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
187 let moves
= this.filterValid(
188 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
190 if (moves
.length
> 0) return true;
197 updateVariables(move) {
198 super.updateVariables(move);
199 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
200 const color
= move.appear
[0].c
;
201 if (move.vanish
.length
== 0) {
202 this.reserve
[color
][move.appear
[0].p
]--;
205 move.movePromoted
= this.promoted
[move.start
.x
][move.start
.y
];
206 move.capturePromoted
= this.promoted
[move.end
.x
][move.end
.y
];
207 this.promoted
[move.start
.x
][move.start
.y
] = false;
208 this.promoted
[move.end
.x
][move.end
.y
] =
210 (move.vanish
[0].p
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
);
211 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]++;
212 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]++;
215 unupdateVariables(move) {
216 super.unupdateVariables(move);
217 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
218 const color
= this.turn
;
219 if (move.vanish
.length
== 0) {
220 this.reserve
[color
][move.appear
[0].p
]++;
223 if (move.movePromoted
) this.promoted
[move.start
.x
][move.start
.y
] = true;
224 this.promoted
[move.end
.x
][move.end
.y
] = move.capturePromoted
;
225 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]--;
226 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]--;
229 static get SEARCH_DEPTH() {
230 // High branching factor
235 let evaluation
= super.evalPosition();
237 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
238 const p
= V
.RESERVE_PIECES
[i
];
239 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
240 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
246 if (move.vanish
.length
> 0) return super.getNotation(move);
249 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
250 return piece
+ "@" + V
.CoordsToSquare(move.end
);