+ setupPieces() {
+ for (let i=0; i<this.size.x; i++) {
+ for (let j=0; j<this.size.y; j++) {
+ if (this.board[i][j] != "") {
+ const sqColor = (this.getColor(i, j) == 'w' ? "white" : "black");
+ const elt = document.getElementById(this.coordsToId({x: i, y: j}));
+ elt.classList.remove("neutral-square");
+ elt.classList.add("bg-" + sqColor);
+ }
+ }
+ }
+ }
+
+ get size() {
+ const baseRatio = 1.6191907514450865; //2801.2 / 1730, "widescreen"
+ const rc =
+ document.getElementById(this.containerId).getBoundingClientRect();
+ const rotate = rc.width < rc.height; //"vertical screen"
+ return {
+ x: this.options["bsize"],
+ y: this.options["bsize"],
+ ratio: (rotate ? 1 / baseRatio : baseRatio)
+ };
+ }
+
+ play(move) {
+ this.playOnBoard(move);
+ this.movesCount++;
+ this.turn = C.GetOppCol(this.turn);
+ }
+
+ getCurrentScore(move) {
+ const oppCol = C.GetOppCol(this.turn);
+ // Search for connecting path of opp color:
+ let explored = {}, component;
+ let min, max;
+ const getIndex = (x, y) => x + "." + y;
+ // Explore one connected component:
+ const neighborsSearch = ([x, y], index) => {
+ // Let's say "white" connects on x and "black" on y
+ const z = (oppCol == 'w' ? x : y);
+ if (z < min)
+ min = z;
+ if (z > max)
+ max = z;
+ explored[index] = true;
+ component[index] = true;
+ for (let [dx, dy] of super.pieces()['k'].moves[0].steps) {
+ const [nx, ny] = [x + dx, y + dy];
+ const nidx = getIndex(nx, ny);
+ if (
+ this.onBoard(nx, ny) &&
+ this.getColor(nx, ny) == oppCol &&
+ !component[nidx]
+ ) {
+ neighborsSearch([nx, ny], nidx);
+ }
+ }
+ };
+ // Explore all components:
+ for (let i=0; i<this.size.x; i++) {
+ for (let j=0; j<this.size.y; j++) {
+ const index = getIndex(i, j);
+ if (this.getColor(i, j) == oppCol && !explored[index]) {
+ component = {};
+ [min, max] = [this.size.x, 0];
+ neighborsSearch([i, j], index);
+ if (max - min == this.size.x - 1)
+ return (oppCol == "w" ? "1-0" : "0-1");
+ }
+ }
+ }
+ return "*";
+ }
+
+ playVisual(move) {
+ move.vanish.forEach(v => {
+ let elt = document.getElementById(this.coordsToId({x: v.x, y: v.y}));
+ elt.classList.remove("bg-" + (v.c == 'w' ? "white" : "black"));
+ });
+ move.appear.forEach(a => {
+ let elt = document.getElementById(this.coordsToId({x: a.x, y: a.y}));
+ elt.classList.add("bg-" + (a.c == 'w' ? "white" : "black"));
+ });