Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Game of Life logical scripting error?

Asked by 7 years ago

I'm creating John Conways game of life in guis. It was going well so far until i'm experiencing a weird logical error. This error is not an actual error, but it's not what the code is mean't to do.

Weird effect: https://gyazo.com/2440f051c3205d78962ff2042d6cf5e2

What it's meant to look like (browser game): https://gyazo.com/b12776e983171beecea1ff00bbe35e32

White (1) is alive, and black (0) is dead,

I feel like the weird effect is coming from the update function (line 46) or from the tempgrid variable It's hard to explain what I'm doing but its basically creating a grid table, comparing the 8 surrounding grid cells and checking certain conditions, then it updates it accordingly.

https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

rules: Any live cell with fewer than two live neighbours dies, as if caused by underpopulation. Any live cell with two or three live neighbours lives on to the next generation. Any live cell with more than three live neighbours dies, as if by overpopulation. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

local grid = {}
local tempgrid = {}

local gameContainer = script.Parent.Parent:WaitForChild("gameContainer")
local tileFolder = gameContainer.gridContainer.tileFolder

local menuContainer = gameContainer.menuContainer
local updateButton = menuContainer.update


function script.createGrid.OnInvoke(size)
    grid = {}
    for i,v in pairs(tileFolder:GetChildren()) do
        if v:IsA("TextButton") then
            v:Destroy()
        end
    end
    for y = 0,size - 1 do
        grid[y + 1] = {}
        for x = 0,size - 1 do
            local button = Instance.new("TextButton")
            button.Name = (y + 1)..":"..(x + 1)
            button.Size = UDim2.new(0, 500/size - 2, 0, 500/size - 2)
            button.Position = UDim2.new(0, (500/size * x) + 1, 0, (500/size * y) + 1)
            button.Text = ""
            button.BorderSizePixel = 0
            button.BackgroundColor3 = Color3.fromRGB(25, 25, 25)
            button.ZIndex = 3
            button.Parent = tileFolder
            grid[y + 1][x + 1] = 0

            button.MouseButton1Click:connect(function()
                button.BackgroundColor3 = Color3.new(1,1,1)
                grid[y + 1][x + 1] = 1
            end)
            button.MouseButton2Click:connect(function()
                button.BackgroundColor3 = Color3.fromRGB(25, 25, 25)
                grid[y + 1][x + 1] = 0
            end)
        end
        wait()
    end
    tempgrid = grid
end

function update()
    for y = 2, #grid - 1 do
        for x = 2,#grid - 1 do
            print(y..":"..x)
            local count = tempgrid[y - 1][x - 1] + tempgrid[y - 1][x] + tempgrid[y - 1][x + 1]
                        + tempgrid[y][x - 1]                          + tempgrid[y][x + 1]
                        + tempgrid[y + 1][x - 1] + tempgrid[y + 1][x] + tempgrid[y + 1][x + 1]

            if tempgrid[y][x] == 1 then --if cell is alive
                if count < 2 then --under population
                    grid[y][x] = 0 
                elseif count > 3 then --over population
                    grid[y][x] = 0
                else
                    grid[y][x] = 1
                end
            else
                if count == 3 then --reproduction
                    grid[y][x] = 1
                else
                    grid[y][x] = 0
                end
            end
        end
    end
    for y = 1, #grid do
        for x = 1, #grid do
            if grid[y][x] == 1 then
                tileFolder[y..":"..x].BackgroundColor3 = Color3.new(1,1,1)
            else
                tileFolder[y..":"..x].BackgroundColor3 = Color3.fromRGB(25, 25, 25)
            end
        end
    end
    tempgrid = grid
end

updateButton.MouseButton1Click:connect(function()
    update()
end)

1 answer

Log in to vote
1
Answered by 7 years ago

Hey, my answer uses formatting that doesn't work in the answer box, so I have put it all here.

Hope it helps, give it a read. I'm pretty sure I figured it out

1
Yup, I think you are right. But I did try and make it so it compares to a different table then makes the changes in another table. I did this with the tempgrid variable and the grid variable. It didn't seem to work. I know its related to that now thanks to your post! LittleBigDeveloper 245 — 7y
0
Sorry I couldn't give a full solution, but glad to help! plasmascreen 143 — 7y
Ad

Answer this question