X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FMarseille.js;h=7d83bd556b1b168a3bf56637859fff0c2d280d5f;hb=b955c65b942d09d24b5c3bed0d755d4f2f8f71f1;hp=02daa22b8d416ba7ca131e14dbf6ec7700b98c97;hpb=78bab51e57306ae9085d83828bbb73b62dd83fd8;p=vchess.git diff --git a/public/javascripts/variants/Marseille.js b/public/javascripts/variants/Marseille.js index 02daa22b..7d83bd55 100644 --- a/public/javascripts/variants/Marseille.js +++ b/public/javascripts/variants/Marseille.js @@ -19,8 +19,6 @@ class MarseilleRules extends ChessRules getTurnFen() { - if (this.startAtFirstMove && this.moves.length==0) - return "w"; return this.turn + this.subTurn; } @@ -43,24 +41,21 @@ class MarseilleRules extends ChessRules const parsedFen = V.ParseFen(fen); this.setFlags(parsedFen.flags); if (parsedFen.enpassant == "-") - this.epSquares = [ [undefined,undefined] ]; + this.epSquares = [ [undefined] ]; else { let res = []; const squares = parsedFen.enpassant.split(","); for (let sq of squares) res.push(V.SquareToCoords(sq)); - if (res.length == 1) - res.push(undefined); //always 2 slots in epSquares[i] this.epSquares = [ res ]; } this.scanKingsRooks(fen); // Extract subTurn from turn indicator: "w" (first move), or // "w1" or "w2" white subturn 1 or 2, and same for black const fullTurn = V.ParseFen(fen).turn; - this.startAtFirstMove = (fullTurn == "w"); this.turn = fullTurn[0]; - this.subTurn = (fullTurn[1] || 1); + this.subTurn = (fullTurn[1] || 0); //"w0" = special code for first move in game } getPotentialPawnMoves([x,y]) @@ -72,41 +67,37 @@ class MarseilleRules extends ChessRules const firstRank = (color == 'w' ? sizeX-1 : 0); const startRank = (color == "w" ? sizeX-2 : 1); const lastRank = (color == "w" ? 0 : sizeX-1); - const pawnColor = this.getColor(x,y); //can be different for checkered + const finalPieces = x + shiftX == lastRank + ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN] + : [V.PAWN]; - if (x+shiftX >= 0 && x+shiftX < sizeX) //TODO: always true + // One square forward + if (this.board[x+shiftX][y] == V.EMPTY) { - const finalPieces = x + shiftX == lastRank - ? [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN] - : [V.PAWN] - // One square forward - if (this.board[x+shiftX][y] == V.EMPTY) + for (let piece of finalPieces) { - for (let piece of finalPieces) - { - moves.push(this.getBasicMove([x,y], [x+shiftX,y], - {c:pawnColor,p:piece})); - } - // Next condition because pawns on 1st rank can generally jump - if ([startRank,firstRank].includes(x) - && this.board[x+2*shiftX][y] == V.EMPTY) - { - // Two squares jump - moves.push(this.getBasicMove([x,y], [x+2*shiftX,y])); - } + moves.push(this.getBasicMove([x,y], [x+shiftX,y], + {c:color,p:piece})); } - // Captures - for (let shiftY of [-1,1]) + // Next condition because pawns on 1st rank can generally jump + if ([startRank,firstRank].includes(x) + && this.board[x+2*shiftX][y] == V.EMPTY) { - if (y + shiftY >= 0 && y + shiftY < sizeY - && this.board[x+shiftX][y+shiftY] != V.EMPTY - && this.canTake([x,y], [x+shiftX,y+shiftY])) + // Two squares jump + moves.push(this.getBasicMove([x,y], [x+2*shiftX,y])); + } + } + // Captures + for (let shiftY of [-1,1]) + { + if (y + shiftY >= 0 && y + shiftY < sizeY + && this.board[x+shiftX][y+shiftY] != V.EMPTY + && this.canTake([x,y], [x+shiftX,y+shiftY])) + { + for (let piece of finalPieces) { - for (let piece of finalPieces) - { - moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY], - {c:pawnColor,p:piece})); - } + moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY], + {c:color,p:piece})); } } } @@ -123,20 +114,24 @@ class MarseilleRules extends ChessRules }); if (epSqs.length == 0) return moves; + const oppCol = V.GetOppCol(color); for (let sq of epSqs) { if (this.subTurn == 1 || (epSqs.length == 2 && // Was this en-passant capture already played at subturn 1 ? + // (Or maybe the opponent filled the en-passant square with a piece) this.board[epSqs[0].x][epSqs[0].y] != V.EMPTY)) { - if (sq.x == x+shiftX && Math.abs(sq.y - y) == 1) + if (sq.x == x+shiftX && Math.abs(sq.y - y) == 1 + // Add condition "enemy pawn must be present" + && this.getPiece(x,sq.y) == V.PAWN && this.getColor(x,sq.y) == oppCol) { let epMove = this.getBasicMove([x,y], [sq.x,sq.y]); epMove.vanish.push({ x: x, y: sq.y, p: 'p', - c: this.getColor(x,sq.y) + c: oppCol }); moves.push(epMove); } @@ -146,87 +141,70 @@ class MarseilleRules extends ChessRules return moves; } - play(move, ingame) + play(move) { -// console.log("play " + this.getNotation(move)); -// console.log(this.turn + " "+ this.subTurn); - if (!!ingame) - move.notation = [this.getNotation(move), this.getLongNotation(move)]; move.flags = JSON.stringify(this.aggregateFlags()); - let lastEpsq = this.epSquares[this.epSquares.length-1]; + move.turn = this.turn + this.subturn; + V.PlayOnBoard(this.board, move); const epSq = this.getEpSquare(move); - if (lastEpsq.length == 1) - lastEpsq.push(epSq); - else + if (this.subTurn == 0) //first move in game { - // New turn - let newEpsqs = [epSq]; - if (this.startAtFirstMove && this.moves.length == 0) - newEpsqs.push(undefined); //at first move, to force length==2 (TODO) - this.epSquares.push(newEpsqs); - } - V.PlayOnBoard(this.board, move); - if (this.startAtFirstMove && this.moves.length == 0) this.turn = "b"; + this.epSquares.push([epSq]); + } // Does this move give check on subturn 1? If yes, skip subturn 2 - else if (this.subTurn==1 && this.underCheck(this.getOppCol(this.turn))) + else if (this.subTurn==1 && this.underCheck(V.GetOppCol(this.turn))) { - this.epSquares[this.epSquares.length-1].push(undefined); - this.turn = this.getOppCol(this.turn); + this.turn = V.GetOppCol(this.turn); + this.epSquares.push([epSq]); move.checkOnSubturn1 = true; } else { if (this.subTurn == 2) - this.turn = this.getOppCol(this.turn); + { + this.turn = V.GetOppCol(this.turn); + let lastEpsq = this.epSquares[this.epSquares.length-1]; + lastEpsq.push(epSq); + } + else + this.epSquares.push([epSq]); this.subTurn = 3 - this.subTurn; } - this.moves.push(move); this.updateVariables(move); - if (!!ingame) - move.hash = hex_md5(this.getFen()); - //console.log(move.checkOnSubturn1 + " " +this.turn + " "+ this.subTurn); } undo(move) { this.disaggregateFlags(JSON.parse(move.flags)); - let lastEpsq = this.epSquares[this.epSquares.length-1]; - if (lastEpsq.length == 2) - { - if (!!move.checkOnSubturn1 || - (this.startAtFirstMove && this.moves.length == 1)) - { - this.epSquares.pop(); //remove real + artificial e.p. squares - } - else - lastEpsq.pop(); - } - else - this.epSquares.pop(); V.UndoOnBoard(this.board, move); - if (this.startAtFirstMove && this.moves.length == 1) - this.turn = "w"; - else if (move.checkOnSubturn1) - { - this.turn = this.getOppCol(this.turn); - this.subTurn = 1; - } - else + if (move.turn[1] == '0' || move.checkOnSubturn1 || this.subTurn == 2) + this.epSquares.pop(); + else //this.subTurn == 1 { - if (this.subTurn == 1) - this.turn = this.getOppCol(this.turn); - this.subTurn = 3 - this.subTurn; + let lastEpsq = this.epSquares[this.epSquares.length-1]; + lastEpsq.pop(); } - this.moves.pop(); + this.turn = move.turn[0]; + this.subTurn = parseInt(move.turn[1]); this.unupdateVariables(move); -// console.log("UNDO " + this.getNotation(move)); -// console.log(this.turn + " "+ this.subTurn); } // NOTE: GenRandInitFen() is OK, // since at first move turn indicator is just "w" + static get VALUES() + { + return { + 'p': 1, + 'r': 5, + 'n': 3, + 'b': 3, + 'q': 7, //slightly less than in orthodox game + 'k': 1000 + }; + } + // No alpha-beta here, just adapted min-max at depth 2(+1) getComputerMove() { @@ -235,10 +213,11 @@ class MarseilleRules extends ChessRules const maxeval = V.INFINITY; const color = this.turn; - const oppCol = this.getOppCol(this.turn); + const oppCol = V.GetOppCol(this.turn); // Search best (half) move for opponent turn const getBestMoveEval = () => { + const turnBefore = this.turn + this.subTurn; let moves = this.getAllValidMoves(); if (moves.length == 0) { @@ -251,7 +230,8 @@ class MarseilleRules extends ChessRules for (let m of moves) { this.play(m); - this.turn = color; //very artificial... + // Now turn is oppCol,2 if m doesn't give check + // Otherwise it's color,1. In both cases the next test makes sense if (!this.atLeastOneMove()) { const score = this.checkGameEnd(); @@ -260,14 +240,12 @@ class MarseilleRules extends ChessRules else { // Found a mate - this.turn = oppCol; this.undo(m); return maxeval * (score == "1-0" ? 1 : -1); } } const evalPos = this.evalPosition(); res = (oppCol == "w" ? Math.max(res, evalPos) : Math.min(res, evalPos)); - this.turn = oppCol; this.undo(m); } return res; @@ -278,7 +256,6 @@ class MarseilleRules extends ChessRules // Rank moves using a min-max at depth 2 for (let i=0; i