/// <summary> /// Ce qui se produit lorqu'on perd la partie /// </summary> /// <param name="btn">Cellule sur laquelle on a cliqué</param> private void Lost(Cel btn) { toolStripMenuItem3.Enabled = false; timer.Enabled = false; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (celsList[i, j].isBomb) { celsList[i, j].ImageIndex = 0; celsList[i, j].BackColor = Color.WhiteSmoke; celsList[i, j].FlatAppearance.MouseOverBackColor = Color.WhiteSmoke; celsList[i, j].FlatAppearance.MouseDownBackColor = Color.WhiteSmoke; } else { celsList[i, j].DisplayNotBomb(); } celsList[i, j].MouseClick -= _Click; celsList[i, j].MouseDown -= _Click; celsList[i, j].FlatStyle = FlatStyle.Flat; celsList[i, j].BackColor = Color.White; } } btn.ImageIndex = 1; btn.Enabled = true; (new FLost()).ShowDialog(); }
/// <summary> /// Permet d'ajouter une cellule à la liste des cellules voisines /// </summary> /// <param name="value">Cellule à ajouter à la liste</param> public void addNeighbor(Cel value) { if (neighbors == null) { neighbors = new List <Cel>(); } neighbors.Add(value); }
/// <summary> /// Crée la grille de jeu /// </summary> private void CreateTable() { // Création des cellules Cel btn; celsList = new Cel[size, size]; game_panel.Controls.Clear(); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { btn = new Cel(); btn.Text = ""; btn.Size = new Size(celSize, celSize); game_panel.Controls.Add(btn); btn.Location = new Point(i * celSize, j * celSize); btn.ImageList = images; btn.isBomb = false; btn.isClicked = false; btn.FlatStyle = FlatStyle.Flat; btn.BackColor = Color.Silver; btn.FlatAppearance.MouseOverBackColor = Color.Beige; btn.MouseClick += new MouseEventHandler(_Click); btn.MouseDown += new MouseEventHandler(_Click); celsList[i, j] = btn; } } AjustForm(); // Placement des mines Random r = new Random(); int bombPlaced = 0; while (bombPlaced < bombs) { int rand1 = 0, rand2 = 0; rand1 = r.Next(0, size - 1); rand2 = r.Next(0, size - 1); if (!celsList[rand1, rand2].isBomb) { celsList[rand1, rand2].isBomb = true; bombPlaced++; } } // Remplissage des liste de voisinage des cellules for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == 0) { celsList[i, j].addNeighbor(celsList[i + 1, j]); } if (j == 0) { celsList[i, j].addNeighbor(celsList[i, j + 1]); } if (i == size - 1) { celsList[i, j].addNeighbor(celsList[i - 1, j]); } if (j == size - 1) { celsList[i, j].addNeighbor(celsList[i, j - 1]); } if (i == 0 && j == 0) { celsList[i, j].addNeighbor(celsList[i + 1, j + 1]); } else if (i == 0 && j == size - 1) { celsList[i, j].addNeighbor(celsList[i + 1, j - 1]); } else if (i == size - 1 && j == size - 1) { celsList[i, j].addNeighbor(celsList[i - 1, j - 1]); } else if (i == size - 1 && j == 0) { celsList[i, j].addNeighbor(celsList[i - 1, j + 1]); } else if (i == 0 && j > 0) { celsList[i, j].addNeighbor(celsList[i + 1, j - 1]); celsList[i, j].addNeighbor(celsList[i + 1, j + 1]); celsList[i, j].addNeighbor(celsList[i, j + 1]); celsList[i, j].addNeighbor(celsList[i, j - 1]); } else if (i == size - 1 && j > 0) { celsList[i, j].addNeighbor(celsList[i - 1, j - 1]); celsList[i, j].addNeighbor(celsList[i - 1, j + 1]); celsList[i, j].addNeighbor(celsList[i, j + 1]); celsList[i, j].addNeighbor(celsList[i, j - 1]); } else if (j == 0 && i > 0) { celsList[i, j].addNeighbor(celsList[i - 1, j + 1]); celsList[i, j].addNeighbor(celsList[i + 1, j + 1]); celsList[i, j].addNeighbor(celsList[i + 1, j]); celsList[i, j].addNeighbor(celsList[i - 1, j]); } else if (j == size - 1 && i > 0) { celsList[i, j].addNeighbor(celsList[i + 1, j - 1]); celsList[i, j].addNeighbor(celsList[i - 1, j - 1]); celsList[i, j].addNeighbor(celsList[i + 1, j]); celsList[i, j].addNeighbor(celsList[i - 1, j]); } else { celsList[i, j].addNeighbor(celsList[i - 1, j - 1]); celsList[i, j].addNeighbor(celsList[i - 1, j]); celsList[i, j].addNeighbor(celsList[i - 1, j + 1]); celsList[i, j].addNeighbor(celsList[i, j - 1]); celsList[i, j].addNeighbor(celsList[i, j + 1]); celsList[i, j].addNeighbor(celsList[i + 1, j - 1]); celsList[i, j].addNeighbor(celsList[i + 1, j]); celsList[i, j].addNeighbor(celsList[i + 1, j + 1]); } } } // Enregistrement du nombre de mines dans le voisinage for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { celsList[i, j].HowManyBombs(); } } }
/// <summary> /// Ce qui se produit lors d'un clic sur une cellule /// </summary> private void _Click(object sender, MouseEventArgs e) { Cel btn = (Cel)sender; if (e.Button == MouseButtons.Left) { if (!(btn.ImageIndex == 2)) { if (btn.isBomb) { Lost(btn); } else { btn.DisplayNotBomb(); int test = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (celsList[i, j].isClicked && !celsList[i, j].isBomb) { test++; } } } if (test == (size * size) - bombs) { Win(); } } } } else if (e.Button == MouseButtons.Right) { if (btn.ImageIndex == 2) { btn.ImageIndex = -1; btn.Text = ""; tb_flags.Text = (int.Parse(tb_flags.Text) + 1).ToString(); if (btn.isBomb) { goodFlags--; } } else if (btn.Text == "" && !btn.isClicked) { if (tb_flags.Text != "0") { btn.ImageIndex = 2; tb_flags.Text = (int.Parse(tb_flags.Text) - 1).ToString(); if (btn.isBomb) { goodFlags++; } } } if (goodFlags == bombs) { Win(); } } }