Beispiel #1
0
        private PlayerMove RunGame(PlayerMove userInputMove, IGameGrid currentGameGrid, bool runAtGameStart)
        {
            var cellUpdater      = Factory.NewCellUpdater();
            var mineUpdater      = MineFactory.NewMineChecker();
            var mineGeneration   = MineFactory.NewMineLocations();
            var gameGridDisplay  = GridFactory.NewDisplayGrid();
            var convertUserInput = Factory.NewUserInputConverter();

            if (runAtGameStart)
            {
                mineUpdater.UpdateCellWithMineStatus(mineGeneration.MineLocations(currentGameGrid.Size), currentGameGrid);
                cellUpdater.UpdateAdjacentMineTotalAtGameStart(currentGameGrid);
            }

            do
            {
                Console.Clear();
                Console.WriteLine(gameGridDisplay.GenerateGameDisplay(currentGameGrid));

                var inputMove = _gameMessageDisplay.ShowUserInputMessage(currentGameGrid.Size);

                userInputMove = convertUserInput.ConvertInputToUserMove(inputMove);
            } while (_userInputValidation.IsCellRevealed(currentGameGrid, userInputMove));

            cellUpdater.UpdateDisplayStatusAfterUserMove(userInputMove, currentGameGrid);

            return(userInputMove);
        }
Beispiel #2
0
        public GameAreaViewModel(IGameGrid gameGrid)
        {
            SetFlagCommand      = new DelegateCommand <GridCell>(OnSetFlag);
            NodeSelectedCommand = new DelegateCommand <GridCell>(OnNodeSelected);
            GameGrid            = gameGrid;

            GenerateGrid();
        }
 public GameGridController(IGameGrid gameGrid)
 {
     _gameGrid = gameGrid;
     _collisionDetector = (ICollisionDetector)new CollisionDetector();
     _fallenTiles = (IFallenTiles)new FallenTiles();
     _gameTimer = new GameTimer(Level.Easy);
     _gameTimer.Tick += new EventHandler(TetrisTick);
     _scoreHolder = ScoreHolder.GetScoreHolder();
 }
Beispiel #4
0
        public string EndGameMessage(IGameGrid currentGameGrid, PlayerMove userInputMove)
        {
            var gameGridDisplay  = GridFactory.NewDisplayGrid();
            var revealedGameGrid = gameGridDisplay.GameOverGridDisplay(currentGameGrid);

            var message = currentGameGrid.GeneratedGameCell[userInputMove.Row, userInputMove.Column]
                          .IsMine
                    ? $"Sorry, you have lost.{Environment.NewLine}Game over!"
                    : $"Congrats!{Environment.NewLine}You have won!";

            return(revealedGameGrid + message);
        }
Beispiel #5
0
 public void UpdateCellWithMineStatus(List <Cell> mineLocations, IGameGrid gameGrid)
 {
     for (var row = 0; row < gameGrid.Size; row++)
     {
         for (var column = 0; column < gameGrid.Size; column++)
         {
             if (mineLocations.Contains(gameGrid.GeneratedGameCell[row, column]))
             {
                 gameGrid.GeneratedGameCell[row, column].IsMine = true;
             }
         }
     }
 }
Beispiel #6
0
        public void UpdateAdjacentMineTotalAtGameStart(IGameGrid currentGameGrid)
        {
            for (var row = 0; row < currentGameGrid.Size; row++)
            {
                for (var column = 0; column < currentGameGrid.Size; column++)
                {
                    var selectedCell = new PlayerMove(row, column);

                    if (!currentGameGrid.GeneratedGameCell[row, column].IsMine)
                    {
                        currentGameGrid.GeneratedGameCell[row, column].AdjacentMinesTotal =
                            _mineUpdater.CalculateAdjacentMineTotal(currentGameGrid, selectedCell);
                    }
                }
            }
        }
Beispiel #7
0
        public string GameOverGridDisplay(IGameGrid initialGameGrid)
        {
            var outputGrid   = "";
            var revealedMine = "* ";

            for (var row = 0; row < initialGameGrid.Size; row++)
            {
                for (var column = 0; column < initialGameGrid.Size; column++)
                {
                    outputGrid += initialGameGrid.GeneratedGameCell[row, column].IsMine
                        ? revealedMine
                        : initialGameGrid.GeneratedGameCell[row, column].AdjacentMinesTotal + _blankSpace;
                }
                outputGrid += Environment.NewLine;
            }

            return(outputGrid);
        }
Beispiel #8
0
        public int CalculateAdjacentMineTotal(IGameGrid gameGrid, PlayerMove playerMove)
        {
            var adjacentMinesOutput = 0;
            var coordinateVariables = new List <int> {
                -1, 0, 1
            };

            foreach (var rowVariable in coordinateVariables)
            {
                foreach (var columnVariable in coordinateVariables)
                {
                    var row    = playerMove.Row + rowVariable;
                    var column = playerMove.Column + columnVariable;

                    if (GreaterThanLowerGridBoundary(row, column) && LesserThanUpperGridBoundary(row, column, gameGrid) &&
                        gameGrid.GeneratedGameCell[row, column].IsMine)
                    {
                        adjacentMinesOutput += 1;
                    }
                }
            }

            return(adjacentMinesOutput);
        }
Beispiel #9
0
        public string GenerateGameDisplay(IGameGrid initialGameGrid)
        {
            var outputGrid          = string.Empty;
            var demonstrationStatus = true;
            var demonstrationMine   = "+ ";
            var unrevealedCell      = ". ";

            for (var row = 0; row < initialGameGrid.Size; row++)
            {
                for (var column = 0; column < initialGameGrid.Size; column++)
                {
                    outputGrid += initialGameGrid.GeneratedGameCell[row, column].DisplayStatus switch
                    {
                        CellDisplayStatus.NotRevealed when initialGameGrid.GeneratedGameCell[row, column].IsMine =>
                        demonstrationStatus ? demonstrationMine : unrevealedCell,
                                                                           CellDisplayStatus.Revealed when !initialGameGrid.GeneratedGameCell[row, column].IsMine =>
                        initialGameGrid.GeneratedGameCell[row, column].AdjacentMinesTotal + _blankSpace,
                                                                           _ => unrevealedCell
                    };
                }
                outputGrid += Environment.NewLine;
            }
            return(outputGrid);
        }
Beispiel #10
0
 public bool IsCellRevealed(IGameGrid gameGrid, PlayerMove userInput)
 {
     return(gameGrid.GeneratedGameCell[userInput.Row, userInput.Column].DisplayStatus == CellDisplayStatus.Revealed);
 }
Beispiel #11
0
 public bool IsGameOver(IGameGrid gameGrid, PlayerMove userInput)
 {
     return(gameGrid.GeneratedGameCell[userInput.Row, userInput.Column].IsMine);
 }
Beispiel #12
0
 public void UpdateDisplayStatusAfterUserMove(PlayerMove userInputMove, IGameGrid currentGameGrid)
 {
     currentGameGrid.GeneratedGameCell[userInputMove.Row, userInputMove.Column].DisplayStatus =
         CellDisplayStatus.Revealed;
 }
Beispiel #13
0
 private bool LesserThanUpperGridBoundary(int row, int column, IGameGrid gameGrid)
 {
     return(row < gameGrid.Size && column < gameGrid.Size);
 }