protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) //TODO fix the bug of the "recursive call"
        {
            if (!suspectedMine && IsEnabled)
            {
                IsEnabled = false;

                //Test if no mines adjacent
                bool noMinesAdjacent = true;
                foreach (KeyValuePair <CardinalDirection, MineField> item in Neigbours)
                {
                    MineField tempMineField = item.Value;
                    if (tempMineField.containsMine || tempMineField.suspectedMine)
                    {
                        noMinesAdjacent = false;
                        break;
                    }
                }

                if (noMinesAdjacent)
                {
                    foreach (KeyValuePair <CardinalDirection, MineField> item in Neigbours)
                    {
                        MineField tempMineField = item.Value;
                        tempMineField.OnMouseLeftButtonDown(e);
                    }
                }
            }
            base.OnMouseLeftButtonDown(e);
        }
示例#2
0
        private void EstablishNeighbours()
        {
            for (int row = 0; row < mineFields.GetLength(0); row++)
            {
                for (int column = 0; column < mineFields.GetLength(1); column++)
                {
                    //Get value
                    MineField tempMineField = mineFields[row, column];

                    //Setup N (Not in Top row)
                    if (row != 0)
                    {
                        tempMineField.Neigbours[CardinalDirection.North] = mineFields[row - 1, column];
                    }

                    //Setup NE
                    if (row != 0 && column != mineFields.GetUpperBound(1))
                    {
                        tempMineField.Neigbours[CardinalDirection.NorthEast] = mineFields[row - 1, column + 1];
                    }

                    //Setup E(Not at the rightmost column)
                    if (column != mineFields.GetUpperBound(1))
                    {
                        tempMineField.Neigbours[CardinalDirection.East] = mineFields[row, column + 1];
                    }

                    //Setup SE
                    if (row != mineFields.GetUpperBound(0) && column != mineFields.GetUpperBound(1))
                    {
                        tempMineField.Neigbours[CardinalDirection.SouthEast] = mineFields[row + 1, column + 1];
                    }

                    //Setup S (Not in bottom row)
                    if (row != mineFields.GetUpperBound(0))
                    {
                        tempMineField.Neigbours[CardinalDirection.South] = mineFields[row + 1, column];
                    }

                    //Setup SW
                    if (row != mineFields.GetUpperBound(0) && column != 0)
                    {
                        tempMineField.Neigbours[CardinalDirection.SouthWest] = mineFields[row + 1, column - 1];
                    }

                    //Setup W (Not at The left border)
                    if (column != 0)
                    {
                        tempMineField.Neigbours[CardinalDirection.West] = mineFields[row, column - 1];
                    }

                    //Setup NW
                    if (row != 0 && column != 0)
                    {
                        tempMineField.Neigbours[CardinalDirection.NorthWest] = mineFields[row - 1, column - 1];
                    }
                }
            }
        }
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            //Failsafe against unset values
            if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue || values[2] == DependencyProperty.UnsetValue || values[3] == DependencyProperty.UnsetValue)
            {
                return("");
            }

            //Convert needed values
            bool suspectedMine = (bool)values[0];
            bool containsMine  = (bool)values[1];
            bool isEnabled     = (bool)values[2];
            ObservableDictionary <CardinalDirection, MineField> neighbours = (ObservableDictionary <CardinalDirection, MineField>)values[3];

            //Implement logic
            if (isEnabled)
            {
                if (suspectedMine)
                {
                    return("F");
                }
                else
                {
                    return("");
                }
            }
            else
            {
                if (containsMine)
                {
                    return("M");
                }
                else
                {
                    int neighboursWithMines = 0;
                    foreach (KeyValuePair <CardinalDirection, MineField> item in neighbours)
                    {
                        MineField tempMineField = item.Value;
                        if (tempMineField.ContainsMine)
                        {
                            neighboursWithMines++;
                        }
                    }
                    if (neighboursWithMines == 0)
                    {
                        return("");
                    }
                    else
                    {
                        return(neighboursWithMines);
                    }
                }
            }
        }
示例#4
0
        private void MineFieldPropertyChanged(Object sender, PropertyChangedEventArgs e)
        {
            //Here we can listen to what property was changed (by name.... i hate it) and react.
            //A Possible solution would be to implemente my own events... that seems liker an overkill of work :(

            if (e.PropertyName.Equals("SuspectedMine")) //TODO convert to Switch
            {
                if (sender is MineField)
                {
                    MineField tempMineField = (MineField)sender;
                    if (tempMineField.SuspectedMine)
                    {
                        MinesFlagged++;
                    }
                    else
                    {
                        MinesFlagged--;
                    }
                }
            }
            if (e.PropertyName.Equals("IsEnabled"))
            {
                if (sender is MineField)
                {
                    MineField tempMineField = (MineField)sender;
                    if (tempMineField.IsEnabled)
                    {
                        fieldsRevealed--;
                    }
                    else
                    {
                        fieldsRevealed++;
                        //here we react depending on the gamestate
                        if (gameState == GameState.NotStarted)
                        {
                            PlaceMines(tempMineField);
                            gameState = GameState.Running;
                        }
                        if (tempMineField.ContainsMine)
                        {
                            GameLost();
                        }
                        else
                        {
                            if (DetectWin())
                            {
                                GameWin();
                            }
                        }
                    }
                }
            }
        }
示例#5
0
        private void PopulateGrid()
        {
            //Gather needed values
            int rows          = gameGrid.RowDefinitions.Count();
            int columns       = gameGrid.ColumnDefinitions.Count();
            int amountOFields = rows * columns;

            //Create the minefields
            for (int r = 0; r < rows; r++)
            {
                for (int c = 0; c < columns; c++)
                {
                    MineField tempMineField = new MineField(this);

                    //Register listeners
                    tempMineField.PropertyChanged += MineFieldPropertyChanged; //With that we dont need to keep a direct reference in the MineField to this object.
                    //Add to list
                    mineFields[r, c] = tempMineField;
                    //Add to grid
                    AddMineField(r, c, tempMineField);
                }
            }
        }
示例#6
0
        private void PlaceMines(MineField mineField)
        {
            //Init needed values
            Random random      = new Random();
            int    placedMines = 0;

            while (placedMines < TotalMines)
            {
                int randomRow    = random.Next(0, mineFields.GetLength(0));
                int randomColumn = random.Next(0, mineFields.GetLength(1));

                MineField pickedMinefield = mineFields[randomRow, randomColumn];

                if (!Object.ReferenceEquals(mineField, pickedMinefield))
                {
                    pickedMinefield.ContainsMine = true;
                    placedMines++;
                }
            }

            //Fore the minefield to update its number (Dirty hack, we should listen to the Neigbourslist, contains mine)
            mineField.ContainsMine = false;
        }
示例#7
0
 private void AddMineField(int row, int column, MineField mineField)
 {
     Grid.SetRow(mineField, row);
     Grid.SetColumn(mineField, column);
     gameGrid.Children.Add(mineField);
 }