Click here to Skip to main content
16,003,746 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
So I'm developing a minesweeper game and im assigning the mines, but I've got to check where are the mines now, in order to generate the numbers. I already created the code to go through the lines and columns, but I'm having trouble on verifying the neighbouring squares to check if they have a mine already there.

Here's how my code looks like now:

VB
Public Sub avisinhos(ByVal line, ByVal column)

        Dim linestart As Integer = -1
        Dim lineend As Integer = 1
        Dim colstart As Integer = -1
        Dim colend As Integer = 1



        If line < 2 Then
            linestart = 0
        End If

        If line > 8 Then
            lineend = 0
        End If

        If column < 2 Then
            colstart = 0
        End If

        If column > 8 Then
            colend = 0
        End If


        For auxlinha = linestart To lineend
            For auxcolumn = colstart To colend
                'check
            Next
        Next

    End Sub


How do I create a IF function to verify the mines and generate the numbers around them?

Best regards.
Posted
Comments
Sergey Alexandrovich Kryukov 1-May-12 14:09pm    
Where is your method/property used to check the state of the cell? Where is your current cell is? Without it, the question is pointless.
--SA
joao.ribeiro.94 1-May-12 14:15pm    
Public Sub initflags()

Dim line, column As Integer
For line = 0 To 9
For column = 0 To 9
mat(line, column) = 0
Next
Next

Dim r, c As Integer

Do Until numbandeiras = 34



Randomize()

line = Int(Rnd() * 10)
column = Int(Rnd() * 10)
r = line
c = column
If mat(line, column) = 0 Then
numbandeiras = numbandeiras + 1

Call avisinhos(line, column)

mat(line, column) = 1

End If

Loop

End Sub
Sergey Alexandrovich Kryukov 7-May-12 19:56pm    
There is nothing random in checking up of the state. Do you understand what are you doing? It does not seem so...
Random is only needed when you populate the array with mines. Also, this is a common mistake: Randomize should not be called inside loop (why? why?!). It should be called only once per application lifetime.
--SA
Sergey Alexandrovich Kryukov 7-May-12 19:57pm    
Reason for my vote of 2
By the results of discussion...
--SA

1 solution

Please see my comment to the question. Who knows what are you trying to do int this code? — but it's no wonder it goes nowhere.

This game is a similar Cellular Automaton, and a very simple one. Please see:
http://en.wikipedia.org/wiki/Cellular_automaton[^].

You need to define the set of cells. In this simple case of finite set with square cells, a simple array of rank 2, X by Y, will do just fine. The value of each array element should carry a current state of the cell: a mine present or not, reveled or not, flag is set or not, etc. As the state is the fundamental notion of the Cellular Automaton, the state should be one distinct type of object.

Then you should define what is a set of the neighborhood. As this is a distinct and fundamental notion of the Cellular Automaton, it's the best to put the definition of the neighborhood set in one discrete function. A function returning the list of cell coordinates would do just fine.

After this step, define the rules for each step of a player, who can eventually loose, discover an mine-free cell, etc. As each rule is very simple and only involves the number of neighboring cells in certain state, the central algorithm of this simple game is also very simple: obtain the neighborhood set (see above) and then simply iterate through it counting the cells in certain state (having a mine). You also need to implement clearing of the mine-free area automatically based on logical inference. You will need to develop some iteration cycle which should finish when all possibilities for inference are exhausted.

Let's summarize it:
    <il>Definition of the set of all cells, also called world.
    <il>Definition of the cell state.
    <il>Definition of the neighborhood.
    <il>Definition of the rule for checking up a cell; implementation based on the iteration through the neighborhood.
    <il>Application of the rule and transition between the states of a cell probed by a player.
    <il>Main player cycle.
    <il>Automatic clean up of the mine-free area based on logical inference.


If you through out ad-hoc approach and apply this regular approach, your work will go smoothly.

—SA
 
Share this answer
 
v3
Comments
joao.ribeiro.94 1-May-12 14:32pm    
I use the mat(line,column) function to store and distribute a mine, whenever there is a mine, mat(line,column)'s value equals 1.
Sergey Alexandrovich Kryukov 7-May-12 19:57pm    
So what?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900