1 class WildebeestRules
extends ChessRules
5 return ([V
.CAMEL
,V
.WILDEBEEST
].includes(b
[1]) ? "Wildebeest/" : "") + b
;
8 static get size() { return {x:10,y:11}; }
10 static get CAMEL() { return 'c'; }
11 static get WILDEBEEST() { return 'w'; }
15 ChessRules
.steps
, //add camel moves:
16 {'c': [ [-3,-1],[-3,1],[-1,-3],[-1,3],[1,-3],[1,3],[3,-1],[3,1] ]}
20 // En-passant after 2-sq or 3-sq jumps
23 const [sx
,sy
,ex
] = [move.start
.x
,move.start
.y
,move.end
.x
];
24 if (this.getPiece(sx
,sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2)
26 const step
= (ex
-sx
) / Math
.abs(ex
-sx
);
31 if (sx
+ 2*step
!= ex
) //3-squares move
40 return undefined; //default
43 getPotentialMovesFrom([x
,y
])
45 switch (this.getPiece(x
,y
))
48 return this.getPotentialCamelMoves([x
,y
]);
50 return this.getPotentialWildebeestMoves([x
,y
]);
52 return super.getPotentialMovesFrom([x
,y
])
56 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
57 getPotentialPawnMoves([x
,y
])
59 const color
= this.turn
;
61 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
62 const shift
= (color
== "w" ? -1 : 1);
63 const startRanks
= (color
== "w" ? [sizeX
-2,sizeX
-3] : [1,2]);
64 const lastRank
= (color
== "w" ? 0 : sizeX
-1);
66 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRank
)
69 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
71 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
72 if (startRanks
.includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
75 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
76 if (x
== startRanks
[0] && this.board
[x
+3*shift
][y
] == V
.EMPTY
)
79 moves
.push(this.getBasicMove([x
,y
], [x
+3*shift
,y
]));
84 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
85 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
87 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1]));
89 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
90 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
92 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1]));
96 if (x
+shift
== lastRank
)
99 let promotionPieces
= [V
.QUEEN
,V
.WILDEBEEST
];
100 promotionPieces
.forEach(p
=> {
102 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
103 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:p
}));
105 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
106 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
108 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:p
}));
110 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
111 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
113 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:p
}));
119 const Lep
= this.epSquares
.length
;
120 const epSquare
= Lep
>0 ? this.epSquares
[Lep
-1] : undefined;
123 for (let epsq
of epSquare
)
125 // TODO: some redundant checks
126 if (epsq
.x
== x
+shift
&& Math
.abs(epsq
.y
- y
) == 1)
128 let epStep
= epsq
.y
- y
;
129 var enpassantMove
= this.getBasicMove([x
,y
], [x
+shift
,y
+epStep
]);
130 enpassantMove
.vanish
.push({
134 c: this.getColor(x
,y
+epStep
)
136 moves
.push(enpassantMove
);
144 // TODO: wildebeest castle
146 getPotentialCamelMoves(sq
)
148 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.CAMEL
], "oneStep");
151 getPotentialWildebeestMoves(sq
)
153 return this.getSlideNJumpMoves(
154 sq
, V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.CAMEL
]), "oneStep");
157 isAttacked(sq
, colors
)
159 return super.isAttacked(sq
, colors
)
160 || this.isAttackedByCamel(sq
, colors
)
161 || this.isAttackedByWildebeest(sq
, colors
);
164 isAttackedByCamel(sq
, colors
)
166 return this.isAttackedBySlideNJump(sq
, colors
,
167 V
.CAMEL
, V
.steps
[V
.CAMEL
], "oneStep");
170 isAttackedByWildebeest(sq
, colors
)
172 return this.isAttackedBySlideNJump(sq
, colors
, V
.WILDEBEEST
,
173 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.CAMEL
]), "oneStep");
178 // No valid move: game is lost (stalemate is a win)
179 return this.turn
== "w" ? "0-1" : "1-0";
182 static get VALUES() {
183 return Object
.assign(
185 {'c': 3, 'w': 7} //experimental
189 static get SEARCH_DEPTH() { return 2; }
191 static GenRandInitFen()
193 let pieces
= { "w": new Array(10), "b": new Array(10) };
194 for (let c
of ["w","b"])
196 let positions
= _
.range(11);
198 // Get random squares for bishops + camels (different colors)
199 let randIndexes
= _
.sample(_
.range(6), 2).map(i
=> { return 2*i
; });
200 let bishop1Pos
= positions
[randIndexes
[0]];
201 let camel1Pos
= positions
[randIndexes
[1]];
202 // The second bishop (camel) must be on a square of different color
203 let randIndexes_tmp
= _
.sample(_
.range(5), 2).map(i
=> { return 2*i
+1; });
204 let bishop2Pos
= positions
[randIndexes_tmp
[0]];
205 let camel2Pos
= positions
[randIndexes_tmp
[1]];
206 for (let idx
of randIndexes
.concat(randIndexes_tmp
)
207 .sort((a
,b
) => { return b
-a
; })) //largest indices first
209 positions
.splice(idx
, 1);
212 let randIndex
= _
.random(6);
213 let knight1Pos
= positions
[randIndex
];
214 positions
.splice(randIndex
, 1);
215 randIndex
= _
.random(5);
216 let knight2Pos
= positions
[randIndex
];
217 positions
.splice(randIndex
, 1);
219 randIndex
= _
.random(4);
220 let queenPos
= positions
[randIndex
];
221 positions
.splice(randIndex
, 1);
223 // Random square for wildebeest
224 randIndex
= _
.random(3);
225 let wildebeestPos
= positions
[randIndex
];
226 positions
.splice(randIndex
, 1);
228 let rook1Pos
= positions
[0];
229 let kingPos
= positions
[1];
230 let rook2Pos
= positions
[2];
232 pieces
[c
][rook1Pos
] = 'r';
233 pieces
[c
][knight1Pos
] = 'n';
234 pieces
[c
][bishop1Pos
] = 'b';
235 pieces
[c
][queenPos
] = 'q';
236 pieces
[c
][camel1Pos
] = 'c';
237 pieces
[c
][camel2Pos
] = 'c';
238 pieces
[c
][wildebeestPos
] = 'w';
239 pieces
[c
][kingPos
] = 'k';
240 pieces
[c
][bishop2Pos
] = 'b';
241 pieces
[c
][knight2Pos
] = 'n';
242 pieces
[c
][rook2Pos
] = 'r';
244 let fen
= pieces
["b"].join("") +
245 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
246 pieces
["w"].join("").toUpperCase() +