-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_game.rb
162 lines (132 loc) · 3.97 KB
/
spec_game.rb
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 'rspec'
require_relative 'game.rb'
require_relative 'cell.rb'
require_relative 'world.rb'
describe "game of life" do
let(:world) { World.new }
let(:cell) { Cell.new(1, 1) }
context "world" do
subject { World.new }
it "Should create a new world" do
subject.is_a?(World).should be_true
end
it "should hawe rows and cols" do
subject.should respond_to(:rows)
subject.should respond_to(:cols)
subject.should respond_to(:cell_grid)
subject.should respond_to(:cell_live_neighbours)
subject.should respond_to(:cells)
subject.should respond_to(:randomly_update)
# subject.should respond_to(:live_cells)
end
it "Should create an propper grid" do
subject.cell_grid.is_a?(Array).should be_true
subject.cell_grid.each do |row|
row.is_a?(Array).should be_true
row.each do |col|
col.is_a?(Cell).should be_true
end
end
end
it "should add all cells" do
subject.cells.count.should == 100
end
it "Detects live cell top" do
subject.cell_grid[cell.y + 1][cell.x].alive = true
subject.cell_live_neighbours(cell).count.should == 1
end
it "Detects live cell top-right" do
subject.cell_grid[cell.y + 1][cell.x + 1].alive = true
subject.cell_live_neighbours(cell).count.should == 1
end
it "Detects live cell right" do
subject.cell_grid[cell.y ][cell.x + 1].alive = true
subject.cell_live_neighbours(cell).count.should == 1
end
it "Detects live cell bottom-right" do
subject.cell_grid[cell.y - 1][cell.x + 1].alive = true
subject.cell_live_neighbours(cell).count.should == 1
end
it "Detects live cell bottom" do
subject.cell_grid[cell.y - 1][cell.x].alive = true
subject.cell_live_neighbours(cell).count.should == 1
end
it "Detects live cell bottom-left" do
subject.cell_grid[cell.y - 1][cell.x - 1].alive = true
subject.cell_live_neighbours(cell).count.should == 1
end
it "Detects live cell left" do
subject.cell_grid[cell.y][cell.x - 1].alive = true
subject.cell_live_neighbours(cell).count.should == 1
end
it "Detects live cell top-left" do
subject.cell_grid[cell.y + 1][cell.x - 1].alive = true
subject.cell_live_neighbours(cell).count.should == 1
end
# it "should randomly populate cells" do
# subject.live_cells.count.should == 0
# subject.randomly_update
# subject.live_cells.count.should_not == 0
# end
end
context "Cell" do
subject {Cell.new}
it "should create new cell" do
subject.is_a?(Cell).should be_true
end
it "Should be alive" do
subject.should respond_to(:alive)
subject.should respond_to(:x)
subject.should respond_to(:y)
subject.should respond_to(:alive?)
subject.should respond_to(:die!)
end
it "Should properly initialize" do
subject.alive.should be_false
subject.x.should == 0
subject.y.should == 0
end
end
context "Game" do
subject {Game.new}
it "should create new game" do
subject.is_a?(Game).should be_true
end
it "should use propper methods" do
subject.should respond_to(:world)
subject.should respond_to(:cells)
end
it "Should propperly initialize" do
subject.world.is_a?(World).should be_true
subject.cells.is_a?(Array).should be_true
end
it "should add" do
game = Game.new(world, [[1,2], [0,2]])
world.cell_grid[1][2].should be_alive
world.cell_grid[0][2].should be_alive
end
end
context "Rules" do
let(:game) {Game.new}
context "Rule Nr.1" do
it "should kill a cell if has none neighbor" do
game.world.cell_grid[1][1].alive = true
game.world.cell_grid[1][1].should be_alive
game.step!
game.world.cell_grid[1][1].should be_dead
end
it "Should kill a cell if has one neighbor" do
game = Game.new(world, [[1,0], [2,0]])
game.step!
world.cell_grid[1][0].should be_dead
world.cell_grid[2][0].should be_dead
end
it "does not kill live cell with two neighbors" do
game = Game.new(world, [[0, 1], [1, 1], [2, 1]])
# binding.pry
game.step!
world.cell_grid[1][1].should be_alive
end
end
end
end