Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fixed those two issues. #5

Merged
merged 1 commit into from

2 participants

@2sb18
  • fixed issue where if no tags were used in PGN, a newline was inserted at start of output.
  • PGN output now indicates if a game has a non-default starting position. Uses SetUp and FEN tags.
  • Added test cases to check changes.
@2sb18 2sb18 - fixed issue where if no tags were used in PGN, a newline was insert…
…ed at start of output.

- PGN output now indicates if a game has a non-default starting position. Uses SetUp and FEN tags.
- Added test cases to check changes.
fe24778
@jhlywa jhlywa merged commit 9889b26 into jhlywa:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 25, 2011
  1. @2sb18

    - fixed issue where if no tags were used in PGN, a newline was insert…

    2sb18 authored
    …ed at start of output.
    
    - PGN output now indicates if a game has a non-default starting position. Uses SetUp and FEN tags.
    - Added test cases to check changes.
This page is out of date. Refresh to see the latest.
Showing with 54 additions and 14 deletions.
  1. +42 −8 chess.js
  2. +12 −6 tests/tests.js
View
50 chess.js
@@ -40,6 +40,8 @@ var Chess = function(fen) {
var KING = 'k';
var SYMBOLS = 'pnbrqkPNBRQK';
+
+ var DEFAULT_POSITION = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
var PAWN_OFFSETS = {
b: [16, 32, 17, 15],
@@ -153,7 +155,7 @@ var Chess = function(fen) {
* starting position
*/
if (typeof fen == 'undefined') {
- load('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
+ load(DEFAULT_POSITION);
} else {
load(fen);
}
@@ -168,11 +170,12 @@ var Chess = function(fen) {
move_number = 1;
history = [];
info = {};
+ update_setup();
}
function reset() {
clear();
- load('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
+ load(DEFAULT_POSITION);
}
function load(fen) {
@@ -225,6 +228,8 @@ var Chess = function(fen) {
ep_square = (tokens[3] == '-') ? EMPTY : SQUARES[tokens[3]];
half_moves = parseInt(tokens[4], 10);
move_number = parseInt(tokens[5], 10);
+
+ update_setup();
return true;
}
@@ -286,12 +291,17 @@ var Chess = function(fen) {
var newline_char = (typeof options_obj === "object" && typeof options_obj.newline_char === "string") ? options_obj.newline_char : "\n";
var max_width = (typeof options_obj === "object" && typeof options_obj.max_width === "number") ? options_obj.max_width : 0;
var current_width = 0;
+ var info_exists = false;
// add the PGN header information
for (var i in info) {
result.push("[" + i + " \"" + info[i] + "\"]" + newline_char);
+ info_exists = true;
+ }
+
+ if (info_exists && history.length) {
+ result.push(newline_char);
}
- result.push(newline_char);
// pop all of history onto reversedHistory
var reversed_history = [];
@@ -301,12 +311,15 @@ var Chess = function(fen) {
// fill the array of move_strings. Example of a move_string: "3. e3 e6"
while(reversed_history.length > 0) {
- if (history.length % 2 === 0) {
- move_string.length ? move_strings.push(move_string) : null;
- move_string = current_move_number + ".";
- current_move_number++;
- }
var current_move = reversed_history.pop();
+ if (current_move.color === 'w') {
+ move_string.length ? move_strings.push(move_string) : null; // done making the last move_string
+ move_string = current_move_number + '.';
+ current_move_number++;
+ } else if (current_move.color === 'b' && current_move_number === 1) {
+ move_string = '1. ...';
+ current_move_number++;
+ }
move_string = move_string + " " + move_to_san(current_move);
make_move(current_move);
}
@@ -342,6 +355,23 @@ var Chess = function(fen) {
}
return info;
}
+
+ // called when the initial board setup is changed with put() or remove()'
+ // modifies the SetUp and FEN properties of the info variable
+ // if the FEN is equal to the default position, the SetUp and FEN are deleted
+ // the setup is only updated if history.length is zero, ie moves haven't been made.
+ function update_setup() {
+ var fen = generate_fen();
+ if (history.length === 0) {
+ if (fen !== DEFAULT_POSITION) {
+ info["SetUp"] = generate_fen();
+ info["FEN"] = '1';
+ } else {
+ delete info["SetUp"];
+ delete info["FEN"];
+ }
+ }
+ }
function get(square) {
var piece = board[SQUARES[square]];
@@ -369,6 +399,8 @@ var Chess = function(fen) {
if (piece.type == KING) {
kings[piece.color] = sq;
}
+
+ update_setup();
return true;
}
@@ -379,6 +411,8 @@ var Chess = function(fen) {
if (piece && piece.type == KING) {
kings[piece.color] = EMPTY;
}
+
+ update_setup();
return piece;
}
View
18 tests/tests.js
@@ -357,7 +357,6 @@ function fen_tests() {
}
function pgn_tests() {
- var chess = new Chess();
var start = new Date;
var passed = true;
var error_message;
@@ -388,13 +387,21 @@ function pgn_tests() {
max_width:65,
pgn: '[Event "Reykjavik WCh"]\n[Site "Reykjavik WCh"]\n[Date "1972.01.07"]\n[EventDate "?"]\n[Round "6"]\n[Result "1-0"]\n[White "Robert James Fischer"]\n[Black "Boris Spassky"]\n[ECO "D59"]\n[WhiteElo "?"]\n[BlackElo "?"]\n[PlyCount "81"]\n\n1. c4 e6 2. Nf3 d5 3. d4 Nf6 4. Nc3 Be7 5. Bg5 O-O 6. e3 h6\n7. Bh4 b6 8. cxd5 Nxd5 9. Bxe7 Qxe7 10. Nxd5 exd5 11. Rc1 Be6\n12. Qa4 c5 13. Qa3 Rc8 14. Bb5 a6 15. dxc5 bxc5 16. O-O Ra7\n17. Be2 Nd7 18. Nd4 Qf8 19. Nxe6 fxe6 20. e4 d4 21. f4 Qe7\n22. e5 Rb8 23. Bc4 Kh8 24. Qh3 Nf8 25. b3 a5 26. f5 exf5\n27. Rxf5 Nh7 28. Rcf1 Qd8 29. Qg3 Re7 30. h4 Rbb7 31. e6 Rbc7\n32. Qe5 Qe8 33. a4 Qd8 34. R1f2 Qe8 35. R2f3 Qd8 36. Bd3 Qe8\n37. Qe4 Nf6 38. Rxf6 gxf6 39. Rxf6 Kg8 40. Bc4 Kh8 41. Qf4',
fen: '4q2k/2r1r3/4PR1p/p1p5/P1Bp1Q1P/1P6/6P1/6K1 b - - 4 41'},
- {moves: ['f3', 'e5', 'g4', 'Qh4#'],
- info: ["White", "child", "Black", "Steve Bragg", "Steve'sComment", "It's not fair"],
+ {moves: ['f3', 'e5', 'g4', 'Qh4#'], // testing max_width being small and having no comments
+ info: [],
max_width:1,
- pgn: '[White "child"]\n[Black "Steve Bragg"]\n[Steve\'sComment "It\'s not fair"]\n\n1. f3 e5\n2. g4 Qh4#',
- fen: 'rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3'}
+ pgn: '1. f3 e5\n2. g4 Qh4#',
+ fen: 'rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3'},
+ {moves: ['Ba5', 'O-O', 'd6', 'd4'], // testing a non-starting position
+ info: [],
+ max_width:20,
+ pgn: '[SetUp "r1bqk1nr/pppp1ppp/2n5/4p3/1bB1P3/2P2N2/P2P1PPP/RNBQK2R b KQkq - 0 1"]\n[FEN "1"]\n\n1. ... Ba5 2. O-O d6\n3. d4',
+ starting_position: 'r1bqk1nr/pppp1ppp/2n5/4p3/1bB1P3/2P2N2/P2P1PPP/RNBQK2R b KQkq - 0 1',
+ fen: 'r1bqk1nr/ppp2ppp/2np4/b3p3/2BPP3/2P2N2/P4PPP/RNBQ1RK1 b kq d3 0 3'}
];
for (var i = 0; i < positions.length; i++) {
+ var chess = ("starting_position" in positions[i]) ? new Chess(positions[i].starting_position) : new Chess();
+ // var chess = (positions.starting_position !== undefined) ? new Chess(positions.starting_position) : new Chess();
passed = true;
error_message = "";
for (var j = 0; j < positions[i].moves.length; j++) {
@@ -410,7 +417,6 @@ function pgn_tests() {
passed = pgn === positions[i].pgn && fen === positions[i].fen;
s += (passed) ? 'PASSED!' : 'FAILED!';
log(s);
- chess.reset();
}
var finish = new Date;
var diff = (finish-start) / 1000;
Something went wrong with that request. Please try again.