public MinesweeperGame(GameBoard board, int x, int y, string playerInitials, string difficulty) { buttons = new clickableButton[x, y]; playerName = playerInitials; level = difficulty; //Populate form with buttons. for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { buttons[i, j] = new clickableButton(21 * i, 21 * j); buttons[i, j].MouseDown += new MouseEventHandler(buttonClick); buttons[i, j].Tag = i + ", " + j; board.Controls.Add(buttons[i, j]); } } activateCells(buttons); setNeighbors(buttons); //Set timer interval, add Tick event, start timer gameTimer.Interval = 1000; gameTimer.Tick += new EventHandler(gameTimer_Tick); gameTimer.Start(); //Change where Timer label displays on form based on size of grid. if (x == 10) { displayTime.Location = new Point(13, 218); } else if (x == 15) { displayTime.Location = new Point(13, 323); } else if (x == 20) { displayTime.Location = new Point(13, 428); } displayTime.AutoSize = true; displayTime.Text = "Time Elapsed: "; board.Controls.Add(displayTime); }
//Method to change the text of a button to match # of LIVE neighbors. public void liveNeighbors(clickableButton button) { switch (button.LiveNeighbors) { case 0: changeButton(button); break; case 1: button.Text = "1"; button.ForeColor = Color.Blue; break; case 2: button.Text = "2"; button.ForeColor = Color.Green; break; case 3: button.Text = "3"; button.ForeColor = Color.Red; break; case 4: button.Text = "4"; button.ForeColor = Color.DarkBlue; break; case 5: button.Text = "5"; button.ForeColor = Color.DarkGreen; break; case 6: button.Text = "6"; button.ForeColor = Color.DarkRed; break; case 7: button.Text = "7"; button.ForeColor = Color.Yellow; break; case 8: button.Text = "8"; button.ForeColor = Color.Orange; break; } }
public GameBoard(int x, int y) { InitializeComponent(); //Generate board size based on size passed in from StartScreen. if (x == 10) { this.Width = 225; this.Height = 248; } else if (x == 15) { this.Width = 330; this.Height = 353; } else if (x == 20) { this.Width = 435; this.Height = 458; } //Prevent resizing of form this.FormBorderStyle = FormBorderStyle.FixedSingle; this.MaximizeBox = false; //New array of buttons clickableButton[,] buttons = new clickableButton[x, y]; //Populate form with buttons. for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { buttons[i, j] = new clickableButton(21 * i, 21 * j); this.Controls.Add(buttons[i, j]); } } }
//Method to change appearance of a button. public void changeButton(clickableButton button) { button.CellVisited = true; button.FlatStyle = FlatStyle.Flat; button.BackColor = Color.LightGray; }