Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do
- Life = {}
- local mt = { __index = Life}
- local sc = 0
- function swait()
- sc = sc + 1
- if sc >= 250 then
- sc = 0
- game:GetService("RunService").Heartbeat:Wait()
- end
- end
- function Life.new(m, n, res)
- local matrix = {}
- local grid = Instance.new("Folder")
- grid.Parent = script
- local s = res/m
- local offset = Vector3.new(0,0,-10)
- for i = 1, m do
- local row = {}
- for j = 1, n do
- row[j] = 0
- local name = (i..','..j)
- local part = Instance.new("Part")
- part.Name = name
- part.CastShadow = false
- part.Anchored = true
- part.CanCollide = false
- part.Massless = true
- part.Size = Vector3.new(1,1,1)
- part.Position = offset+Vector3.new(i*s,j*s,0)
- local mesh = Instance.new("SpecialMesh")
- mesh.MeshType = Enum.MeshType.Brick
- mesh.Scale = Vector3.new(s,s,s)
- mesh.Parent = part
- part.Parent = grid
- end
- matrix[i] = row
- end
- return setmetatable({
- matrix = matrix,
- m = m,
- n = n,
- grid = grid,
- }, mt)
- end
- function Life:set_pos(x,y)
- self.matrix[x][y] = 1
- end
- function Life:unset_pos(x,y)
- self.matrix[x][y] = 0
- end
- function Life:next_gen()
- local X = deepcopy(self.matrix)
- local matrix = self.matrix
- for i = 1, self.m do
- for j = 1, self.n do
- local s = 0
- for p = i-1,i+1 do
- for q = j-1,j+1 do
- if p > 0 and p <= self.m and q > 0 and q <= self.n then
- s = s + self.matrix[p][q]
- end
- end
- end
- s = s - self.matrix[i][j]
- if s == 3 or (s+self.matrix[i][j]) == 3 then
- X[i][j] = 1
- else
- X[i][j] = 0
- end
- swait()
- end
- end
- self.matrix = deepcopy(X)
- end
- function Life:render()
- local matrix = self.matrix
- local grid = self.grid
- for i = 1, self.m do
- for j = 1, self.n do
- local name = (i..','..j)
- if matrix[i][j] == 0 then
- grid[name].Color = Color3.new(1,1,1)
- else
- grid[name].Color = Color3.new(0,0,0)
- end
- swait()
- end
- end
- end
- end
- function deepcopy(object)
- local lookup_table = {}
- local function _copy(object)
- if type(object) ~= "table" then
- return object
- elseif lookup_table[object] then
- return lookup_table[object]
- end
- local new_table = {}
- lookup_table[object] = new_table
- for index, value in pairs(object) do
- new_table[_copy(index)] = _copy(value)
- end
- return setmetatable(new_table, getmetatable(object))
- end
- return _copy(object)
- end
- local wh = 50
- local x,y = wh,wh
- local life = Life.new(x,y,20)
- x,y = x/2,y/2
- x = math.floor(x)
- y = math.floor(y)
- local t = {
- {0,0},
- {1,0},
- {2,0},
- {4,0},
- {0,1},
- {3,2},
- {4,2},
- {1,3},
- {2,3},
- {4,3},
- {0,4},
- {2,4},
- {4,4},
- }
- for i,v in pairs(t) do
- life:set_pos(x+v[1],y+v[2])
- end
- local tps = 1/60
- while wait(tps) do
- life:render()
- life:next_gen()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement