//Creates a new game public void newGame() { INPROGRESS = true; WIN = false; LOSE = false; Tile[][] tempBoard; using (Form2 form2 = new Form2()) { DialogResult d = form2.ShowDialog(); if (d == DialogResult.OK) { percentMines = (double)(form2.percentMines)/100.0; } } tempBoard = new Tile[size][]; //Initializing array for (int i = 0; i < size; i++) { tempBoard[i] = new Tile[size]; for(int j = 0; j < size; j++) { tempBoard[i][j] = new Tile(); } } board = tempBoard; placeMines(); fillInMinecount(); System.Console.WriteLine(ToString()); }
public MineSweeperGame(int columns, int rows, int mines, bool bDoQuestioned, Func<MineSweeperGame,bool,bool> adrawer) { this.columns = columns; this.rows = rows; this.bDoQuestioned = bDoQuestioned; drawer = adrawer; tiles = new Tile[columns, rows]; for (int col = 0; col < columns; col++) { for (int row = 0; row < rows; row++) { tiles [col, row] = new Tile (col, row); } } // Place the mines int minesplaced = 0; do { Point p = RandomTile (); int col = p.X; int row = p.Y; if (!tiles [col, row].isMine()) { tiles [col, row].PlaceMine(); minesplaced++; } } while (minesplaced < 10); // Calculate numbers for each tile for (int col = 0; col < columns; col++) { for (int row = 0; row < rows; row++) { Tile tile = tiles [col, row]; if (!tile.isMine()) { tile.Value = CalculateTileValue (tile); } } } // Reset score currentScore = 0; drawer (this, false); }
public void UpdateSurrounding(Tile tile, List<int> updateTiles) { if (tile.Mines == 0) { for (int x = tile.X - 1; x <= tile.X + 1 && x <_row ; x++) { for (int y = tile.Y - 1; y <= tile.Y + 1 && y < _col; y++) { if (x >= 0 && y >= 0 && _tileArray[y + x * _col] != tile) { if (_tileArray[y + x * _col].Mines == 0 && !_tileArray[y + x * _col].State) { _tileArray[y + x * _col].State = true; UpdateSurrounding(_tileArray[y + x * _col], updateTiles); } _tileArray[y + x * _col].State = true; updateTiles.Add (y + x * _col); } } } } }
public List<int> Click(Tile tile) { List <int> updateTile = new List<int>(); if (tile.State != true) { tile.State = true; tile.IsPlayerRed = IsPlayerRed; UpdateSurrounding(tile, updateTile); if (tile.IsMine) { if (IsPlayerRed) { PlayerRed++; } else { PlayerBlue++; } SystemSound.FromFile ("Mine.wav").PlaySystemSound (); MineCount--; } else { SystemSound.FromFile ("Number.wav").PlaySystemSound (); IsPlayerRed = !IsPlayerRed; } } return updateTile; }
public void Start() { Panel.Enabled = true; Panel.Controls.Clear(); // Create Tiles Tiles = new Tile[RowSize, ColSize]; for (int x = 0; x < RowSize; x++) { for (int y = 0; y < ColSize; y++) { Tile s = new Tile(this, x, y); Tiles[x, y] = s; } } }
private Tile GetNeighbor(Tile target, Neighbors neighbor) { Tile tile = null; int col = target.col; int row = target.row; switch (neighbor) { case Neighbors.UpperLeft: if (col > 0 && row > 0) { tile = tiles [col - 1, row - 1]; } break; case Neighbors.Above: if (row > 0) { tile = tiles [col, row - 1]; } break; case Neighbors.UpperRight: if (col < 7 && row > 0) { tile = tiles [col + 1, row - 1]; } break; case Neighbors.Left: if (col > 0) { tile = tiles [col - 1, row]; } break; case Neighbors.Right: if (col < 7) { tile = tiles [col + 1, row]; } break; case Neighbors.LowerLeft: if (col > 0 && row < 7) { tile = tiles [col - 1, row + 1]; } break; case Neighbors.Below: if (row < 7) { tile = tiles [col, row + 1]; } break; case Neighbors.LowerRight: if (col < 7 && row < 7) { tile = tiles [col + 1, row + 1]; } break; } return tile; }
private void DoUncover(Tile tile) { if (tile.isMine()) { return; } // Score 1 for this tile if it has a value of 0 if (tile.Value == 0) { currentScore++; } // upper left DoNeighbor(tile, Neighbors.UpperLeft); // above DoNeighbor(tile, Neighbors.Above); // upper right DoNeighbor(tile, Neighbors.UpperRight); // left DoNeighbor(tile, Neighbors.Left); // right DoNeighbor(tile, Neighbors.Right); // lower left DoNeighbor(tile, Neighbors.LowerLeft); // below DoNeighbor(tile, Neighbors.Below); // lower right DoNeighbor(tile, Neighbors.LowerRight); }
private void DoNeighbor(Tile tile, Neighbors neighbor) { Tile target = GetNeighbor(tile, neighbor); if (target != null) { if (target.isCovered () && !target.isMine ()) { target.Uncover (); if (target.Value == 0) { DoUncover (target); } } } }
private int CalculateTileValue(Tile tile) { int value = 0; Tile target = null; // upper left target = GetNeighbor(tile, Neighbors.UpperLeft); if (target != null) { if (target.isMine()) { value++; } } // above target = GetNeighbor(tile, Neighbors.Above); if (target != null) { if (target.isMine()) { value++; } } // upper right target = GetNeighbor(tile, Neighbors.UpperRight); if (target != null) { if (target.isMine()) { value++; } } // left target = GetNeighbor(tile, Neighbors.Left); if (target != null) { if (target.isMine()) { value++; } } // right target = GetNeighbor(tile, Neighbors.Right); if (target != null) { if (target.isMine()) { value++; } } // lower left target = GetNeighbor(tile, Neighbors.LowerLeft); if (target != null) { if (target.isMine()) { value++; } } // below target = GetNeighbor(tile, Neighbors.Below); if (target != null) { if (target.isMine()) { value++; } } // lower right target = GetNeighbor(tile, Neighbors.LowerRight); if (target != null) { if (target.isMine()) { value++; } } return value; }
private void RightMouseClick(Tile clicked) { flagCounter.AddRemoveFlag(clicked.MarkFlagOrQuestion(flagCounter.GetAreFlagsAvailable())); }