Exemplo n.º 1
0
        void Reveal(Bomb_or_Cell cell)
        {
            if (game_just_started)
            {
                s = 0;
                game_just_started = false;
                Console.WriteLine("IMMMMMMMMM Working!!!!!!!!!");
                timer1.Start();
            }
            Console.WriteLine("Clicked {0} {1}", cell.X, cell.Y);
            cell.isOpened    = true;
            cell.status.Font = new Font("Times New Roman", 44, cell.status.Font.Style | FontStyle.Bold);
            cell.status.Show();
            cell.btn.Hide();
            count += 1;
            cell.btn.Hide();
            int          new_x;
            int          new_y;
            Bomb_or_Cell t;

            for (int i = 0; i < 8; i++)
            {
                new_x = cell.X + directions[i, 0];
                new_y = cell.Y + directions[i, 1];
                if (Exist(new_x, new_y) && field[new_x, new_y].isBomb == 0 && field[new_x, new_y].isOpened == false && field[new_x, new_y].status.Text == "0")
                {
                    t = field[new_x, new_y];
                    OnCellClicked(t);
                }
            }
        }
Exemplo n.º 2
0
        void OnCellClicked(Bomb_or_Cell cell)
        {
            if (cell.isBomb == 1)
            {
                textBox1.Text = "0";
                timer1.Stop();
                textBox1.Hide();
                ShowBombs();
                MessageBox.Show("Игра окончена!");
                toolStripSplitButton1.HideDropDown();
                panel1.Hide();
                toolStrip1.Hide();

                InitializeComponent();
            }
            else
            {
                //Console.WriteLine("count {0}", count);
                Reveal(cell);
                //Console.WriteLine("count {0}", count);
                if (size * size - count == count_bombs)
                {
                    textBox1.Text = "0";
                    timer1.Stop();
                    textBox1.Hide();
                    MessageBox.Show(String.Format("You won! \n In {0} second(-s) and haven't launched {1} bomb(-s)", s.ToString(), count_bombs));
                    toolStripSplitButton1.HideDropDown();
                    panel1.Hide();
                    toolStrip1.Hide();

                    InitializeComponent();
                }
            }
        }
Exemplo n.º 3
0
        void InitField(int _bombs)
        {
            s = 0;
            game_just_started  = true;
            panel1.Size        = new System.Drawing.Size(90 * size, 90 * (size + 1));
            panel1.MinimumSize = new System.Drawing.Size(90 * size, 90 * (size + 1));
            panel1.MaximumSize = new System.Drawing.Size(90 * size, 90 * (size + 1));
            count_bombs        = Math.Min(_bombs, size * size);
            //Console.WriteLine("///{0} {1}", _bombs, size);
            count = 0;
            field = new Bomb_or_Cell[size, size];

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    field[i, j]             = new Bomb_or_Cell();
                    field[i, j].X           = i;
                    field[i, j].Y           = j;
                    field[i, j].isOpened    = false;
                    field[i, j].isBomb      = 0;
                    field[i, j].status      = new Label();
                    field[i, j].status.Font = new Font("Times New Roman", 44, field[i, j].status.Font.Style | FontStyle.Bold);
                    field[i, j].btn         = new Button();
                }
            }

            for (int i = 0; i < Math.Min(count_bombs, size * size); i++)
            {
                int x = rand.Next(0, size);
                int y = rand.Next(0, size);
                while (field[x, y].isBomb == 1)
                {
                    x = rand.Next(0, size);
                    y = rand.Next(0, size);
                }
                field[x, y].isBomb = 1;
            }
            for (int x = 0; x < size; ++x)
            {
                for (int y = 0; y < size; ++y)
                {
                    Label l = new Label();
                    if (field[x, y].isBomb == 1)
                    {
                        l.Text = "☠";
                    }
                    else
                    {
                        l.Text = field[x, y].Risk.ToString();
                    }
                    l.SetBounds(90 * x, 90 * (y + 1), 90, 90);
                    l.Font   = new Font("Times New Roman", 44, l.Font.Style | FontStyle.Bold);
                    l.Parent = panel1;
                    l.Hide();
                    field[x, y].status = l;

                    Bomb_or_Cell t = new Bomb_or_Cell();
                    field[x, y].X        = x;
                    field[x, y].Y        = y;
                    field[x, y].isOpened = false;
                    //field[x, y] = t;
                    field[x, y].btn = new Button();
                    field[x, y].btn.SetBounds(90 * x, 90 * (y + 1), 90, 90);

                    field[x, y].btn.Parent = panel1;
                    t = field[x, y];

                    field[x, y].btn.Click += (sender, e) => OnCellClicked(t);
                }
            }

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (i > 0)
                    {
                        field[i, j].Risk += field[i - 1, j].isBomb;
                        if (j < size - 1)
                        {
                            field[i, j].Risk += field[i - 1, j + 1].isBomb;
                        }
                        if (j > 0)
                        {
                            field[i, j].Risk += field[i - 1, j - 1].isBomb;
                        }
                    }
                    if (i < size - 1)
                    {
                        field[i, j].Risk += field[i + 1, j].isBomb;
                        if (j < size - 1)
                        {
                            field[i, j].Risk += field[i + 1, j + 1].isBomb;
                        }
                        if (j > 0)
                        {
                            field[i, j].Risk += field[i + 1, j - 1].isBomb;
                        }
                    }
                    if (j < size - 1)
                    {
                        field[i, j].Risk += field[i, j + 1].isBomb;
                    }
                    if (j > 0)
                    {
                        field[i, j].Risk += field[i, j - 1].isBomb;
                    }

                    Label l = new Label();
                    if (field[i, j].isBomb == 1)
                    {
                        l.Text = "☠";
                    }
                    else
                    {
                        l.Text = field[i, j].Risk.ToString();
                    }
                    l.SetBounds(90 * i, 90 * (j + 1), 90, 90);
                    l.Parent = panel1;
                    l.Hide();
                    field[i, j].status = l;
                }
            }
        }