Class: Chess::Pgn

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil, check_moves: false) ⇒ Pgn

Returns a new instance of Pgn.

Parameters:

  • filename (String) (defaults to: nil)

    The path of the PGN file.

  • check_moves (Boolean) (defaults to: false)

    If true check if the moves are legal.

Raises:



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

#blackString

The player of the black pieces, same format as White.

Returns:

  • (String)


28
29
30
# File 'lib/chess/pgn.rb', line 28

def black
  @black
end

#dateString

The starting date of the game, in YYYY.MM.DD form. ?? is used for unknown values.

Returns:

  • (String)


19
20
21
# File 'lib/chess/pgn.rb', line 19

def date
  @date
end

#eventString

The name of the tournament or match event.

Returns:

  • (String)


11
12
13
# File 'lib/chess/pgn.rb', line 11

def event
  @event
end

#movesArray<String>

The array with all moves done in short algebraic chess notation.

Returns:

  • (Array<String>)


36
37
38
# File 'lib/chess/pgn.rb', line 36

def moves
  @moves
end

#resultString

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).

Returns:

  • (String)


33
34
35
# File 'lib/chess/pgn.rb', line 33

def result
  @result
end

#roundString

The playing round ordinal of the game within the event.

Returns:

  • (String)


22
23
24
# File 'lib/chess/pgn.rb', line 22

def round
  @round
end

#siteString

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.

Returns:

  • (String)


16
17
18
# File 'lib/chess/pgn.rb', line 16

def site
  @site
end

#whiteString

The player of the white pieces, in Lastname, Firstname format.

Returns:

  • (String)


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.

Parameters:

  • filename (String)

    The path of the PGN file.

  • check_moves (Boolean) (defaults to: false)

    If true check if the moves are legal.

Returns:

  • (Pgn)

    Returns ‘self`.

Raises:



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_sObject

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.

Parameters:

  • filename (String)

    The path of the PGN 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