Class: PGN::Move

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(move, player) ⇒ Move

Returns a new instance of Move.

Examples:

PGN::Move.new("e4", :white)

Parameters:

  • move (String)

    the move in SAN

  • player (Symbol)

    the player making the 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

#captureObject

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

#castleObject

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

#checkString?

Returns whether the move results in check or mate.

Examples:

move1.check #=> "+"
move2.check #=> "#"

Returns:

  • (String, nil)

    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

#destinationString?

Returns the destination square of the piece.

Examples:

move.destination #=> "e4"

Returns:

  • (String, nil)

    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

#disambiguationObject

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

#pieceObject

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

#playerSymbol

Returns the current player.

Examples:

move.player #=> :white

Returns:

  • (Symbol)

    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

#promotionObject

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

#sanString

Returns the move string.

Examples:

move1.san #=> "O-O-O+"
move2.san #=> "Raxe1"
move3.san #=> "e8=Q#"

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    whether it's white's turn



158
159
160
# File 'lib/pgn/move.rb', line 158

def white?
  player == :white
end