Class: Rchess::Piece
- Inherits:
-
Object
- Object
- Rchess::Piece
- 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
-
#coord ⇒ Object
readonly
Returns the value of attribute coord.
Class Method Summary collapse
Instance Method Summary collapse
- #can_goto_coord?(coord) ⇒ Boolean
- #color ⇒ Object
- #direction ⇒ Object
-
#initialize(options) ⇒ Piece
constructor
A new instance of Piece.
- #is_threaten? ⇒ Boolean
- #type ⇒ Object
Constructor Details
#initialize(options) ⇒ Piece
Returns a new instance of Piece.
28 29 30 31 |
# File 'lib/rchess/piece.rb', line 28 def initialize() @board = .fetch(:board) #The environment @coord = .fetch(:coord) #The position end |
Instance Attribute Details
#coord ⇒ Object (readonly)
Returns the value of attribute coord.
26 27 28 |
# File 'lib/rchess/piece.rb', line 26 def coord @coord end |
Class Method Details
Instance Method Details
#can_goto_coord?(coord) ⇒ 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 |
#color ⇒ Object
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 |
#direction ⇒ Object
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
58 59 60 |
# File 'lib/rchess/piece.rb', line 58 def is_threaten? false #TODO: implement end |
#type ⇒ Object
50 51 52 |
# File 'lib/rchess/piece.rb', line 50 def type @type ||= @board.box_at_coord(@coord) end |