Need help on adding Minesweeper game mechanic

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hello!! I need some help on a game mechanic i want to add. Specifically, when you click on an empty tile in Minesweeper, it’ll reveal all the other tiles around it!

  2. What is the issue? Include screenshots / videos if possible!
    Cant find a solution to my problem, as stated above.

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Looked everywhere for a solution, haven’t found one yet. Checked dev forums and couldn’t find much either, so i decided to make my own post, this one! :smiley:

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

As usual, here is my script.
(ModuleScript, btw)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Debris = game:GetService("Debris")
local IconsFolder = ReplicatedStorage:WaitForChild("Icons")
local SoundsFolder = ReplicatedStorage:WaitForChild("Sounds")

local Tile = {
	Colors = {
		One = Color3.fromRGB(0, 0, 255),
		Two = Color3.fromRGB(0, 128, 0),
		Three = Color3.fromRGB(255, 0, 0),
		Four = Color3.fromRGB(0, 0, 128),
		Five = Color3.fromRGB(128, 0, 0),
		Six = Color3.fromRGB(0, 128, 128),
		Seven = Color3.fromRGB(0, 0, 0),
		Eight = Color3.fromRGB(128, 128, 128),
		Nine = Color3.fromRGB(85, 0, 127),
	}
}

local HitboxSize = Vector3.new(8,4,8) -- Hitbox size for tile detection
local overlapParams = OverlapParams.new()
local MineCount = 0

local function MineTriggered(part, Text)
	part.CanTouch = false
	part.CanQuery = false
	part.ClickDetector.MaxActivationDistance = 0
	
	for i,v in pairs(part.SurfaceGui:GetChildren()) do
		if v:IsA("ImageLabel") and v.Name == "FlagIcon" then
			v:Destroy()
		end
	end
	
	local newIcon = IconsFolder.Mine:Clone()
	newIcon.Parent = part.SurfaceGui
	task.wait(1)
	
	local NewSound = SoundsFolder.Explosion:Clone()
	NewSound.Parent = part
	NewSound:Play()
	
	local explosion = Instance.new("Explosion")
	explosion.Position = part.Position
	explosion.Parent = workspace
	
	Debris:AddItem(NewSound, NewSound.TimeLength)
	Debris:AddItem(part.ClickDetector, 3)
end

local function Colourise(part, Text)
	-- Temporary, i'll find a better solution for this later.
	if MineCount == 1 then
		Text.TextColor3 = Tile.Colors.One
	elseif MineCount == 2 then
		Text.TextColor3 = Tile.Colors.Two
	elseif MineCount == 3 then
		Text.TextColor3 = Tile.Colors.Three
	elseif MineCount == 4 then
		Text.TextColor3 = Tile.Colors.Four
	elseif MineCount == 5 then
		Text.TextColor3 = Tile.Colors.Five
	elseif MineCount == 6 then
		Text.TextColor3 = Tile.Colors.Six
	elseif MineCount == 7 then
		Text.TextColor3 = Tile.Colors.Seven
	elseif MineCount == 8 then
		Text.TextColor3 = Tile.Colors.Eight
	elseif MineCount == 9 then
		Text.TextColor3 = Tile.Colors.Nine
	end
end

function Tile.RevealTile(part, Text)
	task.wait()
	if part:GetAttribute("Mine", true) then
		MineTriggered(part, Text)
		return
	end
	
	local hitContents = workspace:GetPartBoundsInBox(part.CFrame,HitboxSize, overlapParams)

	for i,v in pairs(hitContents) do -- search for tiles in a 3x3 area
		if not v then return end
		if v.Name == "Mine" then
			MineCount += 1
		elseif v.Name == "Tile" and MineCount <= 0 then
			print("chainreact")
			local v_Text = v.SurfaceGui.TextLabel
			v.CanTouch = false
			v.CanQuery = false
		end
	end
	
	--[[if MineCount == 0 then
		for i,v in pairs(part.SurfaceGui:GetChildren()) do
			if v:IsA("TextLabel") then
				print("chainreact")
				Tile.RevealTile(v, Text)
			end
		end
	end]]
	
	part.CanTouch = false
	part.CanQuery = false
	if MineCount > 0 then
		Text.Text = MineCount
		Colourise(part, Text)
	end
	MineCount = 0
	--print("there is "..MineCount.." Mine(s) around this area.")
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Thanks for reading! Hope you can help with my problem! :smiley:

2 Likes

when i made minesweeper i held all info within a 2d array, rather than in game by instances. to get a tile’s array value from its game counterpart you’d just store its array position as a vector2 within it, attributes would work great for this. this way when you mine you can store all the mined info into one event. you’d make one function that checks the adjacent tiles of one tile, then if the adjacent tile is safe it fires that function for that tile as well, continuing in all directions until no safe tiles are left to find.

keep in mind that checking the tile you initially clicked on is different than the sweeping checks for adjacent tiles; Sweeping checks cant clear unsafe tiles. I’d recommend keeping an index of how many tiles have been cleared within the dig function, and if the index infers that the current block is the first then it will do an unsafe dig.

1 Like

There must be 10,000 example of how this is done.. Research would be better than questions here.

as long as you format things in a smart enough way it should turn out pretty much the same, the instance approach would still work it’d just be more costly cause you’d need to raycast or some other stuff to find adjacent tiles

That would be where the research would come into play. Sounds like you’re all over that sir.

1 Like

idk what you mean by research, unless you mean you cant code that yet, which i understand. I gave an explanation of the end result of my own attempt at it, so if you can follow it thats as good as it gets


I’ve made this really long time ago, I can send you how it works

1 Like

sure! that would help alot! :smiley: