Skip to content

Instantly share code, notes, and snippets.

@dgholz
Last active June 10, 2018 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgholz/1043216acf6f53893efaa0802e0c2a30 to your computer and use it in GitHub Desktop.
Save dgholz/1043216acf6f53893efaa0802e0c2a30 to your computer and use it in GitHub Desktop.
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