public
Fork of scrooloose/chesticles
Description: A chess hax in ruby
Clone URL: git://github.com/halorgium/chesticles.git
chesticles / models / square.rb
100644 87 lines (67 sloc) 1.699 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Square
  class InvalidCoordinateError < StandardError; end
  class InvalidOperationError < StandardError; end
 
  attr_reader :x, :y
 
  def initialize(x, y)
    self.x = x
    self.y = y
  end
 
  def x=(x)
    raise(InvalidCoordinateError, "invalid X coord: #{x}") if x < 0 || x > 7
    @x = x
  end
 
  def y=(y)
    raise(InvalidCoordinateError, "invalid Y coord: #{y}") if y < 0 || y > 7
    @y = y
  end
 
  def black?
    x.even? != y.even?
  end
 
  def white?
    x.even? == y.even?
  end
 
  def ==(s)
    x == s.x && y == s.y
  end
 
  def x_delta_to(s)
    s.x - x
  end
 
  def y_delta_to(s)
    s.y - y
  end
 
  def to_s
    "(#{x}, #{y})"
  end
 
  def squares_between(dest)
    dx = x_delta_to(dest)
    dy = y_delta_to(dest)
 
    unless dx == 0 || dy == 0 || dx.abs == dy.abs
      raise(InvalidOperationError, "can only find the squares between a square in a straight line")
    end
 
    min_x = x < dest.x ? x : dest.x
    max_x = x > dest.x ? x : dest.x
    min_y = y < dest.y ? y : dest.y
    max_y = y > dest.y ? y : dest.y
 
    if dy == 0 #horizontal line
      return ((min_x+1)..(max_x-1)).map {|x| Square.new(x, self.y)}
    elsif dx == 0 #vertical line
      return ((min_y+1)..(max_y-1)).map {|y| Square.new(self.x, y)}
    else #diagonal line
      xdiff = dest.x - self.x
      ydiff = dest.y - self.y
 
      #there are no between squares
      return [] if xdiff == 1
 
      xstep = xdiff / xdiff.abs
      ystep = ydiff / ydiff.abs
 
      squares = []
      x = self.x + xstep
      y = self.y + ystep
      while x != dest.x && y != dest.y
        squares << Square.new(x, y)
        x += xstep
        y += ystep
      end
 
      return squares
    end
  end
 
end