Question about a minesweeper game

Rubik

Newcomer
Joined
Apr 25, 2010
Messages
8
Hey guys, I'm having a few problems trying to make this. I'm trying to make a minesweeper type game for a class project and my teacher really isn't helping me out at all, so I'm helping I can get advice from some of you guys. Now I'm not a excellent programmer but I know a moderate amount of things for VB, but not enough to help me with this.

For my game so far I actually created the form to look like the original minesweeper and also created 81 buttons, which I'm sure was a stupid idea. To tell you the truth though, I have absolutely NO idea what to do, I'm lost. I've been doing research and trying to find minesweeper games online to see how the code works, but when I do and look at it, none if it makes sense to me one bit.

I don't want someone to just hand me code, but it would be super helpful if I could get pointed in the correct direction on how to start it and maybe some good and bad ways to go about things.

Thanks,
Nate
 
Well, as far as the 81 button go, no, putting them on the form in the designer is not the best approach. The biggest problem is that it isn't even slightly dynamic. At the very least, you need to be able to refer to the buttons by their coordinate. Ideally, you want to be able to create as many as you need to let the user select different field sizes, but that's up to you. One other thing I think I should point out is that while you can use buttons, a more typical approach would be to draw the minefield yourself using a set of images that represents each possible state for a single grid square (flagged, empty, with a digit from 1 to 8, ect.). Again, up to you.

This isn't really an answer to you question, but a good place to start is with a specification. By writing out a detailed description of how your program is going to work, you make the actual process of coding much, much easier. I don't mean you necessarily have to detail how the program is implemented, but rather how it works from the user's point of view. But you need to be very specific. When you start banging code out (or throwing buttons on a form) before you know exactly what the code is supposed to do, you are bound to run into more problems.

A specification would describe what the layout of the application is, exactly what happens when each button is clicked (for every state the button might be in), so on. Every little thing the user might do would be accounted for, and the result must be very well defined.

Back to your question, you probably don't want to put any minefield buttons on the form in the designer. Instead, you can create them in code and store them in an array. That way you refer to each button by it's location rather than a name.

First you need an array to hold the buttons. I recommend a 2-D array here.
Code:
[COLOR="Green"]' The comma indicates 2 dimensions, as in x[COLOR="Red"],[/COLOR] y[/COLOR]
Dim mineFieldButtons As Button(,)
That declares a variable to hold the array, but doesn't create the array. You will need to wait until the user specifies a size if you want to give him that option, but at some point you'll create the array.
Code:
[COLOR="Green"]' Create our array[/COLOR]
mineFieldButtons = [COLOR="Blue"]New Button(width, height) {}[/COLOR]
That doesn't give us any buttons, though, just an empty array. So now the important part: use a pair of nested for loops to create the actual buttons.
Code:
For X As Integer = 0 To Width - 1
    For Y As Integer = 0 To Height - 1
        [COLOR="Green"]' Create the button[/COLOR]
        mineFieldButtons(X, Y) = [COLOR="Blue"]CreateButton(X, Y)[/COLOR]
        [COLOR="Green"]' Add it to the form[/COLOR]
        Controls.Add(mineFieldButtons(X,Y))
    Next
Next
CreateButton is a function we have yet to define. It will not only create a button, but set its location and size.
Code:
Function CreateButton(X As Integer, Y As Integer) As Button
    Dim result As New Button
[COLOR="Green"]    // Set size
    // Set location based on X and Y[/COLOR]

    Return result
End Fuction

That will get you a grid of buttons, which you can access by coordinate, i.e. mineFieldButtons(gridX, gridY).Text = "BOOM". The problem is you also need to (A) handle the click event for each of them and, (B) in the event handler, figure out which button was clicked. For (A) I recommend you research the AddHandler keyword.

For (B), a good approach would be to create a Point object that contains the buttons X and Y, and assign that Point to the button's .Tag property. This would be done in the CreateButton function. Then, in the event handler, you can cast the sender parameter to a button (using CType), get it's .Tag property, cast that to a Point, and then get the X and Y.

FYI, I've created a minesweeper clone before and the hardest part wasn't creating the minefield. Figuring out the "flood-fill"-style clearing of mineless spots when the user clicks on a button is much trickier. This is why I recommend the specification. As you write down exactly what happens when the user clicks on a button, you realize that it's a little more complicated than it seems. Then you know what you're in for before you start writing the program.
 
hey thanks for the reply man, it seems like I got myself in WAY over my head, but I am determined to finish this to the best of my abilities. I know its not a whole lot but here is what I have so far for creating the grid, is this kind of what you were talking about?
Code:
  Dim x1 As Integer = 0
        Dim Buttons(200) As Button
        For y As Integer = 81 To 225 Step 16
            For x As Integer = 12 To 156 Step 16
                Buttons(x1) = New Button
                Buttons(x1).Location = New Point(x, y)
                Buttons(x1).Width = 16
                Buttons(x1).Height = 16
                Me.Controls.Add(Buttons(x1))
                x1 += 1
            Next
        Next

        Dim rand As New Random()
        Dim number = rand.Next(1, 10)

That's my code for creating the grid, and it seems to work so far. And right now I'm attempting to figure out how to fill the array with random numbers which will be the bomb (I think). Then figure out how to make the buttons go down when clicked.
 
You've taken a big step in the right direction, but your approach is a little unorthodox:
Code:
        For y As Integer = 81 To 225 Step 16
            For x As Integer = 12 To 156 Step 16
You're looping over the locations (pixel coordinates) you want the buttons at. While it works, I recommend against it. You would be better served by looping over the cell coordinates:
Code:
        For y As Integer = 0 To 9 [COLOR="Green"]' 10 Wide[/COLOR]
            For x As Integer = 0 To 9 [COLOR="Green"]' 10 Tall[/COLOR]
Then you can calculate the location of each button based on the X and Y. This might strike you as a little round-a-bout. Why not have the for-loop calculate the location for you? Because it makes the code a little harder to read, and much harder to modify. And on that note, I strongly recommend you use constants instead of putting the values directly in your logic. That would bring us here:
Code:
Const GameWidth As Integer = 10
Const GameHeight As Integer = 10
Const CellWidth As Integer = 16
Const CellHeight As Integer = 16
Const GridLeft As Integer = 12
Const GridTop As Integer = 81

[COLOR="Green"]' Later on...[/COLOR]
    For y As Integer = 0 To GameWidth - 1
        For x As Integer = 0 To GameHeight - 1
            buttons(x, y) = new Button
            Dim ButtonX As Integer = GridLeft + X * CellWidth
            Dim ButtonY As Integer = GridTop + Y * CellHeight
            buttons(x,y).Location = New Point(ButtonX, ButtonY)
Now, that seems like a lot of extra work to get the same job done, but notice two things. First, we don't even need any comments there. You, any other programmer, or you a year in the future can look at that code and know what the loop does. It loops over the game field, makes buttons, and calculates the positions. It's even obvious how it calculates the positions. Because we use descriptive names rather than "magic numbers", we have what we call self-documenting code. You should always comment your code when you need to, but the less often you need to, the better.

The other thing to notice is that we can now easily tweak our program by changing the constants. Want to make the game 20x20? Change GameWidth and GameHeight each to 20. Want to make the cells wider? Change CellWidth to 20.



The bigger problem with your code is that you seem to have overlooked a huge point in my first post: the 2-D array. If you don't understand them that well, I'm glad to answer any questions, but it really isn't too different from a normal array. Instead of identifying each value by one index, you identify each value by two indecies. It makes the array look more like a table (or grid), instead of a list.
Code:
' Why 200?
Dim Buttons(200) As Button
' Becomes
Dim Buttons(GameWidth - 1, GameHeight - 1) As Button
Again, more code, but it explains itself much better.

The reason I so strongly recommend the 2-D array is because it creates a strong connection between the visual and logical aspects of the program. If the user clicks on button(3, 3), you need to show the number of surrounding bombs for that spot. You need to look at each surrounding cell and tally the bombs. (The approach you're taking for assigning the numbers probably won't work. Each spot either has a bomb or it doesn't. You need to calculate the numbers later. We can get into all that later.) For button(3, 3), these are the surrounding cells we need to check:
  • button(2, 2): up, left
  • button(2, 3): up
  • button(2, 4): up, right
  • button(3, 2): left
  • button(3, 4): right
  • button(4, 2): down, left
  • button(4, 3): down
  • button(4, 4): down, right
We can generalize this very easily:
  • button(x - 1, y - 1): up, left
  • button(x, y - 1): up
  • button(x + 1, y - 1): up, right
  • button(x - 1, y): left
  • button(x + 1, y): right
  • button(x - 1, y + 1): down, left
  • button(x , y + 1): down
  • button(x + 1, y + 1): down, right
Now we can apply our surrounding-bomb-counting logic to any (x, y).

How would you do that with your array? If the user clicks button(55), how does the program know which surrounding boxes to check? Button(54) would (usually) be to the left, and button(56) to the right, but you have no easy way to figure out which buttons are above and below. The user interface is a grid. Why not make our data a grid?

Now, if you can digest the loops, the constants, and the 2-D array we can move onto distributing and counting the bombs and what happens when the user clicks a button.
 
Hey again, thanks so much for taking time to reply, I hope I'm not wasting your time. Anyway here's my code so far
Code:
  'The width of the game
        Const GameWidth As Integer = 9
        'The height of the game
        Const GameHeight As Integer = 9
        'Width of the Cell
        Const CellWidth As Integer = 16
        'Height of the cell
        Const CellHeight As Integer = 16
        'Grid
        Const GridLeft As Integer = 12
        'Grid
        Const GridTop As Integer = 81

        'Declaring the minefield as a 2D array
        Dim mineField(GameWidth - 1, GameHeight - 1) As Button
        'Loop to create the minefield with buttons
        For y As Integer = 0 To GameWidth - 1
            For x As Integer = 0 To GameHeight - 1
                mineField(x, y) = New Button
                mineField(x, y).Width = 16
                mineField(x, y).Height = 16
                Dim ButtonX As Integer = GridLeft + x * CellWidth
                Dim ButtonY As Integer = GridTop + y * CellHeight
                mineField(x, y).Location = New Point(ButtonX, ButtonY)
                Me.Controls.Add(mineField(x, y))
                'AddHandler mineField(x, y).Click, AddressOf mineField
            Next
        Next

I understand for the most part the loops and how it works, the only thing I really don't get is this part:
Code:
         'Grid
Const GridLeft As Integer = 12
        'Grid
        Const GridTop As Integer = 81
And I tried to look up how to make a click event in the form to see which button was clicked but as you can tell with the addhandler that it didn't work and I know its not far but it seems like I can only progress one step at a time =(.
Here is my Checklist of what I'm trying to do
1. Make it so when a box is clicked it knows its clicked, if that makes sense.
2. When it's clicked, make it go flat, which goes along with 1.
3. Figure out how to get the bombs and numbers in the array. Once again I attempted and looked on google but kind of failed. But I will keep working!!

Now we can apply our surrounding-bomb-counting logic to any (x, y).

How would you do that with your array? If the user clicks button(55), how does the program know which surrounding boxes to check? Button(54) would (usually) be to the left, and button(56) to the right, but you have no easy way to figure out which buttons are above and below. The user interface is a grid. Why not make our data a grid?
To do that in an array I assume you assign random numbers which are the bombs.

If the user clicks the button I'm not sure exactly how to show it was clicked.
 
First things first. I don't know if it's just the way you pasted it or if that is actually how you have the code set up, but you want all the constants and, most importantly, the array, declared outside of any sub. If they're declared within a sub, they can only be accessed within that sub, but you'll need to access them from other subs too. If you declare them outside a sub but inside the form's code, you can access them anywhere within the form's code.

I understand for the most part the loops and how it works, the only thing I really don't get is this part:
Code:
Code:
Grid
Const GridLeft As Integer = 12
        'Grid
        Const GridTop As Integer = 81
This is the top-left corner of the grid, based on this code:
Code:
        For y As Integer = [COLOR="Red"]81[/COLOR] To 225 Step 16
            For x As Integer = [COLOR="Red"]12[/COLOR] To 156 Step 16
We add GridLeft and GridTop to the calculated position of the buttons because otherwise the grid would be located at the top-left corner of the form. Do you have the program running? If so, try changing GridTop and GridLeft both to zero and see what happens to the program.


Now, as for detecting when a button is clicked, you were pretty close with AddHandler. The second parameter for AddHandler should be the name of a sub that handles the click event. But we don't have any such sub. We need to make one first.

So let's make a sub to handle the click event, then come back to AddHandler. It needs to have the right signature. (In other words, it needs to accept the right parameters.) One place we can look to see what parameters the click event handler should handle is the Click event documentation. There is an example near the bottom of a handler with the right parameters. Your event handler should accept the same parameters. You can give it any fitting name you like.

For the sake of example, I'll assume you've named you click event handler Minefield_Click. That's what you should specify as the handler when you use AddHandler. The syntax for AddHandler is AddHandler event, AddressOf handler. You've got the event right, you just need to specify your new event handler.

Assuming all goes well, any code you type into the Minefield_Click sub will be executed every time any one of the buttons is clicked. But once a button is clicked, we need to know which button. A very wise man once said:
The Mysterious Mister X said:
For (B), a good approach would be to create a Point object that contains the buttons X and Y, and assign that Point to the button's .Tag property. This would be done in the CreateButton function. Then, in the event handler, you can cast the sender parameter to a button (using CType), get it's .Tag property, cast that to a Point, and then get the X and Y.
If you aren't familiar with the .Tag property or the CType keyword, the documentation is one google away. The point is that you can store a piece of data for each button that you can get back in the event handler. If you store the location of the button, you can get that back in the click event handler, and then you know which button was clicked.
 
I got the click event to work! is this the right way of going about it though?
Code:
    Private Sub mineGrid_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.Click
        If TypeOf sender Is Button Then
            Dim b As Button = sender
            b.FlatStyle = FlatStyle.Flat
            lblButtonClicked.Text = b.Tag
        End If
    End Sub
End Class
And heres my addhandler with the .tag
Code:
 mineField(x, y).Tag = CStr(x)
                mineField(x, y).Tag = CStr(y)
                AddHandler mineField(x, y).Click, AddressOf mineGrid_Click
 
Last edited:
Are you putting this code into Visual Studio and running it? Are you checking to make sure it's doing what it's supposed to? The reason I ask is because of this:
Code:
mineField(x, y).Tag = CStr(x)
mineField(x, y).Tag = CStr(y)
[COLOR="Green"]' Also note that the tag does [i]not[/i] need to be a string[/COLOR]
You can only set the tag to one thing at a time. If you need to store more than a single value (X and Y would be two values) you need to use a data type that holds more than a single value.

If you run the program, when you click the button you should notice that only the Y value is coming up. The X will be missing.
For (B), a good approach would be to create a Point object that contains the buttons X and Y, and assign that Point to the button's .Tag property.

Here are problems #2 and #3:
Code:
    Private Sub mineGrid_Click(ByVal sender As Object, [COLOR="Red"]ByVal e As System.Windows.Forms.MouseEventArgs[/COLOR]) [COLOR="Red"]Handles MyBase.Click[/COLOR]
e Shouldn't be a MouseEventArgs. I'm not sure where you got that from and I imagine it's resulting in an error. (If you are getting errors, you should probably mention so.) Double check the documenation.

Also, you have Handles Mybase.Click. That also wasn't in the documentation I linked to. Within the context of your form's code, MyBase refers to the form. You don't want to handle the click event for the form, so you don't want to handle Mybase.Click.


There is another thing that bothers me:
Code:
    Private Sub mineGrid_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.Click
        If TypeOf sender Is Button Then
            [COLOR="Red"]Dim b As Button = sender[/COLOR]
            b.FlatStyle = FlatStyle.Flat
            [COLOR="Red"]lblButtonClicked.Text = b.Tag[/COLOR]
        End If
    End Sub
The lines in red tell me that you aren't coding with option strict on. So let's discuss option strict.

You are checking if sender is a Button before you treat it like a Button, and that's good. You've got exactly the right idea there. Option strict forces you to keep these things in mind. Almost any experienced VB programmer will recommend you enable option strict.

Let's look at a hypothetical to see why. Suppose for a second you didn't think to check whether sender was a Button. Your code would look like this:
Code:
    Private Sub mineGrid_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.Click
        Dim b As Button = sender
        b.FlatStyle = FlatStyle.Flat
        lblButtonClicked.Text = b.Tag
    End Sub
With option strict off, that code would still build and run. And what happens if sender is not a button? Your program crashes. With option strict on, you would get an error with that code:
Code:
    Private Sub mineGrid_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.Click
[COLOR="Red"]        Dim b As Button = sender 'Error: Option Strict On disallows implicit conversions from 'Object' to 'System.Windows.Forms.Button'.[/COLOR]
        b.FlatStyle = FlatStyle.Flat
        lblButtonClicked.Text = b.Tag
    End Sub
You would get an error because the compiler doesn't know that sender is a Button. That's the point of option strict: strict type checking. When you get the error, you say, "Ah ha! I need to check sender and make sure it is a button."

You would need to tell the compiler that you know sender is a Button, and you do that with a type cast. Dim b As Button = sender becomes Dim b As Button = CType(sender, Button). In this case, CType basically means "I know sender is a button and I would like to treat it like a button." By forcing you to acknowledge that you are treating an Object as a Button, Option strict prevents all sorts of problems. Like I said, you did the right thing here, but with option strict on the compiler will always remind you that you need to check the type of an object.

With option strict on, your code will look like this:
Code:
    Private Sub mineGrid_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.Click
        If TypeOf sender Is Button Then
            Dim b As Button = [COLOR="Blue"]CType(sender, Button)[/COLOR]
            b.FlatStyle = FlatStyle.Flat
            lblButtonClicked.Text = [COLOR="Blue"]CStr(b.Tag)[/COLOR]
        End If
    End Sub
That code, however, does not address problems #2 and #3 from above. Once you fix these problems, run the program and make sure it does what it's supposed to be doing before moving on.
 
You're correct about this part how it only shows the y coord.
Code:
mineField(x, y).Tag = CStr(x)
mineField(x, y).Tag = CStr(y)
' Also note that the tag does not need to be a string
I kept getting errors trying the minefield(x,y).tag = CSint(x,y) so I tried it the way I had it and it worked so I kind of figured it was right.

The reason I had this code in there was because my teacher found something similar to what I was doing so I followed that code and came up with this. Which I'm guessing is wrong =p and I'm not getting an error from it. I went to the site you sent me but I kept getting strange errors, so frustrated and no one to really ask I kinda gave up on that for the night and was getting ready to work on it in the morning.
Code:
 Private Sub mineGrid_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.Click

EDIT: Now I realize what I quit trying to get this to work its because I was getting an error in this code and I couldn't figure out what to do.
Code:
 Private Sub mineGrid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles [U][COLOR="Red"]Button[/COLOR][/U].Click
The red underlined part is where I get the error but I don't know what to really do, I've declared minegrid as an event up top, and I tried putting mineGrid where button is and its not cooperating.
Its declared like this:
Code:
'Declare minegrid as a click
    Public Event mineGrid As EventHandler
EDIT2: this is the error I get. Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.


And as for the strict option, I am NOT coding with it, I'm not even sure what that means but I'll read up on it as soon as I'm done posting this.


I'm not sure what to do with problems two and three to be honest, but i'll go back to the site you linked me to and give it another attempt at it.
 
Last edited:
Let's take a couple of steps back. We don't need to declare anything as an event or an EventHandler.

I guess I should have been clearer about the "handles" part. You have a control with an event you want to handle (in our case, "mineField(x, y).Click). You have an event handler that you want to use to handle that event (in our case, "mineField_Click"). You need to connect the event to the event handler. VB gives us two ways to do that: "Handles" and "AddHandler."

The "Handles" clause is intended to be used for a "pre-defined" control, for example, something you put on the form in the designer.

AddHandler is a dynamic way of doing the same thing. You generally use AddHandler for something you create at run-time. You only use one or the other. For example, these two code listings do the same thing:
Code:
Class MyForm
    Inherits Form

    [COLOR="Blue"]WithEvents[/COLOR] b As Button [COLOR="Green"]' Normally, the designer automatically writes this bit for you[/COLOR]

    Public Sub New()
        [COLOR="Green"]' Normally the designer creates the button for you[/COLOR]
        b = New Button
        Controls.Add(b)
    End Sub

    Sub Button_Click(sender As Object, e As EventArgs) [COLOR="Blue"]Handles b.Click[/COLOR]
        MessageBox.Show("Wee-ow!")
    End Sub
End class
Code:
Class MyForm
    Inherits Form

    Dim b As Button [COLOR="Green"]' Normally, the designer automatically writes this bit for you[/COLOR]

    Public Sub New()
        [COLOR="Green"]' Normally the designer creates the button for you[/COLOR]
        b = New Button
        Controls.Add(b)

        [COLOR="Blue"]AddHandler b.Click, AddressOf Button_Click[/COLOR]
    End Sub

    Sub Button_Click(sender As Object, e As EventArgs)
        MessageBox.Show("Wee-ow!")
    End Sub
End class
The parts in blue are the code that connects the event to the handler. Both programs above will have the same exact result. When you use AddHandler, you use it instead of Handles. We don't need the Handles part.

When you create controls at run-time like we are, it's often easier to use AddHandler. If you are storing the controls in an array, like we are, YOU MUST use AddHandler. There is no way to use Handles with an array of controls.

Hopefully, that should solve problem #3.


Now, as for storing both the X and the Y values in the .Tag property, well, I was trying to get you to figure this one out yourself. I can't keep giving you almost complete code. It's against this forum's policy and for a good reason. It's not the right way to learn. Programming is as much about learning how to figure things out as it is about knowing how to do things.

My recommendation was to create a Point and store that in the tag. You see, you can only put one object in the .Tag, but certain kinds of objects can hold multiple pieces of data. A Point object holds an X and a Y, which is exactly what we need.

If we wanted a Point with an X of 25 and a Y of 10, we would create it like this:
Code:
Dim p As Point = New Point(25, 10)
[COLOR="Green"]' or[/COLOR]
Dim p As New Point(25, 10)
Easy, no? Now you have p.X and p.Y, both stored in p, which is a Point. Here is an example of how you could use a Point:
Code:
Dim p As New Point(100, 100)
Me.Location = p [COLOR="Green"]' Set a form's location to X:100, Y:100[/COLOR]
That is not what your code should do. That's an example of how we can put an X and a Y into a single object and treat it as a whole. You can also get the X and the Y back out again.
Code:
Dim p As New Point(75, 33)
[COLOR="Green"]' Later that day[/COLOR]
Dim someX as Integer = p.X
Dim someY an Integer = p.Y
MessageBox.Show("X = " & CStr(someX))
MessageBox.Show("Y = "  & CStr(someY))
So... We have an X and a Y we want to store in a Tag. But a Tag can only hold one object. But we do have a way of putting an X and a Y into one object, so what do we do? Hopefully, that solves #1.

It looks like you may have already solved #2. Just to be sure, I'm going to highlight what you got right:
Code:
 Private Sub mineGrid_Click(ByVal sender As Object, ByVal e As [COLOR="Blue"]System.EventArgs[/COLOR])
 
Yeah, I realized that you were giving me tons of code if not too much and now I'm feeling guilty for having someone else do my work, so if you feel like your basically doing my program for me feel free to not reply anymore I know what else I have to do I'm just having trouble putting it into code.

But I worked on it myself a little bit and went in for help with my teacher and we came up with this in order to get the mines randomly scattered about. I think I got the .tag part working as well. I made 3 labels and was able to put the x coordinate and y coordinate in and it showed up as lets say 1,2 or 7,6, so I assume it's working properly. I don't think I'm going to make it super-great just a very simple version of minesweeper. But that's okay with me. Anyway..
Code:
Dim i As Integer
        Dim r1, r2 As Integer

        Dim NumberOfMines As Integer = 10

        Randomize()

        For r1 = 0 To 8
            For r2 = 0 To 8
                mineGrid(r1, r2) = 0
            Next
        Next


        For i = 1 To NumberOfMines
            r1 = CInt(Rnd() * 9)
            r2 = CInt(Rnd() * 9)
            If (mineGrid(r1, r2) <> 9) Then
                mineGrid(r1, r2) = 9
            Else
                i = i - 1
            End If
        Next i

But what I have for a question is how do you make it so lets say the bomb we have a situation like this for bombs. = 0 empty space, B = bomb, etc
It's not a full game board, but how would I make it so it puts a 1, or a 2, in that space instead of a 0. I know, since I have 9 = a bomb that I need to look to the adjacent squares to check to see if another bomb is close and "I think" this may work, but with the way I'm coding, highly unlikely.

1 B 1 0 0 1 1 2 1
1 1 1 0 0 0 0 1 B
0 0 0 0 0 0 0 1 1
0 0 1 2 2 2 1 1 0
0 0 1 B B 2 B 1 0

Code:
    or findNeightbors = 0 To 8
                Select Case FindNeighbors
                    Case 1
                        DeltaX = x1
                        DeltaY = y1 + 1
                    Case 2
                        DeltaX = x1 + 1
                        DeltaY = y1 + 1
                    Case 3
                        DeltaX = x1 + 1
                        DeltaY = y1
                    Case 4
                        DeltaX = x1 + 1
                        DeltaY = y1 - 1
                    Case 5
                        DeltaX = x1
                        DeltaY = y1 - 1
                    Case 6
                        DeltaX = x1 - 1
                        DeltaY = y1 - 1
                    Case 7
                        DeltaX = x1 - 1
                        DeltaY = y1
                    Case 8
                        DeltaX = x1 - 1
                        DeltaY = y1 + 1
                End Select
                If mineGrid(DeltaX, DeltaY) = 9 And DeltaX > 0 And DeltaY > 0 Then
                    bombCount += 1
                End If
            Next findNeightbors
Changed it from if to a select case, would this work better or did I mess something up somewhere?

EDIT: that code does work for a few pieces but it starts to get jumbled up when 1 or more bombs are touching. Time to try something else! And my roomie helped me get this on paper I know that from the space clicked I need to go up/down/left/right/ and all the diags and I wrote down all the coordinates for them but now the code part is where I'm struggling. But I'll try to get something posted up in an hour or so if I can. Thanks Damp for putting it in a simple simple manner =p.
 
Last edited:
To get the bomb count you would just need to check the 8 positions around the current location.

e.g. if you needed the count for location (3, 4) you would need to check the surrounding eight

(2,3) (2,4) (2,5)
(3,3) (3,4) (3,5)
(4,3) (4,4) (4,5)
 
Hey again, I was wondering, would it be possible if I got some help finishing the program? Don't worry, school is out now, I would just like to have a finished program to show people and maybe even play =)
 
Sweet! Sorry it took so long to reply I've been busy with packing and what not. But I'm struggling to get the code for checking bombs and placing numbers around them. My friend and I worked on it for a good hour or two on a bus ride and we couldn't come up with the logic.

I used to have the code, not sure where it went now, but it was 8 If statements and it was doing something to the coordinates. To be honest I didn't come up with the code, I just tried to alter it to my program and failed. Like I said, I can't find it, I probably saved it to a notepad file and accidentally deleted it. I'll keep looking for it, I hope I have it saved else ware.
 
How are you doing this? Are you calculating the numbers as they are shown? Or are you calculating them all before hand? And have you already gotten the bomb placement sorted out?
 
Back
Top