1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { ArrayFun
} from "@/utils/array";
4 export class CrazyhouseRules
extends ChessRules
{
6 static get PawnSpecs() {
10 // Change names to know that this goes back to pawn after capture:
11 { promotions: ['u', 'o', 'c', 't'] }
16 return ChessRules
.PIECES
.concat(['u', 'o', 'c', 't']);
20 const prefix
= (ChessRules
.PIECES
.includes(b
[1]) ? "" : "Crazyhouse/");
24 static IsGoodFen(fen
) {
25 if (!ChessRules
.IsGoodFen(fen
)) return false;
26 const fenParsed
= V
.ParseFen(fen
);
28 if (!fenParsed
.reserve
|| !fenParsed
.reserve
.match(/^[0-9]{10,10}$/))
33 static ParseFen(fen
) {
34 const fenParts
= fen
.split(" ");
36 ChessRules
.ParseFen(fen
),
37 { reserve: fenParts
[5] }
41 static GenRandInitFen(options
) {
42 return ChessRules
.GenRandInitFen(options
) + " 0000000000 -";
46 return super.getFen() + " " + this.getReserveFen();
50 return super.getFenForRepeat() + "_" + this.getReserveFen();
54 let counts
= new Array(10);
57 i
< V
.PIECES
.length
- 1;
58 i
++ //-1: no king reserve
60 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
61 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
63 return counts
.join("");
66 setOtherVariables(fen
) {
67 super.setOtherVariables(fen
);
68 const fenParsed
= V
.ParseFen(fen
);
69 // Also init reserves (used by the interface to show landable pieces)
70 const reserve
= fenParsed
.reserve
.split("").map(x
=> parseInt(x
, 10));
75 [V
.KNIGHT
]: reserve
[2],
76 [V
.BISHOP
]: reserve
[3],
82 [V
.KNIGHT
]: reserve
[7],
83 [V
.BISHOP
]: reserve
[8],
90 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
91 return this.board
[i
][j
].charAt(0);
94 // Pieces types after pawn promotion
95 static get PromotionMap() {
105 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
106 const p
= this.board
[i
][j
].charAt(1);
107 if (ChessRules
.PIECES
.includes(p
)) return p
;
109 return V
.PromotionMap
[p
];
112 // Used by the interface:
113 getReservePpath(index
, color
) {
114 return color
+ V
.RESERVE_PIECES
[index
];
116 // // Version if some day I have pieces with numbers printed on it:
117 // getReservePpath(index, color) {
120 // color + V.RESERVE_PIECES[index] +
121 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
125 // Ordering on reserve pieces
126 static get RESERVE_PIECES() {
127 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
130 getReserveMoves([x
, y
]) {
131 const color
= this.turn
;
132 const p
= V
.RESERVE_PIECES
[y
];
133 if (this.reserve
[color
][p
] == 0) return [];
135 const pawnShift
= p
== V
.PAWN
? 1 : 0;
136 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
137 for (let j
= 0; j
< V
.size
.y
; j
++) {
138 if (this.board
[i
][j
] == V
.EMPTY
) {
149 start: { x: x
, y: y
}, //a bit artificial...
159 getPotentialMovesFrom([x
, y
]) {
161 // Reserves, outside of board: x == sizeX(+1)
162 return this.getReserveMoves([x
, y
]);
164 return super.getPotentialMovesFrom([x
, y
]);
168 let moves
= super.getAllPotentialMoves();
169 const color
= this.turn
;
170 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
171 moves
= moves
.concat(
172 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
175 return this.filterValid(moves
);
179 if (super.atLeastOneMove()) return true;
180 // Search one reserve move
181 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
182 const moves
= this.filterValid(
183 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
185 if (moves
.length
> 0) return true;
191 super.postPlay(move);
193 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
194 const color
= move.appear
[0].c
;
195 if (move.vanish
.length
== 0) {
196 this.reserve
[color
][move.appear
[0].p
]--;
199 if (move.vanish
.length
== 2) {
200 if (V
.PawnSpecs
.promotions
.includes(move.vanish
[1].p
))
201 this.reserve
[color
][V
.PAWN
]++;
202 else this.reserve
[color
][move.vanish
[1].p
]++;
207 super.postUndo(move);
208 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
209 const color
= this.turn
;
210 if (move.vanish
.length
== 0) {
211 this.reserve
[color
][move.appear
[0].p
]++;
214 if (move.vanish
.length
== 2) {
215 if (V
.PawnSpecs
.promotions
.includes(move.vanish
[1].p
))
216 this.reserve
[color
][V
.PAWN
]--;
217 else this.reserve
[color
][move.vanish
[1].p
]--;
221 static get SEARCH_DEPTH() {
226 let evaluation
= super.evalPosition();
228 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
229 const p
= V
.RESERVE_PIECES
[i
];
230 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
231 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
237 if (move.vanish
.length
> 0) return super.getNotation(move);
239 let piece
= move.appear
[0].p
;
241 const endNotation
= "@" + V
.CoordsToSquare(move.end
);
242 if (piece
!= V
.PAWN
) {
243 if (ChessRules
.PIECES
.includes(piece
)) prefix
= piece
.toUpperCase();
244 else piece
= V
.PromotionMap
[piece
].toUpperCase();
246 return prefix
+ endNotation
;