2
0

life.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. -- life.lua
  2. -- original by Dave Bollinger <DBollinger@compuserve.com> posted to lua-l
  3. -- modified to use ANSI terminal escape sequences
  4. -- modified to use for instead of while
  5. local write=io.write
  6. ALIVE="¥" DEAD="þ"
  7. ALIVE="O" DEAD="-"
  8. function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary
  9. for i=1,10000 do end
  10. -- local i=os.clock()+1 while(os.clock()<i) do end
  11. end
  12. function ARRAY2D(w,h)
  13. local t = {w=w,h=h}
  14. for y=1,h do
  15. t[y] = {}
  16. for x=1,w do
  17. t[y][x]=0
  18. end
  19. end
  20. return t
  21. end
  22. _CELLS = {}
  23. -- give birth to a "shape" within the cell array
  24. function _CELLS:spawn(shape,left,top)
  25. for y=0,shape.h-1 do
  26. for x=0,shape.w-1 do
  27. self[top+y][left+x] = shape[y*shape.w+x+1]
  28. end
  29. end
  30. end
  31. -- run the CA and produce the next generation
  32. function _CELLS:evolve(next)
  33. local ym1,y,yp1,yi=self.h-1,self.h,1,self.h
  34. while yi > 0 do
  35. local xm1,x,xp1,xi=self.w-1,self.w,1,self.w
  36. while xi > 0 do
  37. local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] +
  38. self[y][xm1] + self[y][xp1] +
  39. self[yp1][xm1] + self[yp1][x] + self[yp1][xp1]
  40. next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0
  41. xm1,x,xp1,xi = x,xp1,xp1+1,xi-1
  42. end
  43. ym1,y,yp1,yi = y,yp1,yp1+1,yi-1
  44. end
  45. end
  46. -- output the array to screen
  47. function _CELLS:draw()
  48. local out="" -- accumulate to reduce flicker
  49. for y=1,self.h do
  50. for x=1,self.w do
  51. out=out..(((self[y][x]>0) and ALIVE) or DEAD)
  52. end
  53. out=out.."\n"
  54. end
  55. write(out)
  56. end
  57. -- constructor
  58. function CELLS(w,h)
  59. local c = ARRAY2D(w,h)
  60. c.spawn = _CELLS.spawn
  61. c.evolve = _CELLS.evolve
  62. c.draw = _CELLS.draw
  63. return c
  64. end
  65. --
  66. -- shapes suitable for use with spawn() above
  67. --
  68. HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 }
  69. GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 }
  70. EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 }
  71. FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 }
  72. BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 }
  73. -- the main routine
  74. function LIFE(w,h)
  75. -- create two arrays
  76. local thisgen = CELLS(w,h)
  77. local nextgen = CELLS(w,h)
  78. -- create some life
  79. -- about 1000 generations of fun, then a glider steady-state
  80. thisgen:spawn(GLIDER,5,4)
  81. thisgen:spawn(EXPLODE,25,10)
  82. thisgen:spawn(FISH,4,12)
  83. -- run until break
  84. local gen=1
  85. write("\027[2J") -- ANSI clear screen
  86. while 1 do
  87. thisgen:evolve(nextgen)
  88. thisgen,nextgen = nextgen,thisgen
  89. write("\027[H") -- ANSI home cursor
  90. thisgen:draw()
  91. write("Life - generation ",gen,"\n")
  92. gen=gen+1
  93. if gen>2000 then break end
  94. --delay() -- no delay
  95. end
  96. end
  97. LIFE(40,20)