Last active
December 27, 2015 19:09
-
-
Save glurp/7374717 to your computer and use it in GitHub Desktop.
Gol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'Ruiby' | |
| Ruiby.app width: 400, height: 300, title: "Game of Life" do | |
| def freemap() [[false]*MAXL]*MAXC end | |
| MAXC=MAXL=100 | |
| PASX=default_width/MAXC | |
| PASY=(default_width)/MAXL | |
| @mat=freemap() | |
| @run=false | |
| stack do | |
| @formula = " old ? (nb_neighbors == 2 || nb_neighbors == 3) : ( nb_neighbors == 3 )" | |
| flowi { | |
| labeli "Life Formula : " | |
| @edit=entry(@formula) | |
| buttoni("enter") { instance_eval "def formula(old,nb_neighbors) ; #{@edit.text} ; end" } | |
| } | |
| @cv=canvas(self.default_width,self.default_height, | |
| :mouse_down => proc do |w,e| | |
| no= [e.x/PASX,e.y/PASY] ; @mat[no.first][no.last]=! @mat[no.first][no.last]; no | |
| end, | |
| :expose => proc do |w,ctx| | |
| ctx.set_source_rgba(0,0.5,0.5) | |
| MAXC.times { |col| MAXL.times { |li| (ctx.rectangle(col*PASX,li*PASY,PASX,PASY); ctx.fill()) if @mat[col][li] }} | |
| end | |
| ) | |
| flowi do | |
| button " clear " do @mat=freemap() ; @cv.redraw end | |
| button " random " do | |
| rand(MAXC*MAXL/10).times { @mat[rand(MAXC)][rand(MAXL)]=true } | |
| @cv.redraw | |
| end | |
| button " start " do @run=true ;end | |
| button " stop " do @run=false ; end | |
| end | |
| end | |
| anim 50 do game() if @run ; @cv.redraw end | |
| def game() | |
| mat2=freemap() | |
| MAXC.times do |col| MAXL.times do |li| | |
| nb_neighboring= 0 | |
| [-1, 0, 1].each { |col_off| [-1, 0, 1].each { |li_off| | |
| next if col_off == 0 && li_off == 0 | |
| next if col+col_off < 0 || li+li_off < 0 | |
| next if col+col_off >= MAXC || li+li_off >= MAXL | |
| nb_neighboring += 1 if @mat[col + col_off][li + li_off] | |
| } } | |
| mat2[col][li]= formula(@mat[col][li],nb_neighboring) | |
| end end | |
| @mat=mat2 if @run # seem that GTK:Timer use threading...? | |
| end | |
| def formula(old,nb_neighboring) | |
| old ? (nb_neighboring == 2 || nb_neighboring == 3) : ( nb_neighboring == 3 ) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment