+ this.turn = V.GetOppCol(this.turn);
+ this.movesCount++;
+ }
+ this.postPlay(move);
+ }
+
+ postPlay(move) {
+ if (move.appear.length == 0 && move.vanish.length == 0) {
+ this.stage = 2;
+ this.sideCheckered = this.turn;
+ }
+ else {
+ const c = move.vanish[0].c;
+ const piece = move.vanish[0].p;
+ if (piece == V.KING) {
+ this.kingPos[c][0] = move.appear[0].x;
+ this.kingPos[c][1] = move.appear[0].y;
+ }
+ super.updateCastleFlags(move, piece);
+ if (
+ [1, 6].includes(move.start.x) &&
+ move.vanish[0].p == V.PAWN &&
+ Math.abs(move.end.x - move.start.x) == 2
+ ) {
+ // This move turns off a 2-squares pawn flag
+ this.pawnFlags[move.start.x == 6 ? "w" : "b"][move.start.y] = false;
+ }
+ }
+ this.cmoves.push(this.getCmove(move));
+ }
+
+ undo(move) {
+ this.epSquares.pop();
+ this.disaggregateFlags(JSON.parse(move.flags));
+ V.UndoOnBoard(this.board, move);
+ if (move.appear.length > 0 || move.vanish.length > 0)
+ {
+ this.turn = V.GetOppCol(this.turn);
+ this.movesCount--;
+ }
+ this.postUndo(move);
+ }
+
+ postUndo(move) {
+ if (move.appear.length == 0 && move.vanish.length == 0) this.stage = 1;
+ else super.postUndo(move);
+ this.cmoves.pop();
+ }
+
+ getCurrentScore() {
+ const color = this.turn;
+ if (this.stage == 1) {
+ if (this.atLeastOneMove()) return "*";
+ // Artifically change turn, for checkered pawns
+ this.turn = V.GetOppCol(this.turn);
+ const res =
+ this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"])
+ ? color == "w"
+ ? "0-1"
+ : "1-0"
+ : "1/2";
+ this.turn = V.GetOppCol(this.turn);
+ return res;
+ }
+ // Stage == 2:
+ if (this.sideCheckered == this.turn) {
+ // Check if remaining checkered pieces: if none, I lost
+ if (this.board.some(b => b.some(cell => cell[0] == 'c'))) {
+ if (!this.atLeastOneMove()) return "1/2";
+ return "*";
+ }
+ return color == 'w' ? "0-1" : "1-0";
+ }
+ if (this.atLeastOneMove()) return "*";
+ let res = this.isAttacked(this.kingPos['w'], ["c"]);
+ if (!res) res = this.isAttacked(this.kingPos['b'], ["c"]);
+ if (res) return color == 'w' ? "0-1" : "1-0";
+ return "1/2";
+ }
+
+ evalPosition() {
+ let evaluation = 0;
+ // Just count material for now, considering checkered neutral at stage 1.
+ const baseSign = (this.turn == 'w' ? 1 : -1);
+ for (let i = 0; i < V.size.x; i++) {
+ for (let j = 0; j < V.size.y; j++) {
+ if (this.board[i][j] != V.EMPTY) {
+ const sqColor = this.getColor(i, j);
+ if (this.stage == 1) {
+ if (["w", "b"].includes(sqColor)) {
+ const sign = sqColor == "w" ? 1 : -1;
+ evaluation += sign * V.VALUES[this.getPiece(i, j)];
+ }
+ }
+ else {
+ const sign =
+ this.sideCheckered == this.turn
+ ? (sqColor == 'c' ? 1 : -1) * baseSign
+ : (sqColor == 'c' ? -1 : 1) * baseSign;
+ evaluation += sign * V.VALUES[this.getPiece(i, j)];
+ }