Beispiel #1
0
 private void BothButtonsDown(Square s)
 {
     bothpressed = true;
     if (!s.Revealed && !s.IsFlagged) s.Pressed = true;
     foreach (Square square in GetAdjacent(s))
     {
         if(!square.Revealed && !square.IsFlagged) square.Pressed = true;
     }
 }
Beispiel #2
0
        private void BothButtonsUp(Square s)
        {
            s.Pressed = false;
            int surroundingMines = 0;

            foreach (Square square in GetAdjacent(s))
            {
                if (square.Pressed) square.Pressed = false;
                if (square.IsFlagged) surroundingMines++;
            }

            if (s.Revealed && surroundingMines == (int)s.Content)
            {
                foreach (Square square in GetAdjacent(s))
                {
                    if (!square.Revealed) Reveal(square);
                }
            }
        }
Beispiel #3
0
        private List<Square> GetAdjacent(Square s)
        {
            List<Square> result = new List<Square>();

            for (int x = s.PosX - 1; x < s.PosX + 2; x++)
            {
                for (int y = s.PosY - 1; y < s.PosY + 2; y++)
                {
                    if (x >= 0 && y >= 0 && x < ColCount && y < RowCount)
                    {
                        result.Add(GameGrid[y].Children[x] as Square);
                    }
                }
            }

            return result;
        }
Beispiel #4
0
        private void Reveal(Square s)
        {
            if (!s.Revealed && !s.IsMined && !s.IsFlagged)
            {
                s.Revealed = true;

                int val = 0;
                foreach (Square square in GetAdjacent(s))
                {
                    if (square.IsMined) val++;
                }

                if (val > 0) s.Content = val;

                if (val == 0)
                {
                    foreach (Square square in GetAdjacent(s))
                    {
                        Reveal(square);
                    }
                }
            }

            if (s.IsMined && !s.IsFlagged)
            {
                YouLose();
            }
        }
Beispiel #5
0
        private ObservableCollection<StackPanel> CreateGameGrid()
        {
            ObservableCollection<StackPanel> grid = new ObservableCollection<StackPanel>();

            for (int i = 0; i < RowCount; i++)
            {
                StackPanel row = new StackPanel();
                row.Orientation = Orientation.Horizontal;

                for (int j = 0; j < ColCount; j++)
                {
                    Square s = new Square();

                    s.Style = Application.Current.FindResource("SquareStyle") as Style;
                    s.PosX = j;
                    s.PosY = i;
                    s.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(LeftMouseUp);
                    s.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(MouseDown);
                    s.MouseRightButtonUp += new MouseButtonEventHandler(RightMouseUp);
                    s.MouseRightButtonDown += new MouseButtonEventHandler(MouseDown);

                    row.Children.Add(s);
                }
                grid.Add(row);
            }

            return grid;
        }