1 class CrazyhouseRules
extends ChessRules
5 super.initVariables(fen
);
6 // Also init reserves (used by the interface to show landing pieces)
7 const V
= VariantRules
;
27 const [sizeX
,sizeY
] = VariantRules
.size
;
28 this.promoted
= doubleArray(sizeX
, sizeY
, false);
29 // May be a continuation: adjust numbers of pieces in reserve + promoted pieces
30 this.moves
.forEach(m
=> { this.updateVariables(m
); });
35 const sizeX
= VariantRules
.size
[0];
37 return (i
==sizeX
? "w" : "b");
38 return this.board
[i
][j
].charAt(0);
42 const sizeX
= VariantRules
.size
[0];
44 return VariantRules
.RESERVE_PIECES
[j
];
45 return this.board
[i
][j
].charAt(1);
48 // Used by the interface:
49 getReservePpath(color
, index
)
51 return color
+ VariantRules
.RESERVE_PIECES
[index
];
54 // Put an ordering on reserve pieces
55 static get RESERVE_PIECES() {
56 const V
= VariantRules
;
57 return [V
.PAWN
,V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
60 getReserveMoves([x
,y
])
62 const color
= this.turn
;
63 const p
= VariantRules
.RESERVE_PIECES
[y
];
64 if (this.reserve
[color
][p
] == 0)
67 const [sizeX
,sizeY
] = VariantRules
.size
;
68 const pawnShift
= (p
==VariantRules
.PAWN
? 1 : 0);
69 for (let i
=pawnShift
; i
<sizeX
-pawnShift
; i
++)
71 for (let j
=0; j
<sizeY
; j
++)
73 if (this.board
[i
][j
] == VariantRules
.EMPTY
)
85 start: {x:x
, y:y
}, //a bit artificial...
95 getPotentialMovesFrom([x
,y
])
97 const sizeX
= VariantRules
.size
[0];
100 // Reserves, outside of board: x == sizeX
101 return this.getReserveMoves([x
,y
]);
104 return super.getPotentialMovesFrom([x
,y
]);
109 let moves
= super.getAllValidMoves();
110 const color
= this.turn
;
111 const sizeX
= VariantRules
.size
[0];
112 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
113 moves
= moves
.concat(this.getReserveMoves([sizeX
+(color
=="w"?0:1),i
]));
114 return this.filterValid(moves
);
119 if (!super.atLeastOneMove())
121 const sizeX
= VariantRules
.size
[0];
122 // Scan for reserve moves
123 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
125 let moves
= this.filterValid(this.getReserveMoves([sizeX
,i
]));
126 if (moves
.length
> 0)
134 updateVariables(move)
136 super.updateVariables(move);
137 if (move.vanish
.length
== 2 && move.appear
.length
== 2)
138 return; //skip castle
139 const color
= this.turn
;
140 const V
= VariantRules
;
141 if (move.vanish
.length
== 0)
143 this.reserve
[color
][move.appear
[0].p
]--;
146 move.movePromoted
= this.promoted
[move.start
.x
][move.start
.y
];
147 move.capturePromoted
= this.promoted
[move.end
.x
][move.end
.y
]
148 this.promoted
[move.start
.x
][move.start
.y
] = false;
149 this.promoted
[move.end
.x
][move.end
.y
] = move.movePromoted
150 || (move.vanish
[0].p
== V
.PAWN
&& move.appear
[0].p
!= V
.PAWN
);
151 if (move.capturePromoted
)
152 this.reserve
[color
][VariantRules
.PAWN
]++;
153 else if (move.vanish
.length
== 2)
154 this.reserve
[color
][move.vanish
[1].p
]++;
157 unupdateVariables(move)
159 super.unupdateVariables(move);
160 if (move.vanish
.length
== 2 && move.appear
.length
== 2)
162 const color
= this.turn
;
163 const V
= VariantRules
;
164 if (move.vanish
.length
== 0)
166 this.reserve
[color
][move.appear
[0].p
]++;
169 if (move.movePromoted
)
170 this.promoted
[move.start
.x
][move.start
.y
] = true;
171 this.promoted
[move.end
.x
][move.end
.y
] = move.capturePromoted
;
172 if (move.capturePromoted
)
173 this.reserve
[color
][VariantRules
.PAWN
]--;
174 else if (move.vanish
.length
== 2)
175 this.reserve
[color
][move.vanish
[1].p
]--;
178 static get SEARCH_DEPTH() { return 2; } //high branching factor
182 let evaluation
= super.evalPosition();
184 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
186 const p
= VariantRules
.RESERVE_PIECES
[i
];
187 evaluation
+= this.reserve
["w"][p
] * VariantRules
.VALUES
[p
];
188 evaluation
-= this.reserve
["b"][p
] * VariantRules
.VALUES
[p
];
195 if (move.vanish
.length
> 0)
196 return super.getNotation(move);
199 (move.appear
[0].p
!= VariantRules
.PAWN
? move.appear
[0].p
.toUpperCase() : "");
201 String
.fromCharCode(97 + move.end
.y
) + (VariantRules
.size
[0]-move.end
.x
);
202 return piece
+ "@" + finalSquare
;
205 getLongNotation(move)
207 if (move.vanish
.length
> 0)
208 return super.getLongNotation(move);
210 String
.fromCharCode(97 + move.end
.y
) + (VariantRules
.size
[0]-move.end
.x
);
211 return "@" + finalSquare
;