class PGN::Move

{PGN::Move} knows how to parse a move string in standard algebraic notation to extract all relevant information.

@see en.wikipedia.org/wiki/Algebraic_notation_(chess) Standard

Algebraic Notation

@!attribute san

@return [String] the move string
@example
  move1.san #=> "O-O-O+"
  move2.san #=> "Raxe1"
  move3.san #=> "e8=Q#"

@!attribute player

@return [Symbol] the current player
@example
  move.player #=> :white

@!attribute piece

@return [String, nil] the piece being moved
@example
  move1.piece #=> "Q"
  move2.piece #=> "r"
@note this is nil for castling
@note uppercase represents white, lowercase represents black

@!attribute destination

@return [String, nil] the destination square of the piece
@example
  move.destination #=> "e4"

@!attribute promotion

@return [String, nil] the promotion piece, if applicable

@!attribute check

@return [String, nil] whether the move results in check or mate
@example
  move1.check #=> "+"
  move2.check #=> "#"

@!attribute capture

@return [String, nil] whether the move is a capture
@example
  move.capture #=> "x"

@!attribute disambiguation

@return [String, nil] the disambiguation string if there is one
@example
  move.disambiguation #=> "3"

@!attribute castle

@return [String, nil] the castle string if applicable
@example
  move1.castle #=> "O-O-O"
  move2.castle #=> "O-O"

Constants

SAN_REGEX

A regular expression for matching moves in standard algebraic notation

Attributes

capture[RW]
castle[RW]
check[RW]
destination[RW]
disambiguation[RW]
piece[RW]
player[RW]
promotion[RW]
san[RW]

Public Class Methods

new(move, player) click to toggle source

@param move [String] the move in SAN @param player [Symbol] the player making the move @example

PGN::Move.new("e4", :white)
# File lib/pgn/move.rb, line 91
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

Public Instance Methods

black?() click to toggle source

@return [Boolean] whether it's black's turn

# File lib/pgn/move.rb, line 156
def black?
  self.player == :black
end
capture=(val) click to toggle source
# File lib/pgn/move.rb, line 120
def capture=(val)
  @capture = !!val
end
castle=(val) click to toggle source
# File lib/pgn/move.rb, line 128
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
check?() click to toggle source

@return [Boolean] whether the move results in check

# File lib/pgn/move.rb, line 138
def check?
  self.check == "+"
end
checkmate?() click to toggle source

@return [Boolean] whether the move results in checkmate

# File lib/pgn/move.rb, line 144
def checkmate?
  self.check == "#"
end
disambiguation=(val) click to toggle source
# File lib/pgn/move.rb, line 124
def disambiguation=(val)
  @disambiguation = (val == "" ? nil : val)
end
pawn?() click to toggle source

@return [Boolean] whether the piece being moved is a pawn

# File lib/pgn/move.rb, line 162
def pawn?
  ['P', 'p'].include?(self.piece)
end
piece=(val) click to toggle source
# File lib/pgn/move.rb, line 104
def piece=(val)
  return if san.match("O-O")

  val ||= "P"
  @piece = self.black? ?
    val.downcase :
    val
end
promotion=(val) click to toggle source
# File lib/pgn/move.rb, line 113
def promotion=(val)
  if val
    val.downcase! if self.black?
    @promotion = val.delete("=")
  end
end
white?() click to toggle source

@return [Boolean] whether it's white's turn

# File lib/pgn/move.rb, line 150
def white?
  self.player == :white
end