README.rdoc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. = chess-data
  2. Install from {RubyGems}[https://rubygems.org/gems/chess-data/]:
  3. > gem install chess-data
  4. source:: {chess-data.zip}[http://peterlane.info/downloads/chess-data.zip]
  5. == Description
  6. For searching/filtering datasets of chess games.
  7. Features
  8. * Read and save collections of PGN files
  9. * Filter games looking for games containing positions with certain combinations
  10. of pieces
  11. * Save collections of PGN games or individual positions after filtering.
  12. * PGN lexer follows specification in https://www.chessclub.com/help/PGN-spec
  13. * note that the lexer recognises but ignores comments and NAGs, so they will
  14. not appear in any resaved games
  15. == Position Definitions
  16. Games are stored in an instance of +ChessData+::+Database+, and can be filtered using
  17. the +search+ method. The search method takes a block defining the numbers of pieces of
  18. each type and returns a new database containing just those games which match the
  19. definition. For example, the following filters for 5-4 rook endings:
  20. rook_endings_database = database.search do
  21. exactly 1, "R", "r"
  22. exactly 5, "P"
  23. exactly 4, "p"
  24. end
  25. Filters include:
  26. * 'exactly n, *pieces'
  27. * 'at_least n, *pieces'
  28. * 'at_most n, *pieces'
  29. == Example: Extracting all 5-4 R+P endings
  30. # Read database of games and query
  31. require 'chess-data'
  32. # create a database
  33. database = ChessData::Database.new
  34. # read in pgn files provided on command line and add them to the database
  35. ARGV.each do |file|
  36. puts "Reading from #{file}"
  37. database.add_games_from file
  38. end
  39. # extract those games which at some point reach given position definition
  40. selected = database.search do
  41. exactly 1, "R", "r"
  42. exactly 5, "P"
  43. exactly 4, "p"
  44. end
  45. # report and save result
  46. puts "Found #{selected.size} games"
  47. selected.to_file "selected.pgn"
  48. puts "Selected #{selected.size} out of #{database.size}"
  49. == Example: Study a game move-by-move
  50. The ChessData::Game#play_game method takes a block, which is passed the current
  51. board and next move after each half move. The following example prints out the
  52. board position and information to create a trace of the game:
  53. $game.play_game do |board, next_move|
  54. puts board.to_s
  55. puts
  56. puts "Move #{board.fullmove_number}: #{if board.to_move == "w" then "" else "... " end}#{next_move}"
  57. end
  58. Sample output:
  59. Move 32: Rxd7+
  60. ........
  61. ..kR...p
  62. ....pb.Q
  63. ........
  64. ..P..Pq.
  65. .Pn.....
  66. ......P.
  67. ......BK
  68. Move 32: ... Kxd7
  69. ........
  70. ...k...p
  71. ....pb.Q
  72. ........
  73. ..P..Pq.
  74. .Pn.....
  75. ......P.
  76. ......BK
  77. Move 33: Qxf6
  78. ........
  79. ...k...p
  80. ....pQ..
  81. ........
  82. ..P..Pq.
  83. .Pn.....
  84. ......P.
  85. ......BK
  86. Move 33: ... 1/2-1/2