Class: Rchess::Piece

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rchess/piece.rb

Constant Summary collapse

TYPES =
{
  p: ->(coord, board){ Paths::Pawn.new(  {coord: coord, board: board}) },
  b: ->(coord, board){ Paths::Bishop.new({coord: coord, board: board}) },
  q: ->(coord, board){ Paths::Queen.new( {coord: coord, board: board}) },
  c: ->(coord, board){ Paths::Knight.new({coord: coord, board: board}) },
  r: ->(coord, board){ Paths::Rook.new(  {coord: coord, board: board}) },
  k: ->(coord, baord){ Paths::King.new(  {coord: coord, board: board}) }
}
WHITE_COLOR =
:white
BLACK_COLOR =
:black
COLORS =
{
  WHITE_COLOR => :downcase,
  BLACK_COLOR => :upcase
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Piece

Returns a new instance of Piece.



28
29
30
31
# File 'lib/rchess/piece.rb', line 28

def initialize(options)
  @board = options.fetch(:board) #The environment
  @coord = options.fetch(:coord) #The position
end

Instance Attribute Details

#coordObject (readonly)

Returns the value of attribute coord.



26
27
28
# File 'lib/rchess/piece.rb', line 26

def coord
  @coord
end

Class Method Details

.type_to_color(type, color) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
# 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

Instance Method Details

#can_goto_coord?(coord) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# 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

#colorObject



38
39
40
41
42
43
# 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

#directionObject



45
46
47
48
# File 'lib/rchess/piece.rb', line 45

def direction
  #TODO: make it more dynamic
  self.color == WHITE_COLOR ? :up : :down
end

#is_threaten?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rchess/piece.rb', line 58

def is_threaten?
  false #TODO: implement
end

#typeObject



50
51
52
# File 'lib/rchess/piece.rb', line 50

def type
  @type ||= @board.box_at_coord(@coord)
end