Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local settings={
- sizeX=50;
- sizeY=50;
- squareSizeX=10;
- squareSizeY=10;
- delay=.1;
- }
- local player=game:GetService"Players".LocalPlayer
- local gui=Instance.new"ScreenGui"
- gui.Parent=player.PlayerGui
- gui.Name="Conway's Game of Life"
- local guiRef={}
- local board={}
- local preboard={}
- local mouse=player:GetMouse()
- running=false
- bdown=false
- setVal=true
- mouse.KeyDown:connect(function(key)
- if key==" " then
- running=not running
- elseif key=="c" then
- running=false
- for x=0, settings.sizeX do
- for y=0, settings.sizeY do
- board[x][y]=false
- preboard[x][y]=false
- end
- end
- updateScreen()
- end
- end)
- mouse.Button1Down:connect(function()
- bdown=true
- end)
- mouse.Button1Up:connect(function()
- bdown=false
- end)
- for x=0, settings.sizeX do
- guiRef[x]={}
- board[x]={}
- preboard[x]={}
- for y=0, settings.sizeY do
- board[x][y]=false
- preboard[x][y]=false
- local frame=Instance.new"Frame"
- frame.Parent=gui
- frame.Name="{"..settings.sizeX..", "..settings.sizeY.."}"
- frame.Size=UDim2.new(0, settings.squareSizeX, 0, settings.squareSizeY)
- frame.Position=UDim2.new(0, settings.squareSizeX*x, 0, settings.squareSizeY*y)
- frame.BorderSizePixel=0
- frame.InputBegan:connect(function(iobject)
- if iobject.UserInputType==Enum.UserInputType.MouseButton1 then
- setVal=not board[x][y]
- board[x][y]=not board[x][y]
- preboard[x][y]=not preboard[x][y]
- updateScreen()
- end
- end)
- --[[frame.InputBegan:connect(function(iobject)
- if iobject.UserInputType==Enum.UserInputType.MouseButton1 then
- board[x][y]=not board[x][y]
- preboard[x][y]=not preboard[x][y]
- updateScreen()
- end
- end)]]
- frame.MouseEnter:connect(function()
- if bdown then
- board[x][y]=setVal
- preboard[x][y]=setVal
- updateScreen()
- end
- end)
- guiRef[x][y]=frame
- end
- end
- function cloneBoard()
- for x=0, settings.sizeX do
- for y=0, settings.sizeY do
- preboard[x][y]=board[x][y]
- end
- end
- end
- function updateScreen()
- for x=0, settings.sizeX do
- for y=0, settings.sizeY do
- guiRef[x][y].BackgroundColor3=board[x][y]==true and Color3.new(0, 0, 0) or Color3.new(1, 1, 1)
- end
- end
- end
- function doCycle()
- --[[
- 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- 2. Any live cell with two or three live neighbours lives on to the next generation.
- 3. Any live cell with more than three live neighbours dies, as if by overcrowding.
- 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
- --]]
- cloneBoard()
- for x=0, settings.sizeX do
- for y=0, settings.sizeY do
- local n=0
- for tx=x-1, x+1 do
- for ty=y-1, y+1 do
- 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
- --neighbor
- n=n+1
- end
- end
- end
- local state=preboard[x][y]
- --[[
- n < 2 --> die
- n -> {2, 3} --> live
- n > 3 --> die
- n == 3 && dead --> become alive
- --]]
- if state and n<2 or n>3 then
- board[x][y]=false
- elseif not state and n==3 then
- board[x][y]=true
- end
- end
- end
- end
- workspace.CurrentCamera.CameraType=Enum.CameraType.Custom
- repeat coroutine.yield() until player.Character
- player.Character:Destroy()
- while true do
- coroutine.yield()
- while running do
- doCycle()
- updateScreen()
- wait(settings.delay)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement