class Rchess::Piece

Constants

BLACK_COLOR
COLORS
TYPES
WHITE_COLOR

Attributes

coord[R]

Public Class Methods

new(options) click to toggle source
# File lib/rchess/piece.rb, line 28
def initialize(options)
  @board = options.fetch(:board) #The environment
  @coord = options.fetch(:coord) #The position
end
type_to_color(type, color) click to toggle source
# File lib/rchess/piece.rb, line 33
def self.type_to_color(type, color)
  raise ArgumentError.new("Unknown color #{color}") unless COLORS.keys.include? color
  type.send(COLORS[color])
end

Public Instance Methods

can_goto_coord?(coord) click to toggle source
# File lib/rchess/piece.rb, line 54
def can_goto_coord?(coord)
  destinations.flatten.any?{|dest| dest.x == coord.x && dest.y == coord.y }
end
color() click to toggle source
# File lib/rchess/piece.rb, line 38
def color
  return @color if @color

  cases = COLORS.invert
  self.type.downcase == self.type ? cases[:downcase] : cases[:upcase]
end
direction() click to toggle source
# File lib/rchess/piece.rb, line 45
def direction
  #TODO: make it more dynamic
  self.color == WHITE_COLOR ? :up : :down
end
is_threaten?() click to toggle source
# File lib/rchess/piece.rb, line 58
def is_threaten?
  false #TODO: implement
end
type() click to toggle source
# File lib/rchess/piece.rb, line 50
def type
  @type ||= @board.box_at_coord(@coord)
end

Private Instance Methods

paths() click to toggle source
# File lib/rchess/piece.rb, line 64
def paths
  @paths ||= TYPES[self.type.downcase].call(@coord, @board)
end