+ getCurrentScore() {
+ const color = this.turn;
+ const getScoreLost = () => {
+ // Result if I lose:
+ return color == "w" ? "0-1" : "1-0";
+ };
+ if (!this.atLeastOneMove())
+ // No valid move: I lose (this includes checkmate)
+ return getScoreLost();
+ // Win if the opponent has no pieces left (except king),
+ // and cannot bare king on the next move.
+ let piecesLeft = {
+ // No need to remember all pieces' squares:
+ // variable only used if just one remaining piece.
+ "w": {count: 0, square: null},
+ "b": {count: 0, square: null}
+ };
+ outerLoop: 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 && this.getPiece(i,j) != V.KING) {
+ const sqCol = this.getColor(i,j);
+ piecesLeft[sqCol].count++;
+ piecesLeft[sqCol].square = [i,j];
+ }
+ }
+ }
+ if (Object.values(piecesLeft).every(v => v.count > 0))
+ return "*";
+ // No pieces left for some side: if both kings are bare, it's a draw
+ if (Object.values(piecesLeft).every(v => v.count == 0))
+ return "1/2";
+ if (piecesLeft[color].count > 0)
+ // He could have drawn, but didn't take my last piece...
+ return color == "w" ? "1-0" : "0-1";
+ const oppCol = V.GetOppCol(color);
+ if (piecesLeft[oppCol].count >= 2)
+ // 2 enemy units or more: I lose
+ return getScoreLost();
+ // I don't have any piece, my opponent have one: can I take it?
+ if (this.isAttacked(piecesLeft[oppCol].square, [color]))
+ // Yes! But I still need to take it
+ return "*";
+ // No :(
+ return getScoreLost();
+ }
+