public CampoMinato(int row, int col, int numero_mine) { PosizioniAdiacenti = new int[8, 2] { { +0, +1 }, { -1, +1 }, { -1, +0 }, { -1, -1 }, { +0, -1 }, { +1, -1 }, { +1, +0 }, { +1, +1 } }; ListBtnCelle = new List <Cella>(); this.row = row; this.col = col; this.numero_mine = numero_mine; this.flags = numero_mine; PosizionaMine(); int locY = 60; int locX = 0; int btnCellaSize = 40; Cella btnCella; for (int i = 0; i < row; i++) { locY += btnCellaSize; locX = 0; for (int j = 0; j < col; j++) { locX += btnCellaSize; btnCella = new Cella(i + ":" + j, CampoConMine[i, j].ToString(), btnCellaSize, btnCellaSize, Color.Gray, Color.Gray); btnCella.Location = new Point(locX, locY); ListBtnCelle.Add(btnCella); } } }
public Cella LeggiCella(int index) { Cella cella = ListBtnCelle[index]; return(cella); }
public void btnCella_Click(object sender, EventArgs e) { Cella CurrentButton = sender as Cella; if (!((e is EventArgs) && !(e is KeyEventArgs) && !(e is MouseEventArgs))) { numberOfMoves++; } if (CurrentButton.Image != null) { lblFlags.Text = "Bandiere: " + ++CampoMinato.flags; CurrentButton.Image = null; CurrentButton.Font = new Font(Font.FontFamily, 8); } CurrentButton.Click -= new EventHandler(btnCella_Click); CurrentButton.MouseUp -= btnCella_MouseUp; CurrentButton.ForeColor = Color.Black; CurrentButton.BackColor = Color.LightGray; int minesFound = 0; switch (CurrentButton.Text) { case "0": CurrentButton.ForeColor = Color.LightGray; NumeroCelleLiberate++; string[] numeri = numeri = new string[2]; numeri = CurrentButton.Name.Split(':'); int RowCurrent = Int32.Parse(numeri[0]); int ColCurrent = Int32.Parse(numeri[1]); int NextRowCurrent; int NextColCurrent; int NextRowButton; int NextColButton; string[] numeriNext; foreach (Cella NextButton in CampoMinato.ListBtnCelle) { numeriNext = new string[2]; for (int m = 0; m < 8; m++) { NextRowCurrent = RowCurrent + CampoMinato.PosizioniAdiacenti[m, 0]; NextColCurrent = ColCurrent + CampoMinato.PosizioniAdiacenti[m, 1]; numeriNext = NextButton.Name.Split(':'); NextRowButton = Int32.Parse(numeriNext[0]); NextColButton = Int32.Parse(numeriNext[1]); if (NextRowCurrent == NextRowButton && NextColCurrent == NextColButton) { NextButton.PerformClick(); } } } break; case "99": foreach (Cella button in CampoMinato.ListBtnCelle) { if (button.Text == "99") { button.Click -= new EventHandler(btnCella_Click); button.MouseUp -= btnCella_MouseUp; button.Text = ""; if (button.Image == null) { button.Image = Image.FromFile("../mina.gif"); button.BackColor = Color.Red; } else { button.Image = Image.FromFile("../mina_scovata.gif"); button.BackColor = Color.Transparent; minesFound++; } button.ForeColor = Color.Black; } if (button.BackColor == Color.Gray) { button.Text = ""; button.Enabled = false; } } timer.Stop(); MyLibrary.WriteJson(difficulty, seconds, minesFound, CampoMinato.numero_mine, numberOfMoves, false); btnTornaMenu.Click -= new EventHandler(this.Form1_Load_Message_Box); btnTornaMenu.Click += new EventHandler(this.Form1_Load); Label lblSconfittaGiocatore = new Label(); lblSconfittaGiocatore.Location = new Point(36, 0); lblSconfittaGiocatore.Font = new Font(Font.FontFamily, 14); lblSconfittaGiocatore.ForeColor = Color.White; lblSconfittaGiocatore.AutoSize = true; lblSconfittaGiocatore.Text = "Hai Perso! Mina trovata"; this.Controls.Add(lblSconfittaGiocatore); break; default: NumeroCelleLiberate++; break; } if (MyLibrary.VerificaVittoria(CampoMinato.row, CampoMinato.col, CampoMinato.numero_mine, NumeroCelleLiberate)) { timer.Stop(); MyLibrary.WriteJson(difficulty, seconds, minesFound, CampoMinato.numero_mine, numberOfMoves, true); btnTornaMenu.Click -= new EventHandler(this.Form1_Load_Message_Box); btnTornaMenu.Click += new EventHandler(this.Form1_Load); Label lblVittoriaGiocatore = new Label(); lblVittoriaGiocatore.Location = new Point(36, 0); lblVittoriaGiocatore.Font = new Font(Font.FontFamily, 14); lblVittoriaGiocatore.ForeColor = Color.White; lblVittoriaGiocatore.AutoSize = true; lblVittoriaGiocatore.Text = "Hai Vinto! Tutte le celle liberate!"; foreach (Cella button in CampoMinato.ListBtnCelle) { if (button.Text == "99") { button.Click -= new EventHandler(btnCella_Click); button.MouseUp -= btnCella_MouseUp; button.Text = ""; button.Image = Image.FromFile("../mina_bianca.gif"); button.ForeColor = Color.Black; button.BackColor = Color.White; } if (button.BackColor == Color.Gray) { button.Text = ""; button.Enabled = false; } } this.Controls.Add(lblVittoriaGiocatore); } }