X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=c0dc826fd0ae9d5661991224251cd1b944f73c18;hb=01a135e2aa21365c0a96931fbb6be76876e7c2a1;hp=3a0f1c6332d1c4d4b5b2dc48c5a4c96d0bae3b47;hpb=f3802fcd1279e5d07cdff1341fc5e17c5296dc9c;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index 3a0f1c63..c0dc826f 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -46,14 +46,14 @@ class ChessRules ///////////////// // INITIALIZATION - // fen = "position flags epSquare movesCount" + // fen == "position flags" constructor(fen, moves) { this.moves = moves; // Use fen string to initialize variables, flags and board - this.initVariables(fen); this.board = VariantRules.GetBoard(fen); this.flags = VariantRules.GetFlags(fen); + this.initVariables(fen); } initVariables(fen) @@ -152,7 +152,7 @@ class ChessRules return L>0 ? this.moves[L-1] : null; } get turn() { - return this.movesCount%2==0 ? 'w' : 'b'; + return this.moves.length%2==0 ? 'w' : 'b'; } // Pieces codes @@ -470,8 +470,8 @@ class ChessRules canIplay(color, sq) { - return ((color=='w' && this.movesCount%2==0) - || (color=='b' && this.movesCount%2==1)) + return ((color=='w' && this.moves.length%2==0) + || (color=='b' && this.moves.length%2==1)) && this.getColor(sq[0], sq[1]) == color; } @@ -660,7 +660,7 @@ class ChessRules board[psq.x][psq.y] = psq.c + psq.p; } - // Before move is played: + // Before move is played, update variables + flags updateVariables(move) { const piece = this.getPiece(move.start.x,move.start.y); @@ -691,37 +691,33 @@ class ChessRules } } - play(move, ingame) + unupdateVariables(move) { - // Save flags (for undo) - move.flags = JSON.stringify(this.flags); //TODO: less costly - this.updateVariables(move); + // (Potentially) Reset king position + const c = this.getColor(move.start.x,move.start.y); + if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING) + this.kingPos[c] = [move.start.x, move.start.y]; + } + play(move, ingame) + { if (!!ingame) - { move.notation = this.getNotation(move); - this.moves.push(move); - } + // Save flags (for undo) + move.flags = JSON.stringify(this.flags); //TODO: less costly? + this.updateVariables(move); + this.moves.push(move); this.epSquares.push( this.getEpSquare(move) ); VariantRules.PlayOnBoard(this.board, move); - this.movesCount++; } - undo(move, ingame) + undo(move) { VariantRules.UndoOnBoard(this.board, move); this.epSquares.pop(); - this.movesCount--; - - if (!!ingame) - this.moves.pop(); - - // Update king position, and reset stored/computed flags - const c = this.getColor(move.start.x,move.start.y); - if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING) - this.kingPos[c] = [move.start.x, move.start.y]; - + this.moves.pop(); + this.unupdateVariables(move); this.flags = JSON.parse(move.flags); } @@ -1041,14 +1037,15 @@ class ChessRules } // The score is already computed when calling this function - getPGN(mycolor, score, fenStart) + getPGN(mycolor, score, fenStart, mode) { let pgn = ""; pgn += '[Site "vchess.club"]
'; const d = new Date(); + const opponent = this.mode=="human" ? "Anonymous" : "Computer"; pgn += '[Date "' + d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() + '"]
'; - pgn += '[White "' + (mycolor=='w'?'Myself':'Anonymous') + '"]
'; - pgn += '[Black "' + (mycolor=='b'?'Myself':'Anonymous') + '"]
'; + pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]
'; + pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]
'; pgn += '[Fen "' + fenStart + '"]
'; pgn += '[Result "' + score + '"]

';