public Form1() { InitializeComponent(); // konstanta určující maximální počet polí v řádku/sloupci const int MAX = 50; try { // zjištění velikosti minového pole String hrac = Interaction.InputBox("Zadejte jméno/přezdívku: ", "Hledání min - hráč", ""); // zjištění velikosti minového pole String[] size = Interaction.InputBox("Velikost hrací plochy: (Max. " + MAX + " x " + MAX + ")", "Hledání min - hrací plocha", "10x10").Split('x'); int x_length = int.Parse(size[0]) < MAX?int.Parse(size[0]) : MAX; int y_length = int.Parse(size[1]) < MAX?int.Parse(size[1]) : MAX; // zjištění počtu min int count_of_mines = int.Parse(Interaction.InputBox("Počet min: (menší než " + (x_length * y_length) + ")", "Hledání min - počet min", "5")); if (count_of_mines > (x_length * y_length)) { count_of_mines = x_length * y_length; } //zobrazeni aktualnich statistik statistics stat = new statistics(); string statistika = stat.getStatistika(count_of_mines); Interaction.MsgBox("Statistika nejlepších hráčů pro tuto úroveň:\n\n 1.\tONdra\t34 vteřin\n ě.\tLucka\t51 vteřin\n 1.\tGneck\t63 vteřin"); // vytvoření instance s patřičnými parametry Mines mines = new Mines(x_length, y_length, count_of_mines, this); this.Width = mines._xLength * 50; this.Height = mines._yLength * 50 + 10; mines.createField(); } catch { Environment.Exit(-1); } this.timer1.Enabled = true; }
// zobrazí celé minové pole (při prohře či výhře) public void showField() { for (int y = 0; y < _yLength; y++) { for (int x = 0; x < _xLength; x++) { if (_minesArray[x, y]) { _buttons[x, y].Text = "M"; _buttons[x, y].BackColor = Color.Red; } else { _buttons[x, y].Text = checkMinesAround(x, y).ToString(); _buttons[x, y].BackColor = Mines.getColor(checkMinesAround(x, y)); } _buttons[x, y].Enabled = false; _buttons[x, y].MouseDown -= new MouseEventHandler(Mines_MouseDown); } } }
// projede všechny tlačítka od stisknutého, které mají v okolí nulový počet min a zobrazí je public void showFree(int x, int y) { for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { // ošetření přetečení if (((x + i < 0) || (x + i >= _xLength)) || ((y + j < 0) || (y + j >= _yLength))) { continue; } if ((this.checkMinesAround(x + i, y + j) == 0) && (_buttons[x + i, y + j].Enabled == true) && (_buttons[x + i, y + j].Text != "?")) { _buttons[x, y].Text = "0"; _buttons[x, y].BackColor = Mines.getColor(0); _buttons[x, y].Enabled = false; this.showFree(x + i, y + j); } } } }
public static void Main() { Mines.PlayGame(); }
// událost stisku tlačítka myši private void Mines_MouseDown(object sender, MouseEventArgs e) { // přetypování objektu který vyvolal tento event (tlačítko) na MineButton (přístup k souřadnícím) MineButton minebutton = (MineButton)sender; // levé tlačítko - lze vyvolat pokud je tlačítko aktivní a není označeno uživatelem za minové if ((e.Button == MouseButtons.Left) && (minebutton.Text != "?")) { minebutton.Enabled = false; // kontrola miny if (_minesArray[minebutton._x, minebutton._y]) { this.showField(); _form.timer1.Enabled = false; if (MessageBox.Show("Prohrál jsi, na živu si vydržel po dobu: " + _form.time + "s!\nPak tě mina roztrhala na kusy.\n\nChceš to zkusit znovu?", "Prohra", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { Application.Restart(); } else { Application.Exit(); } } else { minebutton.BackColor = Mines.getColor(this.checkMinesAround(minebutton._x, minebutton._y)); minebutton.Text = this.checkMinesAround(minebutton._x, minebutton._y).ToString(); // pokud má v okolí nulový počet min najde v jeho okolí další s touto vlastností if (this.checkMinesAround(minebutton._x, minebutton._y) == 0) { this.showFree(minebutton._x, minebutton._y); } // zkontroluje počet aktivních tlačítek a vyhodnotí vítězství if (this.checkVictory()) { this.showField(); _form.timer1.Enabled = false; if (MessageBox.Show("Vyhrál jsi v čase: " + _form.time + "s!\nMáš schopnosti superhrdiny.\n\nHrát znovu?", "Výhra", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { Application.Restart(); } else { Application.Exit(); } } } } else if (e.Button == MouseButtons.Right)// pravé tlačítko -> označení tlačítka uživatelem za minové { if (minebutton.Text == "?") { minebutton.Text = ""; } else { minebutton.Text = "?"; } } }