Class: PGN::FEN
- Inherits:
-
Object
- Object
- PGN::FEN
- Defined in:
- lib/pgn/fen.rb
Overview
FEN is responsible for translating between strings in FEN notation and an internal representation of the board.
Constant Summary collapse
- INITIAL =
The FEN string representing the starting position in chess
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
Instance Attribute Summary collapse
-
#active ⇒ 'w', 't'
The current player.
-
#board ⇒ PGN::Board
A Board object for the current board state.
-
#castling ⇒ String
The castling availability.
-
#en_passant ⇒ String
The current en passant square.
-
#fullmove ⇒ String
The fullmove counter.
-
#halfmove ⇒ String
The halfmove clock.
Class Method Summary collapse
-
.from_attributes(attrs) ⇒ PGN::FEN
A FEN object with the given attributes.
-
.start ⇒ PGN::FEN
A FEN object representing the starting position.
Instance Method Summary collapse
-
#board_string ⇒ String
The fen representation of the board.
- #board_string=(board_fen) ⇒ Object
-
#initialize(fen_string = nil) ⇒ FEN
constructor
A new instance of FEN.
- #inspect ⇒ Object
-
#to_position ⇒ PGN::Position
A Position representing the current position.
-
#to_s ⇒ String
The FEN string.
Constructor Details
#initialize(fen_string = nil) ⇒ FEN
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pgn/fen.rb', line 63 def initialize(fen_string = nil) if fen_string self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove = fen_string.split end end |
Instance Attribute Details
#active ⇒ 'w', 't'
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pgn/fen.rb', line 37 class FEN # The FEN string representing the starting position in chess # INITIAL = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" attr_accessor :board, :active, :castling, :en_passant, :halfmove, :fullmove # @return [PGN::FEN] a {PGN::FEN} object representing the starting # position # def self.start PGN::FEN.new(INITIAL) end # @return [PGN::FEN] a {PGN::FEN} object with the given attributes # def self.from_attributes(attrs) fen = PGN::FEN.new attrs.each do |key, val| fen.send("#{key}=", val) end fen end # @param fen_string [String] a string in Forsyth-Edwards Notation # def initialize(fen_string = nil) if fen_string self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove = fen_string.split end end def en_passant=(val) @en_passant = val.nil? ? "-" : val end def castling=(val) @castling = (val.nil? || val.empty?) ? "-" : val end # @param board_fen [String] the fen representation of the board # @example # fen.board_string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string=(board_fen) squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i } .split("/") .map {|row| row.split('') } .map {|row| row.map {|e| e == "_" ? nil : e } } .reverse .transpose self.board = PGN::Board.new(squares) end # @return [String] the fen representation of the board # @example # PGN::FEN.start.board_string #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string self.board .squares .transpose .reverse .map {|row| row.map {|e| e.nil? ? "_" : e } } .map {|row| row.join } .join("/") .gsub(/_+/) {|match| match.length } end # @return [PGN::Position] a {PGN::Position} representing the current # position # def to_position player = self.active == 'w' ? :white : :black castling = self.castling.split('') - ['-'] en_passant = self.en_passant == '-' ? nil : en_passant PGN::Position.new( self.board, player, castling, en_passant, self.halfmove.to_i, self.fullmove.to_i, ) end # @return [String] the FEN string # @example # PGN::FEN.start.to_s #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" # def to_s [ self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove, ].join(" ") end def inspect self.to_s end end |
#board ⇒ PGN::Board
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pgn/fen.rb', line 37 class FEN # The FEN string representing the starting position in chess # INITIAL = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" attr_accessor :board, :active, :castling, :en_passant, :halfmove, :fullmove # @return [PGN::FEN] a {PGN::FEN} object representing the starting # position # def self.start PGN::FEN.new(INITIAL) end # @return [PGN::FEN] a {PGN::FEN} object with the given attributes # def self.from_attributes(attrs) fen = PGN::FEN.new attrs.each do |key, val| fen.send("#{key}=", val) end fen end # @param fen_string [String] a string in Forsyth-Edwards Notation # def initialize(fen_string = nil) if fen_string self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove = fen_string.split end end def en_passant=(val) @en_passant = val.nil? ? "-" : val end def castling=(val) @castling = (val.nil? || val.empty?) ? "-" : val end # @param board_fen [String] the fen representation of the board # @example # fen.board_string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string=(board_fen) squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i } .split("/") .map {|row| row.split('') } .map {|row| row.map {|e| e == "_" ? nil : e } } .reverse .transpose self.board = PGN::Board.new(squares) end # @return [String] the fen representation of the board # @example # PGN::FEN.start.board_string #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string self.board .squares .transpose .reverse .map {|row| row.map {|e| e.nil? ? "_" : e } } .map {|row| row.join } .join("/") .gsub(/_+/) {|match| match.length } end # @return [PGN::Position] a {PGN::Position} representing the current # position # def to_position player = self.active == 'w' ? :white : :black castling = self.castling.split('') - ['-'] en_passant = self.en_passant == '-' ? nil : en_passant PGN::Position.new( self.board, player, castling, en_passant, self.halfmove.to_i, self.fullmove.to_i, ) end # @return [String] the FEN string # @example # PGN::FEN.start.to_s #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" # def to_s [ self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove, ].join(" ") end def inspect self.to_s end end |
#castling ⇒ String
Returns the castling availability.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pgn/fen.rb', line 37 class FEN # The FEN string representing the starting position in chess # INITIAL = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" attr_accessor :board, :active, :castling, :en_passant, :halfmove, :fullmove # @return [PGN::FEN] a {PGN::FEN} object representing the starting # position # def self.start PGN::FEN.new(INITIAL) end # @return [PGN::FEN] a {PGN::FEN} object with the given attributes # def self.from_attributes(attrs) fen = PGN::FEN.new attrs.each do |key, val| fen.send("#{key}=", val) end fen end # @param fen_string [String] a string in Forsyth-Edwards Notation # def initialize(fen_string = nil) if fen_string self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove = fen_string.split end end def en_passant=(val) @en_passant = val.nil? ? "-" : val end def castling=(val) @castling = (val.nil? || val.empty?) ? "-" : val end # @param board_fen [String] the fen representation of the board # @example # fen.board_string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string=(board_fen) squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i } .split("/") .map {|row| row.split('') } .map {|row| row.map {|e| e == "_" ? nil : e } } .reverse .transpose self.board = PGN::Board.new(squares) end # @return [String] the fen representation of the board # @example # PGN::FEN.start.board_string #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string self.board .squares .transpose .reverse .map {|row| row.map {|e| e.nil? ? "_" : e } } .map {|row| row.join } .join("/") .gsub(/_+/) {|match| match.length } end # @return [PGN::Position] a {PGN::Position} representing the current # position # def to_position player = self.active == 'w' ? :white : :black castling = self.castling.split('') - ['-'] en_passant = self.en_passant == '-' ? nil : en_passant PGN::Position.new( self.board, player, castling, en_passant, self.halfmove.to_i, self.fullmove.to_i, ) end # @return [String] the FEN string # @example # PGN::FEN.start.to_s #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" # def to_s [ self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove, ].join(" ") end def inspect self.to_s end end |
#en_passant ⇒ String
Returns the current en passant square.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pgn/fen.rb', line 37 class FEN # The FEN string representing the starting position in chess # INITIAL = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" attr_accessor :board, :active, :castling, :en_passant, :halfmove, :fullmove # @return [PGN::FEN] a {PGN::FEN} object representing the starting # position # def self.start PGN::FEN.new(INITIAL) end # @return [PGN::FEN] a {PGN::FEN} object with the given attributes # def self.from_attributes(attrs) fen = PGN::FEN.new attrs.each do |key, val| fen.send("#{key}=", val) end fen end # @param fen_string [String] a string in Forsyth-Edwards Notation # def initialize(fen_string = nil) if fen_string self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove = fen_string.split end end def en_passant=(val) @en_passant = val.nil? ? "-" : val end def castling=(val) @castling = (val.nil? || val.empty?) ? "-" : val end # @param board_fen [String] the fen representation of the board # @example # fen.board_string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string=(board_fen) squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i } .split("/") .map {|row| row.split('') } .map {|row| row.map {|e| e == "_" ? nil : e } } .reverse .transpose self.board = PGN::Board.new(squares) end # @return [String] the fen representation of the board # @example # PGN::FEN.start.board_string #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string self.board .squares .transpose .reverse .map {|row| row.map {|e| e.nil? ? "_" : e } } .map {|row| row.join } .join("/") .gsub(/_+/) {|match| match.length } end # @return [PGN::Position] a {PGN::Position} representing the current # position # def to_position player = self.active == 'w' ? :white : :black castling = self.castling.split('') - ['-'] en_passant = self.en_passant == '-' ? nil : en_passant PGN::Position.new( self.board, player, castling, en_passant, self.halfmove.to_i, self.fullmove.to_i, ) end # @return [String] the FEN string # @example # PGN::FEN.start.to_s #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" # def to_s [ self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove, ].join(" ") end def inspect self.to_s end end |
#fullmove ⇒ String
The number of full moves. This is incremented after black plays.
Returns the fullmove counter.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pgn/fen.rb', line 37 class FEN # The FEN string representing the starting position in chess # INITIAL = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" attr_accessor :board, :active, :castling, :en_passant, :halfmove, :fullmove # @return [PGN::FEN] a {PGN::FEN} object representing the starting # position # def self.start PGN::FEN.new(INITIAL) end # @return [PGN::FEN] a {PGN::FEN} object with the given attributes # def self.from_attributes(attrs) fen = PGN::FEN.new attrs.each do |key, val| fen.send("#{key}=", val) end fen end # @param fen_string [String] a string in Forsyth-Edwards Notation # def initialize(fen_string = nil) if fen_string self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove = fen_string.split end end def en_passant=(val) @en_passant = val.nil? ? "-" : val end def castling=(val) @castling = (val.nil? || val.empty?) ? "-" : val end # @param board_fen [String] the fen representation of the board # @example # fen.board_string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string=(board_fen) squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i } .split("/") .map {|row| row.split('') } .map {|row| row.map {|e| e == "_" ? nil : e } } .reverse .transpose self.board = PGN::Board.new(squares) end # @return [String] the fen representation of the board # @example # PGN::FEN.start.board_string #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string self.board .squares .transpose .reverse .map {|row| row.map {|e| e.nil? ? "_" : e } } .map {|row| row.join } .join("/") .gsub(/_+/) {|match| match.length } end # @return [PGN::Position] a {PGN::Position} representing the current # position # def to_position player = self.active == 'w' ? :white : :black castling = self.castling.split('') - ['-'] en_passant = self.en_passant == '-' ? nil : en_passant PGN::Position.new( self.board, player, castling, en_passant, self.halfmove.to_i, self.fullmove.to_i, ) end # @return [String] the FEN string # @example # PGN::FEN.start.to_s #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" # def to_s [ self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove, ].join(" ") end def inspect self.to_s end end |
#halfmove ⇒ String
This is the number of halfmoves since the last pawn advance or capture
Returns the halfmove clock.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pgn/fen.rb', line 37 class FEN # The FEN string representing the starting position in chess # INITIAL = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" attr_accessor :board, :active, :castling, :en_passant, :halfmove, :fullmove # @return [PGN::FEN] a {PGN::FEN} object representing the starting # position # def self.start PGN::FEN.new(INITIAL) end # @return [PGN::FEN] a {PGN::FEN} object with the given attributes # def self.from_attributes(attrs) fen = PGN::FEN.new attrs.each do |key, val| fen.send("#{key}=", val) end fen end # @param fen_string [String] a string in Forsyth-Edwards Notation # def initialize(fen_string = nil) if fen_string self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove = fen_string.split end end def en_passant=(val) @en_passant = val.nil? ? "-" : val end def castling=(val) @castling = (val.nil? || val.empty?) ? "-" : val end # @param board_fen [String] the fen representation of the board # @example # fen.board_string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string=(board_fen) squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i } .split("/") .map {|row| row.split('') } .map {|row| row.map {|e| e == "_" ? nil : e } } .reverse .transpose self.board = PGN::Board.new(squares) end # @return [String] the fen representation of the board # @example # PGN::FEN.start.board_string #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" # def board_string self.board .squares .transpose .reverse .map {|row| row.map {|e| e.nil? ? "_" : e } } .map {|row| row.join } .join("/") .gsub(/_+/) {|match| match.length } end # @return [PGN::Position] a {PGN::Position} representing the current # position # def to_position player = self.active == 'w' ? :white : :black castling = self.castling.split('') - ['-'] en_passant = self.en_passant == '-' ? nil : en_passant PGN::Position.new( self.board, player, castling, en_passant, self.halfmove.to_i, self.fullmove.to_i, ) end # @return [String] the FEN string # @example # PGN::FEN.start.to_s #=> "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" # def to_s [ self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove, ].join(" ") end def inspect self.to_s end end |
Class Method Details
.from_attributes(attrs) ⇒ PGN::FEN
53 54 55 56 57 58 59 |
# File 'lib/pgn/fen.rb', line 53 def self.from_attributes(attrs) fen = PGN::FEN.new attrs.each do |key, val| fen.send("#{key}=", val) end fen end |
.start ⇒ PGN::FEN
47 48 49 |
# File 'lib/pgn/fen.rb', line 47 def self.start PGN::FEN.new(INITIAL) end |
Instance Method Details
#board_string ⇒ String
Returns the fen representation of the board.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/pgn/fen.rb', line 100 def board_string self.board .squares .transpose .reverse .map {|row| row.map {|e| e.nil? ? "_" : e } } .map {|row| row.join } .join("/") .gsub(/_+/) {|match| match.length } end |
#board_string=(board_fen) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/pgn/fen.rb', line 86 def board_string=(board_fen) squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i } .split("/") .map {|row| row.split('') } .map {|row| row.map {|e| e == "_" ? nil : e } } .reverse .transpose self.board = PGN::Board.new(squares) end |
#inspect ⇒ Object
144 145 146 |
# File 'lib/pgn/fen.rb', line 144 def inspect self.to_s end |
#to_position ⇒ PGN::Position
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/pgn/fen.rb', line 114 def to_position player = self.active == 'w' ? :white : :black castling = self.castling.split('') - ['-'] en_passant = self.en_passant == '-' ? nil : en_passant PGN::Position.new( self.board, player, castling, en_passant, self.halfmove.to_i, self.fullmove.to_i, ) end |
#to_s ⇒ String
Returns the FEN string.
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/pgn/fen.rb', line 133 def to_s [ self.board_string, self.active, self.castling, self.en_passant, self.halfmove, self.fullmove, ].join(" ") end |