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 getEpSquare(moveOrSquare
) {
35 if (typeof moveOrSquare
!== "object" || moveOrSquare
.vanish
.length
> 0)
36 return super.getEpSquare(moveOrSquare
);
37 // Landing move: no en-passant
41 static GenRandInitFen(randomness
) {
42 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000 -";
47 super.getFen() + " " +
48 this.getReserveFen() + " " +
55 super.getFenForRepeat() + "_" +
56 this.getReserveFen() + "_" +
62 let counts
= new Array(10);
65 i
< V
.PIECES
.length
- 1;
66 i
++ //-1: no king reserve
68 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
69 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
71 return counts
.join("");
76 for (let i
= 0; i
< V
.size
.x
; i
++) {
77 for (let j
= 0; j
< V
.size
.y
; j
++) {
78 if (this.promoted
[i
][j
]) res
+= V
.CoordsToSquare({ x: i
, y: j
}) + ",";
82 if (res
.length
> 0) res
= res
.slice(0, -1);
87 setOtherVariables(fen
) {
88 super.setOtherVariables(fen
);
89 const fenParsed
= V
.ParseFen(fen
);
90 // Also init reserves (used by the interface to show landable pieces)
93 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
94 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
95 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
96 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
97 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
100 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
101 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
102 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
103 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
104 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
107 this.promoted
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
108 if (fenParsed
.promoted
!= "-") {
109 for (let square
of fenParsed
.promoted
.split(",")) {
110 const coords
= V
.SquareToCoords(square
);
111 this.promoted
[coords
.x
][coords
.y
] = true;
117 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
118 return this.board
[i
][j
].charAt(0);
122 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
123 return this.board
[i
][j
].charAt(1);
126 // Used by the interface:
127 getReservePpath(index
, color
) {
128 return color
+ V
.RESERVE_PIECES
[index
];
130 // // Version if some day I have pieces with numbers printed on it:
131 // getReservePpath(index, color) {
134 // color + V.RESERVE_PIECES[index] +
135 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
139 // Ordering on reserve pieces
140 static get RESERVE_PIECES() {
141 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
144 getReserveMoves([x
, y
]) {
145 const color
= this.turn
;
146 const p
= V
.RESERVE_PIECES
[y
];
147 if (this.reserve
[color
][p
] == 0) return [];
149 const pawnShift
= p
== V
.PAWN
? 1 : 0;
150 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
151 for (let j
= 0; j
< V
.size
.y
; j
++) {
152 if (this.board
[i
][j
] == V
.EMPTY
) {
163 start: { x: x
, y: y
}, //a bit artificial...
173 getPotentialMovesFrom([x
, y
]) {
175 // Reserves, outside of board: x == sizeX(+1)
176 return this.getReserveMoves([x
, y
]);
179 return super.getPotentialMovesFrom([x
, y
]);
183 let moves
= super.getAllValidMoves();
184 const color
= this.turn
;
185 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
186 moves
= moves
.concat(
187 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
189 return this.filterValid(moves
);
193 if (!super.atLeastOneMove()) {
194 // Search one reserve move
195 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
196 let moves
= this.filterValid(
197 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
199 if (moves
.length
> 0) return true;
207 super.postPlay(move);
208 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
209 const color
= move.appear
[0].c
;
210 if (move.vanish
.length
== 0) {
211 this.reserve
[color
][move.appear
[0].p
]--;
214 move.movePromoted
= this.promoted
[move.start
.x
][move.start
.y
];
215 move.capturePromoted
= this.promoted
[move.end
.x
][move.end
.y
];
216 this.promoted
[move.start
.x
][move.start
.y
] = false;
217 this.promoted
[move.end
.x
][move.end
.y
] =
219 (move.vanish
[0].p
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
);
220 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]++;
221 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]++;
225 super.postUndo(move);
226 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
227 const color
= this.turn
;
228 if (move.vanish
.length
== 0) {
229 this.reserve
[color
][move.appear
[0].p
]++;
232 if (move.movePromoted
) this.promoted
[move.start
.x
][move.start
.y
] = true;
233 this.promoted
[move.end
.x
][move.end
.y
] = move.capturePromoted
;
234 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]--;
235 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]--;
238 static get SEARCH_DEPTH() {
243 let evaluation
= super.evalPosition();
245 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
246 const p
= V
.RESERVE_PIECES
[i
];
247 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
248 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
254 if (move.vanish
.length
> 0) return super.getNotation(move);
257 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
258 return piece
+ "@" + V
.CoordsToSquare(move.end
);