1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# frozen_string_literal: true
RSpec.describe GameOfLife::Universe do
describe ".new" do
it "creates a @current 2D array (Plane) of height * width" do
universe = described_class.new(width: 10, height: 1)
current_generation = universe.instance_variable_get(:@current)
expect(current_generation).to be_a GameOfLife::Plane
expect(current_generation[0]).to be_a GameOfLife::Plane
expect(current_generation.size).to eq 1
expect(current_generation[0].size).to eq 10
end
it "creates a @future 2D array (Plane) of height * width" do
universe = described_class.new(width: 10, height: 1)
future_generation = universe.instance_variable_get(:@future)
expect(future_generation).to be_a GameOfLife::Plane
expect(future_generation[0]).to be_a GameOfLife::Plane
expect(future_generation.size).to eq 1
expect(future_generation[0].size).to eq 10
end
end
describe "#to_s" do
subject(:universe) { described_class.new(width: 3, height: 3) }
before do
universe[0] = [1, 2, 3]
universe[1] = [4, 5, 6]
universe[2] = [7, 8, 9]
end
it "prints out the universe 2D @current array" do
expect(universe.to_s).to eq "123\n456\n789"
end
end
describe "#evolve!" do
subject(:universe) { described_class.new(width: 3, height: 3) }
before do
# quick way to initialize the whole universe with living cells
9.times { |i| universe[i / 3][i % 3] = GameOfLife::Cell.new(y: i / 3, x: i % 3, alive: true) }
end
it "calls Cell#evolve! on every cell" do
9.times { |i| expect(universe[i / 3][i % 3]).to receive(:evolve!) }
universe.evolve!
end
it "updates the universe current state" do
9.times { |i| expect(universe[i / 3][i % 3]).to be_alive }
universe.evolve!
9.times { |i| expect(universe[i / 3][i % 3]).to be_dead }
end
end
describe "#neighbors" do
subject(:universe) { described_class.new(width: 5, height: 5) }
it "returns surronding neighbors" do
current = GameOfLife::Plane.new(
[
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[. x x x .]),
GameOfLife::Plane.new(%w[. x C x .]),
GameOfLife::Plane.new(%w[. x x x .]),
GameOfLife::Plane.new(%w[. . . . .]),
],
)
universe.instance_variable_set(:@current, current)
cell = GameOfLife::Cell.new(y: 2, x: 2, alive: "C")
expect(universe.send(:neighbors, cell)).to eq %w[x x x x x x x x]
end
it "returns surronding neighbors (beyond top edge)" do
current = GameOfLife::Plane.new(
[
GameOfLife::Plane.new(%w[. x C x .]),
GameOfLife::Plane.new(%w[. x x x .]),
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[. x x x .]),
],
)
universe.instance_variable_set(:@current, current)
cell = GameOfLife::Cell.new(y: 0, x: 2, alive: "C")
expect(universe.send(:neighbors, cell)).to eq %w[x x x x x x x x]
end
it "returns surronding neighbors (beyond left edge)" do
current = GameOfLife::Plane.new(
[
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[x x . . x]),
GameOfLife::Plane.new(%w[C x . . x]),
GameOfLife::Plane.new(%w[x x . . x]),
],
)
universe.instance_variable_set(:@current, current)
cell = GameOfLife::Cell.new(y: 3, x: 0, alive: "C")
expect(universe.send(:neighbors, cell)).to eq %w[x x x x x x x x]
end
it "returns surronding neighbors (beyond diagonal edges)" do
current = GameOfLife::Plane.new(
[
GameOfLife::Plane.new(%w[C x . . x]),
GameOfLife::Plane.new(%w[x x . . x]),
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[x x . . x]),
],
)
universe.instance_variable_set(:@current, current)
cell = GameOfLife::Cell.new(y: 0, x: 0, alive: "C")
expect(universe.send(:neighbors, cell)).to eq %w[x x x x x x x x]
end
it "returns surronding neighbors (beyond reversed diagonal edges)" do
current = GameOfLife::Plane.new(
[
GameOfLife::Plane.new(%w[x . . x x]),
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[. . . . .]),
GameOfLife::Plane.new(%w[x . . x x]),
GameOfLife::Plane.new(%w[x . . x C]),
],
)
universe.instance_variable_set(:@current, current)
cell = GameOfLife::Cell.new(y: 4, x: 4, alive: "C")
expect(universe.send(:neighbors, cell)).to eq %w[x x x x x x x x]
end
end
end
|