Method: PGN::Move#capture

Defined in:
lib/pgn/move.rb,
lib/pgn/move.rb

#captureString?

Returns whether the move is a capture.

Examples:

move.capture #=> "x"

Returns:

  • (String, nil)

    whether the move is a capture



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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/pgn/move.rb', line 58

class Move
  attr_accessor :san, :player
  attr_accessor :piece, :destination, :promotion, :check, :capture, :disambiguation, :castle

  # A regular expression for matching moves in standard algebraic
  # notation
  #
  SAN_REGEX = %r{
    (?<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)

    match.names.each do |name|
      if self.respond_to?(name)
        self.send("#{name}=", match[name])
      end
    end
  end

  def piece=(val)
    return if san.match("O-O")

    val ||= "P"
    @piece = self.black? ?
      val.downcase :
      val
  end

  def promotion=(val)
    if val
      val.downcase! if self.black?
      @promotion = val.delete("=")
    end
  end

  def capture=(val)
    @capture = !!val
  end

  def disambiguation=(val)
    @disambiguation = (val == "" ? nil : val)
  end

  def castle=(val)
    if val
      @castle = "K" if val == "O-O"
      @castle = "Q" if val == "O-O-O"
      @castle.downcase! if self.black?
    end
  end

  # @return [Boolean] whether the move results in check
  #
  def check?
    self.check == "+"
  end

  # @return [Boolean] whether the move results in checkmate
  #
  def checkmate?
    self.check == "#"
  end

  # @return [Boolean] whether it's white's turn
  #
  def white?
    self.player == :white
  end

  # @return [Boolean] whether it's black's turn
  #
  def black?
    self.player == :black
  end

  # @return [Boolean] whether the piece being moved is a pawn
  #
  def pawn?
    ['P', 'p'].include?(self.piece)
  end

end