Add licence and installation instructions.
[kaya.git] / lib / games / chess / piece.rb
blobd2cb66957632e9947d309794111cc03955ac566b
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 module Chess
9   class Piece
10     attr_reader :color, :type
11     SYMBOLS = { :knight => 'N' }
12     TYPES = { 'P' => :pawn,
13               'R' => :rook,
14               'B' => :bishop,
15               'N' => :knight,
16               'Q' => :queen,
17               'K' => :king }
18   
19     def initialize(color, type)
20       @color = color
21       @type = type
22     end
23     
24     def ==(other)
25       @color == other.color and @type == other.type
26     end
27     
28     def name
29       "#@color #@type"
30     end
31     
32     def symbol
33       s = self.class.symbol(type)
34       s = s.downcase if color == :black
35       s
36     end
37     
38     def same_color_of?(other)
39       other and other.color == color
40     end
41     
42     def to_s
43       name
44     end
45     
46     def eql?(other)
47       other.instance_of?(self.class) and self == other
48     end
49     
50     def hash
51       [@color, @type].hash
52     end
53     
54     def self.symbol(type)
55       SYMBOLS[type] || type.to_s[0, 1].upcase
56     end
57     
58     def self.type_from_symbol(sym)
59       TYPES[sym.upcase]
60     end
61   end
62 end