Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,56 @@ <h1>9) Middle Game Demo</h1>
Bc5+ 28. Qf2 Qc1+ 29. Ne1 Qxe1# 0-1
</pre>
</div>

<div id="game10">
<h1>10) PGN with FEN 1</h1>
<div>
<a class="back" href="#">Back</a>
<a class="next" href="#">Next</a>
<a class="flip" href="#">Flip</a>
</div>
<div id="board10" class="board"></div>
<p class="annot"></p>
<pre id="pgn-with-fen-1">
[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
</pre>
</div>
<div id="game11">
<h1>11) PGN with FEN 2</h1>
<div>
<a class="back" href="#">Back</a>
<a class="next" href="#">Next</a>
<a class="flip" href="#">Flip</a>
</div>
<div id="board11" class="board"></div>
<p class="annot"></p>
<pre id="pgn-with-fen-2">
[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
</pre>
</div>

</div>
</body>
</html>
3 changes: 2 additions & 1 deletion javascripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() } );
});
36 changes: 32 additions & 4 deletions javascripts/jchess-0.1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand Down Expand Up @@ -269,21 +274,44 @@ 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"), '');
this.game.body = this.game.body.replace(/^\d+\.+/, '');
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();
Expand All @@ -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) ) {
Expand Down