03d7e418bb1b05404d0bb2eb10bb947990ddedbb
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 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() + " " + this.getReserveFen() + " " + this.getPromotedFen()
50 this.getBaseFen() + "_" +
51 this.getTurnFen() + "_" +
52 this.getFlagsFen() + "_" +
53 this.getEnpassantFen() + "_" +
54 this.getReserveFen() + "_" +
60 let counts
= new Array(10);
63 i
< V
.PIECES
.length
- 1;
64 i
++ //-1: no king reserve
66 counts
[i
] = this.reserve
["w"][V
.PIECES
[i
]];
67 counts
[5 + i
] = this.reserve
["b"][V
.PIECES
[i
]];
69 return counts
.join("");
74 for (let i
= 0; i
< V
.size
.x
; i
++) {
75 for (let j
= 0; j
< V
.size
.y
; j
++) {
76 if (this.promoted
[i
][j
]) res
+= V
.CoordsToSquare({ x: i
, y: j
}) + ",";
80 if (res
.length
> 0) res
= res
.slice(0, -1);
85 setOtherVariables(fen
) {
86 super.setOtherVariables(fen
);
87 const fenParsed
= V
.ParseFen(fen
);
88 // Also init reserves (used by the interface to show landable pieces)
91 [V
.PAWN
]: parseInt(fenParsed
.reserve
[0]),
92 [V
.ROOK
]: parseInt(fenParsed
.reserve
[1]),
93 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[2]),
94 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[3]),
95 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[4])
98 [V
.PAWN
]: parseInt(fenParsed
.reserve
[5]),
99 [V
.ROOK
]: parseInt(fenParsed
.reserve
[6]),
100 [V
.KNIGHT
]: parseInt(fenParsed
.reserve
[7]),
101 [V
.BISHOP
]: parseInt(fenParsed
.reserve
[8]),
102 [V
.QUEEN
]: parseInt(fenParsed
.reserve
[9])
105 this.promoted
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, false);
106 if (fenParsed
.promoted
!= "-") {
107 for (let square
of fenParsed
.promoted
.split(",")) {
108 const coords
= V
.SquareToCoords(square
);
109 this.promoted
[coords
.x
][coords
.y
] = true;
115 if (i
>= V
.size
.x
) return i
== V
.size
.x
? "w" : "b";
116 return this.board
[i
][j
].charAt(0);
120 if (i
>= V
.size
.x
) return V
.RESERVE_PIECES
[j
];
121 return this.board
[i
][j
].charAt(1);
124 // Used by the interface:
125 getReservePpath(index
, color
) {
126 return color
+ V
.RESERVE_PIECES
[index
];
128 // // Version if some day I have pieces with numbers printed on it:
129 // getReservePpath(index, color) {
132 // color + V.RESERVE_PIECES[index] +
133 // "_" + this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]
137 // Ordering on reserve pieces
138 static get RESERVE_PIECES() {
139 return [V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
142 getReserveMoves([x
, y
]) {
143 const color
= this.turn
;
144 const p
= V
.RESERVE_PIECES
[y
];
145 if (this.reserve
[color
][p
] == 0) return [];
147 const pawnShift
= p
== V
.PAWN
? 1 : 0;
148 for (let i
= pawnShift
; i
< V
.size
.x
- pawnShift
; i
++) {
149 for (let j
= 0; j
< V
.size
.y
; j
++) {
150 if (this.board
[i
][j
] == V
.EMPTY
) {
161 start: { x: x
, y: y
}, //a bit artificial...
171 getPotentialMovesFrom([x
, y
]) {
173 // Reserves, outside of board: x == sizeX(+1)
174 return this.getReserveMoves([x
, y
]);
177 return super.getPotentialMovesFrom([x
, y
]);
181 let moves
= super.getAllValidMoves();
182 const color
= this.turn
;
183 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++)
184 moves
= moves
.concat(
185 this.getReserveMoves([V
.size
.x
+ (color
== "w" ? 0 : 1), i
])
187 return this.filterValid(moves
);
191 if (!super.atLeastOneMove()) {
192 // Search one reserve move
193 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
194 let moves
= this.filterValid(
195 this.getReserveMoves([V
.size
.x
+ (this.turn
== "w" ? 0 : 1), i
])
197 if (moves
.length
> 0) return true;
204 updateVariables(move) {
205 super.updateVariables(move);
206 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return; //skip castle
207 const color
= move.appear
[0].c
;
208 if (move.vanish
.length
== 0) {
209 this.reserve
[color
][move.appear
[0].p
]--;
212 move.movePromoted
= this.promoted
[move.start
.x
][move.start
.y
];
213 move.capturePromoted
= this.promoted
[move.end
.x
][move.end
.y
];
214 this.promoted
[move.start
.x
][move.start
.y
] = false;
215 this.promoted
[move.end
.x
][move.end
.y
] =
217 (move.vanish
[0].p
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
);
218 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]++;
219 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]++;
222 unupdateVariables(move) {
223 super.unupdateVariables(move);
224 if (move.vanish
.length
== 2 && move.appear
.length
== 2) return;
225 const color
= this.turn
;
226 if (move.vanish
.length
== 0) {
227 this.reserve
[color
][move.appear
[0].p
]++;
230 if (move.movePromoted
) this.promoted
[move.start
.x
][move.start
.y
] = true;
231 this.promoted
[move.end
.x
][move.end
.y
] = move.capturePromoted
;
232 if (move.capturePromoted
) this.reserve
[color
][V
.PAWN
]--;
233 else if (move.vanish
.length
== 2) this.reserve
[color
][move.vanish
[1].p
]--;
236 static get SEARCH_DEPTH() {
241 let evaluation
= super.evalPosition();
243 for (let i
= 0; i
< V
.RESERVE_PIECES
.length
; i
++) {
244 const p
= V
.RESERVE_PIECES
[i
];
245 evaluation
+= this.reserve
["w"][p
] * V
.VALUES
[p
];
246 evaluation
-= this.reserve
["b"][p
] * V
.VALUES
[p
];
252 if (move.vanish
.length
> 0) return super.getNotation(move);
255 move.appear
[0].p
!= V
.PAWN
? move.appear
[0].p
.toUpperCase() : "";
256 return piece
+ "@" + V
.CoordsToSquare(move.end
);