tags:

views:

160

answers:

2

I've been busy working on a program that can solve Minesweeper puzzles (for no other reason than I think it's fun). When it comes down to the UI, though, I greatly dislike the idea of instantiating over a hundred of the same control, one per cell. Should I create a custom control that handles all of its drawing and input itself? What approach do you guys suggest?

I'm using WPF, and I'm pretty new. Any pointers would be awesome.

A: 

I think you should create a single, owner-drawn control. WPF is cool, but a WPF app still has the same limitations regarding the total number of controls on a form, so having a separate control for each cell in Minesweeper would still be problematic.

Minesweeper is pretty played out, though, as much as I love it myself. Maybe you could have more fun with it by making the cells hexagonal instead of rectangular, and arranging the mines so that they spell out dirty words or something.

MusiGenesis
I do things like this to learn new things. Previously I wrote a program to solve sudoku puzzles without using brute-force. The goal here is to learn the creation of controls, and to have some fun writing the heuristics needed to solve minesweeper without using brute-force.
yodaj007
+2  A: 

Yes a custom control would be a good idea. Also, M-V-VM is a must in a situation like this; it will reduce the complexity of your app greatly.

I'd take a UniformGrid and use Buttons as the squares. You'd have to create a tri-state custom button if you want to add in the "?" intermediate state.

The model for the button would look like

public class MineSquare : INotifyPropertyChanged
{
  // exploded, number, or nothing
  pubic ImageSource ButtonImage {get;private set;}
  // true, then goes to false when clicked
  public bool CanClick {get; private set;}
  // bound to the Command of the button
  public ICommand Click {get; private set;}
}

You deal with the model in code rather than the controls. Drop in nine MineSquares into an ObservableCollection on your ViewModel bound to your UniformGrid and you have a 3x3 minesweeper game. Each button handles its own logic. You can hook into the models via the view model to iterate over all squares and determine if everybody has been clicked.

Will
Minesweeper-View-ViewMinesweeper?
MusiGenesis
Snark.
Will
Hey, at least I didn't say that "is a must" could be turned into "is serious overkill for *Minesweeper*, for Pete's sake". :P
MusiGenesis
Heh... I think MVC is a must in any UI situation. The coolaid, its gone...
Will