Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local runService = game:GetService("RunService")
- local rng = Random.new()
- ----
- local SIZE = 100
- local WHITE = BrickColor.White()
- local BLACK = BrickColor.Black()
- local OFFSETS = {
- {-1, -1},
- {0, -1},
- {1, -1},
- {-1, 1},
- {0, 1},
- {1, 1},
- {-1, 0},
- {1, 0},
- }
- ----
- local function getPart(x, y)
- local part = Instance.new("Part")
- part.Anchored = true
- part.CanCollide = false
- part.Size = Vector3.new(1, 1, 1)
- part.CFrame = CFrame.new(x, 0.5, y)
- part.BrickColor = BLACK
- part.Parent = workspace
- return part
- end
- local tileList = {}
- local boardGrid = {}
- for x = 1, SIZE, 1 do
- boardGrid[x] = {}
- for y = 1, SIZE, 1 do
- boardGrid[x][y] = {
- State = false,
- Next = false,
- Position = {x, y},
- Part = getPart(x, y),
- Neighbors = {}
- }
- end
- end
- for originX = 1, SIZE, 1 do
- for originY = 1, SIZE, 1 do
- local tile = boardGrid[originX][originY]
- for offsetIndex = 1, 8, 1 do
- local offset = OFFSETS[offsetIndex]
- local x = originX + offset[1]
- local y = originY + offset[2]
- if x == 0 then
- x = SIZE
- elseif x == SIZE + 1 then
- x = 1
- end
- if y == 0 then
- y = SIZE
- elseif y == SIZE + 1 then
- y = 1
- end
- table.insert(tile.Neighbors, boardGrid[x][y])
- end
- table.insert(tileList, tile)
- end
- end
- ---
- --[[ GLIDER GUN
- local gliderGun = {
- -- left box
- {2, 6}, {2, 7},
- {3, 6}, {3, 7},
- -- left ship
- {12, 6}, {12, 7}, {12, 8},
- {13, 5}, {13, 9},
- {14, 4}, {14, 10},
- {15, 4}, {15, 10},
- {16, 7},
- {17, 5}, {17, 9},
- {18, 6}, {18, 7}, {18, 8},
- {19, 7},
- -- right ship
- {22, 4}, {22, 5}, {22, 6},
- {23, 4}, {23, 5}, {23, 6},
- {24, 3}, {24, 7},
- {26, 2}, {26, 3}, {26, 7}, {26, 8},
- -- left box
- {36, 4}, {36, 5},
- {37, 4}, {37, 5}
- }
- for _, coord in next, gliderGun do
- local x, y = unpack(coord)
- local tile = boardGrid[x][y]
- --tile.State = true
- tile.Next = true
- end
- --]]
- -- RNG EVERYTHING
- for index, tile in next, tileList do
- tile.Next = rng:NextNumber() > 0.5
- end
- --]]
- --[[ BIG CIRCLE
- for index, tile in next, tileList do
- local x, y = unpack(tile.Position)
- x = x - (SIZE * 0.5)
- y = y - (SIZE * 0.5)
- local mag = math.sqrt((x * x) + (y * y))
- if mag < SIZE * 0.25 then
- tile.Next = rng:NextNumber() > 0.5
- end
- end
- --]]
- ----
- local function setNextTileState(tile)
- local nearLife = 0
- for index = 1, 8, 1 do
- if tile.Neighbors[index].State then
- nearLife = nearLife + 1
- end
- -- no case deals with more life than this
- if nearLife > 3 then
- break
- end
- end
- -- the rules of life
- if tile.State then
- if nearLife < 2 or nearLife > 3 then
- tile.Next = false
- end
- else
- if nearLife == 3 then
- tile.Next = true
- end
- end
- end
- local function stepBoard()
- -- set next states
- for index = 1, SIZE * SIZE do
- setNextTileState(tileList[index])
- end
- -- comit next states and draw changes
- for index = 1, SIZE * SIZE do
- local tile = tileList[index]
- if tile.Next ~= tile.State then
- if tile.Next then
- tile.Part.BrickColor = WHITE
- else
- tile.Part.BrickColor = BLACK
- end
- end
- tile.State = tile.Next
- end
- end
- ----
- while runService.Heartbeat:Wait() do
- stepBoard()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement