Class: RGnuchess::Board::Piece

Inherits:
Object
  • Object
show all
Defined in:
lib/rgnuchess.rb

Overview

Piece represents a single chess piece on a board

Constant Summary collapse

Pieces =
{"P"=>:pawn,"R"=>:rook,"N"=>:knight,"B"=>:bishop,"K"=>:king,"Q"=>:queen,"."=>:empty}
Pieces_reverse =
Pieces.invert

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color = :white, piece = :empty) ⇒ Piece

Returns a new instance of Piece.



119
120
121
# File 'lib/rgnuchess.rb', line 119

def initialize( color=:white, piece=:empty )
  @color, @piece = color, piece
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



133
134
135
# File 'lib/rgnuchess.rb', line 133

def color
  @color
end

#pieceObject (readonly)

Returns the value of attribute piece.



133
134
135
# File 'lib/rgnuchess.rb', line 133

def piece
  @piece
end

Class Method Details

.parse(char) ⇒ Object

parse a character of gnuchess output from a board into a piece



114
115
116
117
118
# File 'lib/rgnuchess.rb', line 114

def Piece.parse(char)
  color = (char.upcase==char) ? :white : :black #upper case = white
  piece = Pieces[char.upcase]
  Piece.new(color,piece)
end

Instance Method Details

#==(other) ⇒ Object



122
123
124
# File 'lib/rgnuchess.rb', line 122

def ==(other)
  other.color==@color && other.piece==:piece
end

#to_sObject



125
126
127
128
129
130
131
132
# File 'lib/rgnuchess.rb', line 125

def to_s
  s=Pieces_reverse[@piece]
  if @color==:white
    s
  else
    s.downcase
  end
end