Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Генерация_кнопок
- {
- public partial class Form1 : Form
- {
- FieldButton[,] TableButton;
- int minesCount, fieldHeight, fieldLength, openedCells, cellsCount;
- bool downDirrection = true;
- public class FieldButton : Button
- {
- public int X, Y, State;
- public bool Flaged { get; set; } = false;
- public FieldButton(int x, int y, int s)
- {
- X = x;
- Y = y;
- State = s;
- }
- }
- public Form1()
- {
- InitializeComponent();
- MaximizeBox = false;
- timer1.Tick += delegate {
- if (downDirrection)
- {
- pictureBox1.Top -= 3;
- if (pictureBox1.Location.Y <= 67)
- {
- downDirrection = false;
- }
- }
- else
- {
- pictureBox1.Top += 3;
- if (pictureBox1.Location.Y >= 82)
- {
- downDirrection = true;
- }
- }
- };
- timer1.Enabled = true;
- }
- private void StartButton_Click(object sender, EventArgs e)
- {
- int Length, Height, count;
- if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" ||
- !Int32.TryParse(textBox1.Text, out Length) ||
- !Int32.TryParse(textBox2.Text, out Height) ||
- !Int32.TryParse(textBox3.Text, out count) ||
- Length < 10 || Length > 20 || Height < 10 || Height > 20 ||
- count < 1 || count > (Height * Length * 0.90))
- {
- MessageBox.Show("Ошибка ввода.");
- }
- else
- {
- for (int i = 0; i < fieldLength; i++)
- {
- for (int j = 0; j < fieldHeight; j++)
- {
- Controls.Remove(TableButton[i, j] as FieldButton);
- }
- }
- fieldLength = Length;
- fieldHeight = Height;
- minesCount = count;
- TableButton = new FieldButton[fieldLength, fieldHeight];
- progressBar1.Visible = true;
- pictureBox1.Dispose();
- if (fieldLength > 17)
- {
- Width = 23 * fieldLength + 46;
- }
- else
- {
- Width = 455;
- }
- this.Height = 23 * fieldHeight + 120;
- cellsCount = fieldHeight * fieldLength - minesCount;
- openedCells = 0;
- progressBar1.Value = 0;
- label4.Visible = true;
- label4.BackColor = Color.FromArgb(0, Color.Black);
- label4.Text = "Cells open: 0";
- CreateInterface();
- }
- }
- public void CreateInterface()
- {
- for (int i = 0; i < fieldLength; i++)
- {
- for (int j = 0; j < fieldHeight; j++)
- {
- TableButton[i, j] = new FieldButton(i, j, 0);
- TableButton[i, j].SetBounds(23 * i + 16, 23 * j + 66, 22, 22);
- TableButton[i, j].FlatStyle = FlatStyle.Flat;
- TableButton[i, j].Click += new EventHandler(CheckMine);
- TableButton[i, j].MouseUp += new MouseEventHandler(SetFlag);
- TableButton[i, j].TabStop = false;
- TableButton[i, j].BackgroundImage = Properties.Resources.BlueBG;
- Controls.Add(TableButton[i, j]);
- }
- }
- int x, y;
- Random rnd = new Random();
- for (int c = 0; c < minesCount;)
- {
- x = rnd.Next(0, fieldLength);
- y = rnd.Next(0, fieldHeight);
- if (TableButton[x, y].State == 0)
- {
- c++;
- TableButton[x, y].State = -1;
- //TableButton[x, y].Text = "X"; // ЭТА ШОБ ВИДНА СРАЗУ БЫЛА ДЕ МИНЫ
- }
- }
- for (int i = 0; i < fieldLength; i++)
- {
- for (int j = 0; j < fieldHeight; j++)
- {
- if (TableButton[i, j].State == -1)
- continue;
- int mines = 0;
- for (int k = -1; k <= 1; k++)
- {
- for (int l = -1; l <= 1; l++)
- {
- int xPos = i + k,
- yPos = j + l;
- if (xPos >= 0 && xPos < fieldLength && yPos >= 0 && yPos < fieldHeight &&
- TableButton[xPos, yPos].State == -1)
- mines++;
- }
- }
- TableButton[i, j].State = mines;
- //if (TableButton[i, j].State != 0)
- //{
- // TableButton[i, j].Text = mines.ToString(); // Dlya Debag'a
- //}
- }
- }
- }
- private void SetFlag(object sender, MouseEventArgs e)
- {
- StartButton.Focus();
- if (e.Button == MouseButtons.Right)
- {
- FieldButton button = (FieldButton)sender;
- int x = button.X;
- int y = button.Y;
- if (!TableButton[button.X, button.Y].Flaged)
- {
- TableButton[button.X, button.Y].BackgroundImage = Properties.Resources.Flag;
- TableButton[button.X, button.Y].Flaged = true;
- }
- else
- {
- TableButton[button.X, button.Y].BackgroundImage = Properties.Resources.BlueBG;
- TableButton[button.X, button.Y].Flaged = false;
- }
- }
- }
- public void MineView()
- {
- for (int i = 0; i < fieldLength; i++)
- {
- for (int j = 0; j < fieldHeight; j++)
- {
- TableButton[i, j].Enabled = false;
- if (TableButton[i, j].State == -1)
- {
- TableButton[i, j].BackgroundImage = Properties.Resources.Mine1;
- }
- else
- {
- TableButton[i, j].BackgroundImage = Properties.Resources.WhiteBG;
- if (TableButton[i, j].State != 0)
- {
- TableButton[i, j].Text = Convert.ToString(TableButton[i, j].State);
- }
- }
- }
- }
- }
- private void CheckEmpty(int x, int y)
- {
- label4.Text = "Cells open:" + ++openedCells;
- progressBar1.Value = (openedCells * 100) / cellsCount;
- int state = TableButton[x, y].State;
- TableButton[x, y].BackgroundImage = Properties.Resources.WhiteBG;
- TableButton[x, y].Enabled = false;
- if (TableButton[x, y].State == 0)
- {
- for (int i = -1; i <= 1; i++)
- {
- for (int j = -1; j <= 1; j++)
- {
- int xPos = x + i,
- yPos = y + j;
- if (xPos >= 0 && xPos < fieldLength && yPos >= 0 && yPos < fieldHeight && TableButton[xPos, yPos].Enabled)
- {
- CheckEmpty(xPos, yPos);
- }
- }
- }
- }
- else
- {
- TableButton[x, y].Text = state.ToString();
- }
- }
- private void CheckMine(object sender, EventArgs e)
- {
- FieldButton button = (FieldButton)sender;
- int x = button.X;
- int y = button.Y;
- if (!TableButton[x, y].Flaged)
- {
- int state = button.State;
- TableButton[x, y].Enabled = false;
- TableButton[x, y].BackColor = Color.FromName("White");
- if (state == -1)
- {
- TableButton[x, y].BackgroundImage = Properties.Resources.Mine1;
- MessageBox.Show("You lose!");
- MineView();
- return;
- }
- else
- {
- CheckEmpty(x, y);
- }
- }
- if (progressBar1.Value >= 100)
- {
- MessageBox.Show("Ю ВОН!!!");
- MineView();
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment