private void RetryButton_Click(object sender, EventArgs e) { RetryButton.Enabled = false; RetryButton.Visible = false; GameInfo.GameStarted = false; Hide(); StartupMenu startupMenu = new StartupMenu(this); startupMenu.FormClosing += new FormClosingEventHandler(StartupMenu_Closing); startupMenu.ShowDialog(); }
public Minesweeper() { InitializeComponent(); StartupMenu startupMenu = new StartupMenu(this); startupMenu.FormClosing += new FormClosingEventHandler(StartupMenu_Closing); if (GameInfo.FirstStarted) { startupMenu.ShowDialog();//show startup menu GameInfo.FirstStarted = false; } while (!GameInfo.GameStarted) { ; //wait until start button is pressed } //<draw mine field> int mineCellWidth = 25; int mineCellLength = 25; int mineCellDistance = 5; Random rnd = new Random(); for (int x = 0; x < GameInfo.BoardX; x++)//generate mine cells { for (int y = 0; y < GameInfo.BoardY; y++) { MineCell currentDrawing = new MineCell() { Size = new Size(mineCellWidth, mineCellLength), Location = new Point(x * mineCellWidth + mineCellDistance, y * mineCellLength + mineCellDistance), Name = Convert.ToString(x, 10) + "," + Convert.ToString(y, 10), BackColor = SystemColors.ControlLight }; MineField.Controls.Add(currentDrawing); currentDrawing.MouseDown += new MouseEventHandler(MineCell_MouseDown); } } for (; ;) //generate mines { int randomXCoord = rnd.Next(0, GameInfo.BoardX); int randomYCoord = rnd.Next(0, GameInfo.BoardY); string SelectedMineCellName = Convert.ToString(randomXCoord) + "," + Convert.ToString(randomYCoord);//the name of the mine cell selected MineCell selectedMineCell = (MineCell)MineField.Controls.Find(SelectedMineCellName, true)[0]; if (selectedMineCell.HasMine == false) { selectedMineCell.HasMine = true; GameInfo.CurrentMineCount++; } else//if the selected mine cell already has mine in it { continue; } if (GameInfo.CurrentMineCount == GameInfo.TotalMineCount)//when generation is complete { break; } } for (int x = 0; x < GameInfo.BoardX; x++)//calculate `ProxMineCount` { for (int y = 0; y < GameInfo.BoardY; y++) { string selectedMineCellName = Convert.ToString(x) + "," + Convert.ToString(y); //the name of the mine cell selected string proxCellName; //the name of the proximity mine cell int[,] proxCoords = { { x + 1, y - 1 },//all 8 adjacent coordinates of `selectedCell` { x + 1, y }, { x + 1, y + 1 }, { x - 1, y - 1 }, { x - 1, y }, { x - 1, y + 1 }, { x, y - 1 }, { x, y + 1 } }; MineCell selectedCell = (MineCell)MineField.Controls.Find(selectedMineCellName, true)[0]; MineCell proxCell; for (int i = 0; i < 8; i++)//cycle through all 8 adjacent cells { proxCellName = Convert.ToString(proxCoords[i, 0]) + "," + Convert.ToString(proxCoords[i, 1]); proxCell = (MineCell)MineField.Controls.Find(proxCellName, true).FirstOrDefault(); if (proxCell != null && proxCell.HasMine) { selectedCell.ProxMineCount++; } } } } //</draw mine field> }
public Minesweeper(StartupMenu startupMenuInstance) { StartupMenuInstance = startupMenuInstance; }