Class: PGN::Game
- Inherits:
-
Object
- Object
- PGN::Game
- Defined in:
- lib/pgn/game.rb
Overview
Constant Summary collapse
- LEFT =
%r{(a|\x1B\[D)\z}
- RIGHT =
%r{(d|\x1B\[C)\z}
- EXIT =
%r{(q|\x03)\z}
Instance Attribute Summary collapse
-
#moves ⇒ Array<String>
A list of the moves in standard algebraic notation.
-
#result ⇒ String
The outcome of the game.
-
#tags ⇒ Hash<String, String>
Metadata about the game.
Instance Method Summary collapse
-
#fen_list ⇒ Array<String>
List of the fen representations of the positions.
-
#initialize(moves, tags = nil, result = nil) ⇒ Game
constructor
A new instance of Game.
-
#play ⇒ Object
Interactively step through the game.
-
#positions ⇒ Array<PGN::Position>
List of the Positions in the game.
- #starting_position ⇒ Object
Constructor Details
#initialize(moves, tags = nil, result = nil) ⇒ Game
Returns a new instance of Game.
63 64 65 66 67 |
# File 'lib/pgn/game.rb', line 63 def initialize(moves, = nil, result = nil) self.moves = moves self. = self.result = result end |
Instance Attribute Details
#moves ⇒ Array<String>
Returns a list of the moves in standard algebraic notation.
52 53 54 55 56 57 58 59 60 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 |
# File 'lib/pgn/game.rb', line 52 class Game attr_accessor :tags, :moves, :result LEFT = %r{(a|\x1B\[D)\z} RIGHT = %r{(d|\x1B\[C)\z} EXIT = %r{(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, = nil, result = nil) self.moves = moves self. = self.result = result 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 do |m| if m.is_a? String MoveText.new(m.gsub("0", "O")) else MoveText.new(m.notation.gsub("0", "O"), m.annotation, m.comment, m.variations) end end end def starting_position @starting_position ||= if fen = (self. && self.['FEN']) PGN::FEN.new(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 ||= begin position = starting_position arr = [position] self.moves.each do |move| new_pos = position.move(move.notation) arr << new_pos position = new_pos end arr end end # @return [Array<String>] list of the fen representations of the positions # def fen_list self.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 self.positions[index].inspect hist[0..2] = (hist[1..2] << STDIN.getch) case hist.join when LEFT index -= 1 if index > 0 when RIGHT index += 1 if index < self.moves.length when EXIT break end end end end |
#result ⇒ String
Returns the outcome of the game.
52 53 54 55 56 57 58 59 60 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 |
# File 'lib/pgn/game.rb', line 52 class Game attr_accessor :tags, :moves, :result LEFT = %r{(a|\x1B\[D)\z} RIGHT = %r{(d|\x1B\[C)\z} EXIT = %r{(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, = nil, result = nil) self.moves = moves self. = self.result = result 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 do |m| if m.is_a? String MoveText.new(m.gsub("0", "O")) else MoveText.new(m.notation.gsub("0", "O"), m.annotation, m.comment, m.variations) end end end def starting_position @starting_position ||= if fen = (self. && self.['FEN']) PGN::FEN.new(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 ||= begin position = starting_position arr = [position] self.moves.each do |move| new_pos = position.move(move.notation) arr << new_pos position = new_pos end arr end end # @return [Array<String>] list of the fen representations of the positions # def fen_list self.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 self.positions[index].inspect hist[0..2] = (hist[1..2] << STDIN.getch) case hist.join when LEFT index -= 1 if index > 0 when RIGHT index += 1 if index < self.moves.length when EXIT break end end end end |
#tags ⇒ Hash<String, String>
Returns metadata about the game.
52 53 54 55 56 57 58 59 60 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 |
# File 'lib/pgn/game.rb', line 52 class Game attr_accessor :tags, :moves, :result LEFT = %r{(a|\x1B\[D)\z} RIGHT = %r{(d|\x1B\[C)\z} EXIT = %r{(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, = nil, result = nil) self.moves = moves self. = self.result = result 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 do |m| if m.is_a? String MoveText.new(m.gsub("0", "O")) else MoveText.new(m.notation.gsub("0", "O"), m.annotation, m.comment, m.variations) end end end def starting_position @starting_position ||= if fen = (self. && self.['FEN']) PGN::FEN.new(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 ||= begin position = starting_position arr = [position] self.moves.each do |move| new_pos = position.move(move.notation) arr << new_pos position = new_pos end arr end end # @return [Array<String>] list of the fen representations of the positions # def fen_list self.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 self.positions[index].inspect hist[0..2] = (hist[1..2] << STDIN.getch) case hist.join when LEFT index -= 1 if index > 0 when RIGHT index += 1 if index < self.moves.length when EXIT break end end end end |
Instance Method Details
#fen_list ⇒ Array<String>
Returns list of the fen representations of the positions.
108 109 110 |
# File 'lib/pgn/game.rb', line 108 def fen_list self.positions.map {|p| p.to_fen.inspect } end |
#play ⇒ Object
Interactively step through the game
Use d
to move forward, a
to move backward, and ^C to exit.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/pgn/game.rb', line 116 def play index = 0 hist = Array.new(3, "") loop do puts "\e[H\e[2J" puts self.positions[index].inspect hist[0..2] = (hist[1..2] << STDIN.getch) case hist.join when LEFT index -= 1 if index > 0 when RIGHT index += 1 if index < self.moves.length when EXIT break end end end |
#positions ⇒ Array<PGN::Position>
Returns list of the Positions in the game.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/pgn/game.rb', line 93 def positions @positions ||= begin position = starting_position arr = [position] self.moves.each do |move| new_pos = position.move(move.notation) arr << new_pos position = new_pos end arr end end |