public MineField(int rows, int columns, int mineCount) { if (rows < 1) { throw new ArgumentOutOfRangeException("rows"); } if (columns < 1) { throw new ArgumentOutOfRangeException("columns"); } if (mineCount < 1 || mineCount >= rows * columns) { throw new ArgumentOutOfRangeException("mineCount"); } _rows = rows; _columns = columns; _mineCount = mineCount; _clearedCount = 0; _squareCount = _rows * _columns; _state = WinState.Unknown; bool[] live = new bool[_squareCount]; Random rnd = Util.Rnd; int ith; for (int i = 0; i < _mineCount; i++) { ith = rnd.Next(_squareCount - i); for (int j = 0; j < _squareCount; j++) { if (live[j] == false) { if (ith == 0) { live[j] = true; break; } ith--; } } } _squares = new List<Square>(); int index; Square square; for (int y = 0; y < _rows; y++) { for (int x = 0; x < _columns; x++) { index = y * _columns + x; square = new Square(this, y, x, live[index]); square.PropertyChanged += new PropertyChangedEventHandler(square_PropertyChanged); _squares.Add(square); } } }
public MineField(int rows = DefaultHeight, int columns = DefaultWidth, int mineCount = DefaultMineCount) { Contract.Requires(rows > 0); Contract.Requires(columns > 0); Contract.Requires(mineCount > 0 && mineCount <= rows * columns); _rows = rows; _columns = columns; _mineCount = mineCount; _clearedCount = 0; _squareCount = _rows * _columns; _state = WinState.Unknown; bool[] live = new bool[_squareCount]; Random rnd = Util.Rnd; int ith; for (int i = 0; i < _mineCount; i++) { ith = rnd.Next(_squareCount - i); for (int j = 0; j < _squareCount; j++) { if (live[j] == false) { if (ith == 0) { live[j] = true; break; } ith--; } } } var squares = new List<Square>(); for (int y = 0; y < _rows; y++) { for (int x = 0; x < _columns; x++) { var index = y * _columns + x; var square = new Square(this, y, x, live[index]); square.PropertyChanged += square_PropertyChanged; squares.Add(square); } } _squares = squares.ToReadOnlyCollection(); }