X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FKnightrelay.js;h=43aef74dfe243f99b0b26a985a7247e47b42a9ca;hb=b83a675a3066c67cc7843ae27ad8aeffd15b0976;hp=79688e2a09f120da9e4ea55e3841c1655f2f9166;hpb=c322a84434326dff1291a57e82dbd995817a5423;p=vchess.git diff --git a/client/src/variants/Knightrelay.js b/client/src/variants/Knightrelay.js index 79688e2a..43aef74d 100644 --- a/client/src/variants/Knightrelay.js +++ b/client/src/variants/Knightrelay.js @@ -5,7 +5,8 @@ export const VariantRules = class KnightrelayRules extends ChessRules { let moves = super.getPotentialMovesFrom([x, y]); // Expand possible moves if guarded by a knight: - if (this.getPiece(x,y) != V.KNIGHT) { + const piece = this.getPiece(x,y); + if (piece != V.KNIGHT) { const color = this.turn; let guardedByKnight = false; for (const step of V.steps[V.KNIGHT]) { @@ -19,16 +20,24 @@ export const VariantRules = class KnightrelayRules extends ChessRules { } } if (guardedByKnight) { + const lastRank = color == "w" ? 0 : V.size.x - 1; for (const step of V.steps[V.KNIGHT]) { if ( V.OnBoard(x+step[0],y+step[1]) && this.getColor(x+step[0],y+step[1]) != color ) { - let m = this.getBasicMove([x,y], [x+step[0],y+step[1]]); - if (!m.appear[0].c || !m.vanish[0].c) - debugger; - moves.push(m); - //moves.push(this.getBasicMove([x,y], [x+step[0],y+step[1]])); + // Potential promotions: + const finalPieces = piece == V.PAWN && x + step[0] == lastRank + ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] + : [piece]; + for (let p of finalPieces) { + moves.push( + this.getBasicMove([x,y], [x+step[0],y+step[1]], { + c: color, + p: p + }) + ); + } } } } @@ -37,6 +46,52 @@ export const VariantRules = class KnightrelayRules extends ChessRules { return moves; } + isAttacked(sq, colors) { + if (super.isAttacked(sq, colors)) + return true; + + // Check if a (non-knight) piece at knight distance + // is guarded by a knight (and thus attacking) + const x = sq[0], + y = sq[1]; + for (const step of V.steps[V.KNIGHT]) { + if ( + V.OnBoard(x+step[0],y+step[1]) && + colors.includes(this.getColor(x+step[0],y+step[1])) && + this.getPiece(x+step[0],y+step[1]) != V.KNIGHT + ) { + for (const step2 of V.steps[V.KNIGHT]) { + const xx = x+step[0]+step2[0], + yy = y+step[1]+step2[1]; + if ( + V.OnBoard(xx,yy) && + colors.includes(this.getColor(xx,yy)) && + this.getPiece(xx,yy) == V.KNIGHT + ) { + return true; + } + } + } + } + + return false; + } + + static get VALUES() { + return { + p: 1, + r: 5, + n: 7, //the knight is valuable + b: 3, + q: 9, + k: 1000 + }; + } + + static get SEARCH_DEPTH() { + return 2; + } + getNotation(move) { if (move.appear.length == 2 && move.appear[0].p == V.KING) // Castle