class PGN::Board

{PGN::Board} represents the squares of a chess board and the pieces on each square. It is responsible for translating between a human readable format (white queen's rook on the bottom left) and the obvious internal representation (white queen's rook is position [0,0]). It takes care of converting square names (e4) to actual locations, and can convert to unicode chess pieces for display purposes.

@!attribute squares

@return [Array<Array<String>>] the pieces on the board

Constants

FILE_TO_INDEX
INDEX_TO_FILE
INDEX_TO_RANK
RANK_TO_INDEX
START

The starting, internal representation of a chess board

UNICODE_PIECES

algebraic to unicode piece lookup

Attributes

squares[RW]

Public Class Methods

new(squares) click to toggle source

@param squares [<Array<Array<String>>>] the squares of the board @example

PGN::Board.new(
  [
    ["R", "P", nil, nil, nil, nil, "p", "r"],
    ["N", "P", nil, nil, nil, nil, "p", "n"],
    ["B", "P", nil, nil, nil, nil, "p", "b"],
    ["Q", "P", nil, nil, nil, nil, "p", "q"],
    ["K", "P", nil, nil, nil, nil, "p", "k"],
    ["B", "P", nil, nil, nil, nil, "p", "b"],
    ["N", "P", nil, nil, nil, nil, "p", "n"],
    ["R", "P", nil, nil, nil, nil, "p", "r"],
  ]
)
# File lib/pgn/board.rb, line 91
def initialize(squares)
  self.squares = squares
end
start() click to toggle source

@return [PGN::Board] a board in the starting position

# File lib/pgn/board.rb, line 72
def self.start
  PGN::Board.new(START)
end

Public Instance Methods

at(*args) click to toggle source

@overload at(str)

Looks up a piece based on the string representation of a square (e4)
@param str [String] the square in algebraic notation

@overload at(file, rank)

Looks up a piece based on zero-indexed coordinates (4, 3)
@param file [Integer] the file the piece is on
@param rank [Integer] the rank the piece is on

@return [String, nil] the piece on the square, or nil if it is

empty

@example

board.at(4,3)  #=> "P"
board.at("e4") #=> "P"
# File lib/pgn/board.rb, line 108
def at(*args)
  case args.length
  when 1
    self.at(*coordinates_for(args.first))
  when 2
    self.squares[args[0]][args[1]]
  end
end
change!(changes) click to toggle source

@param changes [Hash<String, <String, nil>>] changes to make to the board @return [self] @example

board.change!({"e2" => nil, "e4" => "P"})
# File lib/pgn/board.rb, line 122
def change!(changes)
  changes.each do |square, piece|
    self.update(square, piece)
  end
  self
end
coordinates_for(position) click to toggle source

@param position [String] the square in algebraic notation @return [Array<Integer>] the coordinates of the square @example

board.coordinates_for("e4") #=> [4, 3]
# File lib/pgn/board.rb, line 146
def coordinates_for(position)
  file_chr, rank_chr = position.chars.to_a
  file = FILE_TO_INDEX[file_chr]
  rank = RANK_TO_INDEX[rank_chr]
  [file, rank]
end
dup() click to toggle source

@return [PGN::Board] a copy of self with duplicated squares

# File lib/pgn/board.rb, line 176
def dup
  PGN::Board.new(self.squares.map(&:dup))
end
inspect() click to toggle source

@return [String] the board in human readable format with unicode

pieces
# File lib/pgn/board.rb, line 168
def inspect
  self.squares.transpose.reverse.map do |row|
    row.map{|chr| UNICODE_PIECES[chr] }.join(' ')
  end.join("\n")
end
position_for(coordinates) click to toggle source

@param coordinates [Array<Integer>] the coordinates of the square @return [String] the square in algebraic notation @example

board.position_for([4, 3]) #=> "e4"
# File lib/pgn/board.rb, line 158
def position_for(coordinates)
  file, rank = coordinates
  file_chr = INDEX_TO_FILE[file]
  rank_chr = INDEX_TO_RANK[rank]
  [file_chr, rank_chr].join('')
end
update(square, piece) click to toggle source

@param square [String] the square in algebraic notation @param piece [String, nil] the piece to put on the square @return [self] @example

board.update("e4", "P")
# File lib/pgn/board.rb, line 135
def update(square, piece)
  coords = coordinates_for(square)
  self.squares[coords[0]][coords[1]] = piece
  self
end