X-Git-Url: https://git.auder.net/assets/icon_infos.svg?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=2a962a964c726181c2a480a86cdad7a969137cb4;hb=8ddc00a072c5a4aa679e5a420a7f9664d18e03f3;hp=5763af7e1d0f86e40789ff7c9467ce66a3bd1334;hpb=7931e479adf93c87771ded1892a0873af72ae46d;p=vchess.git
diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js
index 5763af7e..2a962a96 100644
--- a/public/javascripts/base_rules.js
+++ b/public/javascripts/base_rules.js
@@ -50,7 +50,7 @@ class ChessRules
/////////////////
// INITIALIZATION
- // fen == "position flags"
+ // fen == "position [flags [turn]]"
constructor(fen, moves)
{
this.moves = moves;
@@ -105,15 +105,14 @@ class ChessRules
k++;
}
}
- const epSq = (this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined);
- this.epSquares = [ epSq ];
+ this.epSquares = [ this.getEpSquare(this.lastMove || fenParts[3]) ];
}
// Check if FEN describe a position
static IsGoodFen(fen)
{
const fenParts = fen.split(" ");
- if (fenParts.length== 0 || fenParts.length > 3)
+ if (fenParts.length== 0)
return false;
// 1) Check position
const position = fenParts[0];
@@ -145,7 +144,7 @@ class ChessRules
return false;
}
// 3) Check turn (if present)
- if (fenParts.length == 3)
+ if (fenParts.length >= 3)
{
if (!["w","b"].includes(fenParts[2]))
return false;
@@ -251,8 +250,20 @@ class ChessRules
}
// En-passant square, if any
- getEpSquare(move)
+ getEpSquare(moveOrSquare)
{
+ if (typeof moveOrSquare === "string")
+ {
+ const square = moveOrSquare;
+ if (square == "-")
+ return undefined;
+ return {
+ x: square[0].charCodeAt()-97,
+ y: V.size.x-parseInt(square[1])
+ };
+ }
+ // Argument is a move:
+ const move = moveOrSquare;
const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x];
if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) == 2)
{
@@ -792,6 +803,7 @@ class ChessRules
this.updateVariables(move);
this.moves.push(move);
this.epSquares.push( this.getEpSquare(move) );
+ this.turn = this.getOppCol(this.turn);
V.PlayOnBoard(this.board, move);
if (!!ingame)
@@ -801,6 +813,7 @@ class ChessRules
undo(move)
{
V.UndoOnBoard(this.board, move);
+ this.turn = this.getOppCol(this.turn);
this.epSquares.pop();
this.moves.pop();
this.unupdateVariables(move);
@@ -1107,7 +1120,7 @@ class ChessRules
return pieces["b"].join("") +
"/pppppppp/8/8/8/8/PPPPPPPP/" +
pieces["w"].join("").toUpperCase() +
- " 1111"; //add flags
+ " 1111 w"; //add flags + turn
}
// Return current fen according to pieces+colors state
@@ -1209,14 +1222,11 @@ class ChessRules
// The score is already computed when calling this function
getPGN(mycolor, score, fenStart, mode)
{
- const zeroPad = x => { return (x<10 ? "0" : "") + x; };
let pgn = "";
pgn += '[Site "vchess.club"]
';
- const d = new Date();
const opponent = mode=="human" ? "Anonymous" : "Computer";
pgn += '[Variant "' + variant + '"]
';
- pgn += '[Date "' + d.getFullYear() + '-' + (d.getMonth()+1) +
- '-' + zeroPad(d.getDate()) + '"]
';
+ pgn += '[Date "' + getDate(new Date()) + '"]
';
pgn += '[White "' + (mycolor=='w'?'Myself':opponent) + '"]
';
pgn += '[Black "' + (mycolor=='b'?'Myself':opponent) + '"]
';
pgn += '[FenStart "' + fenStart + '"]
';