void btn_Click(object sender, EventArgs e) { CMineInfo mine = (CMineInfo)((Button)sender).Tag; //Debug.Write(((CMineInfo)((Button)sender).Tag).ToString()); Debug.WriteLine(mine.ToString()); // performing here as to avoid user every selecting mine on first button click if (0 >= mines.Length) { // AddMine(1, 1); // AddMine(1, 4); // AddMine(2, 3); // AddMine(2, 0); AddMine(1, 0); AddMine(0, 1); AddMine(2, 2); AddMine(3, 3); AddMine(8, 8); // use loop create array of CMineInfo // use random number to select row // use random number to select col // confirm mine not already exists otherwise regenerate random indices until valid // add button tag mineinfo object to array use object stored in tag property, objcts are all already created // lastly, loop through mines set Z property to indicate mines present for (int i = 0; i < Mines.Count; i++) { Mines[i].Z = 1; } } ClickButton((Button)sender); }
private void AddMine(int r, int c) { CMineInfo mine = (CMineInfo)btns[r][c].Tag; // only mines have Count < 0 mine.Count = -1; Mines.Add(mine); }
public void ClickButton(Button btn) { // check if button previously removed if (-1 < Controls.IndexOf(btn)) { CMineInfo mine = (CMineInfo)btn.Tag; // count bounding mines mine.Count = mine.GetCount(); // create label to replace button Label l = new Label(); l.Text = mine.Count.ToString(); l.Location = btn.Location; l.Size = btn.Size; l.AutoSize = false; l.TextAlign = ContentAlignment.MiddleCenter; int x = Controls.IndexOf(btn); Controls.RemoveAt(x); Controls.Add(l); Controls.SetChildIndex(l, x); // recursively call for removing buttons // note, this not most efficient method ... per Neil if (0 == mine.Count) { if (mine.N != null) { ClickButton(btns[mine.N.Row][mine.N.Column]); } if (mine.S != null) { ClickButton(btns[mine.S.Row][mine.S.Column]); } if (mine.E != null) { ClickButton(btns[mine.E.Row][mine.E.Column]); } if (mine.W != null) { ClickButton(btns[mine.W.Row][mine.W.Column]); } } } }
public Form1() { InitializeComponent(); Rows = 10; Columns = 10; int w = 25; // width int h = 25; // height // create buttons as array of arrays btns = new Button[Rows][]; for (int i = 0; i < Rows; i++) { btns[i] = new Button[Columns]; for (int j = 0; j < btns[i].Length; j++) { btns[i][j] = new Button(); btns[i][j].Width = w; btns[i][j].Height = h; btns[i][j].Location = new Point(j * h, i * w); btns[i][j].Tag = new CMineInfo(i, j, 0); // { Button = btns[i][j] }; btns[i][j].Click += new EventHandler(btn_Click); } Controls.AddRange(btns[i]); } // set references to adjacent nodes N,S,E,W,NE,NW,SE,SW for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { CMineInfo mine = (CMineInfo)btns[i][j].Tag; if (0 < i) { mine.N = (CMineInfo)btns[i - 1][j].Tag; } if (i < Rows - 1) { mine.S = (CMineInfo)btns[i + 1][j].Tag; } if (0 < j) { mine.W = (CMineInfo)btns[i][j - 1].Tag; if (mine.N != null) { mine.NW = mine.N.W; } if (mine.S != null) { mine.SW = mine.S.W; } } if (j < Columns - 1) { mine.E = (CMineInfo)btns[i][j + 1].Tag; if (mine.N != null) { mine.NE = mine.N.E; } if (mine.S != null) { mine.SE = mine.S.E; } } } } }
public Button GetButton(CMineInfo mine) { return(btns[mine.Row][mine.Column]); }