Anyways to make minesweeper expansion system?

I have tried a way to solve this. But it keeps timed out or expand incorrectly.
Here’s my way:
When a empty space(unnumbered) revealed, then reveal eight spaces near it.
If there is an empty space revealed by the last one, then this empty space reveal eight spaces near it.

Is there another way to do this? I’m using a table. And I wish to reveal them all without functions calling each other.

This is the right algorithm. This is very similar to “flood fill” algorithms, and there are several ways to do a flood fill, but recursively descending is perfectly fine, esp. for minesweeper which doesn’t have a lot of “pixels” to fill.

If you’re timing out, it’s possible you’re revealing all 8 neighbors & then recursively descending into each one that has a value of 0 – you should make sure your logic is set up to only descend if you’ve just revealed this tile. That way, you don’t back track.

If you need any more help, you’ll probably have to share code. But your algorithm is right, you just have to debug it.

I have 16*12 to fill. Is it too much for it?
Here’s the script:

local Size = {16,12}
local MineChance = 25
local MiniumMines = Size[1]*Size[2]/4
local MaxiumMines = Size[1]*Size[2]/3
local CANSTART = false
local Service = game:GetService("TweenService")
local Steps = 0
local COLORTABLE = {Color3.fromRGB(0, 85, 255),Color3.fromRGB(0, 170, 0),Color3.fromRGB(255, 0, 0),Color3.fromRGB(170, 0, 127),Color3.fromRGB(170, 0, 0),Color3.fromRGB(0, 255, 255),Color3.fromRGB(27, 42, 53),Color3.fromRGB(91, 93, 105)}
local UIS = game:GetService("UserInputService")

function Clear()
	script.Parent.Tiles:ClearAllChildren()
end

function Generate(N)
	repeat
	repeat ---- Generating Mines, repeat untill reach MiniumMines ----
		for i = 1,Size[1]*Size[2] do
			table.insert(TABLE,#TABLE+1,0)
		end
		local MineCount = 0
		for i = 1,#TABLE do
			local INDEX = math.random(0,100)
			if INDEX <= MineChance and i ~= N then
				TABLE[i] = 9
				MineCount = MineCount + 1
			end
		end
		if MineCount < MiniumMines or MineCount > MaxiumMines then
			TABLE = {}
		end
	until MineCount >= MiniumMines and MineCount <= MaxiumMines
	---- Number Indicators ----
	for i = 1,#TABLE do
		local A = 0
		if TABLE[i] ~= 9 then
		if TABLE[i-17] == 9 then
			A = A + 1
		end
		if TABLE[i-16] == 9 then
			A = A + 1
		end
		if TABLE[i-15] == 9 then
			A = A + 1
		end
		if TABLE[i-1] == 9 then
			A = A + 1
		end
		if TABLE[i+1] == 9 then
			A = A + 1
		end
		if TABLE[i+15] == 9 then
			A = A + 1
		end
		if TABLE[i+16] == 9 then
			A = A + 1
		end
		if TABLE[i+17] == 9 then
			A = A + 1
		end
		TABLE[i] = A
		end
		end
		until TABLE[N] == 0
end

function GenerateTiles()
	for i = 1,16*12 do
		local X = (i%16-1)*25 + 25
		local Y = math.floor((i-1)/16)*25
		local Tile = script.Tile:Clone()
		Tile.Parent = script.Parent.Tiles
		Tile.Name = i
		Tile.Position = UDim2.new(0,X,0,Y)
		Tile.Visible = true
	end
	CANSTART = true
end


function Expand(N)
	if TABLE[N - 17] ~= nil then
		local Obj = script.Parent.Tiles:FindFirstChild(N - 17)
		if TABLE[N - 17] < 9 and Obj then
			if Obj.BackgroundColor3 == Color3.fromRGB(125,125,125) then
				Flip(Obj)
			end
		end
	end
	if TABLE[N - 16] ~= nil then
		local Obj = script.Parent.Tiles:FindFirstChild(N - 16)
		if TABLE[N - 16] < 9 and Obj then
			if Obj.BackgroundColor3 == Color3.fromRGB(125,125,125) then
				Flip(Obj)
			end
		end
	end
	if TABLE[N - 15] ~= nil then
		local Obj = script.Parent.Tiles:FindFirstChild(N - 15)
		if TABLE[N - 15] < 9 and Obj then
			if Obj.BackgroundColor3 == Color3.fromRGB(125,125,125) then
				Flip(Obj)
			end
		end
	end
	if TABLE[N - 1] ~= nil then
		local Obj = script.Parent.Tiles:FindFirstChild(N - 1)
		if TABLE[N - 1] < 9 and Obj then
			if Obj.BackgroundColor3 == Color3.fromRGB(125,125,125) then
				Flip(Obj)
			end
		end
	end
	if TABLE[N + 1] ~= nil then
		local Obj = script.Parent.Tiles:FindFirstChild(N + 1)
		if TABLE[N + 1] < 9 and Obj then
			if Obj.BackgroundColor3 == Color3.fromRGB(125,125,125) then
				Flip(Obj)
			end
		end
	end
	if TABLE[N + 15] ~= nil then
		local Obj = script.Parent.Tiles:FindFirstChild(N + 15)
		if TABLE[N + 15] < 9 and Obj then
			if Obj.BackgroundColor3 == Color3.fromRGB(125,125,125) then
				Flip(Obj)
			end
		end
	end
	if TABLE[N + 16] ~= nil then
		local Obj = script.Parent.Tiles:FindFirstChild(N + 16)
		if TABLE[N + 16] < 9 and Obj then
			if Obj.BackgroundColor3 == Color3.fromRGB(125,125,125) then
				Flip(Obj)
			end
		end
	end
	if TABLE[N + 17] ~= nil then
		local Obj = script.Parent.Tiles:FindFirstChild(N + 17)
		if TABLE[N + 17] < 9 and Obj then
			if Obj.BackgroundColor3 == Color3.fromRGB(125,125,125) then
				Flip(Obj)
			end
		end
	end
end

function NewGame()
	Clear()
	GenerateTiles()
	for i = 1,#script.Parent.Tiles:GetChildren() do
		script.Parent.Tiles:GetChildren()[i].MouseButton1Click:Connect(function()
			Steps = Steps + 1
			Flip(script.Parent.Tiles:GetChildren()[i])
		end)
		script.Parent.Tiles:GetChildren()[i].MouseButton2Click:Connect(function()
			Flag(script.Parent.Tiles:GetChildren()[i])
		end)
	end
end

function Flag(Tile)
	if CANSTART then
	if Tile.Text =="!" then
		Tile.Text = ""
	else
		if Tile.Text == "" then
			Tile.Text = "!"
			Tile.TextColor3 = Color3.fromRGB(255,0,0)
		end
		end
	end
end

function GameOver()
	if CANSTART == true then
		CANSTART = false
		for i = 1,#TABLE do
			if script.Parent.Tiles:FindFirstChild(i) and TABLE[i] == 9 then
				script.Parent.Tiles:FindFirstChild(i).BackgroundColor3 = Color3.fromRGB(255,0,0)
			end
		end
	end
end

function Flip(Tile)
	if CANSTART == true and Tile ~= nil then
		if Steps == 1 then
			Generate(tonumber(Tile.Name))
		end
		if TABLE[tonumber(Tile.Name)] == 0 then
			Expand(tonumber(Tile.Name))
			Tile.BackgroundColor3 = Color3.fromRGB(200,200,200)
		end
		if TABLE[tonumber(Tile.Name)] == 9 then
			GameOver()
		end
		if TABLE[tonumber(Tile.Name)] ~= 9 and TABLE[tonumber(Tile.Name)] ~= 0 then
			Tile.Text = TABLE[tonumber(Tile.Name)]
			Tile.TextColor3 = COLORTABLE[TABLE[tonumber(Tile.Name)]]
			Tile.BackgroundColor3 = Color3.fromRGB(200,200,200)
			Tile.AutoButtonColor = false
		end
	else
		if Tile == nil then
			print("TileNIL")
		end
	end
	
end

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.R then
		NewGame()
		Steps = 0
	end
end)

I know the script is kinda terrible.

Yeah so where you generate TABLE, you should also create a table called “HasBeenFlipped”. When you flip a tile, set that to true. At the top of your Flip function, then, you can just add a guard clause to make sure we don’t try to flip a tile twice.

--Make sure we haven't already flipped this tile; if we don't check,
--we can end up in an infinite loop.
if HasBeenFlipped[tonumber(Tile)] then
    return;
end
HasBeenFlipped[tonumber(Tile)] = true;