PhotoShaman

54P3!2

Aug 9th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.13 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace Генерация_кнопок
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         FieldButton[,] TableButton;
  10.         int minesCount, fieldHeight, fieldLength, openedCells, cellsCount;
  11.         bool downDirrection = true;
  12.         public class FieldButton : Button
  13.         {
  14.             public int X, Y, State;
  15.             public bool Flaged { get; set; } = false;
  16.             public FieldButton(int x, int y, int s)
  17.             {
  18.                 X = x;
  19.                 Y = y;
  20.                 State = s;
  21.             }
  22.         }
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.             MaximizeBox = false;
  27.            
  28.             timer1.Tick += delegate {
  29.                 if (downDirrection)
  30.                 {
  31.                     pictureBox1.Top -= 3;
  32.                     if (pictureBox1.Location.Y <= 67)
  33.                     {
  34.                         downDirrection = false;
  35.                     }
  36.                 }
  37.                 else
  38.                 {
  39.                     pictureBox1.Top += 3;
  40.                     if (pictureBox1.Location.Y >= 82)
  41.                     {
  42.                         downDirrection = true;
  43.                     }
  44.                 }
  45.             };
  46.             timer1.Enabled = true;
  47.         }
  48.  
  49.         private void StartButton_Click(object sender, EventArgs e)
  50.         {
  51.             int Length, Height, count;
  52.  
  53.             if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" ||
  54.                 !Int32.TryParse(textBox1.Text, out Length) ||
  55.                 !Int32.TryParse(textBox2.Text, out Height) ||
  56.                 !Int32.TryParse(textBox3.Text, out count) ||
  57.                 Length < 10 || Length > 20 || Height < 10 || Height > 20 ||
  58.                 count < 1 || count > (Height * Length * 0.90))
  59.             {
  60.                 MessageBox.Show("Ошибка ввода.");
  61.             }
  62.             else
  63.             {
  64.                 for (int i = 0; i < fieldLength; i++)
  65.                 {
  66.                     for (int j = 0; j < fieldHeight; j++)
  67.                     {
  68.                         Controls.Remove(TableButton[i, j] as FieldButton);
  69.                     }
  70.                 }
  71.                 fieldLength = Length;
  72.                 fieldHeight = Height;
  73.                 minesCount = count;
  74.                 TableButton = new FieldButton[fieldLength, fieldHeight];
  75.                 progressBar1.Visible = true;
  76.                 pictureBox1.Dispose();
  77.                 if (fieldLength > 17)
  78.                 {
  79.                     Width = 23 * fieldLength + 46;
  80.                 }
  81.                 else
  82.                 {
  83.                     Width = 455;
  84.                 }
  85.                 this.Height = 23 * fieldHeight + 120;
  86.                 cellsCount = fieldHeight * fieldLength - minesCount;
  87.                 openedCells = 0;
  88.                 progressBar1.Value = 0;
  89.                 label4.Visible = true;
  90.                 label4.BackColor = Color.FromArgb(0, Color.Black);
  91.                 label4.Text = "Cells open: 0";
  92.                 CreateInterface();
  93.             }
  94.         }
  95.  
  96.         public void CreateInterface()
  97.         {
  98.             for (int i = 0; i < fieldLength; i++)
  99.             {
  100.                 for (int j = 0; j < fieldHeight; j++)
  101.                 {
  102.                     TableButton[i, j] = new FieldButton(i, j, 0);
  103.                     TableButton[i, j].SetBounds(23 * i + 16, 23 * j + 66, 22, 22);
  104.                     TableButton[i, j].FlatStyle = FlatStyle.Flat;
  105.                     TableButton[i, j].Click += new EventHandler(CheckMine);
  106.                     TableButton[i, j].MouseUp += new MouseEventHandler(SetFlag);
  107.                     TableButton[i, j].TabStop = false;
  108.                     TableButton[i, j].BackgroundImage = Properties.Resources.BlueBG;
  109.                     Controls.Add(TableButton[i, j]);
  110.                 }
  111.             }
  112.  
  113.             int x, y;
  114.             Random rnd = new Random();
  115.             for (int c = 0; c < minesCount;)
  116.             {
  117.                 x = rnd.Next(0, fieldLength);
  118.                 y = rnd.Next(0, fieldHeight);
  119.                 if (TableButton[x, y].State == 0)
  120.                 {
  121.                     c++;
  122.                     TableButton[x, y].State = -1;
  123.                     //TableButton[x, y].Text = "X";  // ЭТА ШОБ ВИДНА СРАЗУ БЫЛА ДЕ МИНЫ
  124.                 }
  125.             }
  126.  
  127.             for (int i = 0; i < fieldLength; i++)
  128.             {
  129.                 for (int j = 0; j < fieldHeight; j++)
  130.                 {
  131.                     if (TableButton[i, j].State == -1)
  132.                         continue;
  133.                     int mines = 0;
  134.                     for (int k = -1; k <= 1; k++)
  135.                     {
  136.                         for (int l = -1; l <= 1; l++)
  137.                         {
  138.                             int xPos = i + k,
  139.                                 yPos = j + l;
  140.                             if (xPos >= 0 && xPos < fieldLength && yPos >= 0 && yPos < fieldHeight &&
  141.                                 TableButton[xPos, yPos].State == -1)
  142.                                 mines++;
  143.                         }
  144.                     }
  145.                     TableButton[i, j].State = mines;
  146.                     //if (TableButton[i, j].State != 0)
  147.                     //{
  148.                     //    TableButton[i, j].Text = mines.ToString(); // Dlya Debag'a
  149.                     //}
  150.                 }
  151.             }
  152.         }
  153.  
  154.         private void SetFlag(object sender, MouseEventArgs e)
  155.         {
  156.             StartButton.Focus();
  157.             if (e.Button == MouseButtons.Right)
  158.             {
  159.                 FieldButton button = (FieldButton)sender;
  160.                 int x = button.X;
  161.                 int y = button.Y;
  162.                 if (!TableButton[button.X, button.Y].Flaged)
  163.                 {
  164.                     TableButton[button.X, button.Y].BackgroundImage = Properties.Resources.Flag;
  165.                     TableButton[button.X, button.Y].Flaged = true;
  166.                 }
  167.                 else
  168.                 {
  169.                     TableButton[button.X, button.Y].BackgroundImage = Properties.Resources.BlueBG;
  170.                     TableButton[button.X, button.Y].Flaged = false;
  171.                 }
  172.             }
  173.         }
  174.  
  175.         public void MineView()
  176.         {
  177.             for (int i = 0; i < fieldLength; i++)
  178.             {
  179.                 for (int j = 0; j < fieldHeight; j++)
  180.                 {
  181.                     TableButton[i, j].Enabled = false;
  182.                     if (TableButton[i, j].State == -1)
  183.                     {
  184.                         TableButton[i, j].BackgroundImage = Properties.Resources.Mine1;
  185.                     }
  186.                     else
  187.                     {
  188.                         TableButton[i, j].BackgroundImage = Properties.Resources.WhiteBG;
  189.                         if (TableButton[i, j].State != 0)
  190.                         {
  191.                             TableButton[i, j].Text = Convert.ToString(TableButton[i, j].State);
  192.                         }
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.  
  198.         private void CheckEmpty(int x, int y)
  199.         {
  200.             label4.Text = "Cells open:" + ++openedCells;
  201.             progressBar1.Value = (openedCells * 100) / cellsCount;
  202.  
  203.             int state = TableButton[x, y].State;
  204.             TableButton[x, y].BackgroundImage = Properties.Resources.WhiteBG;
  205.  
  206.             TableButton[x, y].Enabled = false;
  207.             if (TableButton[x, y].State == 0)
  208.             {
  209.                 for (int i = -1; i <= 1; i++)
  210.                 {
  211.                     for (int j = -1; j <= 1; j++)
  212.                     {
  213.                         int xPos = x + i,
  214.                             yPos = y + j;
  215.                         if (xPos >= 0 && xPos < fieldLength && yPos >= 0 && yPos < fieldHeight && TableButton[xPos, yPos].Enabled)
  216.                         {
  217.                             CheckEmpty(xPos, yPos);
  218.                         }
  219.                     }
  220.                 }
  221.             }
  222.             else
  223.             {
  224.                 TableButton[x, y].Text = state.ToString();
  225.             }
  226.         }
  227.  
  228.         private void CheckMine(object sender, EventArgs e)
  229.         {
  230.             FieldButton button = (FieldButton)sender;
  231.             int x = button.X;
  232.             int y = button.Y;
  233.             if (!TableButton[x, y].Flaged)
  234.             {
  235.                 int state = button.State;
  236.                 TableButton[x, y].Enabled = false;
  237.                 TableButton[x, y].BackColor = Color.FromName("White");
  238.                 if (state == -1)
  239.                 {
  240.                     TableButton[x, y].BackgroundImage = Properties.Resources.Mine1;
  241.                     MessageBox.Show("You lose!");
  242.                     MineView();
  243.                     return;
  244.                 }
  245.                 else
  246.                 {
  247.                     CheckEmpty(x, y);
  248.                 }
  249.             }
  250.             if (progressBar1.Value >= 100)
  251.             {
  252.                 MessageBox.Show("Ю ВОН!!!");
  253.                 MineView();
  254.                 return;
  255.             }
  256.         }
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment