Discuss Scratch

fireakingen
New Scratcher
2 posts

Help with minesweeper

Hello

I'm making a minesweeper clone and need some help. I have made each tile a clone represented by an item in a list, item 1 is the top-left, item 2 is the tile right of that, and so on. I have made all the numbers next to the tiles and it is working great. However, when I click on a blank tile, I can't get all adjacent empty tiles to clear as well. I tried clearing each tile directly neighboring it until I reached a tile with a number, but that broke everything and handling diagonals is much harder as well. How can I get this feature to work?

Here is the project: https://scratch.mit.edu/projects/515298477/

Thank you for any tips.
Anonymous

Last edited by fireakingen (April 14, 2021 04:47:08)

NormallyNormal
Scratcher
49 posts

Help with minesweeper

I think you would probably want something like this: https://en.wikipedia.org/wiki/Breadth-first_search

Since there isn't recursion in scratch, you'll need to do the queue method. Here's how it would work:

When the player clicks a tile, add it to a list called “Queue”. Then, if its not a number or mine, delete it from “Queue”, and add all surrounding unrevealed tiles to “Queue”. If it is a number or mine, delete it but DON'T add the surrounding tiles to “Queue”. Now if that first tile was empty, “Queue” should have eight tiles in it. Start with the first one and preform the same process as above, delete it, and then a new tile will become #1 in “Queue”. As tiles get deleted from “Queue” make them revealed on the board. Keep going until “Queue” is empty, and when it is, stop. This will create the “fill” effect you see in minesweeper. Be sure that you are only adding UNREVEALED tiles to “Queue”, so that the algorithm doesn't double back on ones that have been checked and get caught in a loop.

To get which eight tiles surround a particular tile, lets look at a 8x8 board, like you have, with labels.

01 02 03 04 05 06 07 08
09 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64

Suppose we pick tile 36. I'll put 36 and the tiles around it in bold.

01 02 03 04 05 06 07 08
09 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64

So our relevant tiles are: 17 28 29, 35 36 37, 43 44 45
Notice a pattern?
The first three are (36) - 9, (36) - 8, and (36) - 7
Next three are (36) - 1, (36) - 0, and (36) + 1
Final three are (36) + 7, (36) + 8, and (36) + 9

That will work for MOST tiles. MOST. You can probably spot the problem tiles.
Anything on an edge.
Before we get our eight tiles, we can check if its on an edge.
What do all the tiles on the top edge have in common? They are 8 or less. So, if a tile is 8 or less, don't look for tiles (x) - 9, (x) - 8, or (x) - 7.
What do all the tiles on the bottom edge have in common? They are 57 or more. So if a tile is 57 or more, don't look for tiles (x) + 7, (x) + 8, or (x) + 9.
The sides are a bit more tricky, but can be done.
All the tiles on the far right are perfectly divisible by eight. We can check this with
x/8 == round(x/8)
If that returns true, then it's on the right edge and we don't check tiles (x) - 7, (x) + 1, or (x) + 9.
For the left side, they are all multiples of 8 plus one. We can check this with
(x-1)/8 == round((x-1)/8)
If that returns true, then it's on the left edge and we don't check tiles (x) - 9, (x) - 1, or (x) + 7.

For each step in the breadth first search algorithm, we will check with the above steps to see which tiles of the 8 surrounding to add. It will probably be easier to check if a tile SHOULD be added, because you can negate the above statements. For example, if our tile ISN'T less than eight, add tile (x) - 8. If our tile ISN'T less than 8 AND ISN'T divisible by 8, add tile (x) - 7. Notice how the corner cases require two separate statements combined by an AND operator.

That should do it. If you have any questions, just ask.

Last edited by NormallyNormal (April 14, 2021 10:27:36)

CST1229
Scratcher
1000+ posts

Help with minesweeper

The method I used in my Minesweeper project is that there's a custom block that clicks a tile. If it detects another number or empty tile next to it (via some somewhat complicated addition and modulus), it runs the block recursively with the tile next to it. I do that for all 8 directions.
Also make sure that the tile to click is in bounds or else it will wrap around.

Last edited by CST1229 (April 15, 2021 16:07:25)

Powered by DjangoBB