321bf4cf6d465dc1f1c9c792f5fb3361b83ec044
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(" ");
25 return Object
.assign(ChessRules
.ParseFen(fen
), {
31 getEpSquare(moveOrSquare
) {
32 if (typeof moveOrSquare
!== "object" || moveOrSquare
.vanish
.length
> 0)
33 return super.getEpSquare(moveOrSquare
);
34 // Landing move: no en-passant
38 static GenRandInitFen(randomness
) {
39 return ChessRules
.GenRandInitFen(randomness
) + " 0000000000 -";
44 super.getFen() + " " +
45 this.getReserveFen() + " " +
52 super.getFenForRepeat() + "_" +
53 this.getReserveFen() + "_" +
59 let counts
= new Array(10);
62 i
< V
.PIECES
.length
- 1;
63 i
++ //-1: no king reserve
65 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
66 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
68 return counts
.join("");
73 for (let i
= 0; i
< V
.size
.x
; i
++) {
74 for (let j
= 0; j
< V
.size
.y
; j
++) {
75 if (this.promoted
[i
][j
]) res
+= V
.CoordsToSquare({ x: i
, y: j
}) + ",";
79 if (res
.length
> 0) res
= res
.slice(0, -1);
84 setOtherVariables(fen
) {
85 super.setOtherVariables(fen
);
86 const fenParsed
= V
.ParseFen(fen
);
87 // Also init reserves (used by the interface to show landable pieces)
90 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
91 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
92 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
93 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
94 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
97 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
98 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
99 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
100 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
101 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
104 this.promoted
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
105 if (fenParsed
.promoted
!= "-") {
106 for (let square
of fenParsed
.promoted
.split(",")) {
107 const coords
= V
.SquareToCoords(square
);
108 this.promoted
[coords
.x
][coords
.y
] = true;
114 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
115 return this.board
[i
][j
].charAt(0);
119 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
120 return this.board
[i
][j
].charAt(1);
123 // Used by the interface:
124 getReservePpath(index
, color
) {
125 return color
+ V
.RESERVE_PIECES
[index
];
127 // // Version if some day I have pieces with numbers printed on it:
128 // getReservePpath(index, color) {
131 // color + V.RESERVE_PIECES[index] +
132 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
136 // Ordering on reserve pieces
137 static get RESERVE_PIECES() {
138 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
141 getReserveMoves([x
, y
]) {
142 const color
= this.turn
;
143 const p
= V
.RESERVE_PIECES
[y
];
144 if (this.reserve
[color
][p
] == 0) return [];
146 const pawnShift
= p
== V
.PAWN
? 1 : 0;
147 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
148 for (let j
= 0; j
< V
.size
.y
; j
++) {
149 if (this.board
[i
][j
] == V
.EMPTY
) {
160 start: { x: x
, y: y
}, //a bit artificial...
170 getPotentialMovesFrom([x
, y
]) {
172 // Reserves, outside of board: x == sizeX(+1)
173 return this.getReserveMoves([x
, y
]);
176 return super.getPotentialMovesFrom([x
, y
]);
180 let moves
= super.getAllValidMoves();
181 const color
= this.turn
;
182 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
183 moves
= moves
.concat(
184 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
186 return this.filterValid(moves
);
190 if (!super.atLeastOneMove()) {
191 // Search one reserve move
192 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
193 let moves
= this.filterValid(
194 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
196 if (moves
.length
> 0) return true;
204 super.postPlay(move);
205 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
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
);