-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandrew_mark_test.rb
140 lines (108 loc) · 2.87 KB
/
andrew_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
require "test/unit"
X_WIDTH = 150
Y_WIDTH = 150
class World
@state = "." * X_WIDTH * Y_WIDTH
def self.to_s
return @state
end
def self.add (x,y)
@state[y*X_WIDTH+x] = '#'
end
def self.del (x,y)
@state[y*X_WIDTH+x] = '.'
end
def self.num_neighbors (x, y)
count = 0
count +=1 if present?(x-1,y-1)
count +=1 if present?(x-1,y)
count +=1 if present?(x-1,y+1)
count +=1 if present?(x,y-1)
count +=1 if present?(x,y+1)
count +=1 if present?(x+1,y-1)
count +=1 if present?(x+1,y)
count +=1 if present?(x+1,y+1)
count
end
def self.present? (x,y)
@state[y*X_WIDTH+x] == '#'
end
def self.will_be_alive? (x,y)
alive = present?(x,y)
if alive
return true if num_neighbors(x, y) >= 2 && num_neighbors(x, y) <= 3
else
#fkin daead people
return true if num_neighbors(x, y) == 3
end
false
end
def self.iterate
newState = "." * 50
(0..9).each do |x|
(0..4).each do |y|
if will_be_alive?(x,y)
newState[y*10+x] = '#'
end
end
end
@state = newState
end
def set_state(newState)
@state = newState
end
end
class WorldTest < Test::Unit::TestCase
def setup
end
def test_world_has_string_output_and_matches_output
World.add(5, 2)
World.present?(5, 2)
assert World.will_be_alive?(5, 2) == false
World.del(5,2)
assert World.present?(5, 2) == false
World.add(5, 2)
World.add(5, 3)
assert World.num_neighbors(5, 2) == 1
assert World.num_neighbors(5, 3) == 1
World.add(4, 3)
expected2 = ".........." + # 0
".........." + # 1
".....#...." + # 2
"....##...." + # 3
".........." # 4
#0123456789
assert World.num_neighbors(5, 3) == 2
assert World.will_be_alive?(4, 2) == true
# 3 neighbors living. <-- this goes above
expected2 = ".........." + # 0
".........." + # 1
".....#...." + # 2
"...###...." + # 3
".........." # 4
#0123456789
World.add(3,3)
assert World.present?(4, 2) == false
assert World.will_be_alive?(4, 2) == false
expected2 = ".........." + # 0
".........." + # 1
"....##...." + # 2
"...###...." + # 3
".........." # 4
#0123456789
World.add(4, 2)
assert World.present?(4, 2) == true
assert World.will_be_alive?(4, 2) == false
World.iterate()
assert World.present?(4, 2) == false
end
def teardown
end
end
# expected2 = ".#........" + # 0
# "..#......." + # 1
# "###......." + # 2
# ".........." + # 3
# ".........." # 4
# #0123456789
#World.set_state(expected2)