X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FDark.js;h=f1bd6c0d5c7b9423118a21ce5f4a544945093ba0;hb=69f3d8014e594ef949792d04d97b8286e9c2c268;hp=e3e093b1c38c7af4103d2a7e234d5f6a8a2f7241;hpb=f6dbe8e31a3260487664f1e0b50710b3f3efaf5f;p=vchess.git diff --git a/public/javascripts/variants/Dark.js b/public/javascripts/variants/Dark.js index e3e093b1..f1bd6c0d 100644 --- a/public/javascripts/variants/Dark.js +++ b/public/javascripts/variants/Dark.js @@ -16,6 +16,7 @@ class DarkRules extends ChessRules updateEnlightened() { + const pawnShift = {"w":-1, "b":1}; // Initialize with pieces positions (which are seen) for (let i=0; i 0) - { - this.kingPos[c][0] = move.appear[0].x; - this.kingPos[c][1] = move.appear[0].y; - } + super.updateVariables(move); if (move.vanish.length >= 2 && move.vanish[1].p == V.KING) { // We took opponent king ! - const oppCol = this.getOppCol(c); - this.kingPos[oppCol] = [-1,-1]; + this.kingPos[this.turn] = [-1,-1]; } // Update moves for both colors: @@ -126,6 +134,150 @@ class DarkRules extends ChessRules { return 500; //checkmates evals may be slightly below 1000 } + + // In this special situation, we just look 1 half move ahead + getComputerMove() + { + const maxeval = V.INFINITY; + const color = this.turn; + const oppCol = this.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) => { + let step = undefined; + if (piece == V.BISHOP) + { + if (Math.abs(kp[0] - i) == Math.abs(kp[1] - j)) + { + step = + [ + (i-kp[0]) / Math.abs(i-kp[0]), + (j-kp[1]) / Math.abs(j-kp[1]) + ]; + } + } + else if (piece == V.ROOK) + { + if (kp[0] == i) + step = [0, (j-kp[1]) / Math.abs(j-kp[1])]; + else if (kp[1] == j) + step = [(i-kp[0]) / Math.abs(i-kp[0]), 0]; + } + if (!step) + return false; + // 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]) + { + if (myLight[x][y] && this.board[x][y] != V.EMPTY) + { + obstacle = true; + break; + } + } + if (!obstacle) + return true; + return false; + }; + + // Do I see something which can take my king ? + const kingThreats = () => { + for (let i=0; i= 0 && kingThreats()) + { + // We didn't take opponent king, and our king will be captured: bad + move.eval = -maxeval; + } + this.undo(move); + if (!!move.eval) + continue; + + move.eval = 0; //a priori... + + // Can I take something ? If yes, do it if it seems good... + if (move.vanish.length == 2 && move.vanish[1].c != color) //avoid castle + { + const myPieceVal = V.VALUES[move.appear[0].p]; + const hisPieceVal = V.VALUES[move.vanish[1].p]; + if (myPieceVal <= hisPieceVal) + move.eval = hisPieceVal - myPieceVal + 2; //favor captures + else + { + // Taking a pawn with minor piece, + // or minor piece or pawn with a rook, + // or anything but a queen with a queen, + // or anything with a king. + // ==> Do it at random, although + // this is clearly inferior to what a human can deduce... + move.eval = (Math.random() < 0.5 ? 1 : -1); + } + } + } + + // TODO: also need to implement the case when an opponent piece (in light) + // is threatening something - maybe not the king, but e.g. pawn takes rook. + + moves.sort((a,b) => b.eval - a.eval); + let candidates = [0]; + for (let j=1; j