-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdan_mark_test.rb
163 lines (139 loc) · 4.19 KB
/
dan_mark_test.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
163
require "test/unit"
X = 0
Y = 1
class Universe
attr_accessor :grid
def cells_around_cell(x, y)
[
[x + -1, y + -1],
[x + -1, y + 0],
[x + -1, y + 1],
[x + 0, y + -1],
[x + 0, y + 1],
[x + 1, y + -1],
[x + 1, y + 0],
[x + 1, y + 1],
]
end
def initialize( alive_cells )
@grid = {}
alive_cells.each do |cell|
@grid["#{cell[X]},#{cell[Y]}"] = true
end
end
def alive?( x, y )
return @grid.key? "#{x},#{y}"
end
def evolve
new_grid = {}
@grid.keys.each do |key|
count = number_of_neighbors(key.split(',')[0], key.split(',')[1])
if count >= 2 && count <= 3
new_grid["#{cell[X]},#{cell[Y]}"] = true
end
end
@grid = new_grid
end
def cells_to_test
cells_to_test_hash = {}
@grid.keys.each do |key|
x = key.split(',')[0]
y = key.split(',')[1]
cells_around_cell = cells_around_cell(x, y)
cells.each do |cell|
cells_to_test_hash["#{cell[X]},#{cell[Y]}"]
end
end
cells_to_test = []
cells_to_test_hash.keys.each do |cell_coords|
cells_to_test << []
end
cells_to_test
end
def number_of_alive_neighbors(x, y)
# valid_cells = [
# [x + -1, y + -1],
# [x + -1, y + 0],
# [x + -1, y + 1],
# [x + 0, y + -1],
# [x + 0, y + 1],
# [x + 1, y + -1],
# [x + 1, y + 0],
# [x + 1, y + 1],
# ]
# num_alive_neighbours = 0
# valid_cells.each do |valid_cell|
# num_alive_neighbours += 1 if alive?(valid_cell[0], valid_cell[1])
# end
# return num_alive_neighbours
end
end
class UniverseTest < Test::Unit::TestCase
def setup
alive_cells = [
[0, 0],
[0, 1],
[1, 0],
[1, 1]
]
@universe = Universe.new(alive_cells)
end
def test_state_can_be_set_initially
assert(@universe.alive?(0, 0) == true, "Cell should be alive at 0, 0")
assert(@universe.alive?(0, 1) == true, "Cell should be alive at 0, 1")
assert(@universe.alive?(1, 0) == true, "Cell should be alive at 1, 0")
assert(@universe.alive?(-1, 0) == false, "Cell should be dead at -1, 0")
end
def test_universe_can_evolve
# @universe.evolve
# @universe.grid.count == 4
# assert(@universe.alive?(0, 0) == true, "Cell should be alive at 0, 0")
# assert(@universe.alive?(0, 1) == true, "Cell should be alive at 0, 1")
# assert(@universe.alive?(1, 0) == true, "Cell should be alive at 1, 0")
# assert(@universe.alive?(-1, 0) == false, "Cell should be dead at -1, 0")
end
def test_a_cell_with_three_neighbors_has_three_neighbors
# assert(@universe.number_of_neighbors(0, 0) == 3, 'Cell at 0, 0 should have 3 neighbors.')
end
def test_determine_which_cells_to_test
cells_to_test = [
[0, 0],
[0, 1],
[1, 0],
[1, 1],
[2, -1],
[2, 0],
[2, 1],
[2, 2],
[1, -1],
[1, 2],
[0, -1],
[0, 2],
[-1, -1],
[-1, 0],
[-1, 1],
[-1, 2]
]
assert(@universe.cells_to_test == cells_to_test, 'Blah')
end
def test_cells_around_cell
cells_around_zero_zero = [[1, -1],
[1, 0],
[1, 1],
[0, -1],
[0, 1],
[-1, -1],
[-1, 0],
[-1, 1]]
assert( arrays_equal(@universe.cells_around_cell(0, 0), cells_around_zero_zero ), "Cells around 0, 0 should be the same as we expect.")
end
def arrays_equal(array_one, array_two)
array_one.each_with_index do |array_one_coord, i|
false if array_one_coord[0] == array_two[i][0]
false if array_one_coord[1] == array_two[i][1]
end
return true
end
def teardown
end
end