Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Created June 12, 2012 11:39
Show Gist options
  • Save pachacamac/2917027 to your computer and use it in GitHub Desktop.
Save pachacamac/2917027 to your computer and use it in GitHub Desktop.
Wireworld
#!/usr/bin/env ruby
a=[
' ',
' ......-+ ',
' . ...... ',
' ...+-... . ',
' .... ',
' . ..... ',
' .... ',
' -+...... . ',
' . ...... ',
' ...+-... ',
' '
].map{|e|e.chars.to_a}
H,T,W,N='+','-','.',' '
def load_png(file)
require 'chunky_png'
i=ChunkyPNG::Image.from_file(file)
a=Array.new(i.height){''}
i.height.times{|y|i.width.times{|x|
a[y] << case i[x,y]
when ChunkyPNG::Color.rgb(0xFF,0xFF,0xFF) then W
when ChunkyPNG::Color.rgb(0xFF,0x00,0x00) then H
when ChunkyPNG::Color.rgb(0x00,0x00,0xFF) then T
else N
end
}}
a.map{|e|e.chars.to_a}
end
#a = load_png('7segw.png')
s=a[0].size
w={}.tap{|w|a.each_with_index{|r,y|r.each_with_index{|c,x|w[y*s+x]=c if c!=N}}}
while 1
x=w.dup
w.each{|k,c|x[k]=c==H ? T : c==T ? W : (n=[k-s-1,k-s,k-s+1,k-1,k+1,k+s-1,k+s,k+s+1].count{|e|w[e]==H}) && n>0 && n<3 ? H : c}
w=x
o=''
a.size.times{|i|s.times{|j|o<<(w[i*s+j]||N)};o<<"\n"}
system 'clear'
print o
sleep 0.05
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment