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.freeze

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



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pgn/move.rb', line 91

def initialize(move, player)
  self.player = player
  self.san    = move

  match = move.match(SAN_REGEX)
  return  if match.nil?

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

Instance Attribute Details

#captureString?

Returns whether the move is a capture.

Examples:

move.capture #=> "x"

Returns:

  • (String, nil)

    whether the move is a capture



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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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

#castleString?

Returns the castle string if applicable.

Examples:

move1.castle #=> "O-O-O"
move2.castle #=> "O-O"

Returns:

  • (String, nil)

    the castle string if applicable



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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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

#disambiguationString?

Returns the disambiguation string if there is one.

Examples:

move.disambiguation #=> "3"

Returns:

  • (String, nil)

    the disambiguation string if there is one



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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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

#pieceString?

Note:

this is nil for castling

Note:

uppercase represents white, lowercase represents black

Returns the piece being moved.

Examples:

move1.piece #=> "Q"
move2.piece #=> "r"

Returns:

  • (String, nil)

    the piece being moved



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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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

#promotionString?

Returns the promotion piece, if applicable.

Returns:

  • (String, nil)

    the promotion piece, if applicable



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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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
# File 'lib/pgn/move.rb', line 59

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 = /
    (?<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.freeze

  # @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?

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

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

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

  def promotion=(val)
    if val
      val.downcase! if 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 black?
    end
  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



153
154
155
# File 'lib/pgn/move.rb', line 153

def black?
  player == :black
end

#check?Boolean

Returns whether the move results in check.

Returns:

  • (Boolean)

    whether the move results in check



135
136
137
# File 'lib/pgn/move.rb', line 135

def check?
  check == '+'
end

#checkmate?Boolean

Returns whether the move results in checkmate.

Returns:

  • (Boolean)

    whether the move results in checkmate



141
142
143
# File 'lib/pgn/move.rb', line 141

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



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

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



147
148
149
# File 'lib/pgn/move.rb', line 147

def white?
  player == :white
end