Class: Chess::Pgn
- Inherits:
-
Object
- Object
- Chess::Pgn
- Defined in:
- lib/chess/pgn.rb
Overview
Rappresents a game in PGN (Portable Game Notation) format.
Constant Summary collapse
- TAGS =
Array that include PGN standard tags.
%w[event site date round white black result].freeze
Instance Attribute Summary collapse
-
#black ⇒ String
The player of the black pieces, same format as White.
-
#date ⇒ String
The starting date of the game, in YYYY.MM.DD form.
-
#event ⇒ String
The name of the tournament or match event.
-
#moves ⇒ Array<String>
The array with all moves done in short algebraic chess notation.
-
#result ⇒ String
The result of the game.
-
#round ⇒ String
The playing round ordinal of the game within the event.
-
#site ⇒ String
The location of the event.
-
#white ⇒ String
The player of the white pieces, in Lastname, Firstname format.
Instance Method Summary collapse
-
#initialize(filename = nil, check_moves: false) ⇒ Pgn
constructor
A new instance of Pgn.
-
#load(filename, check_moves: false) ⇒ Pgn
Load a PGN from file.
-
#load_from_string(str, check_moves: false) ⇒ Pgn
Load a PGN from string.
-
#to_s ⇒ Object
PGN to string.
-
#write(filename) ⇒ Object
Write PGN to file.
Constructor Details
#initialize(filename = nil, check_moves: false) ⇒ Pgn
Returns a new instance of Pgn.
40 41 42 43 |
# File 'lib/chess/pgn.rb', line 40 def initialize(filename = nil, check_moves: false) self.load(filename, check_moves: check_moves) if filename @date = '??' end |
Instance Attribute Details
#black ⇒ String
The player of the black pieces, same format as White.
26 27 28 |
# File 'lib/chess/pgn.rb', line 26 def black @black end |
#date ⇒ String
The starting date of the game, in YYYY.MM.DD form. ?? is used for unknown values.
17 18 19 |
# File 'lib/chess/pgn.rb', line 17 def date @date end |
#event ⇒ String
The name of the tournament or match event.
9 10 11 |
# File 'lib/chess/pgn.rb', line 9 def event @event end |
#moves ⇒ Array<String>
The array with all moves done in short algebraic chess notation.
34 35 36 |
# File 'lib/chess/pgn.rb', line 34 def moves @moves end |
#result ⇒ String
The result of the game. This can only have four possible values: 1-0 (White won), 0-1 (Black won), 1/2-1/2 (Draw), or * (other, e.g., the game is ongoing).
31 32 33 |
# File 'lib/chess/pgn.rb', line 31 def result @result end |
#round ⇒ String
The playing round ordinal of the game within the event.
20 21 22 |
# File 'lib/chess/pgn.rb', line 20 def round @round end |
#site ⇒ String
The location of the event. This is in City, Region COUNTRY format, where COUNTRY is the three-letter International Olympic Committee code for the country.
14 15 16 |
# File 'lib/chess/pgn.rb', line 14 def site @site end |
#white ⇒ String
The player of the white pieces, in Lastname, Firstname format.
23 24 25 |
# File 'lib/chess/pgn.rb', line 23 def white @white end |
Instance Method Details
#load(filename, check_moves: false) ⇒ Pgn
Load a PGN from file.
51 52 53 54 |
# File 'lib/chess/pgn.rb', line 51 def load(filename, check_moves: false) str = File.read(filename) load_from_string(str, check_moves: check_moves) end |
#load_from_string(str, check_moves: false) ⇒ Pgn
Load a PGN from string.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/chess/pgn.rb', line 62 def load_from_string(str, check_moves: false) str.gsub!(/\{.*?\}/, '') # remove comments TAGS.each do |t| instance_variable_set(:"@#{t}", str.match(/^\[#{t.capitalize} ".*"\]\s?$/).to_s.strip[t.size + 3..-3]) end @result = '1/2-1/2' if @result == '1/2' game_index = str.index(/^1\./) raise Chess::InvalidPgnFormatError.new if game_index.nil? game = str[game_index..-1].strip @moves = game.tr("\n", ' ').split(/\d+\./).collect(&:strip)[1..-1].map(&:split).flatten @moves.delete_at(@moves.size - 1) if @moves.last.match?(/(0-1)|(1-0)|(1\/2)|(1\/2-1\/2)|(\*)/) @moves.each do |m| raise Chess::InvalidPgnFormatError.new if m !~ MOVE_REGEXP && m !~ SHORT_CASTLING_REGEXP && m !~ LONG_CASTLING_REGEXP end Chess::Game.new(@moves) if check_moves return self end |
#to_s ⇒ Object
PGN to string.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/chess/pgn.rb', line 82 def to_s s = '' TAGS.each do |t| tag = instance_variable_defined?(:"@#{t}") ? instance_variable_get(:"@#{t}") : '' s << "[#{t.capitalize} \"#{tag}\"]\n" end s << "\n" m = '' @moves.each_with_index do |move, i| m << "#{(i / 2) + 1}. " if i.even? m << "#{move} " end m << @result unless @result.nil? s << m.gsub(/(.{1,78})(?: +|$)\n?|(.{78})/, "\\1\\2\n") end |
#write(filename) ⇒ Object
Write PGN to file.
100 101 102 |
# File 'lib/chess/pgn.rb', line 100 def write(filename) File.write(filename, self.to_s) end |