783eb440dfdd17d44e812035dbeeb3f84280bc9c
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 return ChessRules
.PIECES
.concat([V
.CAMEL
,V
.WILDEBEEST
]);
21 ChessRules
.steps
, //add camel moves:
22 {'c': [ [-3,-1],[-3,1],[-1,-3],[-1,3],[1,-3],[1,3],[3,-1],[3,1] ]}
26 // There may be 2 enPassant squares (if pawn jump 3 squares)
29 const L
= this.epSquares
.length
;
30 if (!this.epSquares
[L
-1])
31 return "-"; //no en-passant
33 this.epSquares
[L
-1].forEach(sq
=> {
34 res
+= V
.CoordsToSquare(sq
) + ",";
36 return res
.slice(0,-1); //remove last comma
39 // En-passant after 2-sq or 3-sq jumps
40 getEpSquare(moveOrSquare
)
44 if (typeof moveOrSquare
=== "string")
46 const square
= moveOrSquare
;
50 square
.split(",").forEach(sq
=> {
51 res
.push(V
.SquareToCoords(sq
));
55 // Argument is a move:
56 const move = moveOrSquare
;
57 const [sx
,sy
,ex
] = [move.start
.x
,move.start
.y
,move.end
.x
];
58 if (this.getPiece(sx
,sy
) == V
.PAWN
&& Math
.abs(sx
- ex
) >= 2)
60 const step
= (ex
-sx
) / Math
.abs(ex
-sx
);
65 if (sx
+ 2*step
!= ex
) //3-squares move
74 return undefined; //default
77 getPotentialMovesFrom([x
,y
])
79 switch (this.getPiece(x
,y
))
82 return this.getPotentialCamelMoves([x
,y
]);
84 return this.getPotentialWildebeestMoves([x
,y
]);
86 return super.getPotentialMovesFrom([x
,y
])
90 // Pawns jump 2 or 3 squares, and promote to queen or wildebeest
91 getPotentialPawnMoves([x
,y
])
93 const color
= this.turn
;
95 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
96 const shift
= (color
== "w" ? -1 : 1);
97 const startRanks
= (color
== "w" ? [sizeX
-2,sizeX
-3] : [1,2]);
98 const lastRank
= (color
== "w" ? 0 : sizeX
-1);
100 if (x
+shift
>= 0 && x
+shift
< sizeX
&& x
+shift
!= lastRank
)
103 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
105 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
]));
106 if (startRanks
.includes(x
) && this.board
[x
+2*shift
][y
] == V
.EMPTY
)
109 moves
.push(this.getBasicMove([x
,y
], [x
+2*shift
,y
]));
110 if (x
== startRanks
[0] && this.board
[x
+3*shift
][y
] == V
.EMPTY
)
113 moves
.push(this.getBasicMove([x
,y
], [x
+3*shift
,y
]));
118 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
119 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
121 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1]));
123 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
124 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
126 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1]));
130 if (x
+shift
== lastRank
)
133 let promotionPieces
= [V
.QUEEN
,V
.WILDEBEEST
];
134 promotionPieces
.forEach(p
=> {
136 if (this.board
[x
+shift
][y
] == V
.EMPTY
)
137 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
], {c:color
,p:p
}));
139 if (y
>0 && this.canTake([x
,y
], [x
+shift
,y
-1])
140 && this.board
[x
+shift
][y
-1] != V
.EMPTY
)
142 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
-1], {c:color
,p:p
}));
144 if (y
<sizeY
-1 && this.canTake([x
,y
], [x
+shift
,y
+1])
145 && this.board
[x
+shift
][y
+1] != V
.EMPTY
)
147 moves
.push(this.getBasicMove([x
,y
], [x
+shift
,y
+1], {c:color
,p:p
}));
153 const Lep
= this.epSquares
.length
;
154 const epSquare
= Lep
>0 ? this.epSquares
[Lep
-1] : undefined;
157 for (let epsq
of epSquare
)
159 // TODO: some redundant checks
160 if (epsq
.x
== x
+shift
&& Math
.abs(epsq
.y
- y
) == 1)
162 let epStep
= epsq
.y
- y
;
163 var enpassantMove
= this.getBasicMove([x
,y
], [x
+shift
,y
+epStep
]);
164 enpassantMove
.vanish
.push({
168 c: this.getColor(x
,y
+epStep
)
170 moves
.push(enpassantMove
);
178 // TODO: wildebeest castle
180 getPotentialCamelMoves(sq
)
182 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.CAMEL
], "oneStep");
185 getPotentialWildebeestMoves(sq
)
187 return this.getSlideNJumpMoves(
188 sq
, V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.CAMEL
]), "oneStep");
191 isAttacked(sq
, colors
)
193 return super.isAttacked(sq
, colors
)
194 || this.isAttackedByCamel(sq
, colors
)
195 || this.isAttackedByWildebeest(sq
, colors
);
198 isAttackedByCamel(sq
, colors
)
200 return this.isAttackedBySlideNJump(sq
, colors
,
201 V
.CAMEL
, V
.steps
[V
.CAMEL
], "oneStep");
204 isAttackedByWildebeest(sq
, colors
)
206 return this.isAttackedBySlideNJump(sq
, colors
, V
.WILDEBEEST
,
207 V
.steps
[V
.KNIGHT
].concat(V
.steps
[V
.CAMEL
]), "oneStep");
212 // No valid move: game is lost (stalemate is a win)
213 return this.turn
== "w" ? "0-1" : "1-0";
216 static get VALUES() {
217 return Object
.assign(
219 {'c': 3, 'w': 7} //experimental
223 static get SEARCH_DEPTH() { return 2; }
225 static GenRandInitFen()
227 let pieces
= { "w": new Array(10), "b": new Array(10) };
228 for (let c
of ["w","b"])
230 let positions
= _
.range(11);
232 // Get random squares for bishops + camels (different colors)
233 let randIndexes
= _
.sample(_
.range(6), 2).map(i
=> { return 2*i
; });
234 let bishop1Pos
= positions
[randIndexes
[0]];
235 let camel1Pos
= positions
[randIndexes
[1]];
236 // The second bishop (camel) must be on a square of different color
237 let randIndexes_tmp
= _
.sample(_
.range(5), 2).map(i
=> { return 2*i
+1; });
238 let bishop2Pos
= positions
[randIndexes_tmp
[0]];
239 let camel2Pos
= positions
[randIndexes_tmp
[1]];
240 for (let idx
of randIndexes
.concat(randIndexes_tmp
)
241 .sort((a
,b
) => { return b
-a
; })) //largest indices first
243 positions
.splice(idx
, 1);
246 let randIndex
= _
.random(6);
247 let knight1Pos
= positions
[randIndex
];
248 positions
.splice(randIndex
, 1);
249 randIndex
= _
.random(5);
250 let knight2Pos
= positions
[randIndex
];
251 positions
.splice(randIndex
, 1);
253 randIndex
= _
.random(4);
254 let queenPos
= positions
[randIndex
];
255 positions
.splice(randIndex
, 1);
257 // Random square for wildebeest
258 randIndex
= _
.random(3);
259 let wildebeestPos
= positions
[randIndex
];
260 positions
.splice(randIndex
, 1);
262 let rook1Pos
= positions
[0];
263 let kingPos
= positions
[1];
264 let rook2Pos
= positions
[2];
266 pieces
[c
][rook1Pos
] = 'r';
267 pieces
[c
][knight1Pos
] = 'n';
268 pieces
[c
][bishop1Pos
] = 'b';
269 pieces
[c
][queenPos
] = 'q';
270 pieces
[c
][camel1Pos
] = 'c';
271 pieces
[c
][camel2Pos
] = 'c';
272 pieces
[c
][wildebeestPos
] = 'w';
273 pieces
[c
][kingPos
] = 'k';
274 pieces
[c
][bishop2Pos
] = 'b';
275 pieces
[c
][knight2Pos
] = 'n';
276 pieces
[c
][rook2Pos
] = 'r';
278 return pieces
["b"].join("") +
279 "/ppppppppppp/11/11/11/11/11/11/PPPPPPPPPPP/" +
280 pieces
["w"].join("").toUpperCase() +
285 const VariantRules
= WildebeestRules
;