Advertisement
Guest User

ComputerCraft Game of Life

a guest
Mar 19th, 2013
4,664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. -- ComputerCraft Game of Life
  2. -- For 8x5 advanced monitor
  3. -- By Numerical (youtube/jreflex93)
  4.  
  5. w = 41
  6. h = 33
  7.  
  8. grid = {}
  9. for ih = 1, h do
  10.     grid[ih] = {}
  11.     for iw = 1, w do
  12.         rand = math.random(0, 99)
  13.         if(rand < 10)
  14.             then grid[ih][iw] = true
  15.             else grid[ih][iw] = false
  16.         end
  17.     end
  18. end
  19. gridNext = {}
  20. for ih = 1, h do
  21.     gridNext[ih] = {}
  22.     for iw = 1, w do
  23.         gridNext[ih][iw] = false
  24.     end
  25. end
  26.  
  27. function printGrid()
  28.     term.clear()
  29.     for ih = 1, h do
  30.         for iw = 1, w do
  31.             term.setCursorPos((iw+iw-1), ih)
  32.             if(grid[ih][iw]) then
  33.                 term.setTextColor(colors.lime) else
  34.                 term.setTextColor(colors.black)
  35.             end
  36.             term.write("@ ")
  37.         end
  38.     end
  39. end
  40.  
  41. function calcNext()
  42.     for ih = 1, h do
  43.         for iw = 1, w do
  44.             n = 0
  45.             if(iw~=w and grid[ih][iw+1]) then n = n + 1 end -- right
  46.             if(iw~=1 and grid[ih][iw-1]) then n = n + 1 end -- left
  47.             if(ih~=h and grid[ih+1][iw]) then n = n + 1 end -- down
  48.             if(ih~=1 and grid[ih-1][iw]) then n = n + 1 end -- up
  49.             if(ih~=1 and iw~=1 and grid[ih-1][iw-1]) then n = n + 1 end -- up left
  50.             if(ih~=1 and iw~=w and grid[ih-1][iw+1]) then n = n + 1 end -- up right
  51.             if(ih~=h and iw~=1 and grid[ih+1][iw-1]) then n = n + 1 end -- down left
  52.             if(ih~=h and iw~=w and grid[ih+1][iw+1]) then n = n + 1 end -- down right
  53.             if(n == 3 or (n == 2 and grid[ih][iw]))
  54.                 then gridNext[ih][iw] = true
  55.                 else gridNext[ih][iw] = false
  56.             end
  57.         end
  58.     end
  59. end
  60.  
  61. function setNext()
  62.     for ih = 1, h do
  63.         for iw = 1, w do
  64.             grid[ih][iw] = gridNext[ih][iw]
  65.         end
  66.     end
  67. end
  68.  
  69. term.clear()
  70. term.setCursorPos(35,31)
  71. term.setTextColor(colors.yellow)
  72. term.setBackgroundColor(colors.black)
  73. sleep(0.5)
  74. term.write("3... ")
  75. sleep(1)
  76. term.write("2... ")
  77. sleep(1)
  78. term.write("1... ")
  79. sleep(1)
  80.  
  81.  
  82. while(true) do
  83.     calcNext()
  84.     setNext()
  85.     printGrid()
  86.     sleep(0.1)
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement