Sha256: bfb6ad1c57733231a78e9bd6f9ff194efa50d98efae42d5bb510cd75fa4cc44a

Contents?: true

Size: 766 Bytes

Versions: 3

Compression:

Stored size: 766 Bytes

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
picolena-0.1.6 rails_plugins/rspec/examples/stories/game-of-life/life/grid.rb
picolena-0.1.7 rails_plugins/rspec/examples/stories/game-of-life/life/grid.rb
picolena-0.1.8 rails_plugins/rspec/examples/stories/game-of-life/life/grid.rb