1class Grid
2
3 attr_accessor :contents
4
5 def initialize(rows, cols)
6 @contents = []
7 rows.times do @contents << [0] * cols end
8 end
9
10 def rows
11 @contents.size
12 end
13
14 def columns
15 @contents[0].size
16 end
17
18 def ==(other)
19 self.contents == other.contents
20 end
21
22 def create_at(row,col)
23 @contents[row][col] = 1
24 end
25
26 def destroy_at(row,col)
27 @contents[row][col] = 0
28 end
29
30 def self.from_string(str)
31 row_strings = str.split(' ')
32 grid = new(row_strings.size, row_strings[0].size)
33
34 row_strings.each_with_index do |row, row_index|
35 row_chars = row.split(//)
36 row_chars.each_with_index do |col_char, col_index|
37 grid.create_at(row_index, col_index) if col_char == 'X'
38 end
39 end
40 return grid
41 end
42
43end