Skip to content

Commit

Permalink
Merge pull request #32 from loloof64/master
Browse files Browse the repository at this point in the history
slightly improved pgn generation
  • Loading branch information
davecom committed Oct 3, 2023
2 parents 517aa40 + e44fc59 commit 45af157
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/chess.dart
Expand Up @@ -1302,17 +1302,23 @@ class Chess {
reversed_history.add(undo_move());
}

var start_move_number = 1;
if (header['FEN'] != null) {
final move_number_string = header['FEN'].split(' ')[5];
start_move_number = int.parse(move_number_string);
}

final moves = <String?>[];
var move_string = '';
var pgn_move_number = 1;
var pgn_move_number = start_move_number;

/* build the list of moves. a move_string looks like: "3. e3 e6" */
while (reversed_history.isNotEmpty) {
final move = reversed_history.removeLast()!;

/* if the position started with black to move, start PGN with 1. ... */
if (pgn_move_number == 1 && move.color == BLACK) {
move_string = '1. ...';
/* if the position started with black to move, start PGN with ${start_move_number}. ... */
if (pgn_move_number == start_move_number && move.color == BLACK) {
move_string = '$start_move_number. ...';
pgn_move_number++;
} else if (move.color == WHITE) {
/* store the previous generated move_string if we have one */
Expand Down
27 changes: 27 additions & 0 deletions test/tests.dart
Expand Up @@ -896,6 +896,33 @@ void main() {
'[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 1-0',
'fen': '4q2k/2r1r3/4PR1p/p1p5/P1Bp1Q1P/1P6/6P1/6K1 b - - 4 41'
},
{
'moves': [
'e7+',
'Ke8',
'Ke6'
],
'header': [],
'max_width': 80,
'starting_position': '3k4/8/3KP3/8/8/8/8/8 w - - 0 42',
'pgn':
'[SetUp "1"]\n[FEN "3k4/8/3KP3/8/8/8/8/8 w - - 0 42"]\n\n42. e7+ Ke8 43. Ke6',
'fen': '4k3/4P3/4K3/8/8/8/8/8 b - - 2 43'
},
{
'moves': [
'Ke8',
'e7',
'Kf7',
'Kd7'
],
'header': [],
'max_width': 80,
'starting_position': '3k4/8/3KP3/8/8/8/8/8 b - - 0 39',
'pgn':
'[SetUp "1"]\n[FEN "3k4/8/3KP3/8/8/8/8/8 b - - 0 39"]\n\n39. ... Ke8 40. e7 Kf7 41. Kd7',
'fen': '8/3KPk2/8/8/8/8/8/8 b - - 2 41'
},
{
'moves': [
'f3',
Expand Down

0 comments on commit 45af157

Please sign in to comment.