Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created February 1, 2010 16:44
Show Gist options
  • Save Mon-Ouie/291809 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/291809 to your computer and use it in GitHub Desktop.
class Grid
class << self
alias :rand :new
end
def initialize(width, height)
@w, @h = width, height
@answer = (0..@h).inject([]) do |ary, x|
ary << (0..@w).inject([]) do |sub_ary, y|
sub_ary << (rand(2) == 1)
end
end
@grid = (0...@h).inject([]) do |ary, x|
ary << (0...@w).inject([]) do |sub_ary, y|
sub_ary << @answer[x, 2].inject(0) do |sum, sub_answer|
sum + sub_answer[y, 2].select { |i| i }.size
end
end
end
end
def print_on(io)
@grid.each { |line| io.puts line * " " }
end
def [](x, y)
@grid[x][y]
end
attr_reader :w, :h, :answer
end
Grid.new(5, 5).print_on $stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment