Created
February 1, 2010 16:44
-
-
Save Mon-Ouie/291809 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 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