diff --git a/index.html b/index.html index 6f332be..3d667cb 100644 --- a/index.html +++ b/index.html @@ -229,6 +229,56 @@

9) Middle Game Demo

Bc5+ 28. Qf2 Qc1+ 29. Ne1 Qxe1# 0-1 + +
+

10) PGN with FEN 1

+
+ Back + + Flip +
+
+

+
+    [Event "Gausdal"]
+    [Site "?"]
+    [Date "1982.??.??"]
+    [Round "?"]
+    [White "Akesson"]
+    [Black "Dlugy"]
+    [Result "0-1"]
+    [Annotator "T4Z"]
+    [SetUp "1"]
+    [FEN "1rr3k1/5p1p/1q4p1/p2pPP2/1pb2RP1/1Pn3NP/PK1Q2B1/2R5 b - - 0 1"]
+
+1... Nxa2 2. Kxa2 Bxb3 3. Kxb3 a4+ 4. Kb2 a3+ 5. Kb3 Rc3+ 6. Rxc3 bxc3+ 7. Kxc3 Qc5+ 8. Kd3 Rb3+ 9. Ke2 Rb2 0-1 
+      
+
+
+

11) PGN with FEN 2

+
+ Back + + Flip +
+
+

+
+    [Event "Wijk aan Zee"]
+    [Site "?"]
+    [Date "1989.??.??"]
+    [Round "?"]
+    [White "Kujf"]
+    [Black "Hodgson"]
+    [Result "0-1"]
+    [Annotator "Tv3A"]
+    [SetUp "1"]
+    [FEN "2krr3/pppn1ppp/6b1/n2P4/1q4P1/5P1N/PP1QB2P/1NKR3R b - - 0 1"]
+
+1... Qb3 2. Bd3 Qxa2 3. Qb4 Re2 4. Bxe2 Nb3+ 5. Qxb3 Qxb3 0-1
+      
+
+ diff --git a/javascripts/app.js b/javascripts/app.js index c47dc75..9f3fa53 100644 --- a/javascripts/app.js +++ b/javascripts/app.js @@ -33,5 +33,6 @@ jQuery(function($) { loadChessGame( '#game9', { pgn : $('#middle-game').html() }, function(chess) { chess.transitionTo(25); }); - + loadChessGame( '#game10', { pgn : $('#pgn-with-fen-1').html() } ); + loadChessGame( '#game11', { pgn : $('#pgn-with-fen-2').html() } ); }); diff --git a/javascripts/jchess-0.1.0.js b/javascripts/jchess-0.1.0.js index a0fdea0..6b6de79 100644 --- a/javascripts/jchess-0.1.0.js +++ b/javascripts/jchess-0.1.0.js @@ -83,7 +83,12 @@ jQuery.eachWithContext = function(context, object, callback) { // If pgn was passed in, parse it if (this.settings.pgn) this.parsePGN(this.settings.pgn); - + /* if header has a FEN tag then setup board again */ + if(this.game.header.FEN) { + // this.setUpBoard( this.parseFEN( this.game.header.FEN) ); + console.log("setting the board for FEN " + this.game.header.FEN); + this.settings.fen = this.game.header.FEN; + }; this.setUpBoard( this.parseFEN( this.settings.fen ) ); this.writeBoard(); }, @@ -269,9 +274,29 @@ jQuery.eachWithContext = function(context, object, callback) { var result = re.exec(pgn); this.game.header[headers[i]] = (result == null) ? "" : result[1]; } + + re_fen = new RegExp('[Ff][Ee][Nn] "([^"]*)"]'); + console.log(re_fen); + console.log(pgn); + console.log(re_fen.exec(pgn)); + var fen_result = re_fen.exec(pgn); + if (fen_result) { this.game.header["FEN"] = fen_result[1]; + console.log("fen result " + this.parseFEN(fen_result[1])); + this.setUpBoard( this.parseFEN( fen_result[1])); + this.clearBoard(); + } // Find the body - this.game.body = /(1\. ?(N[acfh]3|[abcdefgh][34]).*)/m.exec(pgn)[1]; + // this.game.body = /(1\. ?(N[acfh]3|[abcdefgh][34]).*)/m.exec(pgn)[1]; + /* not sure from the regexp */ + this.game.body = /(\d{1,3}(\.[^.[0-9A-Z?].*|\.\.\. .*))/m.exec(pgn)[1]; + console.log(this.game.body); + + var first_move = 0; // if game is setup from the fen and starts with black + if((/^\d{1,3}\.\.\. /).test(this.game.body)) { + console.log("player black") + first_move = 1; + } // Remove numbers, remove result this.game.body = this.game.body.replace(new RegExp("1-0|1/2-1/2|0-1"), ''); @@ -279,11 +304,14 @@ jQuery.eachWithContext = function(context, object, callback) { this.game.body = this.game.body.replace(/\s\d+\.+/g, ' '); var moves = $.trim(this.game.body).split(/\s+/); - // console.log(moves); + console.log(moves); + console.log(this.game.body); // This must be a separate variable from i, since annotations don't // count as moves. var move_number = 0; + + $.eachWithContext(this, moves, function(i, move) { if ( /annotation-\d+/.test(move) ) { this.game.annotations[move_number] = this.game.raw_annotations.shift(); @@ -293,7 +321,7 @@ jQuery.eachWithContext = function(context, object, callback) { this.game.moves[move_number] = move; // console.log("Processing move: " + move_number + '.' + move); - var player = (move_number % 2 == 0) ? 'w' : 'b'; + var player = ((move_number + first_move) % 2 == 0) ? 'w' : 'b'; // If the move was to castle if ( this.patterns.castle_queenside.test(move) ) {