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 // Also init reserves (used by the interface to show landable pieces)
85 V
.ParseFen(fen
).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
]);
174 return super.getPotentialMovesFrom([x
, y
]);
178 let moves
= super.getAllPotentialMoves();
179 const color
= this.turn
;
180 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
181 moves
= moves
.concat(
182 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
185 return this.filterValid(moves
);
189 if (!super.atLeastOneMove()) {
190 // Search one reserve move
191 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
192 let moves
= this.filterValid(
193 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
195 if (moves
.length
> 0) return true;
203 super.postPlay(move);
205 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
206 const color
= move.appear
[0].c
;
207 if (move.vanish
.length
== 0) {
208 this.reserve
[color
][move.appear
[0].p
]--;
211 move.movePromoted
= this.promoted
[move.start
.x
][move.start
.y
];
212 move.capturePromoted
= this.promoted
[move.end
.x
][move.end
.y
];
213 this.promoted
[move.start
.x
][move.start
.y
] = false;
214 this.promoted
[move.end
.x
][move.end
.y
] =
216 (move.vanish
[0].p
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
);
217 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]++;
218 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]++;
222 super.postUndo(move);
223 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
224 const color
= this.turn
;
225 if (move.vanish
.length
== 0) {
226 this.reserve
[color
][move.appear
[0].p
]++;
229 if (move.movePromoted
) this.promoted
[move.start
.x
][move.start
.y
] = true;
230 this.promoted
[move.end
.x
][move.end
.y
] = move.capturePromoted
;
231 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]--;
232 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]--;
235 static get SEARCH_DEPTH() {
240 let evaluation
= super.evalPosition();
242 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
243 const p
= V
.RESERVE_PIECES
[i
];
244 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
245 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
251 if (move.vanish
.length
> 0) return super.getNotation(move);
254 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
255 return piece
+ "@" + V
.CoordsToSquare(move.end
);