Advertisement
itchyzombie

Conway's Game of Life for ROBLOX

Sep 23rd, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. local settings={
  2. sizeX=50;
  3. sizeY=50;
  4. squareSizeX=10;
  5. squareSizeY=10;
  6. delay=.1;
  7. }
  8.  
  9. local player=game:GetService"Players".LocalPlayer
  10.  
  11.  
  12.  
  13. local gui=Instance.new"ScreenGui"
  14. gui.Parent=player.PlayerGui
  15. gui.Name="Conway's Game of Life"
  16.  
  17. local guiRef={}
  18. local board={}
  19. local preboard={}
  20. local mouse=player:GetMouse()
  21.  
  22. running=false
  23. bdown=false
  24. setVal=true
  25. mouse.KeyDown:connect(function(key)
  26.     if key==" " then
  27.         running=not running
  28.     elseif key=="c" then
  29.         running=false
  30.         for x=0, settings.sizeX do
  31.             for y=0, settings.sizeY do
  32.                 board[x][y]=false
  33.                 preboard[x][y]=false
  34.             end
  35.         end
  36.         updateScreen()
  37.     end
  38. end)
  39.  
  40. mouse.Button1Down:connect(function()
  41.     bdown=true 
  42. end)
  43.  
  44. mouse.Button1Up:connect(function()
  45.     bdown=false
  46. end)
  47.  
  48. for x=0, settings.sizeX do
  49.     guiRef[x]={}
  50.     board[x]={}
  51.     preboard[x]={}
  52.     for y=0, settings.sizeY do
  53.         board[x][y]=false
  54.         preboard[x][y]=false
  55.         local frame=Instance.new"Frame"
  56.         frame.Parent=gui
  57.         frame.Name="{"..settings.sizeX..", "..settings.sizeY.."}"
  58.         frame.Size=UDim2.new(0, settings.squareSizeX, 0, settings.squareSizeY)
  59.         frame.Position=UDim2.new(0, settings.squareSizeX*x, 0, settings.squareSizeY*y)
  60.         frame.BorderSizePixel=0
  61.         frame.InputBegan:connect(function(iobject)
  62.             if iobject.UserInputType==Enum.UserInputType.MouseButton1 then
  63.                 setVal=not board[x][y]
  64.                 board[x][y]=not board[x][y]
  65.                 preboard[x][y]=not preboard[x][y]
  66.                 updateScreen()
  67.             end
  68.         end)
  69.         --[[frame.InputBegan:connect(function(iobject)
  70.             if iobject.UserInputType==Enum.UserInputType.MouseButton1 then
  71.                 board[x][y]=not board[x][y]
  72.                 preboard[x][y]=not preboard[x][y]
  73.                 updateScreen()
  74.             end
  75.            
  76.          end)]]
  77.        
  78.         frame.MouseEnter:connect(function()
  79.             if bdown then
  80.                 board[x][y]=setVal
  81.                 preboard[x][y]=setVal
  82.                 updateScreen()
  83.             end
  84.         end)
  85.        
  86.         guiRef[x][y]=frame
  87.     end
  88. end
  89.  
  90. function cloneBoard()
  91.     for x=0, settings.sizeX do
  92.         for y=0, settings.sizeY do
  93.             preboard[x][y]=board[x][y]
  94.         end
  95.     end
  96. end
  97.  
  98. function updateScreen()
  99.     for x=0, settings.sizeX do
  100.         for y=0, settings.sizeY do
  101.             guiRef[x][y].BackgroundColor3=board[x][y]==true and Color3.new(0, 0, 0) or Color3.new(1, 1, 1)
  102.         end
  103.     end
  104. end
  105.  
  106. function doCycle()
  107.     --[[       
  108.         1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  109.         2. Any live cell with two or three live neighbours lives on to the next generation.
  110.         3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  111.         4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
  112.     --]]
  113.     cloneBoard()
  114.     for x=0, settings.sizeX do
  115.         for y=0, settings.sizeY do
  116.             local n=0
  117.             for tx=x-1, x+1 do
  118.                 for ty=y-1, y+1 do
  119.                     if (ty~=y or tx~=x) and ((tx>=0 and ty>=0) and (tx<=settings.sizeX and ty<=settings.sizeY)) and preboard[tx][ty] then
  120.                         --neighbor
  121.                         n=n+1
  122.                     end
  123.                 end
  124.             end
  125.             local state=preboard[x][y]
  126.             --[[
  127.                 n < 2 --> die
  128.                 n -> {2, 3} --> live
  129.                 n > 3 --> die
  130.                 n == 3 && dead --> become alive
  131.             --]]
  132.             if state and n<2 or n>3 then
  133.                 board[x][y]=false
  134.             elseif not state and n==3 then
  135.                 board[x][y]=true
  136.             end
  137.         end
  138.     end
  139. end
  140.  
  141.  
  142.  
  143.  
  144. workspace.CurrentCamera.CameraType=Enum.CameraType.Custom
  145. repeat coroutine.yield() until player.Character
  146. player.Character:Destroy()
  147.  
  148. while true do
  149. coroutine.yield()
  150. while running do
  151. doCycle()
  152. updateScreen()
  153. wait(settings.delay)       
  154. end    
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement