Last active
June 10, 2018 23:07
-
-
Save dgholz/1043216acf6f53893efaa0802e0c2a30 to your computer and use it in GitHub Desktop.
This file contains 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 TileWithNeighbours | |
@@instances = [] | |
attr_accessor :instances | |
def initialize(filename, **neighbours) | |
@filename = filename | |
@neighbours = neighbours | |
@@instances << self | |
end | |
attr_accessor :filename | |
attr_accessor :neighbours | |
def matches(**neighbours) | |
neighbours.default_proc = proc do |hsh,k| @neighbours[k] end | |
@neighbours.each do |direction, type| | |
if type != neighbours[direction] | |
return false | |
end | |
end | |
return true | |
end | |
def self.fit_with(**neighbours) | |
@@instances.select { |tile| tile.matches(**neighbours) } | |
end | |
def to_s | |
"#{@filename} with #{@neighbours}" | |
end | |
end | |
boo = TileWithNeighbours.new('boo', :north => :trees) | |
foo = TileWithNeighbours.new('foo', :north => :grass, :east => :trees, :south => :trees) | |
puts TileWithNeighbours.fit_with(:south => :trees) # boo, foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment