9558fe4bf1b5048b251964eaea34c3aa07c03ed5
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 // May be a continuation: adjust numbers of pieces according to captures + rebirths
28 this.moves
.forEach(m
=> {
29 if (m
.vanish
.length
== 2)
30 this.reserve
[m
.appear
[0].c
][m
.vanish
[1].p
]++;
31 else if (m
.vanish
.length
== 0)
32 this.reserve
[m
.appear
[0].c
][m
.appear
[0].p
]--;
34 // TODO: keep track of promoted pawns ==> give a pawn if captured.
39 const sizeX
= VariantRules
.size
[0];
41 return (i
==sizeX
? "w" : "b");
42 return this.board
[i
][j
].charAt(0);
46 const sizeX
= VariantRules
.size
[0];
48 return VariantRules
.RESERVE_PIECES
[j
];
49 return this.board
[i
][j
].charAt(1);
52 // Used by the interface:
53 getReservePpath(color
, index
)
55 return color
+ VariantRules
.RESERVE_PIECES
[index
];
58 // Put an ordering on reserve pieces
59 static get RESERVE_PIECES() {
60 const V
= VariantRules
;
61 return [V
.PAWN
,V
.ROOK
,V
.KNIGHT
,V
.BISHOP
,V
.QUEEN
];
64 getReserveMoves([x
,y
])
66 const color
= this.turn
;
67 const p
= VariantRules
.RESERVE_PIECES
[y
];
68 if (this.reserve
[color
][p
] == 0)
71 const [sizeX
,sizeY
] = VariantRules
.size
;
72 const pawnShift
= (p
==VariantRules
.PAWN
? 1 : 0);
73 for (let i
=pawnShift
; i
<sizeX
-pawnShift
; i
++)
75 for (let j
=0; j
<sizeY
; j
++)
77 if (this.board
[i
][j
] == VariantRules
.EMPTY
)
89 start: {x:sizeX
, y:y
}, //a bit artificial...
99 getPotentialMovesFrom([x
,y
])
101 const sizeX
= VariantRules
.size
[0];
103 return super.getPotentialMovesFrom([x
,y
]);
104 // Reserves, outside of board: x == sizeX
105 return this.getReserveMoves([x
,y
]);
110 let moves
= super.getAllValidMoves();
111 const color
= this.turn
;
112 const sizeX
= VariantRules
.size
[0];
113 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
114 moves
= moves
.concat(this.getReserveMoves([sizeX
,i
]));
115 return this.filterValid(moves
);
120 if (!super.atLeastOneMove())
122 const sizeX
= VariantRules
.size
[0];
123 // Scan for reserve moves
124 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
126 let moves
= this.filterValid(this.getReserveMoves([sizeX
,i
]));
127 if (moves
.length
> 0)
135 updateVariables(move)
137 super.updateVariables(move);
138 const color
= this.turn
;
139 if (move.vanish
.length
==2)
140 this.reserve
[color
][move.vanish
[1].p
]++;
141 if (move.vanish
.length
==0)
142 this.reserve
[color
][move.appear
[0].p
]--;
145 unupdateVariables(move)
147 super.unupdateVariables(move);
148 const color
= this.turn
;
149 if (move.vanish
.length
==2)
150 this.reserve
[color
][move.vanish
[1].p
]--;
151 if (move.vanish
.length
==0)
152 this.reserve
[color
][move.appear
[0].p
]++;
155 static get SEARCH_DEPTH() { return 2; } //high branching factor
159 if (move.vanish
.length
> 0)
160 return super.getNotation(move);
163 (move.appear
[0].p
!= VariantRules
.PAWN
? move.appear
[0].p
.toUpperCase() : "");
165 String
.fromCharCode(97 + move.end
.y
) + (VariantRules
.size
[0]-move.end
.x
);
166 return piece
+ "@" + finalSquare
;