예제 #1
0
        public List <Cell> MineLocations(int gridSize)
        {
            var generatedMineList = new List <Cell>();
            var gameGrid          = GridFactory.NewGameGrid(gridSize);

            for (var row = 0; row < gameGrid.Size; row++)
            {
                for (var column = 0; column < gameGrid.Size; column++)
                {
                    generatedMineList.Add(gameGrid.GeneratedGameCell[row, column]);
                    gameGrid.GeneratedGameCell[row, column].IsMine = true;
                }
            }

            var selectedMineLocations = new List <Cell>();

            for (var cell = 0; cell < gameGrid.Size; cell++)
            {
                var rnd        = new Random();
                var randomMine = generatedMineList.Count;
                var mine       = rnd.Next(randomMine);
                selectedMineLocations.Add(generatedMineList[mine]);
                generatedMineList.Remove(generatedMineList[mine]);
            }

            return(selectedMineLocations);
        }
예제 #2
0
        public void GenerateABoardOfTheCorrectSize(int size, int expected)
        {
            //Arrange

            //Act
            var result = GridFactory.NewGameGrid(size);

            //Assert
            Assert.Equal(expected, result.Size);
        }
예제 #3
0
        public void DisplayABoardWithTheCorrectDimensions(int size, string expected)
        {
            //Arrange
            var newDisplay = GridFactory.NewDisplayGrid();
            var gameGrid   = GridFactory.NewGameGrid(size);

            //Act
            var result = newDisplay.GenerateGameDisplay(gameGrid);

            //Assert
            Assert.Equal(expected, result);
        }
예제 #4
0
        public void UpdateTheStatusOfACellToRecordAMineAsFalse()
        {
            //Arrange
            var gridSize         = 2;
            var newGame          = GridFactory.NewGameGrid(gridSize);
            var updateMineStatus = new MineUpdater();
            var mineStub         = new StubForMineLocationZeroZero();

            //Act
            updateMineStatus.UpdateCellWithMineStatus(mineStub.MineLocations(newGame.Size), newGame);

            //Assert
            Assert.False(newGame.GeneratedGameCell[1, 1].IsMine);
        }
예제 #5
0
        public void ReturnTwoAdjacentMinesOnASizeTwoGridAfterAPlayerMove()
        {
            //Arrange
            var newGameGrid      = GridFactory.NewGameGrid(2);
            var updateMineStatus = new MineUpdater();
            var mineStub         = new StubForTwoMineLocations();
            var userInputMove    = new PlayerMove(1, 1);

            //Act
            updateMineStatus.UpdateCellWithMineStatus(mineStub.MineLocations(newGameGrid.Size), newGameGrid);
            var result = updateMineStatus.CalculateAdjacentMineTotal(newGameGrid, userInputMove);

            //Assert
            Assert.Equal(2, result);
        }
예제 #6
0
        public void ReturnTrueIfPlayerHasSelectedAMine()
        {
            //Arrange
            var size             = 2;
            var newValidation    = Factory.NewUserInputValidation();
            var gameGrid         = GridFactory.NewGameGrid(size);
            var userInput        = new PlayerMove(0, 0);
            var updateMineStatus = new MineUpdater();
            var mineStub         = new StubForMineLocationZeroZero();

            //Act
            updateMineStatus.UpdateCellWithMineStatus(mineStub.MineLocations(gameGrid.Size), gameGrid);

            //Assert
            Assert.True(newValidation.IsGameOver(gameGrid, userInput));
        }
예제 #7
0
        public void DisplayBoardWithGeneratedMinesCorrectly()
        {
            //Arrange
            var gridSize      = 2;
            var newGrid       = GridFactory.NewGameGrid(gridSize);
            var gameDisplay   = GridFactory.NewDisplayGrid();
            var mineLogic     = MineFactory.NewMineChecker();
            var mineLocations = new StubForTwoMineLocations();

            mineLogic.UpdateCellWithMineStatus(mineLocations.MineLocations(gridSize), newGrid);

            //Act

            var result = gameDisplay.GenerateGameDisplay(newGrid);

            //Assert
            Assert.Equal(". + \n+ . \n", result);
        }
예제 #8
0
        public void ShowTheCorrectGameWinningMessageWhenAPlayerCompletesAGridWithoutHittingAMine()
        {
            //Arrange
            var gridSize          = 2;
            var newMessageDisplay = Factory.NewMessageDisplay();
            var gameGrid          = GridFactory.NewGameGrid(gridSize);
            var gameCellUpdater   = Factory.NewCellUpdater();
            var mineUpdater       = MineFactory.NewMineChecker();
            var mineStub          = new StubForTwoMineLocations();
            var userMove          = new PlayerMove(0, 1);
            var expected          =
                $"* 2 {Environment.NewLine}* 2 {Environment.NewLine}Congrats!{Environment.NewLine}You have won!";

            //Act
            mineUpdater.UpdateCellWithMineStatus(mineStub.MineLocations(gridSize), gameGrid);
            gameCellUpdater.UpdateAdjacentMineTotalAtGameStart(gameGrid);
            var result = newMessageDisplay.EndGameMessage(gameGrid, userMove);

            //Assert
            Assert.Equal(expected, result);
        }
예제 #9
0
        public void ShowTheCorrectGameLosingMessageWhenAPlayerHitsAMine()
        {
            //Arrange
            var gridSize          = 2;
            var newMessageDisplay = Factory.NewMessageDisplay();
            var gameGrid          = GridFactory.NewGameGrid(gridSize);
            var gameCellUpdater   = Factory.NewCellUpdater();
            var mineUpdater       = MineFactory.NewMineChecker();
            var mineStub          = new StubForTwoMineLocations();
            var userMove          = new PlayerMove(0, 0);
            var expected          =
                $"* 2 {Environment.NewLine}* 2 {Environment.NewLine}Sorry, you have lost.{Environment.NewLine}Game over!";

            //Act
            mineUpdater.UpdateCellWithMineStatus(mineStub.MineLocations(gridSize), gameGrid);
            gameCellUpdater.UpdateAdjacentMineTotalAtGameStart(gameGrid);
            var result = newMessageDisplay.EndGameMessage(gameGrid, userMove);

            //Assert
            Assert.Equal(expected, result);
        }
예제 #10
0
        public void NewGame()
        {
            //For the game, [0,0] is located in the top left corner, with the largest row/column being bottom right.
            //Player move is always entered as Row then Column.

            var gridSize        = GetGridSize();
            var currentGameGrid = GridFactory.NewGameGrid(gridSize);

            var userInputMove   = new PlayerMove(0, 0);
            var maxNonMineCells = gridSize * gridSize - gridSize;
            var turnCount       = 0;


            while (!_userInputValidation.IsGameOver(currentGameGrid, userInputMove) && turnCount < maxNonMineCells)
            {
                var runAtGameStart = turnCount == 0;
                userInputMove = RunGame(userInputMove, currentGameGrid, runAtGameStart);
                turnCount++;
            }

            Console.Clear();
            Console.WriteLine(_gameMessageDisplay.EndGameMessage(currentGameGrid, userInputMove));
        }