#Minesweeper dilemma

32 messages · Page 1 of 1 (latest)

lofty zenith
#

I'm trying to make a minesweeper game, but I'm having trouble with adding 1 to each not-bomb tile for every bomb tile it touches. Here's my code

local position = Vector3.new(0,0,0)

local blockCount = 25
local blockCounting = 0

local totalFlags = 100
local currentFlags = 0

for i = 1, blockCount, 1 do
    for e = 1, blockCount, 1 do
        blockCounting += 1
        print("ran")
        local block = game.ServerStorage.BlockClone:Clone()
        block.Name = block.Name .. blockCounting
        block.Position = position
        block.Parent = workspace.Blocks
        
        local flagInt = block:WaitForChild("FlagCount")
        local click = block:WaitForChild("Click")
        
        if (e + i) % 2 == 0 then
            block.BrickColor = BrickColor.new("Slime green")
        else
            block.BrickColor = BrickColor.new("Sand green")
        end
        
        if currentFlags < totalFlags then
            
        end
        position += Vector3.new(5,0,0)
    end
    position += Vector3.new(-5*blockCount,0,-5)
end

local tileList = {}
for i, tilePart in ipairs(workspace.Blocks:GetChildren()) do
    table.insert(tileList, tilePart.Name)
end

for i = 1, 100, 1 do
    local flagTile = math.random(1, #tileList)
    
    local block = workspace.Blocks:FindFirstChild(tileList[flagTile])
    local flagInt = block:WaitForChild("FlagCount")

    flagInt.Value = -1
    block.BrickColor = BrickColor.new('Really red')
    currentFlags += 1
    print(currentFlags)

    table.remove(tileList, flagTile)
end
feral monolith
#

Quick change: I moved table.insert into the process of the actual block creation since they will all be dealt with individually anyways. So no need to scan through all 625 blocks again.

If you want to add 1 to each not-bomb tile, we first needed to create a table that covers all 8 directions.
After a tile is flagged, we then run a numeric loop for all 8 directions, and doing small math to determine the BlockNumber of the adjacent tile in each direction. (the math I included only works if the board is square)
Use an if-statement to make sure it's not below 0 or above the total number of tiles.
Use another if-statement checking if the int value isn't already -1 (isn't a flag tile), then we add onto it.
After that, done!

Since the script itself is too long to send here, here's the modified script that also includes blocks with displays. (use it however you wish)

#

@lofty zenith Hope this helps!

rain yacht
#

since he is making his own so its not a copy

#

coping things doesnt mean u will get better

feral monolith
#

Minesweeper already exists, I don't think it's out of the question to copy or plagiarize code that's offered to you ;-;
What matters most is if you understand the concept, it's a fruitless effort to make a truly original method of programming now-a-days.

rain yacht
#

that makes it useless making ur own version is much better than coping something else

feral monolith
#

Yeah if the situation calls for it, but if there's no reason to change already-working and efficient code that you can already understand and can reproduce on your own, it's just a waste of energy.

rain yacht
#

na making a own version makes it better to understand it than getting someone elses work

feral monolith
rain yacht
#

na

#

its better to do it by himself its much better to learn than have someone give up a 10 hour script that does semiwork
then have someone else send a file that has everything that ruins basically everything

#

its better to give a example than send a file that is basically fully working

feral monolith
#

Dude please tell me you aren't illiterate.

He literally asked for help, so I explained the concept to him, and provided the example as needed. (not gonna hold out on him until a couple hours later if he actually needed a relative/practical example)
As long as he understands how it works in the end and how to replicate, there's no problem.
I only said he can use it how he wishes, not that he can just copy+paste the stuff.

lofty zenith
#

@feral monolith, thank you so much! I did the exact thing you did, but likely in a different method, but I was having issues with :FindFirstChild(), so I ditched it.

#

Also, how to I open that file? It keeps bringing me to internet explorer

feral monolith
rain yacht
feral monolith
feral monolith
rain yacht
lofty zenith
#

I ditched the script, as it kept giving me an error with FindFirstChild()

lofty zenith
#

I have the piece that generates 100 random flags.

rain yacht
#

So was it a local or a script

lofty zenith
#

A server script

#

It's a multiplayer game

rain yacht
#

So use the Local to do the Color Changing part and the server to do the Explode part