changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/examples/stories/game-of-life/life/grid.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
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