HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

C# Minesweeper project

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
projectminesweeperstackoverflow

Problem

I made a very simple Minesweeper using C# and WPF. My method for checking the adjacent mines for buttons is pretty long and could use some improvements, so how would I make it shorter and more "reasonable" so that it would not take so many code lines?

MiniMine.cs

```
namespace boardgame
{
class MiniMine : boardGame

{
//MineBoard gameboard;
private MiinaAlusta gameboard;

//row
private const int row = 6;

// Column
private const int colum = 6;

//ButtonCount = row*row
public const int buttonCount = row * row;

//MineCount = 10
private const int minecount = 10;

//Mine
private const string mine = "*";

//Empty
public const string Empty = "";

//Content
private string[] content = new string[buttonCount];

//MineInfor
private int[] mineinfo = new int[buttonCount];

//Points
private int points;

//Mines string
private List mines = new List(minecount);

//Cells
new protected Button[] cells = new Button[buttonCount];

// Default constructor
public MiniMine()
{

}

//Cells are herited from BoardGame class
public MiniMine(UserControl MineBoard)
{
cells = new Button[buttonCount];
gameboard.mineBoard.Children.CopyTo(cells, 10);
}

public new void Play(object sender)
{
Button bttn = (Button)sender;
int index = gameboard.MiniMiinaLauta.Children.IndexOf(btn);

// Checks if index is found from Mines list.
if (mines.Contains(index))
{

btn.IsEnabled = false;
((Button)cells[index]).Content = mine;

// IF button containts a mine, alert the user and call the method to show all mines.
MessageBox.Show("Game Over!"); // Its a hit!
C

Solution

You can write the MiinaInfo function like this:

class Minesweeper
{
    HashSet mines = new HashSet();
    int row = 6;
    int column = 6;

    int xy(int x, int y) => y * column + x;

    private int MiinaInfo(int index)
    {
        var x = index % column;
        var y = index / column;

        // Is {x,y} {plus,minus} 1 ok?
        bool xm1 = x - 1 >= 0, xp1 = x + 1 = 0, yp1 = y + 1 < row;
        bool ___ = true;

        return 0
            + (xm1 && ym1 && mines.Contains(xy(x - 1, y - 1)) ? 1 : 0)
            + (___ && ym1 && mines.Contains(xy(x + 0, y - 1)) ? 1 : 0)
            + (xp1 && ym1 && mines.Contains(xy(x + 1, y - 1)) ? 1 : 0)
            + (xm1 && ___ && mines.Contains(xy(x - 1, y + 0)) ? 1 : 0)
            + 0 // Center does not count.
            + (xp1 && ___ && mines.Contains(xy(x + 1, y + 0)) ? 1 : 0)
            + (xm1 && yp1 && mines.Contains(xy(x - 1, y + 1)) ? 1 : 0)
            + (___ && yp1 && mines.Contains(xy(x + 0, y + 1)) ? 1 : 0)
            + (xp1 && yp1 && mines.Contains(xy(x + 1, y + 1)) ? 1 : 0)
            + 0;
    }
}


It may be harder to read at first sight, but it actually is quite simple.

First, the index is converted into its x and y parts. Since the code does not use fixed numbers like 35, it easily applies to rectangular fields of any size.

I chose the very short variable names xm1, xp1, ym1, yp1 because they should have the same length, and minus and plus don't have that.

When you read the code in columns, it always follows the pattern (-1, 0, +1), or (xm1, ___, xp1). Therefore it should be easy to verify it for typos. Each of the 8 neighbors has one line of check, starting in the upper left, in typical European reading direction.

The xy function converts a pair of coordinates back into an index, as required by the mines field.

Code Snippets

class Minesweeper
{
    HashSet<int> mines = new HashSet<int>();
    int row = 6;
    int column = 6;

    int xy(int x, int y) => y * column + x;

    private int MiinaInfo(int index)
    {
        var x = index % column;
        var y = index / column;

        // Is {x,y} {plus,minus} 1 ok?
        bool xm1 = x - 1 >= 0, xp1 = x + 1 < column;
        bool ym1 = y - 1 >= 0, yp1 = y + 1 < row;
        bool ___ = true;

        return 0
            + (xm1 && ym1 && mines.Contains(xy(x - 1, y - 1)) ? 1 : 0)
            + (___ && ym1 && mines.Contains(xy(x + 0, y - 1)) ? 1 : 0)
            + (xp1 && ym1 && mines.Contains(xy(x + 1, y - 1)) ? 1 : 0)
            + (xm1 && ___ && mines.Contains(xy(x - 1, y + 0)) ? 1 : 0)
            + 0 // Center does not count.
            + (xp1 && ___ && mines.Contains(xy(x + 1, y + 0)) ? 1 : 0)
            + (xm1 && yp1 && mines.Contains(xy(x - 1, y + 1)) ? 1 : 0)
            + (___ && yp1 && mines.Contains(xy(x + 0, y + 1)) ? 1 : 0)
            + (xp1 && yp1 && mines.Contains(xy(x + 1, y + 1)) ? 1 : 0)
            + 0;
    }
}

Context

StackExchange Code Review Q#144174, answer score: 6

Revisions (0)

No revisions yet.