Hey all! I’ve been working on a multiplayer minesweeper battling game, based on the old computer game. It has been coming across nicely, but there are a few changes I think might need to be implemented in order to prevent issues in the future.
What does my code do?
The section of code I’m sharing below manages the communication of the pieces on the game board. This means that it will check the surrounding tiles for mines, and display a number for how many mines are around it. If there are 0 mines around the tile, it will tell its neighbors to automatically clear, which creates this really cool expanding effect, that also generates a unique board every time:
It’s completely random, and has changable values for the amount of mines, allowed time, etc.
The one thing it doesn’t have the capability of, being able to set a certain game board length/width. For example: a 10x10 board, all the way to a huge 100x100 board. This is because of the way I have defined each pieces neighbors.
Example code for piece a1:
local arow = game.Workspace.real.arow
-- arow
local a1ne = {brow.b1, brow.b2, arow.a2} -- the neighbors of a1, referencing game objects
arow.a1:FindFirstChild("isperma"):GetPropertyChangedSignal("Value"):Connect(function() -- check if already defined
wait(0.000001) -- this is bad, finding a solution currently
if cleartosearch == true then
if arow.a1:FindFirstChild("isperma").Value == true then
if arow.a1:FindFirstChild("status").Value == "0" then -- aka, its not a mine
print('checking for bombs')
for index, neigh in pairs(a1ne) do -- the table assigned with a1's neighbors
if neigh:FindFirstChild('status').Value == "1" then
if finalbomba1.Value == false then -- value object within a1
amountbombsa1.Value = amountbombsa1.Value + 1 -- other value object within a1
arow.a1.SurfaceGui.TextLabel.Text = tostring(amountbombsa1.Value) -- cosmetic stuff
arow.a1.SurfaceGui.Enabled = true
end
end
end
finalbomba1.Value = true
if amountbombsa1.Value == 0 then -- it has no bombs around it
for i, v in pairs(a1ne) do
if v:FindFirstChild('isperma').Value == false then -- make all the neighbors around it cleared
v.BrickColor = BrickColor.new('Shamrock')
v.isperm.Value = true
v.status.Value = "0"
end
end
end
end
end
end
end
end)
Now this code runs smoothly (other than that gross wait), but it has an issue. It’s a pain to expand the game board, and there is no potential for board customization. In order to implement each neighbor I have to manually write out the tiles surrounding the piece, and then copy and paste this function with each of the pieces! I thought of using script.Parent
with an induvial script for each of the pieces, but it doesn’t solve the neighbor problem that way.
End Goal
I want to find some way to make checking the neighbors of each tile a process that can be used even with a changing board. I want to give the player the ability to create board that can be from 5x5 all the way to 128x128. I currently can’t do this because I haven’t found a way to create a table for each pieces neighbors, other than manually defining it. I tried thinking of putting tables into each piece as an object that we can use FindFirstChild
to reference, but that doesn’t seem possible.
Any help would be appreciated!