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)
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.
-
#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.
42 43 44 45 46 |
# File 'lib/chess/pgn.rb', line 42 def initialize(filename = nil, check_moves: false) self.load(filename, check_moves: check_moves) if filename @date = '??' @round = '1' end |
Instance Attribute Details
#black ⇒ String
The player of the black pieces, same format as White.
28 29 30 |
# File 'lib/chess/pgn.rb', line 28 def black @black end |
#date ⇒ String
The starting date of the game, in YYYY.MM.DD form. ?? is used for unknown values.
19 20 21 |
# File 'lib/chess/pgn.rb', line 19 def date @date end |
#event ⇒ String
The name of the tournament or match event.
11 12 13 |
# File 'lib/chess/pgn.rb', line 11 def event @event end |
#moves ⇒ Array<String>
The array with all moves done in short algebraic chess notation.
36 37 38 |
# File 'lib/chess/pgn.rb', line 36 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).
33 34 35 |
# File 'lib/chess/pgn.rb', line 33 def result @result end |
#round ⇒ String
The playing round ordinal of the game within the event.
22 23 24 |
# File 'lib/chess/pgn.rb', line 22 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.
16 17 18 |
# File 'lib/chess/pgn.rb', line 16 def site @site end |
#white ⇒ String
The player of the white pieces, in Lastname, Firstname format.
25 26 27 |
# File 'lib/chess/pgn.rb', line 25 def white @white end |
Instance Method Details
#load(filename, check_moves: false) ⇒ Pgn
Load a PGN from file.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/chess/pgn.rb', line 54 def load(filename, check_moves: false) data = File.open(filename, 'r').read data.gsub!(/\{.*?\}/, '') # remove comments TAGS.each do |t| instance_variable_set("@#{t}", data.match(/^\[#{t.capitalize} ".*"\]\s?$/).to_s.strip[t.size+3..-3]) end @result = '1/2-1/2' if @result == '1/2' game_index = data.index(/^1\./) raise Chess::InvalidPgnFormatError.new(filename) if game_index.nil? game = data[game_index..-1].strip @moves = game.gsub("\n", ' ').split(/\d+\./).collect{|t| t.strip}[1..-1].collect{|t| t.split(' ')}.flatten @moves.delete_at(@moves.size-1) if @moves.last =~ /(0-1)|(1-0)|(1\/2)|(1\/2-1\/2)|(\*)/ @moves.each do |m| if m !~ MOVE_REGEXP && m !~ SHORT_CASTLING_REGEXP && m !~ LONG_CASTLING_REGEXP raise Chess::InvalidPgnFormatError.new(filename) end end Chess::Game.new(@moves) if check_moves return self end |
#to_s ⇒ Object
PGN to string.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/chess/pgn.rb', line 76 def to_s s = '' TAGS.each do |t| tag = 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 % 2 == 0 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.
94 95 96 |
# File 'lib/chess/pgn.rb', line 94 def write(filename) File.open(filename, 'w') { |f| f.write(self.to_s) } end |