private void ClickOnCell(object sender, MouseButtonEventArgs e)
        {
            #region Генерация поля после первого нажатия на любую клетку
            GameManager.ClickCount++;
            if (GameManager.ClickCount == 1)
            {
                GameManager.Generate(cell, Grid.GetRow((Button)sender), Grid.GetColumn((Button)sender));      // Вызываем генерацию поля и передаём туда клетку, на которую нажали


                for (int m = 0; m < 10; m++)
                {
                    for (int k = 0; k < 10; k++)
                    {
                        GameManager.MineAroundCounter(cell, m, k);            // Прохожу по полю и получаю количество мин вокруг для каждой клетки
                    }
                }
            }
            #endregion


            int i = Grid.GetRow((Button)sender), j = Grid.GetColumn((Button)sender);


            // Если не выбран режим установки флагов и была нажата левая кнопа мыши
            #region Нажатие на кнопку в обычном режиме
            if (!GameManager.IsSettingFlags && e.LeftButton == MouseButtonState.Pressed)
            {
                if (cell[i, j].IsMine && !cell[i, j].IsFlagged)             // Если кнопка, на которую мы нажали - мина и она не помечена
                {
                    MessageBox.Show("Вы проиграли");                        // То мы проигрываем
                    Reload();
                }
                else if (!cell[i, j].IsMine && !cell[i, j].IsFlagged)       // Если это не мина и клетка не помечена флагом
                {
                    // Если нажать на открытую клетку
                    if (cell[i, j].IsOpen)
                    {
                        int flagArround = 0;


                        #region Подсчёт отмеченных клеток вокруг
                        if (i == 0)
                        {
                            if (j == 0)
                            {
                                for (int m = i; m <= i + 1; m++)
                                {
                                    for (int k = j; k <= j + 1; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                            else if (j == 9)
                            {
                                for (int m = i; m <= i + 1; m++)
                                {
                                    for (int k = j - 1; k <= j; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int m = i; m <= i + 1; m++)
                                {
                                    for (int k = j - 1; k <= j + 1; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                        }
                        else if (i == 9)
                        {
                            if (j == 0)
                            {
                                for (int m = i - 1; m <= i; m++)
                                {
                                    for (int k = j; k <= j + 1; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                            else if (j == 9)
                            {
                                for (int m = i - 1; m <= i; m++)
                                {
                                    for (int k = j - 1; k <= j; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int m = i - 1; m <= i; m++)
                                {
                                    for (int k = j - 1; k <= j + 1; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (j == 0)
                            {
                                for (int m = i - 1; m <= i + 1; m++)
                                {
                                    for (int k = j; k <= j + 1; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                            else if (j == 9)
                            {
                                for (int m = i - 1; m <= i + 1; m++)
                                {
                                    for (int k = j - 1; k <= j; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int m = i - 1; m <= i + 1; m++)
                                {
                                    for (int k = j - 1; k <= j + 1; k++)
                                    {
                                        if (cell[m, k].IsFlagged)
                                        {
                                            flagArround++;
                                        }
                                    }
                                }
                            }
                        }
                        #endregion


                        if (cell[i, j].MineAround == flagArround)               // И количество отмеченных клеток == количеству мин вокруг
                        {
                            GameManager.Around(cell, buttons, i, j);            // То открываем клетки вокруг
                        }
                    }


                    if (cell[i, j].MineAround == 0)                         // Если мин вокруг ноль, то открываем клетки вокруг
                    {
                        GameManager.OpenZero(cell, buttons, i, j);
                    }


                    buttons[i, j].Content = cell[i, j].MineAround;          // Открываем клетку, на которую мы нажали
                    GameManager.ColorChanger(cell, buttons, i, j);          // Устанавливаем соответствующий цвет текста кнопки
                    cell[i, j].IsOpen = true;
                }
            }
            #endregion


            // Если выбран режим установки флагов или нажатие на правую кнопку, при этом клетка не открыта
            #region Нажатие на кнопку в режиме флага
            else if ((GameManager.IsSettingFlags || e.RightButton == MouseButtonState.Pressed) && !cell[i, j].IsOpen)
            {
                FlagAdd(i, j);

                #region Победа
                if (GameManager.MineFlagged == GameManager.MineCount && GameManager.OtherCellsFlagged == 0) // Если количество помеченных мин == изначальному количеству мин и нет помеченных других клеток
                {
                    MessageBox.Show("Вы победили");                                                         // То игрок побеждает
                    Reload();
                }
                #endregion
            }
            #endregion
        }
Example #2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (mGameEnded)
            {
                newGame();
                return;
            }

            var button = (Button)sender;

            var colomn = Grid.GetColumn(button);
            var row    = Grid.GetRow(button);

            var index = colomn + (row * 3);

            if (mResults[index] != MarkType.Free)
            {
                return;
            }

            if (mPlayer1turn)
            {
                mResults[index] = MarkType.Cross;
            }
            else
            {
                mResults[index] = MarkType.Nought;
            }

            button.Content = mPlayer1turn ? "X" : "O";

            if (!mPlayer1turn)
            {
                button.Foreground = Brushes.Red;
            }
            // changes value on each click
            mPlayer1turn ^= true;
            #endregion

            #region winner rows
            // check if there is a winner on row 0
            if (mResults[0] != MarkType.Free && (mResults[0] & mResults[1] & mResults[2]) == mResults[0])
            {
                // game end
                mGameEnded = true;

                Button0_0.Background = Button1_0.Background = Button2_0.Background = Brushes.Green;
            }

            // check if there is a winner on row 1
            if (mResults[3] != MarkType.Free && (mResults[3] & mResults[4] & mResults[5]) == mResults[3])
            {
                // game end
                mGameEnded = true;

                Button0_1.Background = Button1_1.Background = Button2_1.Background = Brushes.Green;
            }

            // check if there is a winner on row 2
            if (mResults[6] != MarkType.Free && (mResults[6] & mResults[7] & mResults[8]) == mResults[6])
            {
                // game end
                mGameEnded = true;

                Button0_2.Background = Button1_2.Background = Button2_2.Background = Brushes.Green;
            }
            #endregion

            #region winner colomn
            // check if there is a winner on col 0
            if (mResults[0] != MarkType.Free && (mResults[0] & mResults[3] & mResults[6]) == mResults[0])
            {
                // game end
                mGameEnded = true;

                Button0_0.Background = Button0_1.Background = Button0_2.Background = Brushes.Green;
            }

            // check if there is a winner on col 1
            if (mResults[1] != MarkType.Free && (mResults[1] & mResults[4] & mResults[7]) == mResults[1])
            {
                // game end
                mGameEnded = true;

                Button1_0.Background = Button1_1.Background = Button1_2.Background = Brushes.Green;
            }

            // check if there is a winner on col 2
            if (mResults[2] != MarkType.Free && (mResults[2] & mResults[5] & mResults[8]) == mResults[2])
            {
                // game end
                mGameEnded = true;

                Button2_0.Background = Button2_1.Background = Button2_2.Background = Brushes.Green;
            }

            #endregion

            #region winner diagonal

            // check if there is a winner on diag top left bottom right
            if (mResults[0] != MarkType.Free && (mResults[0] & mResults[4] & mResults[8]) == mResults[0])
            {
                // game end
                mGameEnded = true;

                Button0_0.Background = Button1_1.Background = Button2_2.Background = Brushes.Green;
            }

            // check if there is a winner on diag top right to bottom left
            if (mResults[2] != MarkType.Free && (mResults[2] & mResults[4] & mResults[6]) == mResults[2])
            {
                // game end
                mGameEnded = true;

                Button2_0.Background = Button1_1.Background = Button0_2.Background = Brushes.Green;
            }
            #endregion


            #region draw
            if (!mResults.Any(result => result == MarkType.Free))
            {
                mGameEnded = true;
                //turn all orange
                Container.Children.Cast <Button>().ToList().ForEach(buttons =>
                {
                    buttons.Background = Brushes.Orange;
                });
                #endregion
            }
        }
Example #3
0
        private void ShowInputForm(object sender, RoutedEventArgs e)
        {
            string btnName  = ((Button)sender).Name;
            char   lastChar = btnName[btnName.Length - 1];
            //Open File Dialog to select picture
            OpenFileDialog op = new OpenFileDialog();

            op.Title  = "Select a picture";
            op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
                        "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                        "Portable Network Graphic (*.png)|*.png";
            if (op.ShowDialog() == true)
            {
                //Set new content on button
                // define button as button
                Button BtnClicked = e.Source as Button;
                //clear content
                BtnClicked.Content = "";
                //Create new stackpanel
                //var NewStackPanel = new StackPanel();
                //create new image object
                Image NewImage = new Image();
                //Set image object attributes
                NewImage.Source  = new BitmapImage(new Uri(op.FileName, UriKind.RelativeOrAbsolute));
                NewImage.Stretch = Stretch.Fill;
                //add to stackpanel object
                //NewStackPanel.Children.Add(NewImage);
                //add to button
                BtnClicked.Content    = NewImage;
                BtnClicked.Background = Brushes.Transparent;
                //add byte[] to player instance
                players[$"player{lastChar}"].Img = DbHandler.ImageToByteArray(System.Drawing.Image.FromFile(op.FileName));
            }

            //Show correct input form
            if (sender != null)
            {
                var element = (UIElement)e.Source;
                int c       = Grid.GetColumn(element);
                switch (c)
                {
                case 0:
                    InputForm0.Visibility = Visibility.Visible;
                    break;

                case 1:
                    InputForm1.Visibility = Visibility.Visible;
                    break;

                case 2:
                    InputForm2.Visibility = Visibility.Visible;
                    break;

                case 3:
                    InputForm3.Visibility = Visibility.Visible;
                    break;

                case 4:
                    InputForm4.Visibility = Visibility.Visible;
                    break;
                }
            }
        }