-class DarkRules extends ChessRules
+import { ChessRules } from "@/base_rules";
+import { ArrayFun} from "@/utils/array";
+import { randInt } from "@/utils/alea";
+
+export const VariantRules = class DarkRules extends ChessRules
{
// Standard rules, in the shadow
setOtherVariables(fen)
super.setOtherVariables(fen);
const [sizeX,sizeY] = [V.size.x,V.size.y];
this.enlightened = {
- "w": doubleArray(sizeX,sizeY),
- "b": doubleArray(sizeX,sizeY)
+ "w": ArrayFun.init(sizeX,sizeY),
+ "b": ArrayFun.init(sizeX,sizeY)
};
// Setup enlightened: squares reachable by each side
// (TODO: one side would be enough ?)
super.updateVariables(move);
if (move.vanish.length >= 2 && move.vanish[1].p == V.KING)
{
- // We took opponent king !
+ // We took opponent king ! (because if castle vanish[1] is a rook)
this.kingPos[this.turn] = [-1,-1];
}
- // Update moves for both colors:
+ // Update lights for both colors:
this.updateEnlightened();
}
}
}
- // Update moves for both colors:
+ // Update lights for both colors:
this.updateEnlightened();
}
- checkGameEnd()
- {
- // No valid move: our king disappeared
- return this.turn == "w" ? "0-1" : "1-0";
+ getCurrentScore()
+ {
+ const color = this.turn;
+ const kp = this.kingPos[color];
+ if (kp[0] < 0) //king disappeared
+ return (color == "w" ? "0-1" : "1-0");
+ if (this.atLeastOneMove()) // game not over
+ return "*";
+ return "1/2"; //no moves but kings still there (seems impossible)
}
static get THRESHOLD_MATE()
const color = this.turn;
const oppCol = V.GetOppCol(color);
const pawnShift = (color == "w" ? -1 : 1);
- const kp = this.kingPos[color];
// Do not cheat: the current enlightment is all we can see
const myLight = JSON.parse(JSON.stringify(this.enlightened[color]));
// Can a slider on (i,j) apparently take my king?
// NOTE: inaccurate because assume yes if some squares are shadowed
const sliderTake = ([i,j], piece) => {
+ const kp = this.kingPos[color];
let step = undefined;
if (piece == V.BISHOP)
{
// Check for obstacles
let obstacle = false;
for (
- let x=kp[0]+step[0], y=kp[1]+step[1];
- x != i && y != j;
- x += step[0], y+= step[1])
- {
+ let x=kp[0]+step[0], y=kp[1]+step[1];
+ x != i && y != j;
+ x += step[0], y += step[1])
+ {
if (myLight[x][y] && this.board[x][y] != V.EMPTY)
{
obstacle = true;
// Do I see something which can take my king ?
const kingThreats = () => {
+ const kp = this.kingPos[color];
for (let i=0; i<V.size.x; i++)
{
for (let j=0; j<V.size.y; j++)
move.eval = -maxeval;
}
this.undo(move);
- if (!!move.eval)
+
+ if (!!move.eval)
continue;
move.eval = 0; //a priori...
let candidates = [0];
for (let j=1; j<moves.length && moves[j].eval == moves[0].eval; j++)
candidates.push(j);
- return moves[sample(candidates)];
+ return moves[candidates[randInt(candidates.length)]];
}
}