Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top