-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_cell.rb
More file actions
executable file
·200 lines (155 loc) · 3.75 KB
/
class_cell.rb
File metadata and controls
executable file
·200 lines (155 loc) · 3.75 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def ar2init(x,y,s = nil)
Array.new(x).map{Array.new(y, s)}
end
class Cell
require "./cell_rules"
attr_reader :x, :y
attr_accessor :state
@@rule = Rules.new(birth:[4,6,7,8], stable:[3,4,6,7,8])
@@ww = Window.width - 120
@@wh = Window.height
@@size = 9
@@num_x = ((@@ww-1)/(@@size+1).to_i)
@@num_y = ((@@wh-1)/(@@size+1).to_i)
@@image = Image.new(@@ww,@@wh,[0,0,0])
@@generation = 0
@@live = Image.new(@@size,@@size,[20,20,20]).circle(@@size/2,@@size/2,@@size/2,[0,255,0])
@@dead = Image.new(@@size,@@size,[20,20,20])
def initialize(x, y, state = false)
@x = x
@y = y
@state = state ? @@live : @@dead
end
def set
self.state = @@live
end
def self.draw(cells)
cells.each do |c|
c.each do |cell|
Window.draw(cell.x, cell.y, cell.state)
end
end
end
=begin
def self.draw(cells)
cells.each do |c|
c.each do |cell|
if cell.state == @@live
@@image[cell.x, cell.y] = [0,200,0]
else
@@image[cell.x, cell.y] = [0,0,0]
end
end
end
Window.draw(0,0,@@image)
end
=end
def self.state_update(cells)
next_cell_state = ar2init(@@num_x, @@num_y)
adjoin_num = get_adjoin_cells_nun(cells)
@@num_x.times do |x|
@@num_y.times do |y|
if @@rule.birth?(adjoin_num[x][y]) && cells[x][y].state == @@dead
next_cell_state[x][y] = @@live
elsif @@rule.stable?(adjoin_num[x][y]) && cells[x][y].state == @@live
next_cell_state[x][y] = @@live
else
next_cell_state[x][y] = @@dead
end
end
end
@@num_x.times do |x|
@@num_y.times do |y|
cells[x][y].state = next_cell_state[x][y]
end
end
@@generation += 1
end
def self.get_adjoin_cells_nun(cells)
cells_num = ar2init(@@num_x, @@num_y, 0)
a_x = [1, 1, 1, 0, -1, -1, -1, 0]
a_y = [1, 0, -1, -1, -1, 0, 1, 1]
@@num_x.times do |x|
@@num_y.times do |y|
if cells[x][y].state == @@live
8.times do |i|
tx = (x + a_x[i]) % @@num_x
ty = (y + a_y[i]) % @@num_y
cells_num[tx][ty] += 1
end
end
end
end
return cells_num
end
def self.test(cells)
p get_adjoin_cells_nun(cells)
end
def self.get_input(cells)
end
def self.rule_update
case @@generation
when 200
@@rule = Rules.new(birth:[1,2,4], stable:[2,3,4,5,6,7])
when 300
@@rule = Rules.new(birth:[5,8], stable:[2,4,5,8])
when 400
@@rule = Rules.new(birth:[1,2,4], stable:[2,3,4,5,6,7])
when 500
@@rule = Rules.new(birth:[8], stable:[1,2,3,4,5,7,8])
when 600
@@rule = Rules.new(birth:[2,3], stable:[1,2,3,4,5,6])
when 700
@@rule = Rules.new(birth:[], stable:[2,3])
when 800
@@rule = Rules.new(birth:[3], stable:[3])
when 900
@@rule = Rules.new(birth:[1,2,4], stable:[2,3,4,5,6,7])
when 1000
@@rule = Rules.new(birth:[], stable:[2,3])
when 1100
@@rule = Rules.new(birth:[3], stable:[2,3])
when 1200
@@rule = Rules.new(birth:[1,3,5,6], stable:[1,3,5,6,7,8])
when 1300
@@rule = Rules.new(birth:[8], stable:[1,8])
when 1400
@@rule = Rules.new(birth:[2], stable:[2,4,6,8])
when 1500
@@rule = Rules.new(birth:[3,6,7,8], stable:[3,4,6,7,8])
when 1800
@@rule = Rules.new(birth:[6,7,8], stable:[3,4,6,7,8])
when 1900
@@rule = Rules.new(birth:[2], stable:[2,3,4])
when 2100
@@rule = Rules.new(birth:[2], stable:[2,3])
when 2300
@@rule = Rules.new(birth:[3,6,7,8], stable:[3,4,6,7,8])
when 2500
@@rule = Rules.new(birth:[3], stable:[2,3])
when 2800
@@generation = 0
end
end
def self.size
@@size
end
def self.num_x
@@num_x
end
def self.num_y
@@num_y
end
def self.dead
@@dead
end
def self.live
@@live
end
def self.size=(n)
@@size=n
end
def self.generation
@@generation
end
end