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



83
84
85
86
87
88
89
# File 'lib/pgn/game.rb', line 83

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.



72
73
74
# File 'lib/pgn/game.rb', line 72

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



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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/pgn/game.rb', line 71

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

  # The current {PGN::Position} (the last position after replaying all
  # moves), or the starting position when there are no moves.
  #
  # @return [PGN::Position]
  def current_position
    positions.last
  end

  # Append a move in SAN, validating legality when the native engine is
  # available. Raises ArgumentError for an illegal move (when the engine is
  # loaded). Grows the memoized position list in step, if it is populated.
  #
  # @param san [String, PGN::MoveText] the move to append
  # @return [self]
  def push(san)
    move = standardize_castling(san)
    if PGN::Bitboard.const_defined?(:Engine, false) && !current_position.legal?(move.notation)
      raise ArgumentError, "illegal move: #{san}"
    end

    @moves << move
    @positions << @positions.last.move(move.notation) if @positions
    self
  end

  # Remove and return the last move, or nil if there are none. Shrinks the
  # memoized position list in step, if it is populated.
  #
  # @return [PGN::MoveText, nil]
  def pop
    return nil if @moves.empty?

    move = @moves.pop
    @positions&.pop
    move
  end

  # Whether any position has occurred three times in this game (the
  # threefold-repetition draw). Uses {PGN::Position#hash} (the Zobrist
  # hash of the FEN-relevant state) over {#positions}, so a prior or
  # subsequent call that also needs the position list shares the replay.
  #
  # @return [Boolean]
  def threefold?
    counts = Hash.new(0)
    positions.each { |position| counts[position.hash] += 1 }
    counts.any? { |_, count| count >= 3 }
  end

  # The terminal status of the game: :checkmate, :stalemate, or :draw
  # (insufficient material, 50-move rule, or threefold repetition). nil
  # if the game is still in progress. Requires the native extension for
  # checkmate/stalemate detection.
  #
  # @return [Symbol, nil]
  def outcome
    final = positions.last
    result = final&.outcome
    return result if result
    return :draw if threefold?

    nil
  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

  # Build a fresh, navigable {PGN::Node} tree over the mainline. The tree
  # is a live view of the underlying +MoveText+ structure; mutate it
  # through the node API, then call +#root+ again for a fresh tree.
  def root
    PGN::Node.new(
      move: nil, parent: nil, line: @moves, index: -1,
      starting_position: starting_position, game: self
    )
  end

  private

  # A MoveText is reused as-is (no new object) when its notation needs no
  # '0'->'O' fix; otherwise a new MoveText is built. clean_text is idempotent
  # (it only strips a *single* outermost brace pair), so reusing or rebuilding
  # a MoveText never corrupts a comment that still contains inner braces.
  def standardize_castling(entry)
    return MoveText.new(MoveText.normalize_castling(entry)) if entry.is_a?(String)

    notation = MoveText.normalize_castling(entry.notation)
    return entry if notation.equal?(entry.notation)

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

#pgnObject

Returns the value of attribute pgn.



72
73
74
# File 'lib/pgn/game.rb', line 72

def pgn
  @pgn
end

#resultString

Returns the outcome of the game.

Examples:

game.result #=> "1-0"

Returns:

  • (String)

    the outcome of the game



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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/pgn/game.rb', line 71

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

  # The current {PGN::Position} (the last position after replaying all
  # moves), or the starting position when there are no moves.
  #
  # @return [PGN::Position]
  def current_position
    positions.last
  end

  # Append a move in SAN, validating legality when the native engine is
  # available. Raises ArgumentError for an illegal move (when the engine is
  # loaded). Grows the memoized position list in step, if it is populated.
  #
  # @param san [String, PGN::MoveText] the move to append
  # @return [self]
  def push(san)
    move = standardize_castling(san)
    if PGN::Bitboard.const_defined?(:Engine, false) && !current_position.legal?(move.notation)
      raise ArgumentError, "illegal move: #{san}"
    end

    @moves << move
    @positions << @positions.last.move(move.notation) if @positions
    self
  end

  # Remove and return the last move, or nil if there are none. Shrinks the
  # memoized position list in step, if it is populated.
  #
  # @return [PGN::MoveText, nil]
  def pop
    return nil if @moves.empty?

    move = @moves.pop
    @positions&.pop
    move
  end

  # Whether any position has occurred three times in this game (the
  # threefold-repetition draw). Uses {PGN::Position#hash} (the Zobrist
  # hash of the FEN-relevant state) over {#positions}, so a prior or
  # subsequent call that also needs the position list shares the replay.
  #
  # @return [Boolean]
  def threefold?
    counts = Hash.new(0)
    positions.each { |position| counts[position.hash] += 1 }
    counts.any? { |_, count| count >= 3 }
  end

  # The terminal status of the game: :checkmate, :stalemate, or :draw
  # (insufficient material, 50-move rule, or threefold repetition). nil
  # if the game is still in progress. Requires the native extension for
  # checkmate/stalemate detection.
  #
  # @return [Symbol, nil]
  def outcome
    final = positions.last
    result = final&.outcome
    return result if result
    return :draw if threefold?

    nil
  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

  # Build a fresh, navigable {PGN::Node} tree over the mainline. The tree
  # is a live view of the underlying +MoveText+ structure; mutate it
  # through the node API, then call +#root+ again for a fresh tree.
  def root
    PGN::Node.new(
      move: nil, parent: nil, line: @moves, index: -1,
      starting_position: starting_position, game: self
    )
  end

  private

  # A MoveText is reused as-is (no new object) when its notation needs no
  # '0'->'O' fix; otherwise a new MoveText is built. clean_text is idempotent
  # (it only strips a *single* outermost brace pair), so reusing or rebuilding
  # a MoveText never corrupts a comment that still contains inner braces.
  def standardize_castling(entry)
    return MoveText.new(MoveText.normalize_castling(entry)) if entry.is_a?(String)

    notation = MoveText.normalize_castling(entry.notation)
    return entry if notation.equal?(entry.notation)

    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



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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/pgn/game.rb', line 71

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

  # The current {PGN::Position} (the last position after replaying all
  # moves), or the starting position when there are no moves.
  #
  # @return [PGN::Position]
  def current_position
    positions.last
  end

  # Append a move in SAN, validating legality when the native engine is
  # available. Raises ArgumentError for an illegal move (when the engine is
  # loaded). Grows the memoized position list in step, if it is populated.
  #
  # @param san [String, PGN::MoveText] the move to append
  # @return [self]
  def push(san)
    move = standardize_castling(san)
    if PGN::Bitboard.const_defined?(:Engine, false) && !current_position.legal?(move.notation)
      raise ArgumentError, "illegal move: #{san}"
    end

    @moves << move
    @positions << @positions.last.move(move.notation) if @positions
    self
  end

  # Remove and return the last move, or nil if there are none. Shrinks the
  # memoized position list in step, if it is populated.
  #
  # @return [PGN::MoveText, nil]
  def pop
    return nil if @moves.empty?

    move = @moves.pop
    @positions&.pop
    move
  end

  # Whether any position has occurred three times in this game (the
  # threefold-repetition draw). Uses {PGN::Position#hash} (the Zobrist
  # hash of the FEN-relevant state) over {#positions}, so a prior or
  # subsequent call that also needs the position list shares the replay.
  #
  # @return [Boolean]
  def threefold?
    counts = Hash.new(0)
    positions.each { |position| counts[position.hash] += 1 }
    counts.any? { |_, count| count >= 3 }
  end

  # The terminal status of the game: :checkmate, :stalemate, or :draw
  # (insufficient material, 50-move rule, or threefold repetition). nil
  # if the game is still in progress. Requires the native extension for
  # checkmate/stalemate detection.
  #
  # @return [Symbol, nil]
  def outcome
    final = positions.last
    result = final&.outcome
    return result if result
    return :draw if threefold?

    nil
  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

  # Build a fresh, navigable {PGN::Node} tree over the mainline. The tree
  # is a live view of the underlying +MoveText+ structure; mutate it
  # through the node API, then call +#root+ again for a fresh tree.
  def root
    PGN::Node.new(
      move: nil, parent: nil, line: @moves, index: -1,
      starting_position: starting_position, game: self
    )
  end

  private

  # A MoveText is reused as-is (no new object) when its notation needs no
  # '0'->'O' fix; otherwise a new MoveText is built. clean_text is idempotent
  # (it only strips a *single* outermost brace pair), so reusing or rebuilding
  # a MoveText never corrupts a comment that still contains inner braces.
  def standardize_castling(entry)
    return MoveText.new(MoveText.normalize_castling(entry)) if entry.is_a?(String)

    notation = MoveText.normalize_castling(entry.notation)
    return entry if notation.equal?(entry.notation)

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

Instance Method Details

#current_positionPGN::Position

The current Position (the last position after replaying all moves), or the starting position when there are no moves.

Returns:



152
153
154
# File 'lib/pgn/game.rb', line 152

def current_position
  positions.last
end

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



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pgn/game.rb', line 130

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



144
145
146
# File 'lib/pgn/game.rb', line 144

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

#initial_fenObject



106
107
108
# File 'lib/pgn/game.rb', line 106

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

#outcomeSymbol?

The terminal status of the game: :checkmate, :stalemate, or :draw (insufficient material, 50-move rule, or threefold repetition). nil if the game is still in progress. Requires the native extension for checkmate/stalemate detection.

Returns:

  • (Symbol, nil)


203
204
205
206
207
208
209
210
# File 'lib/pgn/game.rb', line 203

def outcome
  final = positions.last
  result = final&.outcome
  return result if result
  return :draw if threefold?

  nil
end

#playObject

Interactively step through the game

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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/pgn/game.rb', line 216

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

#popPGN::MoveText?

Remove and return the last move, or nil if there are none. Shrinks the memoized position list in step, if it is populated.

Returns:



177
178
179
180
181
182
183
# File 'lib/pgn/game.rb', line 177

def pop
  return nil if @moves.empty?

  move = @moves.pop
  @positions&.pop
  move
end

#positionsArray<PGN::Position>

Returns list of the Positions in the game.

Returns:



120
121
122
# File 'lib/pgn/game.rb', line 120

def positions
  @positions ||= each_position.to_a
end

#push(san) ⇒ self

Append a move in SAN, validating legality when the native engine is available. Raises ArgumentError for an illegal move (when the engine is loaded). Grows the memoized position list in step, if it is populated.

Parameters:

Returns:

  • (self)


162
163
164
165
166
167
168
169
170
171
# File 'lib/pgn/game.rb', line 162

def push(san)
  move = standardize_castling(san)
  if PGN::Bitboard.const_defined?(:Engine, false) && !current_position.legal?(move.notation)
    raise ArgumentError, "illegal move: #{san}"
  end

  @moves << move
  @positions << @positions.last.move(move.notation) if @positions
  self
end

#rootObject

Build a fresh, navigable Node tree over the mainline. The tree is a live view of the underlying MoveText structure; mutate it through the node API, then call #root again for a fresh tree.



239
240
241
242
243
244
# File 'lib/pgn/game.rb', line 239

def root
  PGN::Node.new(
    move: nil, parent: nil, line: @moves, index: -1,
    starting_position: starting_position, game: self
  )
end

#starting_positionObject



110
111
112
113
114
115
116
# File 'lib/pgn/game.rb', line 110

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

#threefold?Boolean

Whether any position has occurred three times in this game (the threefold-repetition draw). Uses Position#hash (the Zobrist hash of the FEN-relevant state) over #positions, so a prior or subsequent call that also needs the position list shares the replay.

Returns:

  • (Boolean)


191
192
193
194
195
# File 'lib/pgn/game.rb', line 191

def threefold?
  counts = Hash.new(0)
  positions.each { |position| counts[position.hash] += 1 }
  counts.any? { |_, count| count >= 3 }
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.



102
103
104
# File 'lib/pgn/game.rb', line 102

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