MAXCHANGES=10 math.randomseed(os.time()) PRINTINTERVAL=2000 NOISE=500 function next(t,x,y) local sum=(t[x-1][y-1] or 0)+(t[x-1][y] or 0)+(t[x-1][y+1] or 0)+(t[x][y-1] or 0)+(t[x][y+1] or 0)+(t[x+1][y-1] or 0)+(t[x+1][y] or 0)+(t[x+1][y+1] or 0) if sum<2 or sum>3 then return 0 elseif sum==3 then return 1 else return t[x][y] end end function evaluate(t,t2) local mistakes=0 for x=1,#t2 do for y=1,#t2[1] do if next(t,x,y)~=t2[x][y] then mistakes=mistakes+1 end end end return mistakes end function scanchange() repeat local changed=false io.write(".") for X=1,#final do for Y=1,#final[1] do test[X][Y]=1-test[X][Y] after=evaluate(test,final) if before<=after then test[X][Y]=1-test[X][Y] else before=after changed=true end end end until not changed end function show() scanchange() print("\n\n\n") for i=1,#final do for j=1,#final[1] do io.write((test[i][j]==0 and " " or "#")) end io.write(" ") for j=1,#final[1] do local tempnext=next(test,i,j) if tempnext==0 and final[i][j]==0 then io.write(" ") elseif tempnext==0 and final[i][j]==1 then io.write(".") elseif tempnext==1 and final[i][j]==0 then io.write("*") elseif tempnext==1 and final[i][j]==1 then io.write("#") end end print() end print(before,MAXCHANGES) end final={ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}, {0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,1,1,1,1,1,0,}, {0,1,0,0,1,1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,}, {0,1,0,0,1,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,}, {0,1,0,0,1,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,}, {0,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,1,1,1,1,0,0,}, {0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,}, {0,1,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,}, {0,1,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,}, {0,1,0,0,1,0,0,0,0,1,1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,}, {0,1,0,0,1,0,0,0,0,0,1,0,0,1,1,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,1,1,1,1,1,0,}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}, } test={[0]={},[#final+1]={}} for i=1,#final do test[i]={} for j=1,#final[1] do --test[i][j]=math.random(0,1) test[i][j]=final[i][j] end end before=evaluate(test,final) show() tot=0 while true do tot=tot+1 if tot%PRINTINTERVAL==0 then show() end changes={} for i=1,math.random(1,MAXCHANGES) do randx,randy=math.random(1,#final),math.random(1,#final[1]) table.insert(changes,{randx,randy}) test[randx][randy]=1-test[randx][randy] end after=evaluate(test,final) if before>=after then before=after elseif math.random(1,NOISE)==1 and after-before<=1 then before=after else for i,v in ipairs(changes) do test[v[1]][v[2]]=1-test[v[1]][v[2]] end end end