X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/ddd5b4cf19a92582fd114914be5bd5a04d3522a7..3f607d565bc0e2c7b1b738301c11c16d069913d5:/vendor/gems/rspec-1.1.2/examples/stories/game-of-life/life/grid.rb diff --git a/vendor/gems/rspec-1.1.2/examples/stories/game-of-life/life/grid.rb b/vendor/gems/rspec-1.1.2/examples/stories/game-of-life/life/grid.rb new file mode 100644 index 000000000..aca23087c --- /dev/null +++ b/vendor/gems/rspec-1.1.2/examples/stories/game-of-life/life/grid.rb @@ -0,0 +1,43 @@ +class Grid + + attr_accessor :contents + + def initialize(rows, cols) + @contents = [] + rows.times do @contents << [0] * cols end + end + + def rows + @contents.size + end + + def columns + @contents[0].size + end + + def ==(other) + self.contents == other.contents + end + + def create_at(row,col) + @contents[row][col] = 1 + end + + def destroy_at(row,col) + @contents[row][col] = 0 + end + + def self.from_string(str) + row_strings = str.split(' ') + grid = new(row_strings.size, row_strings[0].size) + + row_strings.each_with_index do |row, row_index| + row_chars = row.split(//) + row_chars.each_with_index do |col_char, col_index| + grid.create_at(row_index, col_index) if col_char == 'X' + end + end + return grid + end + +end