예제 #1
0
        /// <summary>
        /// Handle field click
        /// </summary>
        /// <param name="sender">Button object</param>
        /// <param name="e">event</param>
        private void OnFieldClick(object sender, RoutedEventArgs e)
        {
            if (!this.gameIsFinished)
            {
                Button btn   = sender as Button;
                State  state = fields[(Int32)btn.Tag].State;
                Point  point = fields[(Int32)btn.Tag].Point;

                if (state.Id != 3 && !state.Clicked)
                {
                    if (state != null && state.IsBomb)
                    {
                        this.ShowAllBombs();
                    }
                    else
                    {
                        btn.Background = Brushes.White;
                        state.Clicked  = true;

                        CountClick();

                        if (state.Number != null)
                        {
                            btn.Content = state.Number;
                        }
                        else
                        {
                            this.ClearAllFieldsAround(point);

                            btn.Content = "";
                        }
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Clear fields with empty number and show closest fields with number around provided point
        /// </summary>
        /// <param name="buttonPoint">Point with x and y coordinate</param>
        public void ClearAllFieldsAround(Point buttonPoint)
        {
            List <Field> foundFields = new List <Field>
            {
                fields.Find(field => field.Point.X + 1 == buttonPoint.X && field.Point.Y == buttonPoint.Y),
                fields.Find(field => field.Point.X - 1 == buttonPoint.X && field.Point.Y == buttonPoint.Y),
                fields.Find(field => field.Point.X == buttonPoint.X && field.Point.Y + 1 == buttonPoint.Y),
                fields.Find(field => field.Point.X == buttonPoint.X && field.Point.Y - 1 == buttonPoint.Y),
                fields.Find(field => field.Point.X + 1 == buttonPoint.X && field.Point.Y + 1 == buttonPoint.Y),
                fields.Find(field => field.Point.X + 1 == buttonPoint.X && field.Point.Y - 1 == buttonPoint.Y),
                fields.Find(field => field.Point.X - 1 == buttonPoint.X && field.Point.Y + 1 == buttonPoint.Y),
                fields.Find(field => field.Point.X - 1 == buttonPoint.X && field.Point.Y - 1 == buttonPoint.Y)
            };

            List <Field> filtered = foundFields.FindAll(e => e != null);

            foreach (Field filteredField in filtered)
            {
                if (!filteredField.State.Clicked && filteredField.State.Id != 3)
                {
                    filteredField.Button.Background = Brushes.White;
                    filteredField.State.Clicked     = true;

                    CountClick();

                    if (filteredField.State.Number == null)
                    {
                        ClearAllFieldsAround(filteredField.Point);
                    }
                    else
                    {
                        filteredField.Button.Content = filteredField.State.Number;
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Initialize Grid with fields and start game
        /// </summary>
        public void InitGame()
        {
            Random randNum = new Random();
            int    Min     = 0;
            int    Max     = this.dimensions;

            this.clickedFields   = 0;
            this.info.Visibility = Visibility.Hidden;
            this.time.Visibility = Visibility.Hidden;
            this.startTime       = DateTime.Now.ToString("h:mm:ss tt");

            randomBombs = Point.GenerateRandomPoints(Min, Max, Convert.ToInt32(this.dimensions * this.mode));


            for (int x = 0; x < this.dimensions; x++)
            {
                this.mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
                this.mainGrid.RowDefinitions.Add(new RowDefinition());


                for (int y = 0; y < this.dimensions; y++)
                {
                    Field field = Field.AddButton(x, y, this.mainGrid, fields.Count());

                    field.Button.Click     += OnFieldClick;
                    field.Button.MouseDown += OnFieldRightClick;


                    fields.Add(field);
                }
            }

            foreach (Field field in fields)
            {
                Point  buttonPoint = field.Point;
                Button button      = field.Button;

                //@todo optimize
                foreach (Point randomBomb in randomBombs)
                {
                    bool found = randomBomb.X == buttonPoint.X && randomBomb.Y == buttonPoint.Y;

                    if (found)
                    {
                        field.State = new State(0, 0, true);
                    }
                }

                if (field.State == null)
                {
                    field.State = new State(0, 0, false);
                }
            }

            foreach (Field field in fields)
            {
                Point buttonPoint = field.Point;

                int bombsAround = Point.GetBombsAround(buttonPoint, randomBombs);

                if (bombsAround > 0)
                {
                    field.State.Number = bombsAround;
                }
                else
                {
                    field.State.Number = null;
                }

                field.Button.Foreground = Brushes.Black;
            }


            this.bombFlags = this.randomBombs.Count();
            ShowCurrentFlags();
        }