I am working on a minesweeper game, problem is well, just look at the image:
The tiles you see are physical parts with decals on them they are named 1-81 from the top right down. so the very top right on is 1 and the very bottom left one is 81.
The tiles have numbers displayed even if a bomb isnt next to them as it will look to the next tile name, and if thats in the top of bottom of another row it will be wrong.
I know the code has done nothing to account for that, that’s because I dont even know where to begin doing something like that.
here is the code:
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Tiles = game.Workspace:WaitForChild('Tiles')
local MineAmmount = 5
local FirstClick = true
local mineExcept
local images = {
["0"] = "rbxassetid://116277024059622",
["1"] = "rbxassetid://86859798607558",
["2"] = "rbxassetid://112380134886854",
["3"] = "rbxassetid://91719019875877",
["4"] = "rbxassetid://137217826661977",
["5"] = "rbxassetid://121337137503859",
["6"] = "rbxassetid://123633267039417",
["7"] = "rbxassetid://140313058714412",
["8"] = "rbxassetid://88648128062491",
["Mine"] = "rbxassetid://6737405081",
["Flag"] = "rbxassetid://10720664742"
}
function assignMine()
local chosenTile = math.random(1,81)
if Tiles[chosenTile].Mined.Value ~= true and Tiles[chosenTile].Name ~= mineExcept then
Tiles[chosenTile].Mined.Value = true
--Tiles[chosenTile].Decal.Texture = images["0"]
else
assignMine()
end
end
function spawnMines()
---for i, tile in pairs(Tiles:GetChildren()) do
-- tile.Decal.Texture = images["0"]
--end
for i = 1, MineAmmount do
assignMine()
end
end
function getMinesNearbyAll()
for i, tile in pairs(Tiles:GetChildren()) do
if tile.Mined.Value == true then
else
local tileName = tonumber(tile.Name)
local minesNearby = 0
local positionsToCheck = {-1, 1, -9, 9, -10, 10, -8, 8}
for _, offset in ipairs(positionsToCheck) do
local neighborTileName = tileName + offset
local neighborTile = Tiles:FindFirstChild(tostring(neighborTileName))
if neighborTile and neighborTile.Mined.Value == true then
minesNearby = minesNearby + 1
end
end
tile.MinesNearby.Value = minesNearby
end
end
end
function revealAll()
for i, tile in pairs(Tiles:GetChildren()) do
if tile.Mined.Value == true then
tile.Decal.Texture = images["Mine"]
else
local tileName = tonumber(tile.Name)
local minesNearby = 0
local positionsToCheck = {-1, 1, -9, 9, -10, 10, -8, 8}
for _, offset in ipairs(positionsToCheck) do
local neighborTileName = tileName + offset
local neighborTile = Tiles:FindFirstChild(tostring(neighborTileName))
if neighborTile and neighborTile.Mined.Value == true then
minesNearby = minesNearby + 1
end
end
tile.Decal.Texture = images[tostring(minesNearby)] or images["0"]
end
end
end
function floodReveal(tile)
local alreadyChecked = {}
local positionsToCheck = {-1, 1, -9, 9, -10, 10, -8, 8}
local minesNearby = 0
local function get(localTile)
print("get function fired")
local tileName = tonumber(localTile.Name)
for _, offset in ipairs(positionsToCheck) do
local neighborTileName = tileName + offset
local neighborTile = Tiles:FindFirstChild(tostring(neighborTileName))
if neighborTile and alreadyChecked[neighborTileName] == nil then
--if rayCast(localTile, neighborTile) then
if neighborTile.Mined.Value == false then
local nearby = getMineNearby(neighborTile, "true")
print(nearby)
neighborTile.Decal.Texture = images[tostring(nearby)]
alreadyChecked[neighborTileName] = true
task.spawn(get, neighborTile)
end
--end
end
end
end
get(tile)
end
function rayCast(start, target)
local rayDirection = target.Position - start.Position
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {start}
local rayResult = workspace:Raycast(start.Position, rayDirection, rayParams)
if rayResult.Instance then
return true
else
return false
end
end
function getMineNearby(tile, instruction)
if tile.Mined.Value == true then
tile.Decal.Texture = images["Mine"]
revealAll()
else
local tileName = tonumber(tile.Name)
local minesNearby = 0
local tileRow = tile.Row.Value
local positionsToCheck = {-1, 1, -9, 9, -10, 10, -8, 8}
for _, offset in ipairs(positionsToCheck) do
local neighborTileName = tileName + offset
local neighborTile = Tiles:FindFirstChild(tostring(neighborTileName))
if neighborTile and neighborTile.Mined.Value == true then
minesNearby = minesNearby + 1
end
end
if minesNearby == 0 and instruction == "false" then
tile.Decal.Texture = images["0"]
floodReveal(tile)
else
tile.Decal.Texture = images[tostring(minesNearby)]
return minesNearby
end
end
end
for i, CD in pairs(Tiles:GetDescendants()) do
if CD:IsA("ClickDetector") then
CD.MouseClick:Connect(function()
if FirstClick == true then
FirstClick = false
mineExcept = CD.Parent.Name
spawnMines()
getMinesNearbyAll()
task.wait(1)
getMineNearby(CD.Parent, "false")
else
print('left click')
getMineNearby(CD.Parent, "false")
end
end)
CD.RightMouseClick:Connect(function()
print("right click")
if CD.Parent.Decal.Texture == "" then
CD.Parent.Decal.Texture = images["Flag"]
elseif CD.Parent.Decal.Texture == images["Flag"] then
CD.Parent.Decal.Texture = ""
end
end)
end
end