Skip to main content
golfed to 314 characters, upped grid size to 56x56 (for free!) and fixed a bug with extra cells in the first iteration
Source Link
Paul Prestidge
  • 2.4k
  • 15
  • 21

Ruby 1.9 + SDL (380 325325 314)

EDIT: 314 characters, and fixed a bug with extra cells appearing alive on the first iteration. Upped the grid size to 56 since the color routine only looks at the lowest 8 bits.

EDIT: Golfed down to 325 characters. Grid width/height is now 28 since 28*9 is the largest you can have while still using the value as the background colour. It also processes only one SDL event per iteration now, which obviates the inner loop completely. Pretty tight I think!

The simulation starts paused, with all cells dead. You can press any key to toggle pause/unpause, and click any cell to toggle it between alive and dead. Runs an iteration every tenth of a second.

The wrapping is a bit wonky.

require'sdl'
W=28
R,b,p=0..W*W,[]
SDL.init 32W=56
R=0..T=W*W
b=[]
s=SDL::Screen.open S=W*9,S,0,0
loop{r="#{e=SDL::Event.poll
r=e.class.name}"
r['yU']?p=!p $_^=1:r[?Q]?exit: r['nU']?b[e.y/9*W+e.x/9]^=1:0
b=R.map{|i|v=[~W,-W,-2755,-1,1,2755,W,29]57].select{|f||f|b[(b+bi+f)[i+f]%T]}.size;v==3||v==2&&b[i]}if pif$_
R.map{|i|s.fillRect i%W*9,i/W*9,9,9,[b[i]?0:S]*3}
s.flip
sleep 0.1}

Looks like this:

Screenshot of the app in action

Fun challenge! I welcome any improvements anybody can see.

Ruby 1.9 + SDL (380 325)

EDIT: Golfed down to 325 characters. Grid width/height is now 28 since 28*9 is the largest you can have while still using the value as the background colour. It also processes only one SDL event per iteration now, which obviates the inner loop completely. Pretty tight I think!

The simulation starts paused, with all cells dead. You can press any key to toggle pause/unpause, and click any cell to toggle it between alive and dead. Runs an iteration every tenth of a second.

The wrapping is a bit wonky.

require'sdl'
W=28
R,b,p=0..W*W,[]
SDL.init 32
s=SDL::Screen.open S=W*9,S,0,0
loop{e=SDL::Event.poll
r=e.class.name
r['yU']?p=!p :r[?Q]?exit: r['nU']?b[e.y/9*W+e.x/9]^=1:0
b=R.map{|i|v=[~W,-W,-27,-1,1,27,W,29].select{|f|(b+b)[i+f]}.size;v==3||v==2&&b[i]}if p
R.map{|i|s.fillRect i%W*9,i/W*9,9,9,[b[i]?0:S]*3}
s.flip
sleep 0.1}

Looks like this:

Screenshot of the app in action

Fun challenge! I welcome any improvements anybody can see.

Ruby 1.9 + SDL (380 325 314)

EDIT: 314 characters, and fixed a bug with extra cells appearing alive on the first iteration. Upped the grid size to 56 since the color routine only looks at the lowest 8 bits.

EDIT: Golfed down to 325 characters. Grid width/height is now 28 since 28*9 is the largest you can have while still using the value as the background colour. It also processes only one SDL event per iteration now, which obviates the inner loop completely. Pretty tight I think!

The simulation starts paused, with all cells dead. You can press any key to toggle pause/unpause, and click any cell to toggle it between alive and dead. Runs an iteration every tenth of a second.

The wrapping is a bit wonky.

require'sdl'
SDL.init W=56
R=0..T=W*W
b=[]
s=SDL::Screen.open S=W*9,S,0,0
loop{r="#{e=SDL::Event.poll}"
r['yU']?$_^=1:r[?Q]?exit: r['nU']?b[e.y/9*W+e.x/9]^=1:0
b=R.map{|i|v=[~W,-W,-55,-1,1,55,W,57].select{|f|b[(i+f)%T]}.size;v==3||v==2&&b[i]}if$_
R.map{|i|s.fillRect i%W*9,i/W*9,9,9,[b[i]?0:S]*3}
s.flip
sleep 0.1}

Looks like this:

Screenshot of the app in action

Fun challenge! I welcome any improvements anybody can see.

golfed code to 325 characters, various improvements and fixes
Source Link
Paul Prestidge
  • 2.4k
  • 15
  • 21

Ruby 1.9 + SDL (380380 325)

EDIT: Golfed down to 325 characters. Grid width/height is now 28 since 28*9 is the largest you can have while still using the value as the background colour. It also processes only one SDL event per iteration now, which obviates the inner loop completely. Pretty tight I think!

The simulation starts paused, with all cells dead. You can press any key to toggle pause/unpause, and click any cell to toggle it between alive and dead. Runs an iteration every tenth of a second.

The wrapping is a bit wonky.

require'sdl'
L=SDL
E=L::Event2W=28
XR,b,p=0..400
b=[]W*W,[]
LSDL.init L::INIT_VIDEO32
s=Ls=SDL::setVideoModeScreen.open S=180S=W*9,S,160,L::SWSURFACE0
loop {while e=Ee=SDL::Event.poll;r=epoll
r=e.class.name;p=name
r['yU']?p=!p if r['yU'];exit if :r[?Q];b[o=eQ]?exit: r['nU']?b[e.y/9*20+e9*W+e.x/9]=!b[o] if r['nU']end9]^=1:0
b=Xb=R.map{|i|v=[-21|i|v=[~W,-20W,-1927,-1,0,1,1927,20W,21]29].select{|f|b[|f|(i+fb+b)%400][i+f]}.size;v==3||v==4&&b[i]size;v==3||v==2&&b[i]}if p
XR.map{|i|s.fillRect(i%20*9 i%W*9,i/20*9W*9,9,9,[b[i]?0:S]*3)}
s.flip
sleep 0.1}

Looks like this:

Screenshot of the app in actionScreenshot of the app in action

Fun challenge! I welcome any improvements anybody can see.

Ruby 1.9 + SDL (380)

The simulation starts paused, with all cells dead. You can press any key to toggle pause/unpause, and click any cell to toggle it between alive and dead. Runs an iteration every tenth of a second.

The wrapping is a bit wonky.

require'sdl'
L=SDL
E=L::Event2
X,p=0..400
b=[]
L.init L::INIT_VIDEO
s=L::setVideoMode S=180,S,16,L::SWSURFACE
loop {while e=E.poll;r=e.class.name;p=!p if r['yU'];exit if r[?Q];b[o=e.y/9*20+e.x/9]=!b[o] if r['nU']end
b=X.map{|i|v=[-21,-20,-19,-1,0,1,19,20,21].select{|f|b[(i+f)%400]}.size;v==3||v==4&&b[i]}if p
X.map{|i|s.fillRect(i%20*9,i/20*9,9,9,[b[i]?0:S]*3)}
s.flip
sleep 0.1}

Looks like this:

Screenshot of the app in action

Fun challenge! I welcome any improvements anybody can see.

Ruby 1.9 + SDL (380 325)

EDIT: Golfed down to 325 characters. Grid width/height is now 28 since 28*9 is the largest you can have while still using the value as the background colour. It also processes only one SDL event per iteration now, which obviates the inner loop completely. Pretty tight I think!

The simulation starts paused, with all cells dead. You can press any key to toggle pause/unpause, and click any cell to toggle it between alive and dead. Runs an iteration every tenth of a second.

The wrapping is a bit wonky.

require'sdl'
W=28
R,b,p=0..W*W,[]
SDL.init 32
s=SDL::Screen.open S=W*9,S,0,0
loop{e=SDL::Event.poll
r=e.class.name
r['yU']?p=!p :r[?Q]?exit: r['nU']?b[e.y/9*W+e.x/9]^=1:0
b=R.map{|i|v=[~W,-W,-27,-1,1,27,W,29].select{|f|(b+b)[i+f]}.size;v==3||v==2&&b[i]}if p
R.map{|i|s.fillRect i%W*9,i/W*9,9,9,[b[i]?0:S]*3}
s.flip
sleep 0.1}

Looks like this:

Screenshot of the app in action

Fun challenge! I welcome any improvements anybody can see.

Source Link
Paul Prestidge
  • 2.4k
  • 15
  • 21

Ruby 1.9 + SDL (380)

The simulation starts paused, with all cells dead. You can press any key to toggle pause/unpause, and click any cell to toggle it between alive and dead. Runs an iteration every tenth of a second.

The wrapping is a bit wonky.

require'sdl'
L=SDL
E=L::Event2
X,p=0..400
b=[]
L.init L::INIT_VIDEO
s=L::setVideoMode S=180,S,16,L::SWSURFACE
loop {while e=E.poll;r=e.class.name;p=!p if r['yU'];exit if r[?Q];b[o=e.y/9*20+e.x/9]=!b[o] if r['nU']end
b=X.map{|i|v=[-21,-20,-19,-1,0,1,19,20,21].select{|f|b[(i+f)%400]}.size;v==3||v==4&&b[i]}if p
X.map{|i|s.fillRect(i%20*9,i/20*9,9,9,[b[i]?0:S]*3)}
s.flip
sleep 0.1}

Looks like this:

Screenshot of the app in action

Fun challenge! I welcome any improvements anybody can see.