Example #1
0
        private void SelectTile(object arg)
        {
            GameTileModel oldTile = (GameTileModel)arg;

            bool newTileIsMine     = oldTile.IsMine;
            bool newTileIsFlagged  = IsSettingFlag ^ oldTile.IsFlagged;
            bool newTileIsSelected = oldTile.IsSelected || !IsSettingFlag;

            if ((oldTile.IsFlagged != newTileIsFlagged) || (oldTile.IsSelected != newTileIsSelected))
            {
                int index   = GameBoardCollection.IndexOf(oldTile);
                var newTile = new GameTileModel(oldTile.Row, oldTile.Col, newTileIsMine, newTileIsSelected, newTileIsFlagged, oldTile.NumMineNeighbors);
                GameBoardCollection[index] = newTile;
                OnPropertyChanged("NumberOfMinesRemainingToFlag");
                logger.Trace("Gametile altered at position [%d,%d], index %d\nOld Tile - %s\nNewTile - %s",
                             oldTile.Row, oldTile.Col, index, oldTile.ToString(), newTile.ToString());
                if (newTile.NumMineNeighbors == 0 && !IsSettingFlag)
                {
                    SelectNeighborsWithNoNeighboringMines(newTile);
                }
                if (CheckForGameWin())
                {
                    CurrentGameStatus = GameStatus.Won;
                }
                else if (newTile.IsMine && newTile.IsSelected)
                {
                    RevealAllTiles();
                    CurrentGameStatus = GameStatus.Lost;
                }
            }
        }
        public GameTileCollectionModel(int boardHeight = 10, int boardWidth = 10, int numMines = 10)
        {
            prTileList = new List <List <GameTileModel> >();
            for (int i = 0; i < boardHeight; i++)
            {
                prTileList.Add(new List <GameTileModel>());
                for (int j = 0; j < boardWidth; j++)
                {
                    prTileList[i].Add(null);
                }
            }

            List <int> tileNumbers = Enumerable.Range(0, (boardHeight * boardWidth - 1)).ToList();
            Random     rand        = new Random();

            for (int i = 0; i < numMines; i++)
            {
                int index      = rand.Next(0, tileNumbers.Count - 1);
                int randNumber = tileNumbers[index];
                prTileList[(randNumber / boardWidth)][(randNumber % boardWidth)] = new GameTileModel(0, 0, true, false, false);
                tileNumbers.RemoveAt(index);
            }

            for (int i = 0; i < boardHeight; i++)
            {
                for (int j = 0; j < boardWidth; j++)
                {
                    if (prTileList[i][j] == null)
                    {
                        prTileList[i][j] = new GameTileModel(0, 0);
                    }
                }
            }
        }
Example #3
0
 private void RevealAllTiles()
 {
     for (int i = 0; i < GameBoardCollection.Count; i++)
     {
         if (!GameBoardCollection[i].IsSelected)
         {
             GameTileModel oldTile = GameBoardCollection[i];
             GameBoardCollection[i] = new GameTileModel(oldTile.Row, oldTile.Col, oldTile.IsMine, true, oldTile.IsFlagged, oldTile.NumMineNeighbors);
         }
     }
 }
Example #4
0
        private void StartGame(object arg)
        {
            int boardHeight, boardWidth;

            if (CurrentDifficulty == GameDifficulty.Beginner)
            {
                boardHeight = 10;
                boardWidth  = 10;
            }
            else if (CurrentDifficulty == GameDifficulty.Intermediate)
            {
                boardHeight = 16;
                boardWidth  = 16;
            }
            else
            {
                boardHeight = 16;
                boardWidth  = 30;
            }
            GameBoardCollection = new ObservableCollection <GameTileModel>();
            for (int i = 0; i < boardHeight; i++)
            {
                for (int j = 0; j < boardWidth; j++)
                {
                    GameBoardCollection.Add(new GameTileModel(i, j));
                }
            }

            List <int> tileNumbers = Enumerable.Range(0, (boardHeight * boardWidth - 1)).ToList();
            Random     rand        = new Random();

            for (int i = 0; i < NumberOfMines; i++)
            {
                int index      = rand.Next(0, tileNumbers.Count - 1);
                int randNumber = tileNumbers[index];
                int row        = randNumber / boardWidth;
                int col        = randNumber % boardWidth;
                GameBoardCollection[randNumber] = new GameTileModel(row, col, true, false, false);
                tileNumbers.RemoveAt(index);
            }

            for (int i = 0; i < boardHeight; i++)
            {
                for (int j = 0; j < boardWidth; j++)
                {
                    int index            = (i * boardWidth) + j;
                    int numMineNeighbors = GetNumMineNeighbors(i, j, boardWidth, boardHeight);
                    GameBoardCollection[index].NumMineNeighbors = numMineNeighbors;
                }
            }
            CurrentGameStatus = GameStatus.InProgress;
            CurrentState      = ViewState.Game;
        }
        /// <summary>
        /// Method that enables changes to a game tile, whether it is being selected or flagged
        /// </summary>
        /// <param name="row">Row in game board of tile to be changed</param>
        /// <param name="col">Row in game board of tile to be changed</param>
        /// <param name="isSwitchingFlaggedStatus">Boolean indicating whether the flag status should change</param>
        /// <param name="isMineBeingSelected">Boolean indicating whether the tile has been selected by the user to reveal</param>
        public void AlterGameTile(Guid identifier, bool isSwitchingFlaggedStatus, bool isMineBeingSelected)
        {
            GameTileModel oldTile = null;
            int           row     = -1;
            int           col     = -1;

            for (int i = 0; i < prTileList.Count; i++)
            {
                for (int j = 0; j < prTileList[i].Count; j++)
                {
                    if (prTileList[i][j].TileIdentifier.Equals(identifier))
                    {
                        oldTile = prTileList[i][j];
                        row     = i;
                        col     = j;
                        break;
                    }
                }
                if (oldTile != null)
                {
                    break;
                }
            }

            if (oldTile == null)
            {
                throw new ArgumentNullException("Could not find specified tile");
            }

            bool newTileIsMine     = oldTile.IsMine;
            bool newTileIsFlagged  = isSwitchingFlaggedStatus ^ oldTile.IsFlagged;
            bool newTileIsSelected = oldTile.IsSelected || isMineBeingSelected;

            if ((oldTile.IsFlagged != newTileIsFlagged) || (oldTile.IsSelected != newTileIsSelected))
            {
                var newTile = new GameTileModel(0, 0, newTileIsMine, newTileIsSelected, newTileIsFlagged);
                prTileList[row][col] = newTile;
                int index = (prTileList[0].Count * row) + col;
                logger.Trace("Gametile altered at [%d,%d], index %d\nOld Tile - %s\nNewTile - %s", row, col, index, oldTile.ToString(), newTile.ToString());
            }
        }
Example #6
0
        private void SelectNeighborsWithNoNeighboringMines(GameTileModel newestSelectedTile)
        {
            int row         = newestSelectedTile.Row;
            int col         = newestSelectedTile.Col;
            int boardWidth  = 0;
            int boardHeight = 0;

            switch (CurrentDifficulty)
            {
            case GameDifficulty.Beginner:
                boardWidth  = 10;
                boardHeight = 10;
                break;

            case GameDifficulty.Intermediate:
                boardWidth  = 16;
                boardHeight = 16;
                break;

            case GameDifficulty.Expert:
                boardWidth  = 30;
                boardHeight = 16;
                break;
            }

            if (row - 1 >= 0 && col - 1 >= 0)
            {
                int upperLeftIndex = ((row - 1) * boardWidth) + col - 1;
                SelectTile(GameBoardCollection[upperLeftIndex]);
            }
            if (row - 1 >= 0)
            {
                int upperIndex = ((row - 1) * boardWidth) + col;
                SelectTile(GameBoardCollection[upperIndex]);
            }
            if (row - 1 >= 0 && col + 1 < boardWidth)
            {
                int upperRightIndex = ((row - 1) * boardWidth) + col + 1;
                SelectTile(GameBoardCollection[upperRightIndex]);
            }
            if (col - 1 >= 0)
            {
                int leftIndex = (row * boardWidth) + col - 1;
                SelectTile(GameBoardCollection[leftIndex]);
            }
            if (col + 1 < boardWidth)
            {
                int rightIndex = (row * boardWidth) + col + 1;
                SelectTile(GameBoardCollection[rightIndex]);
            }
            if (row + 1 < boardHeight && col - 1 >= 0)
            {
                int lowerLeftIndex = ((row + 1) * boardWidth) + col - 1;
                SelectTile(GameBoardCollection[lowerLeftIndex]);
            }
            if (row + 1 < boardHeight)
            {
                int lowerIndex = ((row + 1) * boardWidth) + col;
                SelectTile(GameBoardCollection[lowerIndex]);
            }
            if (row + 1 < boardHeight && col + 1 < boardWidth)
            {
                int lowerRightIndex = ((row + 1) * boardWidth) + col + 1;
                SelectTile(GameBoardCollection[lowerRightIndex]);
            }
        }