public
Fork of scrooloose/chesticles
Description: A chess hax in ruby
Clone URL: git://github.com/halorgium/chesticles.git
chesticles / models / king.rb
100644 39 lines (33 sloc) 0.866 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class King < Piece
  def move(m)
    if m.castle?
      self.castle(m)
    else
      super
    end
  end
 
  protected
    def legal_move?(move)
      move.straight? && move.distance == 1
    end
 
    def castle(move)
      castling_rook(move).square = castling_rook_new_square(move)
      self.square = move.square
    end
 
    def castling_rook(move)
      board.piece_for case(move.square)
        when Square.new(2,7): Square.new(0,7)
        when Square.new(6,7): Square.new(7,7)
        when Square.new(2,0): Square.new(0,0)
        when Square.new(6,0): Square.new(7,0)
      end
    end
 
    def castling_rook_new_square(move)
      case(move.square)
        when Square.new(2,7): Square.new(3,7)
        when Square.new(6,7): Square.new(5,7)
        when Square.new(2,0): Square.new(3,0)
        when Square.new(6,0): Square.new(5,0)
      end
    end
end