Class: ChessRB::Piece
- Inherits:
-
Object
- Object
- ChessRB::Piece
- Defined in:
- lib/chess_rb/piece.rb
Constant Summary collapse
- E =
0
- WP =
1
- WN =
2
- WLB =
3
- WDB =
4
- WR =
5
- WQ =
6
- WK =
7
- BP =
11
- BN =
12
- BLB =
13
- BDB =
14
- BR =
15
- BQ =
16
- BK =
17
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
Instance Method Summary collapse
- #color ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(code) ⇒ Piece
constructor
A new instance of Piece.
- #to_s(d) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(code) ⇒ Piece
Returns a new instance of Piece.
8 9 10 11 12 |
# File 'lib/chess_rb/piece.rb', line 8 def initialize(code) @code = code @desc = ChessRB::Piece.constants.select{ |v| ChessRB::Piece.const_get(v) == code }[0].to_s end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
6 7 8 |
# File 'lib/chess_rb/piece.rb', line 6 def code @code end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
6 7 8 |
# File 'lib/chess_rb/piece.rb', line 6 def desc @desc end |
Instance Method Details
#color ⇒ Object
18 19 20 |
# File 'lib/chess_rb/piece.rb', line 18 def color self.desc[0] end |
#empty? ⇒ Boolean
22 23 24 |
# File 'lib/chess_rb/piece.rb', line 22 def empty? return @code == 0 end |
#to_s(d) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/chess_rb/piece.rb', line 26 def to_s(d) case @code when WP d ? "♟" : "♙" when WN d ? "♞" : "♘" when WLB d ? "♝" : "♗" when WDB d ? "♝" : "♗" when WR d ? "♜" : "♖" when WQ d ? "♛" : "♕" when WK d ? "♚" : "♔" when BP d ? "♙" : "♟" when BN d ? "♘" : "♞" when BLB d ? "♗" : "♝" when BDB d ? "♗" : "♝" when BR d ? "♖" : "♜" when BQ d ? "♕" : "♛" when BK d ? "♔" : "♚" end end |
#type ⇒ Object
14 15 16 |
# File 'lib/chess_rb/piece.rb', line 14 def type self.desc[-1, 1] end |