Hi,
Programming MineSweeper is an interesting exercise. A lot of your code makes perfect sense, but I do have several comments.
1.
You have a Control that holds cells, and want to use each cell as a button. However your cells aren't buttons, they don't even "have" a button, they are loosely associated with a button: in
individualButton()
you create a Button that you don't store in the Cell, you do store it in the grid. But then in
floodfill()
you create (a lot of) buttons that aren't stored at all, they only are used to call their
PerformClick()
method which calls Button_Click() which calls show(). By that time, each grid location has at least two buttons, one of them being shown, others wandering in the background to confuse you. To make matters worse, you are likely to have an avalanche of button clicking, flood filling, more button clicking, etc.
My suggestion here is twofold:
(a) make Cell inherit from Button (so a cell
is a Button)
(b) don't create extra buttons that only serve your calculations and not the user interface, i.e. give the Cell class a public Reveal() method that gets called when a Cell is clicked but also when it needs to be revealed due to the floodfill.
2.
Several minor comments:
(a) you have a
FlowLayoutPanel
, IMO a
TableLayoutPanel
could be more appropriate. Beware, you'd need the MouseDown event, not the (Mouse)Click event, in order to get both right and left mouse clicks then.
(b) your
bombPos()
may not be exactly what you intended as it may place fewer than
bombCount
bombs when the random function happens to clash.
(c) you are a Java programmer, aren't you. There are numeral places where a property would be appropriate, rather than a getter and a setter. And you can declare an object without giving it a dummy value, e.g. in floodfill
Cell neighbour = new Cell();
can be reduced to
Cell neighbour;
(which will be undefined rather than null, howver the compiler will keep a watch on you giving it its value before referencing it).
(d) your floodfill isn't finished: as it is it performs a single sweep over the entire board, and you really need the click-floodfill-click chain (with its avalanche risk) to get it flood to the top and/or to the left.
An easier approach would be to put your floodfill code inside a while loop running until nothing more is revealed.
(e) your code displaying elapsed time is a bit peculiar. The DateTime and TimeSpan types could be a great help here, example:
private void timer1_Tick(object sender, EventArgs e) {
TimeSpan elapsed=DateTime.Now - startTime;
labTime.Text = string.Format("Elapsed time: {0:N1} seconds", elapsed.TotalSeconds);
}
(f) Storing the button's grid coordinates in its name is currently limiting your board size to 10*10, and it is completely unnecessary if you follow my #1 comment.