Example #1
0
        readonly int rep = 5; // number of retrys allowed when user allowed an invalid input

        /* places the battleship on board by getting the required input from the user.
         * inputs required are Orientation - Horizontal(H)/Vertical(V)
         * length of the battleship should be between 1 and 5
         * start position of the battleship. A coordinate from the grid. Should be XY.
         * returns length of the battleship
         * */
        public int PlaceBattleShip()
        {
            try
            {
                ui = new ValidateUserInputs();
                ea = new ExitApplication();

                // gets the orientation
                GetBattleshipPlacementType();

                // gets the battleship length
                GetBattleshipLength();

                // get the start position XY coordinates
                GetStartPosition();

                // places the battleship on board with the above 3 inputs
                BoardBattleship();

                return(battleshipLen);
            }
            catch
            {
                throw;
            }
        }
Example #2
0
        static bool IsSunk  = false; // to track if game is finished

        // it takes the input firing shot from the user and diplays hit/miss and continue till game is finished
        public void play(int battleshiplen)
        {
            try
            {
                ui = new ValidateUserInputs();
                int  loop  = 0;
                bool track = false;

                DisplayGrid dg = new DisplayGrid();

                do
                {
                    // get the firingshot
                    GetFiringShot();

                    // process the firing shot
                    track = ProcessFiringShot();

                    // show the grids with updated firing grid with Hit or Miss
                    dg.ShowBoards();

                    Console.WriteLine(message);
                    if (track) // if its a hit
                    {
                        // check if game finished
                        if (TrackHitShot(battleshiplen))
                        {
                            Console.WriteLine(message);
                            Console.WriteLine("Game Over !!!");

                            IsSunk = true; // game finished
                        }
                    }

                    loop++;
                } while (!IsSunk); // repeat till game is finished
            }
            catch
            {
                throw;
            }
        }