graphics 1.0.0b1 → 1.0.0b4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/History.rdoc +30 -0
- data/Manifest.txt +39 -7
- data/README.rdoc +48 -4
- data/Rakefile +8 -2
- data/examples/boid.rb +9 -18
- data/examples/bounce.rb +15 -23
- data/examples/canvas.rb +75 -0
- data/examples/collision.rb +6 -6
- data/examples/demo.rb +5 -7
- data/examples/editor.rb +12 -9
- data/examples/fluid.rb +2 -3
- data/examples/fluid2.rb +1 -1
- data/examples/{lito2.rb → gol.rb} +0 -0
- data/examples/{zenspider4.rb → gol2.rb} +0 -0
- data/examples/logo.rb +4 -7
- data/examples/maze.rb +136 -0
- data/examples/tank.rb +10 -11
- data/examples/tank2.rb +12 -17
- data/examples/targeting.rb +1 -1
- data/examples/vants.rb +1 -1
- data/examples/walker.rb +3 -12
- data/examples/walker2.rb +197 -0
- data/examples/zombies.rb +31 -35
- data/ext/sdl/extconf.rb +31 -0
- data/ext/sdl/sdl.c +1067 -0
- data/ext/sdl/sge/INSTALL +72 -0
- data/ext/sdl/sge/LICENSE +504 -0
- data/ext/sdl/sge/Makefile +83 -0
- data/ext/sdl/sge/Makefile.conf +63 -0
- data/ext/sdl/sge/README +219 -0
- data/ext/sdl/sge/Todo +7 -0
- data/ext/sdl/sge/WhatsNew +224 -0
- data/ext/sdl/sge/sge.h +31 -0
- data/ext/sdl/sge/sge_blib.cpp +1939 -0
- data/ext/sdl/sge/sge_blib.h +68 -0
- data/ext/sdl/sge/sge_bm_text.cpp +451 -0
- data/ext/sdl/sge/sge_bm_text.h +71 -0
- data/ext/sdl/sge/sge_collision.cpp +388 -0
- data/ext/sdl/sge/sge_collision.h +54 -0
- data/ext/sdl/sge/sge_config.h +6 -0
- data/ext/sdl/sge/sge_internal.h +152 -0
- data/ext/sdl/sge/sge_misc.cpp +92 -0
- data/ext/sdl/sge/sge_misc.h +37 -0
- data/ext/sdl/sge/sge_primitives.cpp +2516 -0
- data/ext/sdl/sge/sge_primitives.h +111 -0
- data/ext/sdl/sge/sge_rotation.cpp +683 -0
- data/ext/sdl/sge/sge_rotation.h +46 -0
- data/ext/sdl/sge/sge_shape.cpp +762 -0
- data/ext/sdl/sge/sge_shape.h +365 -0
- data/ext/sdl/sge/sge_surface.cpp +1090 -0
- data/ext/sdl/sge/sge_surface.h +100 -0
- data/ext/sdl/sge/sge_textpp.cpp +785 -0
- data/ext/sdl/sge/sge_textpp.h +270 -0
- data/ext/sdl/sge/sge_tt_text.cpp +1456 -0
- data/ext/sdl/sge/sge_tt_text.h +114 -0
- data/graphics_setup.sh +26 -0
- data/lib/graphics.rb +1 -1
- data/lib/graphics/body.rb +50 -3
- data/lib/graphics/extensions.rb +13 -7
- data/lib/graphics/simulation.rb +126 -46
- data/test/test_graphics.rb +52 -12
- data/test/test_sdl.rb +1 -0
- metadata +54 -23
- metadata.gz.sig +0 -0
- data/.gemtest +0 -0
- data/examples/lito.rb +0 -108
- data/examples/zenspider1.rb +0 -93
- data/examples/zenspider2.rb +0 -123
- data/examples/zenspider3.rb +0 -104
- data/rubysdl_setup.sh +0 -34
data/test/test_graphics.rb
CHANGED
@@ -190,6 +190,42 @@ class TestBody < Minitest::Test
|
|
190
190
|
|
191
191
|
assert_body 0, 50, 10, 0, 0, b # TODO: maybe should be 9?
|
192
192
|
end
|
193
|
+
|
194
|
+
def test_angle_to
|
195
|
+
# b is at 50, 50
|
196
|
+
|
197
|
+
b2 = Graphics::Body.new w
|
198
|
+
|
199
|
+
b2.x, b2.y = 60, 50
|
200
|
+
assert_in_epsilon 0, b.angle_to(b2)
|
201
|
+
|
202
|
+
b2.x, b2.y = 50, 40
|
203
|
+
assert_in_epsilon 270, b.angle_to(b2)
|
204
|
+
|
205
|
+
b2.x, b2.y = 60, 60
|
206
|
+
assert_in_epsilon 45, b.angle_to(b2)
|
207
|
+
|
208
|
+
b2.x, b2.y = 0, 0
|
209
|
+
assert_in_epsilon 225, b.angle_to(b2)
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_distance_to_squared
|
213
|
+
# b is at 50, 50
|
214
|
+
|
215
|
+
b2 = Graphics::Body.new w
|
216
|
+
|
217
|
+
b2.x, b2.y = 60, 50
|
218
|
+
assert_in_epsilon 100, b.distance_to_squared(b2)
|
219
|
+
|
220
|
+
b2.x, b2.y = 50, 40
|
221
|
+
assert_in_epsilon 100, b.distance_to_squared(b2)
|
222
|
+
|
223
|
+
b2.x, b2.y = 60, 60
|
224
|
+
assert_in_epsilon((10*Math.sqrt(2))**2, b.distance_to_squared(b2))
|
225
|
+
|
226
|
+
b2.x, b2.y = 0, 0
|
227
|
+
assert_in_epsilon((50*Math.sqrt(2))**2, b.distance_to_squared(b2))
|
228
|
+
end
|
193
229
|
end
|
194
230
|
|
195
231
|
class TestInteger < Minitest::Test
|
@@ -234,7 +270,7 @@ class TestSimulation < Minitest::Test
|
|
234
270
|
|
235
271
|
s = []
|
236
272
|
|
237
|
-
def s.method_missing
|
273
|
+
def s.method_missing *a
|
238
274
|
@data ||= []
|
239
275
|
@data << a
|
240
276
|
end
|
@@ -244,7 +280,6 @@ class TestSimulation < Minitest::Test
|
|
244
280
|
end
|
245
281
|
|
246
282
|
self.screen = s
|
247
|
-
|
248
283
|
end
|
249
284
|
end
|
250
285
|
|
@@ -257,21 +292,23 @@ class TestSimulation < Minitest::Test
|
|
257
292
|
end
|
258
293
|
|
259
294
|
def test_angle
|
295
|
+
h = t.h-1
|
296
|
+
|
260
297
|
t.angle 50, 50, 0, 10, :white
|
261
|
-
exp << [:draw_line, 50, 50, 60.0, 50.0, white
|
298
|
+
exp << [:draw_line, 50, h-50, 60.0, h-50.0, white]
|
262
299
|
|
263
300
|
t.angle 50, 50, 90, 10, :white
|
264
|
-
exp << [:draw_line, 50,
|
301
|
+
exp << [:draw_line, 50, 49, 50.0, h-60.0, white]
|
265
302
|
|
266
303
|
t.angle 50, 50, 180, 10, :white
|
267
|
-
exp << [:draw_line, 50, 50, 40.0, 50.0, white
|
304
|
+
exp << [:draw_line, 50, h-50, 40.0, h-50.0, white]
|
268
305
|
|
269
306
|
t.angle 50, 50, 270, 10, :white
|
270
|
-
exp << [:draw_line, 50, 50, 50.0,
|
307
|
+
exp << [:draw_line, 50, h-50, 50.0, h-40.0, white]
|
271
308
|
|
272
309
|
t.angle 50, 50, 45, 10, :white
|
273
310
|
d45 = 10 * Math.sqrt(2) / 2
|
274
|
-
exp << [:draw_line, 50, 50, 50+d45, 50-d45, white
|
311
|
+
exp << [:draw_line, 50, h-50, 50+d45, h-50-d45, white]
|
275
312
|
|
276
313
|
assert_equal exp, t.screen.data
|
277
314
|
end
|
@@ -306,7 +343,9 @@ class TestSimulation < Minitest::Test
|
|
306
343
|
|
307
344
|
def test_ellipse
|
308
345
|
t.ellipse 0, 0, 25, 25, :white
|
309
|
-
|
346
|
+
|
347
|
+
h = t.h-1
|
348
|
+
exp << [:draw_ellipse, 0, h, 25, 25, t.color[:white]]
|
310
349
|
|
311
350
|
assert_equal exp, t.screen.data
|
312
351
|
end
|
@@ -329,8 +368,8 @@ class TestSimulation < Minitest::Test
|
|
329
368
|
|
330
369
|
def test_hline
|
331
370
|
t.hline 42, :white
|
332
|
-
h = t.h
|
333
|
-
exp << [:draw_line, 0, h-42, 100, h-42, t.color[:white]
|
371
|
+
h = t.h - 1
|
372
|
+
exp << [:draw_line, 0, h-42, 100, h-42, t.color[:white]]
|
334
373
|
|
335
374
|
assert_equal exp, t.screen.data
|
336
375
|
end
|
@@ -341,13 +380,14 @@ class TestSimulation < Minitest::Test
|
|
341
380
|
|
342
381
|
def test_line
|
343
382
|
t.line 0, 0, 25, 25, :white
|
344
|
-
h = t.h
|
345
|
-
exp << [:draw_line, 0, h
|
383
|
+
h = t.h - 1
|
384
|
+
exp << [:draw_line, 0, h, 25, h-25, t.color[:white]]
|
346
385
|
|
347
386
|
assert_equal exp, t.screen.data
|
348
387
|
end
|
349
388
|
|
350
389
|
def test_point
|
390
|
+
skip "not yet"
|
351
391
|
t.point 2, 10, :white
|
352
392
|
|
353
393
|
exp = [nil, nil, t.color[:white]]
|
data/test/test_sdl.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# TODO
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.0b4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Davis
|
@@ -29,7 +29,7 @@ cert_chain:
|
|
29
29
|
xJcC6UN6NHMOVMyAXsr2HR0gRRx4ofN1LoP2KhXzSr8UMvQYlwPmE0N5GQv1b5AO
|
30
30
|
VpzF30vNaJK6ZT7xlIsIlwmH
|
31
31
|
-----END CERTIFICATE-----
|
32
|
-
date: 2015-
|
32
|
+
date: 2015-09-15 00:00:00.000000000 Z
|
33
33
|
dependencies:
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: rsdl
|
@@ -46,61 +46,61 @@ dependencies:
|
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0.1'
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
|
-
name:
|
49
|
+
name: minitest
|
50
50
|
requirement: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
55
|
-
type: :
|
54
|
+
version: '5.8'
|
55
|
+
type: :development
|
56
56
|
prerelease: false
|
57
57
|
version_requirements: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '5.8'
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
63
|
+
name: rdoc
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '4.0'
|
69
69
|
type: :development
|
70
70
|
prerelease: false
|
71
71
|
version_requirements: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '4.0'
|
76
76
|
- !ruby/object:Gem::Dependency
|
77
|
-
name:
|
77
|
+
name: rake-compiler
|
78
78
|
requirement: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0.9'
|
83
83
|
type: :development
|
84
84
|
prerelease: false
|
85
85
|
version_requirements: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ~>
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '0.9'
|
90
90
|
- !ruby/object:Gem::Dependency
|
91
91
|
name: hoe
|
92
92
|
requirement: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ~>
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '3.
|
96
|
+
version: '3.14'
|
97
97
|
type: :development
|
98
98
|
prerelease: false
|
99
99
|
version_requirements: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ~>
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '3.
|
103
|
+
version: '3.14'
|
104
104
|
description: |-
|
105
105
|
Graphics provides a simple framework to implement games and/or
|
106
106
|
simulations and is designed to follow mathematical conventions, NOT
|
@@ -124,33 +124,64 @@ extra_rdoc_files:
|
|
124
124
|
- README.rdoc
|
125
125
|
files:
|
126
126
|
- .autotest
|
127
|
-
- .gemtest
|
128
127
|
- History.rdoc
|
129
128
|
- Manifest.txt
|
130
129
|
- README.rdoc
|
131
130
|
- Rakefile
|
132
131
|
- examples/boid.rb
|
133
132
|
- examples/bounce.rb
|
133
|
+
- examples/canvas.rb
|
134
134
|
- examples/collision.rb
|
135
135
|
- examples/demo.rb
|
136
136
|
- examples/editor.rb
|
137
137
|
- examples/fluid.rb
|
138
138
|
- examples/fluid2.rb
|
139
|
-
- examples/
|
140
|
-
- examples/
|
139
|
+
- examples/gol.rb
|
140
|
+
- examples/gol2.rb
|
141
141
|
- examples/logo.rb
|
142
142
|
- examples/math.rb
|
143
|
+
- examples/maze.rb
|
143
144
|
- examples/radar.rb
|
144
145
|
- examples/tank.rb
|
145
146
|
- examples/tank2.rb
|
146
147
|
- examples/targeting.rb
|
147
148
|
- examples/vants.rb
|
148
149
|
- examples/walker.rb
|
149
|
-
- examples/
|
150
|
-
- examples/zenspider2.rb
|
151
|
-
- examples/zenspider3.rb
|
152
|
-
- examples/zenspider4.rb
|
150
|
+
- examples/walker2.rb
|
153
151
|
- examples/zombies.rb
|
152
|
+
- ext/sdl/extconf.rb
|
153
|
+
- ext/sdl/sdl.c
|
154
|
+
- ext/sdl/sge/INSTALL
|
155
|
+
- ext/sdl/sge/LICENSE
|
156
|
+
- ext/sdl/sge/Makefile
|
157
|
+
- ext/sdl/sge/Makefile.conf
|
158
|
+
- ext/sdl/sge/README
|
159
|
+
- ext/sdl/sge/Todo
|
160
|
+
- ext/sdl/sge/WhatsNew
|
161
|
+
- ext/sdl/sge/sge.h
|
162
|
+
- ext/sdl/sge/sge_blib.cpp
|
163
|
+
- ext/sdl/sge/sge_blib.h
|
164
|
+
- ext/sdl/sge/sge_bm_text.cpp
|
165
|
+
- ext/sdl/sge/sge_bm_text.h
|
166
|
+
- ext/sdl/sge/sge_collision.cpp
|
167
|
+
- ext/sdl/sge/sge_collision.h
|
168
|
+
- ext/sdl/sge/sge_config.h
|
169
|
+
- ext/sdl/sge/sge_internal.h
|
170
|
+
- ext/sdl/sge/sge_misc.cpp
|
171
|
+
- ext/sdl/sge/sge_misc.h
|
172
|
+
- ext/sdl/sge/sge_primitives.cpp
|
173
|
+
- ext/sdl/sge/sge_primitives.h
|
174
|
+
- ext/sdl/sge/sge_rotation.cpp
|
175
|
+
- ext/sdl/sge/sge_rotation.h
|
176
|
+
- ext/sdl/sge/sge_shape.cpp
|
177
|
+
- ext/sdl/sge/sge_shape.h
|
178
|
+
- ext/sdl/sge/sge_surface.cpp
|
179
|
+
- ext/sdl/sge/sge_surface.h
|
180
|
+
- ext/sdl/sge/sge_textpp.cpp
|
181
|
+
- ext/sdl/sge/sge_textpp.h
|
182
|
+
- ext/sdl/sge/sge_tt_text.cpp
|
183
|
+
- ext/sdl/sge/sge_tt_text.h
|
184
|
+
- graphics_setup.sh
|
154
185
|
- lib/graphics.rb
|
155
186
|
- lib/graphics/body.rb
|
156
187
|
- lib/graphics/extensions.rb
|
@@ -159,9 +190,9 @@ files:
|
|
159
190
|
- lib/graphics/v.rb
|
160
191
|
- resources/images/body.png
|
161
192
|
- resources/images/turret.png
|
162
|
-
- rubysdl_setup.sh
|
163
193
|
- test/test_graphics.rb
|
164
|
-
|
194
|
+
- test/test_sdl.rb
|
195
|
+
homepage: https://github.com/zenspider/graphics
|
165
196
|
licenses:
|
166
197
|
- MIT
|
167
198
|
metadata: {}
|
metadata.gz.sig
CHANGED
Binary file
|
data/.gemtest
DELETED
File without changes
|
data/examples/lito.rb
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
require 'matrix'
|
2
|
-
|
3
|
-
class Matrix
|
4
|
-
def rotate x, y
|
5
|
-
# I can't find a neat matrix-math solution for
|
6
|
-
# this, so let's do it with regular 'ol `map`.
|
7
|
-
Matrix[ *self.to_a.rotate(y).map {|row| row.rotate x} ]
|
8
|
-
end
|
9
|
-
|
10
|
-
# Pad or shrink a matrix
|
11
|
-
def take x, y
|
12
|
-
Matrix.build(y, x){|i, j| if self[i, j].nil? then 0 else self[i, j] end }
|
13
|
-
end
|
14
|
-
|
15
|
-
# Bitwise operations on boolean matrices
|
16
|
-
def & other
|
17
|
-
Matrix.Raise ErrDimensionMismatch unless
|
18
|
-
self.row_count == other.row_count and
|
19
|
-
self.column_count == other.column_count
|
20
|
-
|
21
|
-
Matrix.build(self.row_count){|i, j| self[i, j] & other[i, j] }
|
22
|
-
end
|
23
|
-
|
24
|
-
def | other
|
25
|
-
Matrix.Raise ErrDimensionMismatch unless
|
26
|
-
self.row_count == other.row_count and
|
27
|
-
self.column_count == other.column_count
|
28
|
-
|
29
|
-
Matrix.build(self.row_count){|i, j| self[i, j] | other[i, j] }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def sum l
|
34
|
-
l.reduce :+
|
35
|
-
end
|
36
|
-
|
37
|
-
def twos grid
|
38
|
-
grid.map{|i| if i == 2 then 1 else 0 end}
|
39
|
-
end
|
40
|
-
|
41
|
-
def threes grid
|
42
|
-
grid.map{|i| if i == 3 then 1 else 0 end}
|
43
|
-
end
|
44
|
-
|
45
|
-
AROUND = [-1, 0, 1].product([-1, 0, 1])
|
46
|
-
|
47
|
-
def neighbors grid
|
48
|
-
sum(AROUND.map{|x, y| grid.rotate x, y } ) - grid
|
49
|
-
end
|
50
|
-
|
51
|
-
def life grid
|
52
|
-
((twos neighbors grid) & grid) | (threes neighbors grid)
|
53
|
-
end
|
54
|
-
|
55
|
-
size, width, count = 10, 64, 256
|
56
|
-
|
57
|
-
require "sdl"
|
58
|
-
|
59
|
-
SDL.init SDL::INIT_VIDEO
|
60
|
-
SDL::WM::set_caption "Conway's Game of Life", "Conway's Game of Life"
|
61
|
-
|
62
|
-
screen = SDL::Screen.open 640, 640, 16, SDL::HWSURFACE|SDL::DOUBLEBUF
|
63
|
-
|
64
|
-
black = screen.format.map_rgb 0, 0, 0
|
65
|
-
white = screen.format.map_rgb 255, 255, 255
|
66
|
-
|
67
|
-
w, h = screen.w, screen.h
|
68
|
-
|
69
|
-
matrix = Matrix[[1, 1, 1],
|
70
|
-
[0, 0, 1],
|
71
|
-
[1, 1, 1]].take(width, width).rotate(-(width/2), -(width/2))
|
72
|
-
|
73
|
-
paused = false
|
74
|
-
step = false
|
75
|
-
(1..(1.0/0)).each do |n|
|
76
|
-
puts n if n % 100 == 0
|
77
|
-
|
78
|
-
screen.fill_rect 0, 0, w, h, black
|
79
|
-
|
80
|
-
matrix.to_a.each_with_index do |row, y|
|
81
|
-
row.each_with_index do |c, x|
|
82
|
-
if c == 1 then
|
83
|
-
screen.fill_rect x*size, y*size, size-1, size-1, white
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
screen.flip
|
89
|
-
|
90
|
-
while event = SDL::Event.poll
|
91
|
-
case event
|
92
|
-
when SDL::Event::KeyDown then
|
93
|
-
c = event.sym.chr
|
94
|
-
exit if c == "q" or c == "Q" or c == "\e"
|
95
|
-
step = true if c == " "
|
96
|
-
puts n
|
97
|
-
paused = ! paused
|
98
|
-
when SDL::Event::Quit then
|
99
|
-
exit
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
matrix = life matrix unless paused
|
104
|
-
if step then
|
105
|
-
paused = true
|
106
|
-
step = false
|
107
|
-
end
|
108
|
-
end
|
data/examples/zenspider1.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
#!/usr/bin/ruby -w
|
2
|
-
|
3
|
-
srand 42
|
4
|
-
|
5
|
-
class GameOfLife
|
6
|
-
attr_accessor :cells
|
7
|
-
attr_accessor :cache
|
8
|
-
|
9
|
-
def initialize
|
10
|
-
self.cells = []
|
11
|
-
end
|
12
|
-
|
13
|
-
def randomize n, m
|
14
|
-
dimensions = n.times.to_a
|
15
|
-
cells.replace dimensions.product(dimensions).sample(m).sort
|
16
|
-
end
|
17
|
-
|
18
|
-
def run max = 1.0 / 0
|
19
|
-
(1..max).each do |n|
|
20
|
-
yield n
|
21
|
-
update
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def update
|
26
|
-
cells.replace considered.select { |(x, y)| alive? x, y }.sort
|
27
|
-
end
|
28
|
-
|
29
|
-
def considered
|
30
|
-
cells.map { |(x, y)| neighbors_for(x, y) }.flatten(1).uniq
|
31
|
-
end
|
32
|
-
|
33
|
-
MIN = { true => 2, false => 3 }
|
34
|
-
|
35
|
-
def alive? x, y
|
36
|
-
count = (neighbors_for(x, y) & cells).size
|
37
|
-
min = MIN[cells.include? [x, y]]
|
38
|
-
count.between? min, 3
|
39
|
-
end
|
40
|
-
|
41
|
-
delta = [-1, 0, 1]
|
42
|
-
same = [0, 0]
|
43
|
-
DELTAS = (delta.product(delta) - [same])
|
44
|
-
|
45
|
-
@@neighbors = Hash.new { |h, k| h[k] = {} }
|
46
|
-
|
47
|
-
def neighbors_for x, y
|
48
|
-
DELTAS.map { |(dx, dy)| [x+dx, y+dy] }.reject { |(m, n)| m < 0 || n < 0 }
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
size, width, count = 20, 32, 512
|
53
|
-
|
54
|
-
gol = GameOfLife.new
|
55
|
-
gol.randomize width, count
|
56
|
-
|
57
|
-
if ARGV.first == "prof" then
|
58
|
-
gol.run 50 do |n|
|
59
|
-
$stderr.print "."
|
60
|
-
end
|
61
|
-
warn "done"
|
62
|
-
else
|
63
|
-
require "sdl"
|
64
|
-
|
65
|
-
SDL.init SDL::INIT_VIDEO
|
66
|
-
SDL::WM::set_caption "Conway's Game of Life", "Conway's Game of Life"
|
67
|
-
|
68
|
-
screen = SDL::Screen.open 640, 640, 16, SDL::DOUBLEBUF
|
69
|
-
|
70
|
-
black = screen.format.map_rgb 0, 0, 0
|
71
|
-
white = screen.format.map_rgb 255, 255, 255
|
72
|
-
|
73
|
-
w, h = screen.w, screen.h
|
74
|
-
|
75
|
-
gol.run do
|
76
|
-
screen.fill_rect 0, 0, w, h, black
|
77
|
-
|
78
|
-
gol.cells.each do |(x, y)|
|
79
|
-
screen.fill_rect x*size, y*size, size-1, size-1, white
|
80
|
-
end
|
81
|
-
|
82
|
-
screen.flip
|
83
|
-
|
84
|
-
while event = SDL::Event.poll
|
85
|
-
case event
|
86
|
-
when SDL::Event::KeyDown, SDL::Event::Quit
|
87
|
-
exit
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
gol.update
|
92
|
-
end
|
93
|
-
end
|