-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgol.rb
353 lines (291 loc) · 7.16 KB
/
gol.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#
# Conway's game of life in Gosu/Chingu
# http://toastymofo.blogspot.com/2010/06/conways-game-of-life-in-ruby-gosu.html
#
# Developed by r.kachowski ( http://www.toastymofo.net/ )
# Additions by ippa ( http://ippa.se/gaming )
# Additions by Roberto Zanon ( https://github.com/robertoz-01 )
#
require 'rubygems' rescue nil
require 'chingu'
class Main < Chingu::Window
def initialize
super(640, 480, false)
self.input={:esc => :exit}
push_game_state(GameOfLife)
end
def draw
fill_rect([0, 0, 640, 480], 0xffffffff, -2)
super
end
end
class GameOfLife < Chingu::GameState
CELL_SIZE = 4
@@tick =0
def initialize
super
@grid = generate_grid
self.input={:left_mouse_button => :start_painting,
:released_left_mouse_button => :stop_painting,
:right_mouse_button => :start_erasing,
:released_right_mouse_button => :stop_erasing,
:z => :reset,
:n => :update_grid,
:space => :toggle_running,
[:mouse_wheel_up, :left_arrow] => :prev_pattern,
[:mouse_wheel_down, :right_arrow] => :next_pattern,
:up_arrow => :next_color,
:down_arrow => :prev_color
}
@pattern = :pixel
@pattern_nr = 0
@painting = false
@erasing = false
@running = false
@current_color_idx = 0
@pattern_info = Chingu::Text.create(:x => 1, :y => 1, :size => 16, :color => Gosu::Color::BLACK)
update_pattern_info
end
def update_pattern_info
@pattern_info.text = "Current pattern: #{@pattern}. Current color: #{current_color_name}"
end
def prev_pattern
@pattern_nr -= 1
@pattern_nr = PATTERNS.keys.size-1 if @pattern_nr < 0
@pattern = PATTERNS.keys[@pattern_nr]
update_pattern_info
end
def next_pattern
@pattern_nr += 1
@pattern_nr = 0 if @pattern_nr >= PATTERNS.keys.size
@pattern = PATTERNS.keys[@pattern_nr]
update_pattern_info
end
def prev_color
@current_color_idx += 1
@current_color_idx %= colors.length
update_pattern_info
end
def next_color
@current_color_idx -= 1
@current_color_idx %= colors.length
update_pattern_info
end
def draw_pattern_at_mouse(pattern = :pixel, to_grid = false)
start_x = ($window.mouse_x/CELL_SIZE).floor
y = ($window.mouse_y/CELL_SIZE).floor - 1
PATTERNS[pattern].each_line do |line|
x = start_x
line.each_char do |char|
@grid[x][y] = current_color if char == 'o' && to_grid
draw_cell(x, y) if char == 'o'
x += 1
end
y += 1
end
end
def update
super
update_grid if @running
$window.caption = "Conway Generation #{@@tick}. Start/Stop with 'Space'. Run 1 generation with 'N'. Reset with 'Z'."
end
def draw
super
draw_grid
if @painting
draw_pattern_at_mouse(@pattern, true)
@painting = false if @running # Only put out pattern Once if game is running
else
draw_pattern_at_mouse(@pattern)
end
end
private
def generate_grid
width = $window.width/CELL_SIZE
height = $window.height/CELL_SIZE
grid = Array.new(width)
col = Array.new(height)
col.map! { false }
grid.map! { Array.new(col) }
grid
end
def draw_grid
@grid.each_with_index do |a, x|
a.each_with_index do |c, y|
if c != false
$window.fill_rect([x*CELL_SIZE, y*CELL_SIZE, CELL_SIZE, CELL_SIZE], @grid[x][y], 0)
end
end
end
end
def reset
@grid = generate_grid
@@tick = 0
@running = false
end
def update_grid
@new_grid = Marshal.load(Marshal.dump(@grid))
@grid.each_with_index do |a, x|
a.each_with_index do |c, y|
minus_x =x-1
minus_y = y-1
plus_x = x+1
plus_y = y+1
minus_x = @grid.length-1 if minus_x <0
minus_y = a.length-1 if minus_y <0
plus_y = 0 if plus_y >= a.length
plus_x = 0 if plus_x >= @grid.length
live_neighbours = {}
update_neighbours_count(live_neighbours, minus_x, y)
update_neighbours_count(live_neighbours, plus_x, y)
update_neighbours_count(live_neighbours, x, minus_y)
update_neighbours_count(live_neighbours, x, plus_y)
update_neighbours_count(live_neighbours, minus_x, plus_y)
update_neighbours_count(live_neighbours, plus_x, minus_y)
update_neighbours_count(live_neighbours, minus_x, minus_y)
update_neighbours_count(live_neighbours, plus_x, plus_y)
neighbours_count = live_neighbours.values.inject(0, :+)
case neighbours_count
when 0..1 then
@new_grid[x][y] = false
when 2 then
@new_grid[x][y] = @grid[x][y] if @grid[x][y] != false
when 3 then
@new_grid[x][y] = neighbours_common_color(live_neighbours)
when 4..8 then
@new_grid[x][y] = false
end
@new_grid[x][y] = [*colors, false].sample if Random.rand < 0.00003
end
end
@grid = @new_grid
@@tick+=1
end
def update_neighbours_count(live_neighbours, x, y)
if @grid[x][y] != false
live_neighbours[@grid[x][y]] ||= 0
live_neighbours[@grid[x][y]] +=1
end
end
def neighbours_common_color(live_neighbours)
live_neighbours.sort_by { |color_count1, color_count2| color_count1[1] <=> color_count2[1] }.first[0]
end
def current_color_name
{0xffff0000 => 'RED',
0xff00ff00 => 'GREEN',
0xff0000ff => 'BLUE'}[current_color]
end
def current_color
colors[@current_color_idx]
end
def colors
[0xffff0000, 0xff00ff00, 0xff0000ff]
end
def toggle_running
@running = !@running
end
def start_painting
@painting = true
end
def stop_painting
@painting = false
end
def start_erasing
@erasing = true
end
def stop_erasing
@erasing = false
end
def draw_cell(x, y)
current_with_alpha = (current_color & 0x00ffffff) | 0xaa000000
$window.fill_rect([x*CELL_SIZE, y*CELL_SIZE, CELL_SIZE, CELL_SIZE], current_with_alpha, 1)
end
end
PATTERNS = Hash.new
PATTERNS[:pixel] = %q{
o
}
#
# Spaceships
#
PATTERNS[:glider] = %q{
---o
-o-o
--oo
}
PATTERNS[:lightweight_spaceship] = %q{
-oooo
o---o
----o
o--o-
}
#
# Oscillators
#
PATTERNS[:blinker] = %q{
ooo
}
PATTERNS[:beacon] = %q{
-ooo
ooo-
}
PATTERNS[:toad] = %q{
oo--
o---
---o
--oo
}
PATTERNS[:pulsar] = %q{
---ooo---ooo--
--------------
-o----o-o----o
-o----o-o----o
-o----o-o----o
---ooo---ooo--
--------------
---ooo---ooo--
-o----o-o----o
-o----o-o----o
-o----o-o----o
--------------
---ooo---ooo--
}
#
# Guns
#
PATTERNS[:gospel_glider_gun] = %q{
------------------------o-----------
----------------------o-o-----------
------------oo------oo------------oo
-----------o---o----oo------------oo
oo--------o-----o---oo--------------
oo--------o---o-oo----o-o-----------
----------o-----o-------o-----------
-----------o---o--------------------
------------oo----------------------
}
PATTERNS[:block_laying_switch_engine] = %q{
----------o-o--
oo-------o-----
oo--------o--o-
------------ooo
}
#
# Long lived patterns
#
PATTERNS[:rpentomino] = %q{
--oo
-oo
--o
}
PATTERNS[:diehard] = %q{
oo---o-o
oo----o-
------o-
}
PATTERNS[:acorn] = %q{
--o-----
----o---
-oo--ooo
}
Main.new.show