X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FMarseille.js;h=f372698dd7ef638725918a027bafa938098ec9e8;hb=edcd679ab1fe609641451586ef1e9484925c4f83;hp=0d228b0c0407a40baf7a0f903d06138f4d61c80d;hpb=3fa426b632d92a65843038a161677069de5db27e;p=vchess.git diff --git a/public/javascripts/variants/Marseille.js b/public/javascripts/variants/Marseille.js index 0d228b0c..f372698d 100644 --- a/public/javascripts/variants/Marseille.js +++ b/public/javascripts/variants/Marseille.js @@ -43,15 +43,13 @@ 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); @@ -72,7 +70,6 @@ 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]; @@ -83,7 +80,7 @@ class MarseilleRules extends ChessRules for (let piece of finalPieces) { moves.push(this.getBasicMove([x,y], [x+shiftX,y], - {c:pawnColor,p:piece})); + {c:color,p:piece})); } // Next condition because pawns on 1st rank can generally jump if ([startRank,firstRank].includes(x) @@ -103,7 +100,7 @@ class MarseilleRules extends ChessRules for (let piece of finalPieces) { moves.push(this.getBasicMove([x,y], [x+shiftX,y+shiftY], - {c:pawnColor,p:piece})); + {c:color,p:piece})); } } } @@ -120,6 +117,7 @@ class MarseilleRules extends ChessRules }); if (epSqs.length == 0) return moves; + const oppCol = this.getOppCol(color); for (let sq of epSqs) { if (this.subTurn == 1 || (epSqs.length == 2 && @@ -127,14 +125,16 @@ class MarseilleRules extends ChessRules // (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); } @@ -153,32 +153,30 @@ class MarseilleRules extends ChessRules move.color = this.turn; } move.flags = JSON.stringify(this.aggregateFlags()); - let lastEpsq = this.epSquares[this.epSquares.length-1]; - const epSq = this.getEpSquare(move); - if (lastEpsq.length == 1) - lastEpsq.push(epSq); - else - { - // 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); + const epSq = this.getEpSquare(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))) { - this.epSquares[this.epSquares.length-1].push(undefined); this.turn = this.getOppCol(this.turn); + this.epSquares.push([epSq]); move.checkOnSubturn1 = true; } else { if (this.subTurn == 2) + { this.turn = this.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); @@ -190,31 +188,28 @@ class MarseilleRules extends ChessRules 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"; + this.epSquares.pop(); + } else if (move.checkOnSubturn1) { this.turn = this.getOppCol(this.turn); this.subTurn = 1; + this.epSquares.pop(); } else { if (this.subTurn == 1) + { this.turn = this.getOppCol(this.turn); + let lastEpsq = this.epSquares[this.epSquares.length-1]; + lastEpsq.pop(); + } + else + this.epSquares.pop(); this.subTurn = 3 - this.subTurn; } this.moves.pop(); @@ -224,6 +219,18 @@ class MarseilleRules extends ChessRules // 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() { @@ -233,11 +240,10 @@ class MarseilleRules extends ChessRules const maxeval = V.INFINITY; const color = this.turn; const oppCol = this.getOppCol(this.turn); -//if (this.moves.length == 25) -// debugger; -console.log("turn before " + this.turn + " " + this.subTurn); + // Search best (half) move for opponent turn const getBestMoveEval = () => { + const turnBefore = this.turn + this.subTurn; let moves = this.getAllValidMoves(); if (moves.length == 0) { @@ -250,7 +256,8 @@ console.log("turn before " + this.turn + " " + this.subTurn); 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(); @@ -259,26 +266,18 @@ console.log("turn before " + this.turn + " " + this.subTurn); 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; }; let moves11 = this.getAllValidMoves(); -console.log("turn after " + this.turn + " " + this.subTurn); - -if (this.moves.length == 25) - debugger; - - let doubleMoves = []; // Rank moves using a min-max at depth 2 for (let i=0; i { return (color=="w" ? 1 : -1) * (b.eval - a.eval); }); @@ -369,7 +312,6 @@ console.log("turn intermBIS " + this.turn + " " + this.subTurn); { candidates.push(i); } -console.log("turn END " + this.turn + " " + this.subTurn); const selected = doubleMoves[_.sample(candidates, 1)].moves; if (selected.length == 1) @@ -380,15 +322,21 @@ console.log("turn END " + this.turn + " " + this.subTurn); getPGN(mycolor, score, fenStart, mode) { let pgn = ""; - pgn += '[Site "vchess.club"]
'; + pgn += '[Site "vchess.club"]\n'; const opponent = mode=="human" ? "Anonymous" : "Computer"; - pgn += '[Variant "' + variant + '"]
'; - pgn += '[Date "' + getDate(new Date()) + '"]
'; - pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]
'; - pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]
'; - pgn += '[FenStart "' + fenStart + '"]
'; - pgn += '[Fen "' + this.getFen() + '"]
'; - pgn += '[Result "' + score + '"]

'; + pgn += '[Variant "' + variant + '"]\n'; + pgn += '[Date "' + getDate(new Date()) + '"]\n'; + const whiteName = ["human","computer"].includes(mode) + ? (mycolor=='w'?'Myself':opponent) + : "analyze"; + const blackName = ["human","computer"].includes(mode) + ? (mycolor=='b'?'Myself':opponent) + : "analyze"; + pgn += '[White "' + whiteName + '"]\n'; + pgn += '[Black "' + blackName + '"]\n'; + pgn += '[FenStart "' + fenStart + '"]\n'; + pgn += '[Fen "' + this.getFen() + '"]\n'; + pgn += '[Result "' + score + '"]\n\n'; let counter = 1; let i = 0; @@ -404,7 +352,7 @@ console.log("turn END " + this.turn + " " + this.subTurn); pgn += move + (i < this.moves.length-1 ? " " : ""); } } - pgn += "

"; + pgn += "\n\n"; // "Complete moves" PGN (helping in ambiguous cases) counter = 1; @@ -422,7 +370,7 @@ console.log("turn END " + this.turn + " " + this.subTurn); } } - return pgn; + return pgn + "\n"; } }