Class: PGN::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/pgn/game.rb

Overview

Game holds all of the information about a game. It is either the result of parsing a PGN file, or created by hand.

A Game has an interactive #play method, and can also return a list of positions in Position format or FEN.

Constant Summary collapse

LEFT =
/(a|\x1B\[D)\z/
RIGHT =
/(d|\x1B\[C)\z/
EXIT =
/(q|\x03)\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(moves, tags = nil, result = nil, pgn = nil, comment = nil) ⇒ Game

Returns a new instance of Game.

Parameters:

  • moves (Array<String>)

    a list of moves in SAN

  • tags (Hash<String, String>) (defaults to: nil)

    metadata about the game

  • result (String) (defaults to: nil)

    the outcome of the game



73
74
75
76
77
78
79
# File 'lib/pgn/game.rb', line 73

def initialize(moves, tags = nil, result = nil, pgn = nil, comment = nil)
  self.moves   = moves
  self.tags    = tags
  self.result  = result
  self.pgn     = pgn
  self.comment = comment
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



62
63
64
# File 'lib/pgn/game.rb', line 62

def comment
  @comment
end

#movesArray<String>

Returns a list of the moves in standard algebraic notation.

Examples:

game.moves #=> ["e4", "c5", "Nf3", "d6", "d4", "cxd4"]

Returns:

  • (Array<String>)

    a list of the moves in standard algebraic notation



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pgn/game.rb', line 61

class Game
  attr_accessor :tags, :result, :pgn, :comment
  attr_reader :moves

  LEFT  = /(a|\x1B\[D)\z/
  RIGHT = /(d|\x1B\[C)\z/
  EXIT  = /(q|\x03)\z/

  # @param moves [Array<String>] a list of moves in SAN
  # @param tags [Hash<String, String>] metadata about the game
  # @param result [String] the outcome of the game
  #
  def initialize(moves, tags = nil, result = nil, pgn = nil, comment = nil)
    self.moves   = moves
    self.tags    = tags
    self.result  = result
    self.pgn     = pgn
    self.comment = comment
  end

  # @param moves [Array<String>] a list of moves in SAN
  #
  # Standardize castling moves to use O's instead of 0's
  #
  def moves=(moves)
    @moves = moves.map { |m| standardize_castling(m) }
  end

  # @return [String] a canonical PGN string for this game, ending with a
  #   trailing newline.
  #
  def to_pgn
    PGN::Serializer.new(self).to_s
  end

  def initial_fen
    tags && tags['FEN']
  end

  def starting_position
    @starting_position ||= if initial_fen
                             PGN::FEN.new(initial_fen).to_position
                           else
                             PGN::Position.start
                           end
  end

  # @return [Array<PGN::Position>] list of the {PGN::Position}s in the game
  #
  def positions
    @positions ||= each_position.to_a
  end

  # @return [Enumerator, self] with a block: yields each {PGN::Position}
  #   in order (starting position, then one per move) and returns self.
  #   Without a block: returns an Enumerator that yields the same.
  #
  # The replay loop is shared with {#positions} so eager and lazy paths
  # produce identical position objects in identical order.
  def each_position
    return enum_for(:each_position) unless block_given?

    position = starting_position
    yield position
    moves.each do |move|
      position = position.move(move.notation)
      yield position
    end
    self
  end

  # @return [Array<String>] list of the fen representations of the positions
  #
  def fen_list
    positions.map { |p| p.to_fen.inspect }
  end

  # Interactively step through the game
  #
  # Use +d+ to move forward, +a+ to move backward, and +^C+ to exit.
  #
  def play
    index = 0
    hist = Array.new(3, '')

    loop do
      puts "\e[H\e[2J"
      puts positions[index].inspect
      hist[0..2] = (hist[1..2] << $stdin.getch)

      case hist.join
      when LEFT
        index -= 1 if index.positive?
      when RIGHT
        index += 1 if index < moves.length
      when EXIT
        break
      end
    end
  end

  private

  # A MoveText is reused as-is (no new object) when its notation needs no
  # '0'->'O' fix and its comment has no braces left to clean; a braced
  # comment still needs MoveText.new's second clean_text pass to match the
  # legacy whittle byte-output.
  def standardize_castling(entry)
    return MoveText.new(entry.include?('0') ? entry.gsub('0', 'O') : entry) if entry.is_a?(String)

    notation = entry.notation.include?('0') ? entry.notation.gsub('0', 'O') : entry.notation
    return entry if notation.equal?(entry.notation) && (entry.comment.nil? || !entry.comment.include?('{'))

    MoveText.new(notation, entry.annotation, entry.comment, entry.variations)
  end
end

#pgnObject

Returns the value of attribute pgn.



62
63
64
# File 'lib/pgn/game.rb', line 62

def pgn
  @pgn
end

#resultString

Returns the outcome of the game.

Examples:

game.result #=> "1-0"

Returns:

  • (String)

    the outcome of the game



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pgn/game.rb', line 61

class Game
  attr_accessor :tags, :result, :pgn, :comment
  attr_reader :moves

  LEFT  = /(a|\x1B\[D)\z/
  RIGHT = /(d|\x1B\[C)\z/
  EXIT  = /(q|\x03)\z/

  # @param moves [Array<String>] a list of moves in SAN
  # @param tags [Hash<String, String>] metadata about the game
  # @param result [String] the outcome of the game
  #
  def initialize(moves, tags = nil, result = nil, pgn = nil, comment = nil)
    self.moves   = moves
    self.tags    = tags
    self.result  = result
    self.pgn     = pgn
    self.comment = comment
  end

  # @param moves [Array<String>] a list of moves in SAN
  #
  # Standardize castling moves to use O's instead of 0's
  #
  def moves=(moves)
    @moves = moves.map { |m| standardize_castling(m) }
  end

  # @return [String] a canonical PGN string for this game, ending with a
  #   trailing newline.
  #
  def to_pgn
    PGN::Serializer.new(self).to_s
  end

  def initial_fen
    tags && tags['FEN']
  end

  def starting_position
    @starting_position ||= if initial_fen
                             PGN::FEN.new(initial_fen).to_position
                           else
                             PGN::Position.start
                           end
  end

  # @return [Array<PGN::Position>] list of the {PGN::Position}s in the game
  #
  def positions
    @positions ||= each_position.to_a
  end

  # @return [Enumerator, self] with a block: yields each {PGN::Position}
  #   in order (starting position, then one per move) and returns self.
  #   Without a block: returns an Enumerator that yields the same.
  #
  # The replay loop is shared with {#positions} so eager and lazy paths
  # produce identical position objects in identical order.
  def each_position
    return enum_for(:each_position) unless block_given?

    position = starting_position
    yield position
    moves.each do |move|
      position = position.move(move.notation)
      yield position
    end
    self
  end

  # @return [Array<String>] list of the fen representations of the positions
  #
  def fen_list
    positions.map { |p| p.to_fen.inspect }
  end

  # Interactively step through the game
  #
  # Use +d+ to move forward, +a+ to move backward, and +^C+ to exit.
  #
  def play
    index = 0
    hist = Array.new(3, '')

    loop do
      puts "\e[H\e[2J"
      puts positions[index].inspect
      hist[0..2] = (hist[1..2] << $stdin.getch)

      case hist.join
      when LEFT
        index -= 1 if index.positive?
      when RIGHT
        index += 1 if index < moves.length
      when EXIT
        break
      end
    end
  end

  private

  # A MoveText is reused as-is (no new object) when its notation needs no
  # '0'->'O' fix and its comment has no braces left to clean; a braced
  # comment still needs MoveText.new's second clean_text pass to match the
  # legacy whittle byte-output.
  def standardize_castling(entry)
    return MoveText.new(entry.include?('0') ? entry.gsub('0', 'O') : entry) if entry.is_a?(String)

    notation = entry.notation.include?('0') ? entry.notation.gsub('0', 'O') : entry.notation
    return entry if notation.equal?(entry.notation) && (entry.comment.nil? || !entry.comment.include?('{'))

    MoveText.new(notation, entry.annotation, entry.comment, entry.variations)
  end
end

#tagsHash<String, String>

Returns metadata about the game.

Examples:

game.tags #=> {"White" => "Kasparov", "Black" => "Deep Blue"}

Returns:

  • (Hash<String, String>)

    metadata about the game



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pgn/game.rb', line 61

class Game
  attr_accessor :tags, :result, :pgn, :comment
  attr_reader :moves

  LEFT  = /(a|\x1B\[D)\z/
  RIGHT = /(d|\x1B\[C)\z/
  EXIT  = /(q|\x03)\z/

  # @param moves [Array<String>] a list of moves in SAN
  # @param tags [Hash<String, String>] metadata about the game
  # @param result [String] the outcome of the game
  #
  def initialize(moves, tags = nil, result = nil, pgn = nil, comment = nil)
    self.moves   = moves
    self.tags    = tags
    self.result  = result
    self.pgn     = pgn
    self.comment = comment
  end

  # @param moves [Array<String>] a list of moves in SAN
  #
  # Standardize castling moves to use O's instead of 0's
  #
  def moves=(moves)
    @moves = moves.map { |m| standardize_castling(m) }
  end

  # @return [String] a canonical PGN string for this game, ending with a
  #   trailing newline.
  #
  def to_pgn
    PGN::Serializer.new(self).to_s
  end

  def initial_fen
    tags && tags['FEN']
  end

  def starting_position
    @starting_position ||= if initial_fen
                             PGN::FEN.new(initial_fen).to_position
                           else
                             PGN::Position.start
                           end
  end

  # @return [Array<PGN::Position>] list of the {PGN::Position}s in the game
  #
  def positions
    @positions ||= each_position.to_a
  end

  # @return [Enumerator, self] with a block: yields each {PGN::Position}
  #   in order (starting position, then one per move) and returns self.
  #   Without a block: returns an Enumerator that yields the same.
  #
  # The replay loop is shared with {#positions} so eager and lazy paths
  # produce identical position objects in identical order.
  def each_position
    return enum_for(:each_position) unless block_given?

    position = starting_position
    yield position
    moves.each do |move|
      position = position.move(move.notation)
      yield position
    end
    self
  end

  # @return [Array<String>] list of the fen representations of the positions
  #
  def fen_list
    positions.map { |p| p.to_fen.inspect }
  end

  # Interactively step through the game
  #
  # Use +d+ to move forward, +a+ to move backward, and +^C+ to exit.
  #
  def play
    index = 0
    hist = Array.new(3, '')

    loop do
      puts "\e[H\e[2J"
      puts positions[index].inspect
      hist[0..2] = (hist[1..2] << $stdin.getch)

      case hist.join
      when LEFT
        index -= 1 if index.positive?
      when RIGHT
        index += 1 if index < moves.length
      when EXIT
        break
      end
    end
  end

  private

  # A MoveText is reused as-is (no new object) when its notation needs no
  # '0'->'O' fix and its comment has no braces left to clean; a braced
  # comment still needs MoveText.new's second clean_text pass to match the
  # legacy whittle byte-output.
  def standardize_castling(entry)
    return MoveText.new(entry.include?('0') ? entry.gsub('0', 'O') : entry) if entry.is_a?(String)

    notation = entry.notation.include?('0') ? entry.notation.gsub('0', 'O') : entry.notation
    return entry if notation.equal?(entry.notation) && (entry.comment.nil? || !entry.comment.include?('{'))

    MoveText.new(notation, entry.annotation, entry.comment, entry.variations)
  end
end

Instance Method Details

#each_position {|position| ... } ⇒ Enumerator, self

The replay loop is shared with #positions so eager and lazy paths produce identical position objects in identical order.

Yields:

  • (position)

Returns:

  • (Enumerator, self)

    with a block: yields each Position in order (starting position, then one per move) and returns self. Without a block: returns an Enumerator that yields the same.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pgn/game.rb', line 120

def each_position
  return enum_for(:each_position) unless block_given?

  position = starting_position
  yield position
  moves.each do |move|
    position = position.move(move.notation)
    yield position
  end
  self
end

#fen_listArray<String>

Returns list of the fen representations of the positions.

Returns:

  • (Array<String>)

    list of the fen representations of the positions



134
135
136
# File 'lib/pgn/game.rb', line 134

def fen_list
  positions.map { |p| p.to_fen.inspect }
end

#initial_fenObject



96
97
98
# File 'lib/pgn/game.rb', line 96

def initial_fen
  tags && tags['FEN']
end

#playObject

Interactively step through the game

Use d to move forward, a to move backward, and ^C to exit.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/pgn/game.rb', line 142

def play
  index = 0
  hist = Array.new(3, '')

  loop do
    puts "\e[H\e[2J"
    puts positions[index].inspect
    hist[0..2] = (hist[1..2] << $stdin.getch)

    case hist.join
    when LEFT
      index -= 1 if index.positive?
    when RIGHT
      index += 1 if index < moves.length
    when EXIT
      break
    end
  end
end

#positionsArray<PGN::Position>

Returns list of the Positions in the game.

Returns:



110
111
112
# File 'lib/pgn/game.rb', line 110

def positions
  @positions ||= each_position.to_a
end

#starting_positionObject



100
101
102
103
104
105
106
# File 'lib/pgn/game.rb', line 100

def starting_position
  @starting_position ||= if initial_fen
                           PGN::FEN.new(initial_fen).to_position
                         else
                           PGN::Position.start
                         end
end

#to_pgnString

Returns a canonical PGN string for this game, ending with a trailing newline.

Returns:

  • (String)

    a canonical PGN string for this game, ending with a trailing newline.



92
93
94
# File 'lib/pgn/game.rb', line 92

def to_pgn
  PGN::Serializer.new(self).to_s
end