Add shogi serializer.
[kaya.git] / lib / games / chess / piece.rb
blobb987721f7599b87dad837b949e88296bab6fbf4a
1 module Chess
2   class Piece
3     attr_reader :color, :type
4     SYMBOLS = { :knight => 'N' }
5     TYPES = { 'P' => :pawn,
6               'R' => :rook,
7               'B' => :bishop,
8               'N' => :knight,
9               'Q' => :queen,
10               'K' => :king }
11   
12     def initialize(color, type)
13       @color = color
14       @type = type
15     end
16     
17     def ==(other)
18       @color == other.color and @type == other.type
19     end
20     
21     def name
22       "#@color #@type"
23     end
24     
25     def symbol
26       s = self.class.symbol(type)
27       s = s.downcase if color == :black
28       s
29     end
30     
31     def same_color_of?(other)
32       other and other.color == color
33     end
34     
35     def to_s
36       name
37     end
38     
39     def eql?(other)
40       other.instance_of?(self.class) and self == other
41     end
42     
43     def hash
44       [@color, @type].hash
45     end
46     
47     def self.symbol(type)
48       SYMBOLS[type] || type.to_s[0, 1].upcase
49     end
50     
51     def self.type_from_symbol(sym)
52       TYPES[sym.upcase]
53     end
54   end
55 end