public void TryParse_InvalidCol_NotNumber()
 {
     string input = "4 z";
     Coordinates coordinates = new Coordinates();
     bool result = Coordinates.TryParse(input, ref coordinates);
     Assert.IsFalse(result);
 }
 public void TryParse_InvalidRow_ParsedFailed()
 {
     string input = "5 4";
     Coordinates coordinates = new Coordinates();
     bool result = Coordinates.TryParse(input, ref coordinates);
     Assert.IsFalse(result);
 }
 public void TryParse_InputCountIsDifferentFromTwo_ParsedFailed()
 {
     string input = "3    4";
     Coordinates coordinates = new Coordinates();
     bool result = Coordinates.TryParse(input, ref coordinates);
     Assert.IsFalse(result);
 }
 public void TryParse_InputCountIsTwo_Parsed()
 {
     string input = "3 4";
     Coordinates coordinates = new Coordinates();
     bool result = Coordinates.TryParse(input, ref coordinates);
     Assert.IsTrue(result);
 }
 public void Row_PropertieGet()
 {
     string input = "3 0";
     Coordinates coordinates = new Coordinates();
     Coordinates.TryParse(input, ref coordinates);
     Assert.AreEqual(3, coordinates.Row);
 }
        private static bool ReadInput(out bool IsCoordinates, ref Coordinates coordinates, ref Command command)
        {
            Console.Write("Enter a row and column: ");
            string consoleInput = Console.ReadLine();

            coordinates = new Coordinates();
            command = new Command();

            if (Command.TryParse(consoleInput, ref command))
            {
                IsCoordinates = false;
                return true;
            }
            else if (Coordinates.TryParse(consoleInput, ref coordinates))
            {
                IsCoordinates = true;
                return true;
            }

            else
            {
                IsCoordinates = false;
                return false;
            }
        }
 public void RemainingBaloons_OneShot()
 {
     GameBoardManager gameBoardManager = new GameBoardManager();
     gameBoardManager.GenerateNewGameBoard();
     string input = "0 0";
     Coordinates coordinates = new Coordinates();
     Coordinates.TryParse(input, ref coordinates);
     gameBoardManager.ShootBaloons(coordinates);
     Assert.IsTrue(gameBoardManager.RemainingBaloons < 50);
 }
        public void ShootBaloons(Coordinates currentCoordinates)
        {
            char currentBaloon;
            currentBaloon = GetBaloonColor(currentCoordinates);
            Coordinates neighborCoordinates = new Coordinates();

            if (currentBaloon < '1' || currentBaloon > '4')
            {
                throw new BaloonsPopExceptions.PopedBallonException("Illegal move: cannot pop missing ballon!");
                return;
            }

            AddNewBaloonToGameBoard(currentCoordinates, '.');
            baloonCounter--;

            neighborCoordinates.Col = currentCoordinates.Col - 1;
            neighborCoordinates.Row = currentCoordinates.Row;

            while (currentBaloon == GetBaloonColor(neighborCoordinates))
            {
                AddNewBaloonToGameBoard(neighborCoordinates, '.');
                baloonCounter--;
                neighborCoordinates.Col--;
            }

            neighborCoordinates.Col = currentCoordinates.Col + 1; neighborCoordinates.Row = currentCoordinates.Row;
            while (currentBaloon == GetBaloonColor(neighborCoordinates))
            {
                AddNewBaloonToGameBoard(neighborCoordinates, '.');
                baloonCounter--;
                neighborCoordinates.Col++;
            }

            neighborCoordinates.Col = currentCoordinates.Col;
            neighborCoordinates.Row = currentCoordinates.Row - 1;
            while (currentBaloon == GetBaloonColor(neighborCoordinates))
            {

                AddNewBaloonToGameBoard(neighborCoordinates, '.');
                baloonCounter--;
                neighborCoordinates.Row--;
            }

            neighborCoordinates.Col = currentCoordinates.Col;
            neighborCoordinates.Row = currentCoordinates.Row + 1;
            while (currentBaloon == GetBaloonColor(neighborCoordinates))
            {
                AddNewBaloonToGameBoard(neighborCoordinates, '.');
                baloonCounter--;
                neighborCoordinates.Row++;
            }

            shootCounter++;
            LandFlyingBaloons();
        }
Beispiel #9
0
        /// <summary>
        /// Run method start and control game logic.
        /// </summary>
        /// <param name="gameBoardManager">Take instance of GameBoardManager from the main method.</param>
        public void Run(GameBoardManager gameBoardManager)
        {
            this.gameBoardManager = gameBoardManager;
            bool isCoordinates;
            bool isCommand;
            Coordinates coordinates = new Coordinates();
            Command command = new Command();
            //TopScore topScore = new TopScore();
            TopScore.Instance.OpenTopScoreList();

            while (this.gameBoardManager.RemainingBaloons > 0)
            {
                Console.Write("Enter a row and column: ");
                string consoleInput = Console.ReadLine();

                isCoordinates = Coordinates.TryParse(consoleInput, ref coordinates);
                isCommand = Command.TryParse(consoleInput, ref command);

                if (isCoordinates)
                {
                    try
                    {
                        this.gameBoardManager.ShootBaloons(coordinates);
                    }
                    catch (PopedBallonException exp)
                    {
                        Console.WriteLine(exp.Message);
                    }

                    this.gameBoardManager.PrintGameBoard();
                }
                else if (isCommand)
                {
                    switch (command.Value)
                    {
                        case CommandTypes.top:
                            TopScore.Instance.PrintScoreList();
                            break;
                        case CommandTypes.restart:
                            this.gameBoardManager.GenerateNewGameBoard();
                            this.gameBoardManager.PrintGameBoard();
                            break;
                        case CommandTypes.exit:
                            return;
                    }
                }
                else
                {
                    Console.WriteLine("The input isn't in correct format!");
                }
            }

            this.CheckTopScore();
        }
 public void ShootBaloons_CoordinatesTwoOne()
 {
     GameBoardManager gameBoardManager = new GameBoardManager();
     gameBoardManager.GenerateNewGameBoard();
     string input = "2 1";
     Coordinates coordinates = new Coordinates();
     Coordinates.TryParse(input, ref coordinates);
     gameBoardManager.ShootBaloons(coordinates);
     char arr = gameBoardManager.GameBoard[6, 2];
     char result = '.';
     Assert.AreEqual(result, arr);
 }
        public static bool TryParse(string input, ref Coordinates result)
        {
            char[] separators = { ' ', ',' };
            string[] substrings = input.Split(separators);

            if (substrings.Count<string>() != 2)
            {
                Console.WriteLine("Invalid move or command!");
                return false;
            }

            string coordinateRow = substrings[0].Trim();
            int row;
            if (int.TryParse(coordinateRow, out row))
            {
                if (row >= 0 && row <= 4)
                {
                    result.Row = row;
                }
                else
                {
                    Console.WriteLine("Wrong row coordinate");
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Invalid move or command!");
                return false;
            }

            string coordinateCol = substrings[1].Trim();
            int col;
            if (int.TryParse(coordinateCol, out col))
            {
                if (col >= 0 && col <= 9)
                {
                    result.Col = col;
                }
                else
                {
                    Console.WriteLine("Wrong column coordinate");
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Invalid move or command!");
                return false;
            }

            return true;
        }
        /// <summary>
        /// Tries to parse the input.
        /// </summary>
        /// <param name="input">String that should be parsed.</param>
        /// <param name="result">Coordinates that should be set a value.</param>
        /// <returns>Boolean value that shows is the parse successful.</returns>
        public static bool TryParse(string input, ref Coordinates result)
        {
            char[] separators = { ' ', ',' };
            string[] substrings = input.Split(separators);

            if (substrings.Count<string>() != 2)
            {
                return false;
            }

            string coordinateRow = substrings[0].Trim();
            int row;
            if (int.TryParse(coordinateRow, out row))
            {
                if (row >= 0 && row <= MaxRows)
                {
                    result.Row = row;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

            string coordinateCol = substrings[1].Trim();
            int col;
            if (int.TryParse(coordinateCol, out col))
            {
                if (col >= 0 && col <= MaxCols)
                {
                    result.Col = col;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

            return true;
        }
Beispiel #13
0
        public void GenerateNewGameBoard()
        {
            FillBlankGameBoard();

            Random randomGenerator = new Random();
            Coordinates coordinates = new Coordinates();
            for (int col = 0; col < 10; col++)
            {
                for (int row = 0; row < 5; row++)
                {
                    coordinates.Col = col;
                    coordinates.Row = row;
                    char ballonColor = (char)(randomGenerator.Next(1, 5) + (int)'0');
                    AddNewBaloonToGameBoard(coordinates, ballonColor);
                }
            }
        }
Beispiel #14
0
 private void AddNewBaloonToGameBoard(Coordinates coordinates, char ballonColor)
 {
     int xPosition, yPosition;
     xPosition = 4 + coordinates.Col * 2;
     yPosition = 2 + coordinates.Row;
     gameBoard[xPosition, yPosition] = ballonColor;
 }
Beispiel #15
0
 private void Swap(Coordinates c, Coordinates c1)
 {
     char tmp = GetBaloonColor(c);
     AddNewBaloonToGameBoard(c, GetBaloonColor(c1));
     AddNewBaloonToGameBoard(c1, tmp);
 }
        static void Main()
        {
            Console.WriteLine("Welcome to “Balloons Pops” game. Please try to pop the balloons. Use 'top' to view the top scoreboard, 'restart' to start a new game and 'exit' to quit the game.");
            
            GameBoard gameBoard = new GameBoard();
            gameBoard.GenerateNewGameBoard();

            Console.WriteLine(gameBoard.ToString());

            TopScore.OpenTopScoreList();
            
            bool isCoordinates;
            Coordinates coordinates = new Coordinates();
            Command command = new Command();

            while (gameBoard.RemainingBaloons > 0)
            {
                if (ReadInput(out isCoordinates, ref coordinates, ref command))
                {
                    if (isCoordinates)
                    {
                        try
                        {
                            gameBoard.ShootBaloons(coordinates);
                        }
                        catch (PopedBallonException exp)
                        {
                            Console.WriteLine(exp.Message);
                        }
                        
                        Console.WriteLine(gameBoard.ToString());
                    }
                    else
                    {
                        switch (command.Value)
                        {
                            case "top":
                                {
                                    TopScore.PrintScoreList();
                                }
                                break;
                            case "restart":
                                {
                                    gameBoard.GenerateNewGameBoard();
                                    Console.WriteLine(gameBoard.ToString());
                                }
                                break;
                            case "exit":
                                {
                                    return;
                                }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Wrong Input!");
                }
            }

            Player player = new Player();
            player.Score = gameBoard.ShootCounter;

            if (TopScore.IsTopScore(player))
            {
                Console.WriteLine("Please enter your name for the top scoreboard: ");
                player.Name = Console.ReadLine();
                TopScore.AddToTopScoreList(player);
            }
            TopScore.SaveTopScoreList();
            TopScore.PrintScoreList();
        }
        /// <summary>
        /// Method for count and mark hit balloons
        /// </summary>
        /// <param name="currentCoordinates">Position of the user hit.</param>
        public void ShootBaloons(Coordinates currentCoordinates)
        {
            char currentBaloon;
            currentBaloon = this.GetBaloonColor(currentCoordinates);

            if (currentBaloon < '1' || currentBaloon > '4')
            {
                throw new BaloonsPopExceptions.PopedBallonException("Cannot pop missing ballon!");
            }

            this.AddNewBaloonToGameBoard(currentCoordinates, '.');
            this.baloonCounter--;

            this.PopLeftAndRightNeighbor(currentCoordinates, currentBaloon);

            this.PopUpAndDownNeighbors(currentCoordinates, currentBaloon);

            this.shootCounter++;
            this.LandFlyingBaloons();
        }
        /// <summary>
        /// Method for count and mark hit vertical balloons 
        /// </summary>
        /// <param name="currentCoordinates">Position of the user hit.</param>
        /// <param name="currentBaloon">Balloon color of user hit.</param>
        private void PopLeftAndRightNeighbor(Coordinates currentCoordinates, char currentBaloon)
        {
            Coordinates neighborCoordinates = new Coordinates();

            neighborCoordinates.Col = currentCoordinates.Col - 1;
            neighborCoordinates.Row = currentCoordinates.Row;
            while (currentBaloon == this.GetBaloonColor(neighborCoordinates))
            {
                this.AddNewBaloonToGameBoard(neighborCoordinates, '.');
                this.baloonCounter--;
                neighborCoordinates.Col--;
            }

            neighborCoordinates.Col = currentCoordinates.Col + 1;
            neighborCoordinates.Row = currentCoordinates.Row;
            while (currentBaloon == this.GetBaloonColor(neighborCoordinates))
            {
                this.AddNewBaloonToGameBoard(neighborCoordinates, '.');
                this.baloonCounter--;
                neighborCoordinates.Col++;
            }
        }
        /// <summary>
        /// Method for count and mark hit horizontal balloons 
        /// </summary>
        /// <param name="currentCoordinates">Position of the user hit.</param>
        /// <param name="currentBaloon">Balloon color of user hit.</param>
        private void PopUpAndDownNeighbors(Coordinates currentCoordinates, char currentBaloon)
        {
            Coordinates neighborCoordinates = new Coordinates();

            neighborCoordinates.Col = currentCoordinates.Col;
            neighborCoordinates.Row = currentCoordinates.Row - 1;
            while (currentBaloon == this.GetBaloonColor(neighborCoordinates))
            {
                this.AddNewBaloonToGameBoard(neighborCoordinates, '.');
                this.baloonCounter--;
                neighborCoordinates.Row--;
            }

            neighborCoordinates.Col = currentCoordinates.Col;
            neighborCoordinates.Row = currentCoordinates.Row + 1;
            while (currentBaloon == this.GetBaloonColor(neighborCoordinates))
            {
                this.AddNewBaloonToGameBoard(neighborCoordinates, '.');
                this.baloonCounter--;
                neighborCoordinates.Row++;
            }
        }
 public void ShootCounter_ThreeShoots()
 {
     GameBoardManager gameBoardManager = new GameBoardManager();
     gameBoardManager.GenerateNewGameBoard();
     string input = "0 0";
     Coordinates coordinates = new Coordinates();
     Coordinates.TryParse(input, ref coordinates);
     gameBoardManager.ShootBaloons(coordinates);
     input = "1 1";
     Coordinates.TryParse(input, ref coordinates);
     gameBoardManager.ShootBaloons(coordinates);
     input = "2 2";
     Coordinates.TryParse(input, ref coordinates);
     gameBoardManager.ShootBaloons(coordinates);
     Assert.AreEqual(3, gameBoardManager.ShootCounter);
 }
 public void ShootBaloons_HitPosition()
 {
     GameBoardManager gameBoardManager = new GameBoardManager();
     gameBoardManager.GenerateNewGameBoard();
     string input = "2 1";
     Coordinates coordinates = new Coordinates();
     Coordinates.TryParse(input, ref coordinates);
     gameBoardManager.ShootBaloons(coordinates);
     input = "0 1";
     Coordinates.TryParse(input, ref coordinates);
     gameBoardManager.ShootBaloons(coordinates);
 }
Beispiel #22
0
 private void SwapBaloonsPosition(Coordinates currentCoordinates, Coordinates newCoordinates)
 {
     char currentBallon = GetBaloonColor(currentCoordinates);
     AddNewBaloonToGameBoard(currentCoordinates, GetBaloonColor(newCoordinates));
     AddNewBaloonToGameBoard(newCoordinates, currentBallon);
 }
Beispiel #23
0
 private void LandFlyingBaloons()
 {
     Coordinates currentCoordinates = new Coordinates();
     for (int col = 0; col < 10; col++)
     {
         for (int row = 0; row <= 4; row++)
         {
             currentCoordinates.Col = col;
             currentCoordinates.Row = row;
             if (GetBaloonColor(currentCoordinates) == '.')
             {
                 for (int rowIndex = row; rowIndex > 0; rowIndex--)
                 {
                     Coordinates oldCoordinates = new Coordinates();
                     Coordinates newCoordinates = new Coordinates();
                     oldCoordinates.Col = col;
                     oldCoordinates.Row = rowIndex;
                     newCoordinates.Col = col;
                     newCoordinates.Row = rowIndex - 1;
                     SwapBaloonsPosition(oldCoordinates, newCoordinates);
                 }
             }
         }
     }
 }
Beispiel #24
0
        private char GetBaloonColor(Coordinates coordinates)
        {
            int xPosition = 4 + coordinates.Col * 2;
            int yPosition = 2 + coordinates.Row;

            char ballonColor = gameBoard[xPosition, yPosition];
            return ballonColor;
        }
Beispiel #25
0
 private void LandFlyingBaloons()
 {
     Coordinates c = new Coordinates();
     for (int i = 0; i < 10; i++)
     {
         for (int j = 0; j <= 4; j++)
         {
             c.Col = i;
             c.Row = j;
             if (GetBaloonColor(c) == '.')
             {
                 for (int k = j; k > 0; k--)
                 {
                     Coordinates tempCoordinates = new Coordinates();
                     Coordinates tempCoordinates1 = new Coordinates();
                     tempCoordinates.Col = i;
                     tempCoordinates.Row = k;
                     tempCoordinates1.Col = i;
                     tempCoordinates1.Row = k - 1;
                     Swap(tempCoordinates, tempCoordinates1);
                 }
             }
         }
     }
 }