/// <summary> /// Replaces a tile when it is clicked /// </summary> /// <param name="sender">a tile on the gamepanel which is clicked</param> /// <param name="e"></param> private void replaceTile(object sender, MouseEventArgs e) { Tile clickedTile = sender as Tile; Tile repTile = new Tile(); Point repLocation = clickedTile.Location; string repTag = clickedTile.Tag.ToString(); int repRowId = clickedTile.RowId; int repColumnId = clickedTile.ColumnId; if (e.Button == MouseButtons.Right) { if (clickedTile.Name.ToString() == "Uncovered") { gamePanel.Controls.Remove(clickedTile); repTile = new FlagTile(); repTile.Location = repLocation; repTile.Tag = repTag; repTile.RowId = repRowId; repTile.ColumnId = repColumnId; repTile.MouseDown += replaceTile; gamePanel.Controls.Add(repTile); flagCount--; } else if (clickedTile.Name.ToString() == "Flag" && clickedTile.Tag.ToString() != "Bomb") { gamePanel.Controls.Remove(clickedTile); repTile = new UncoveredTile(); repTile.Location = repLocation; repTile.Tag = repTag; repTile.RowId = repRowId; repTile.ColumnId = repColumnId; repTile.MouseDown += replaceTile; gamePanel.Controls.Add(repTile); flagCount++; } else if (clickedTile.Name.ToString() == "Flag" && clickedTile.Tag.ToString() == "Bomb") { gamePanel.Controls.Remove(clickedTile); repTile = new BombTile(); repTile.Location = repLocation; repTile.Tag = repTag; repTile.RowId = repRowId; repTile.ColumnId = repColumnId; repTile.MouseDown += replaceTile; gamePanel.Controls.Add(repTile); flagCount++; } flagLabel.Text = flagCount.ToString(); } else if (e.Button == MouseButtons.Left) { if (clickedTile.Tag.ToString() != "Bomb" && clickedTile.Name != "Flag") { checkAdjacent(clickedTile); } else if (clickedTile.Tag.ToString() == "Bomb" && clickedTile.Name != "Flag") { gamePanel.Controls.Remove(clickedTile); repTile = new BombTile(); repTile.Location = repLocation; repTile.Tag = repTag; repTile.RowId = repRowId; repTile.ColumnId = repColumnId; repTile.BackgroundImage = Properties.Resources.tileBombHit; repTile.MouseDown += replaceTile; gamePanel.Controls.Add(repTile); gameOver = true; foreach (Control x in gamePanel.Controls) { if (x is Tile) { ((Tile)x).Disable(); } } string lostText = "You Lost!" + "\r" + "Score = " + second / gameCount; MessageBox.Show("You Lost!", "Game Over"); } checkWin(); } }
/// <summary> /// Creates the tiles and adds them to a list /// </summary> private void createTiles() { int bombCount = 0; int tileCount = 0; int rowLenght = 0; List <Tile> unMixedBombList = new List <Tile>(); switch (difficulty) { case 0: bombCount = 10; tileCount = 81; rowLenght = 9; tilesToWin = tileCount - bombCount; this.MaximumSize = new Size(216, 303); this.MinimumSize = new Size(216, 303); break; case 1: bombCount = 40; tileCount = 256; rowLenght = 16; tilesToWin = tileCount - bombCount; this.MaximumSize = new Size(356, 443); this.MinimumSize = new Size(356, 443); break; case 2: bombCount = 99; tileCount = 480; rowLenght = 30; tilesToWin = tileCount - bombCount; this.MaximumSize = new Size(636, 443); this.MinimumSize = new Size(636, 443); break; } flagCount = bombCount; flagLabel.Text = bombCount.ToString(); for (int i = 1; i <= tileCount; i++) { if (i <= bombCount) { BombTile tileAdd = new BombTile(); unMixedBombList.Add(tileAdd); tileAdd.MouseDown += replaceTile; } else { UncoveredTile tileAdd = new UncoveredTile(); unMixedBombList.Add(tileAdd); tileAdd.MouseDown += replaceTile; } } List <Tile> mixedList = mixTiles(unMixedBombList, rowLenght); for (int i = 0; i < mixedList.Count; i++) { gamePanel.Controls.Add(mixedList[i]); } }