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

How do I tidy up this memory game script to make it more suitable for expansion?

Asked by 4 years ago

Hio, I got this code for a working memory/matching pairs game. Is there a way to make the script more clean so it's easier to add more tiles?

local tileFolder = script.Parent:WaitForChild("Tiles")
local playBoard = script.Parent:WaitForChild("Board")
local imageList = {
1734698534,
1668590180,
1748068112,
2195005382,

1465638492,
1465638243,
1273842056,
2506267802,

3984473813,
4879077545,
1266972204,
1173305686,
}
local TOTAL_TILES = 6
local currentTiles = {tileFolder.Tile1, tileFolder.Tile2, tileFolder.Tile3, tileFolder.Tile4, tileFolder.Tile5, tileFolder.Tile6}

for i = #currentTiles, 2, -1 do
    local j = math.random(#currentTiles)
    currentTiles[i], currentTiles[j] = currentTiles[j], currentTiles[i]
end

local pickedID_1 = math.random(1,#imageList) 
local ID1 = "http://www.roblox.com/asset/?id="..imageList[pickedID_1]
table.remove(imageList, pickedID_1)

local pickedID_2 = math.random(1,#imageList) 
local ID2 = "http://www.roblox.com/asset/?id="..imageList[pickedID_2]
table.remove(imageList, pickedID_2)

local pickedID_3 = math.random(1,#imageList) 
local ID3 = "http://www.roblox.com/asset/?id="..imageList[pickedID_3]
table.remove(imageList, pickedID_3)

currentTiles[1].Image.Texture = ID1
currentTiles[2].Image.Texture = ID1
currentTiles[3].Image.Texture = ID2
currentTiles[4].Image.Texture = ID2
currentTiles[5].Image.Texture = ID3
currentTiles[6].Image.Texture = ID3

local WINNER = false
local firsttileFlipped = false
local tile_1_Flipped
local secondtileFlipped = false
local tile_2_Flipped

for _,tile in pairs(tileFolder:GetChildren()) do
    tile.ClickDetector.MouseClick:Connect(function()
        if not tile.Correct.Value == true then
            if not firsttileFlipped then
                tile_1_Flipped = tile -- Object?
                firsttileFlipped = not firsttileFlipped -- First tile flipped
                print(tile.Name.." has been flipped!")
                tile.CFrame = tile.CFrame * CFrame.Angles(0, 0, math.rad(180))
                playBoard["Flip"]:Play()    
            elseif firsttileFlipped and not secondtileFlipped then
                tile_2_Flipped = tile
                if tile_1_Flipped.Name == tile_2_Flipped.Name then
                    print("Tile already flipped!")
                else
                    secondtileFlipped = not secondtileFlipped -- Second tile flipped
                    print(tile.Name.." has been flipped!")
                    tile.CFrame = tile.CFrame * CFrame.Angles(0, 0, math.rad(180))
                    playBoard["Flip"]:Play()
                    if tile_1_Flipped.Image.Texture == tile_2_Flipped.Image.Texture then
                        print("Pair found!")
                        tile_1_Flipped.Correct.Value = true
                        tile_2_Flipped.Correct.Value = true     
                    else
                        print("Wrong pair!")
                        wait(1)
                        tile_1_Flipped.CFrame = tile_1_Flipped.CFrame * CFrame.Angles(0, 0, math.rad(180))
                        tile_2_Flipped.CFrame = tile_2_Flipped.CFrame * CFrame.Angles(0, 0, math.rad(180))                  
                    end
                    wait(.5)
                    tile_1_Flipped = nil
                    tile_2_Flipped = nil
                    firsttileFlipped = false
                    secondtileFlipped = false   
                end 
            end
        else
            print("Tile already found!")
        end
    end)
end

while wait(1) do
    if not WINNER then
        local AMOUNT = 0
        for _,tile in pairs(tileFolder:GetChildren()) do
            if tile.Correct.Value == true then
                AMOUNT = AMOUNT + 1
            end
        end
        if AMOUNT == TOTAL_TILES then
            WINNER = true
            print("Congratulations, you solved the memory game!")
            playBoard.BrickColor = BrickColor.new("Gold")
        else
            print(AMOUNT)
        end
    end
end

Answer this question