CapsAdmin

Untitled

Nov 29th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 KB | None | 0 0
  1. setfenv(1, _G)
  2. local DEMO = {}
  3.  
  4. DEMO.Author = "CapsAdmin"
  5. DEMO.Title = "game of life????"
  6.  
  7. local board = {{}}
  8. local res = 2
  9.  
  10. function DEMO:OnStart(w, h)
  11.     for x = -res, w+res, res do
  12.     for y = -res, h+res, res do
  13.         board[x] = board[x] or {}
  14.         board[x][y] = board[x][y] or math.random() > 0.5 and 1 or 0
  15.     end
  16.     end
  17. end
  18.  
  19. local function count(board, x,y)
  20.     return
  21.         board[x-res][y-res] +
  22.         board[x][y-res] +
  23.         board[x+res][y-res] +
  24.         board[x+res][y] +
  25.         board[x+res][y+res] +
  26.         board[x][y+res] +
  27.         board[x-res][y+res] +
  28.         board[x-res][y]        
  29. end
  30.  
  31. function DEMO:OnDraw(w, h)
  32.     surface.SetDrawColor(255, 255, 255, 255)
  33.    
  34.     for x = 0, w, res do
  35.     for y = 0, h, res do       
  36.         if board[x][y] == 1 then
  37.             local neighbours = count(board, x,y)
  38.            
  39.             -- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  40.             if neighbours < 2 then
  41.                 board[x][y] = 0
  42.             end
  43.            
  44.             -- Any live cell with two or three live neighbours lives on to the next generation.
  45.             if neighbours == 2 or neighbours == 3 then
  46.                 continue
  47.             end
  48.            
  49.             -- Any live cell with more than three live neighbours dies, as if by overcrowding.
  50.             if neighbours > 3 then
  51.                 board[x][y] = 0
  52.             end
  53.            
  54.             surface.DrawRect(x,y, res, res)
  55.         end
  56.        
  57.         if board[x][y] == 0 then
  58.             -- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
  59.             if neighbours == 3 then
  60.                 board[x][y] = 1
  61.             end
  62.         end
  63.     end
  64.     end
  65. end
  66.  
  67. demoman.AddDemo(DEMO, true)
Advertisement
Add Comment
Please, Sign In to add comment