public override int PopBaloons(int row, int col, Playfield playfield)
        {
            if (playfield == null)
            {
                throw new ArgumentNullException("playfield", "Playfield can't be null.");
            }

            bool isRowValid = row >= 0 && row < playfield.Height;
            bool isColValid = col >= 0 && col < playfield.Width;

            if (isRowValid && isColValid)
            {
                string selectedCellValue = playfield.Field[row, col];

                if (selectedCellValue == ".")
                {
                    return 0;
                }
                else
                {
                    return this.PopBaloons(row, col, playfield, selectedCellValue);
                }
            }

            return 0;
        }
        private int PopBaloons(int row, int col, Playfield playfield, string selectedCellValue = null)
        {
            int poppedBaloons = 0;
            bool isRowValid = row >= 0 && row < playfield.Height;
            bool isColValid = col >= 0 && col < playfield.Width;

            if (isRowValid && isColValid)
            {
                if (playfield.Field[row, col] == selectedCellValue)
                {
                    // Pop current cell
                    playfield.Field[row, col] = ".";
                    poppedBaloons += 1;

                    // Up
                    poppedBaloons += this.PopBaloons(row - 1, col, playfield, selectedCellValue);

                    // Down
                    poppedBaloons += this.PopBaloons(row + 1, col, playfield, selectedCellValue);

                    // Left
                    poppedBaloons += this.PopBaloons(row, col + 1, playfield, selectedCellValue);

                    // Right
                    poppedBaloons += this.PopBaloons(row, col - 1, playfield, selectedCellValue);
                }
            }

            return poppedBaloons;
        }
        public void InitialWidthShouldBeFiveForNewInstance()
        {
            Playfield playfield = new Playfield();
            int expected = 5;
            int actual = playfield.Width;

            Assert.AreEqual(expected, actual, "When new instance is created without parameters, the width of the playfield should have value of 5.");
        }
        public void InitialHeightShouldBeTenForNewInstance()
        {
            Playfield playfield = new Playfield();
            int expected = 10;
            int actual = playfield.Height;

            Assert.AreEqual(expected, actual, "When new instance is created without parameters, the height of the playfield should have value of 10.");
        }
Beispiel #5
0
        /// <summary>
        /// This method implements the factory design pattern for instantiating a playfield
        /// with the user desired dimensions.
        /// </summary>
        /// <returns></returns>
        private Playfield InitializePlayfield()
        {
            int playfieldSize = ConsoleIOFacade.ReadPlayfieldSize();
            Playfield playfield = null;

            bool isPlayfieldSizeIncorrect = true;

            while (isPlayfieldSizeIncorrect)
            {
                PlayfieldFactory playfiledFactory = null;

                switch (playfieldSize)
                {
                    case 1:
                        playfiledFactory = new SmallPlayfieldFactory();
                        playfield = playfiledFactory.CreatePlayfield();
                        isPlayfieldSizeIncorrect = false;
                        break;
                    case 2:
                        playfiledFactory = new MediumPlayfieldFactory();
                        playfield = playfiledFactory.CreatePlayfield();
                        isPlayfieldSizeIncorrect = false;
                        break;
                    case 3:
                        playfiledFactory = new LargePlayfieldFactory();
                        playfield = playfiledFactory.CreatePlayfield();
                        isPlayfieldSizeIncorrect = false;
                        break;
                    default:
                        // Extract to consoleIO
                        Console.WriteLine("You have entered incorrect field size");
                        break;
                }
            }

            return playfield;
        }
Beispiel #6
0
 /// <summary>
 /// This method requires a playfield and a pop strategy and initialize all the game properties 
 /// to their initial state.
 /// </summary>
 /// <param name="gamePlayfield"></param>
 /// <param name="gamePopStrategy"></param>
 private void InitializeGame(Playfield gamePlayfield, PopStrategy gamePopStrategy)
 {
     this.playfield = gamePlayfield;
     this.popStrategy = gamePopStrategy;
     this.balloonsLeft = gamePlayfield.Width * gamePlayfield.Height;
     this.userMoves = 0;
 }
 public void ZeroWidthShouldThrowException()
 {
     Playfield playfield = new Playfield(10, 0);
 }
 public void ZeroHeightShouldThrowException()
 {
     Playfield playfield = new Playfield(0);
 }
 public void NegativeWidthShouldThrowException()
 {
     Playfield playfield = new Playfield(10, -20);
 }
 public void NegativeHeightShouldThrowException()
 {
     Playfield playfield = new Playfield(-20);
 }