Class: PGN::Move
- Inherits:
-
Object
- Object
- PGN::Move
- Defined in:
- lib/pgn/move.rb
Overview
Move knows how to parse a move string in standard algebraic notation to extract all relevant information.
Constant Summary collapse
- SAN_REGEX =
A regular expression for matching moves in standard algebraic notation
/ (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x
Instance Attribute Summary collapse
-
#capture ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
-
#castle ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
-
#check ⇒ String?
Whether the move results in check or mate.
-
#destination ⇒ String?
The destination square of the piece.
-
#disambiguation ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
-
#piece ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
-
#player ⇒ Symbol
The current player.
-
#promotion ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
-
#san ⇒ String
The move string.
Instance Method Summary collapse
-
#black? ⇒ Boolean
Whether it's black's turn.
-
#check? ⇒ Boolean
Whether the move results in check.
-
#checkmate? ⇒ Boolean
Whether the move results in checkmate.
-
#initialize(move, player) ⇒ Move
constructor
A new instance of Move.
-
#pawn? ⇒ Boolean
Whether the piece being moved is a pawn.
-
#white? ⇒ Boolean
Whether it's white's turn.
Constructor Details
#initialize(move, player) ⇒ Move
Returns a new instance of Move.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/pgn/move.rb', line 95 def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end |
Instance Attribute Details
#capture ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
#castle ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
#check ⇒ String?
Returns whether the move results in check or mate.
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
#destination ⇒ String?
Returns the destination square of the piece.
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
#disambiguation ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
#piece ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
#player ⇒ Symbol
Returns the current player.
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
#promotion ⇒ Object
piece/promotion/capture/disambiguation/castle have custom writers below (color normalization, nil-coalescing, etc.), so expose only readers here to avoid redefining attr_accessor setters (Lint/DuplicateMethods).
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
#san ⇒ String
Returns the move string.
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 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 |
# File 'lib/pgn/move.rb', line 59 class Move attr_accessor :san, :player, :destination, :check # piece/promotion/capture/disambiguation/castle have custom writers below # (color normalization, nil-coalescing, etc.), so expose only readers here # to avoid redefining attr_accessor setters (Lint/DuplicateMethods). attr_reader :piece, :promotion, :capture, :disambiguation, :castle # A regular expression for matching moves in standard algebraic # notation # SAN_REGEX = / (?<piece> [BKNQR] ){0} (?<destination> [a-h][1-8] ){0} (?<promotion> =[BNQR] ){0} (?<check> [#+] ){0} (?<capture> x ){0} (?<disambiguation> [a-h]?[1-8]? ){0} (?<castle> O-O(?:-O)? ){0} (?<normal> \g<piece>? \g<disambiguation> \g<capture>? \g<destination> \g<promotion>? ){0} \A (?:\g<castle> | \g<normal>) \g<check>? \z /x # @param move [String] the move in SAN # @param player [Symbol] the player making the move # @example # PGN::Move.new("e4", :white) # def initialize(move, player) self.player = player self.san = move match = move.match(SAN_REGEX) return if match.nil? self.piece = match[:piece] self.destination = match[:destination] self.promotion = match[:promotion] self.check = match[:check] self.capture = match[:capture] self.disambiguation = match[:disambiguation] self.castle = match[:castle] end def piece=(val) # Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute # is set later in #initialize. Use a non-allocating prefix check instead # of `san.match('O-O')`, which allocated a MatchData on every Move.new. return if san.start_with?('O') val ||= 'P' @piece = black? ? val.downcase : val end def promotion=(val) return unless val val.downcase! if black? @promotion = val.delete('=') end def capture=(val) @capture = !!val end def disambiguation=(val) @disambiguation = (val == '' ? nil : val) end def castle=(val) return unless val @castle = 'K' if val == 'O-O' @castle = 'Q' if val == 'O-O-O' @castle.downcase! if black? end # @return [Boolean] whether the move results in check # def check? check == '+' end # @return [Boolean] whether the move results in checkmate # def checkmate? check == '#' end # @return [Boolean] whether it's white's turn # def white? player == :white end # @return [Boolean] whether it's black's turn # def black? player == :black end # @return [Boolean] whether the piece being moved is a pawn # def pawn? %w[P p].include?(piece) end end |
Instance Method Details
#black? ⇒ Boolean
Returns whether it's black's turn.
164 165 166 |
# File 'lib/pgn/move.rb', line 164 def black? player == :black end |
#check? ⇒ Boolean
Returns whether the move results in check.
146 147 148 |
# File 'lib/pgn/move.rb', line 146 def check? check == '+' end |
#checkmate? ⇒ Boolean
Returns whether the move results in checkmate.
152 153 154 |
# File 'lib/pgn/move.rb', line 152 def checkmate? check == '#' end |
#pawn? ⇒ Boolean
Returns whether the piece being moved is a pawn.
170 171 172 |
# File 'lib/pgn/move.rb', line 170 def pawn? %w[P p].include?(piece) end |
#white? ⇒ Boolean
Returns whether it's white's turn.
158 159 160 |
# File 'lib/pgn/move.rb', line 158 def white? player == :white end |