Created
March 31, 2012 12:32
-
-
Save kidrane/2263459 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Field | |
attr_reader :length, :width, :bad_points | |
include Enumerable | |
def initialize length, width | |
@length = length | |
@width = width | |
@bad_points = [] | |
end | |
def add_bad_point x, y | |
@bad_points << [x, y] | |
end | |
def each | |
(1...length).each do |left| | |
(1...width).each do |top| | |
((left + 1)..length).each do |right| | |
((top + 1)..width).each do |bottom| | |
yield left, top, right, bottom if valid? left, top, right, bottom | |
end | |
end | |
end | |
end | |
end | |
def valid? x1, y1, x2, y2 | |
bad_points.each do |bad_point| | |
x, y = bad_point | |
if x == x1 or x == x2 | |
return false if (y1..y2).include? y | |
elsif y == y1 or y == y2 | |
return false if (x1..x2).include? x | |
end | |
end | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment