X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FUltima.js;h=d6676436a24f21641ae57783ebd656a4bad85fff;hb=a3c86ec9b60326a8ae3d8f237493fb09627aed95;hp=9212afba9612ace343a13f3665920be3a93760ff;hpb=2eef6db6cdce30fe785e601b88858c7fc743eee8;p=vchess.git diff --git a/public/javascripts/variants/Ultima.js b/public/javascripts/variants/Ultima.js index 9212afba..d6676436 100644 --- a/public/javascripts/variants/Ultima.js +++ b/public/javascripts/variants/Ultima.js @@ -52,6 +52,33 @@ class UltimaRules extends ChessRules getPotentialMovesFrom([x,y]) { + // Pre-check: is thing on this square immobilized? + // In this case add potential suicide as a move "taking the immobilizer" + const piece = this.getPiece(x,y); + const color = this.getColor(x,y); + const oppCol = this.getOppCol(color); + const V = VariantRules; + const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); + const [sizeX,sizeY] = V.size; + for (let step of adjacentSteps) + { + const [i,j] = [x+step[0],y+step[1]]; + if (i>=0 && i=0 && j filter directly in functions below } getSlideNJumpMoves([x,y], steps, oneStep) @@ -96,28 +118,243 @@ class UltimaRules extends ChessRules return moves; } + // Modify capturing moves among listed pawn moves + addPawnCaptures(moves, byChameleon) + { + const steps = VariantRules.steps[VariantRules.ROOK]; + const [sizeX,sizeY] = VariantRules.size; + const color = this.turn; + const oppCol = this.getOppCol(color); + moves.forEach(m => { + if (!!byChameleon && m.start.x!=m.end.x && m.start.y!=m.end.y) + return; //chameleon not moving as pawn + // Try capturing in every direction + for (let step of steps) + { + const sq2 = [m.end.x+2*step[0],m.end.y+2*step[1]]; + if (sq2[0]>=0 && sq2[0]=0 && sq2[1] { + // Check piece-king rectangle (if any) corners for enemy pieces + if (m.end.x == kp[0] || m.end.y == kp[1]) + return; //"flat rectangle" + const corner1 = [Math.max(m.end.x,kp[0]), Math.min(m.end.y,kp[1])]; + const corner2 = [Math.min(m.end.x,kp[0]), Math.max(m.end.y,kp[1])]; + for (let [i,j] of [corner1,corner2]) + { + if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) == oppCol) + { + const piece = this.getPiece(i,j); + if (!byChameleon || piece == VariantRules.ROOK) + { + m.vanish.push( new PiPo({ + x:i, + y:j, + p:piece, + c:oppCol + }) ); + } + } + } + }); + } + + // Coordinator getPotentialRookMoves(sq) { - return super.getPotentialQueenMoves(sq); + let moves = super.getPotentialQueenMoves(sq); + this.addRookCaptures(moves); + return moves; } + // Long-leaper + getKnightCaptures(startSquare, byChameleon) + { + // Look in every direction for captures + const V = VariantRules; + const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); + const [sizeX,sizeY] = V.size; + const color = this.turn; + const oppCol = this.getOppCol(color); + let moves = []; + const [x,y] = [startSquare[0],startSquare[1]]; + const piece = this.getPiece(x,y); //might be a chameleon! + outerLoop: + for (let step of steps) + { + let [i,j] = [x+step[0], y+step[1]]; + while (i>=0 && i=0 && j=sizeX || j<0 || j>=sizeY || this.getColor(i,j)==color + || (!!byChameleon && this.getPiece(i,j)!=V.KNIGHT)) + { + continue; + } + // last(thing), cur(thing) : stop if "cur" is our color, or beyond board limits, + // or if "last" isn't empty and cur neither. Otherwise, if cur is empty then + // add move until cur square; if cur is occupied then stop if !!byChameleon and + // the square not occupied by a leaper. + let last = [i,j]; + let cur = [i+step[0],j+step[1]]; + let vanished = [ new PiPo({x:x,y:y,c:color,p:piece}) ]; + while (cur[0]>=0 && cur[0]=0 && cur[1] { + const key = m.end.x + sizeX * m.end.y; + if (!mergedMoves[key]) + mergedMoves[key] = m; + else + { + for (let i=1; i { moves.push(mergedMoves[k]); }); + return moves; + } + + // Withdrawer + addQueenCaptures(moves, byChameleon) + { + if (moves.length == 0) + return; + const [x,y] = [moves[0].start.x,moves[0].start.y]; + const V = VariantRules; + const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); + let capturingDirections = []; + const color = this.turn; + const oppCol = this.getOppCol(color); + const [sizeX,sizeY] = V.size; + adjacentSteps.forEach(step => { + const [i,j] = [x+step[0],y+step[1]]; + if (i>=0 && i=0 && j { + const step = [ + m.end.x!=x ? (m.end.x-x)/Math.abs(m.end.x-x) : 0, + m.end.y!=y ? (m.end.y-y)/Math.abs(m.end.y-y) : 0 + ]; + // NOTE: includes() and even _.isEqual() functions fail... + // TODO: this test should be done only once per direction + if (capturingDirections.some(dir => + { return (dir[0]==-step[0] && dir[1]==-step[1]); })) + { + const [i,j] = [x-step[0],y-step[1]]; + m.vanish.push(new PiPo({ + x:i, + y:j, + p:this.getPiece(i,j), + c:oppCol + })); + } + }); } getPotentialQueenMoves(sq) { + let moves = super.getPotentialQueenMoves(sq); + this.addQueenCaptures(moves); + return moves; + } + + getPotentialImmobilizerMoves(sq) + { + // Immobilizer doesn't capture return super.getPotentialQueenMoves(sq); } @@ -140,12 +377,14 @@ class UltimaRules extends ChessRules isAttackedByRook(sq, colors) { // Enemy king must be on same file and a rook on same row (or reverse) + return false; } isAttackedByKnight(sq, colors) { // Square (x,y) must be on same line as a knight, // and there must be empty square(s) behind. + return false; } isAttackedByBishop(sq, colors) @@ -153,12 +392,14 @@ class UltimaRules extends ChessRules // switch on piece nature on square sq: a chameleon attack as this piece // ==> call the appropriate isAttackedBy... (exception of immobilizers) // Other exception: a chameleon cannot attack a chameleon (seemingly...) + return false; } isAttackedByQueen(sq, colors) { // Square (x,y) must be adjacent to a queen, and the queen must have // some free space in the opposite direction from (x,y) + return false; } updateVariables(move) @@ -173,6 +414,12 @@ class UltimaRules extends ChessRules } } + checkGameEnd() + { + // No valid move: game is lost (stalemate is a win) + return this.turn == "w" ? "0-1" : "1-0"; + } + static get VALUES() { //TODO: totally experimental! return { 'p': 1, @@ -223,7 +470,7 @@ class UltimaRules extends ChessRules randIndex = _.random(1); const rookPos = positions[randIndex]; positions.splice(randIndex, 1); - const immobilizerPos = positions[2]; + const immobilizerPos = positions[0]; pieces[c][bishop1Pos] = 'b'; pieces[c][bishop2Pos] = 'b'; @@ -244,4 +491,15 @@ class UltimaRules extends ChessRules { return "0000"; //TODO: or "-" ? } + + getNotation(move) + { + if (move.appear.length == 0) + { + const startSquare = + String.fromCharCode(97 + move.start.y) + (VariantRules.size[0]-move.start.x); + return "^" + startSquare; //suicide + } + return super.getNotation(move); + } }