示例#1
0
        // boards the Battleship on board with the 3 user inputs
        public void BoardBattleship()
        {
            try
            {
                GetCoordinates co = new GetCoordinates();
                int            x  = 0;
                int            y  = 0;

                // get the X and Y coordinates from the start position
                co.GetXY(startPosition, out x, out y);

                if (placementType == 'H') // if horizontal orientation
                {
                    // places the battleship horizontally
                    for (int col = y; col < y + battleshipLen; col++)
                    {
                        DisplayGrid.ownGrid[x, col] = 'B';
                    }
                }
                else if (placementType == 'V') // if vertical orientation
                {
                    // places the battleship vertically
                    for (int row = x; row < x + battleshipLen; row++)
                    {
                        DisplayGrid.ownGrid[row, y] = 'B';
                    }
                }
                Console.WriteLine("Battleship placed Successfully.");
            }
            catch
            {
                throw;
            }
        }
示例#2
0
        /* Validates if the Battleship can be placed at the position entered by the user
         * it should fit from the start position towards right or bottom. shouldn't exceed the grid
         * returns true if a valid position else returns false
         * */
        public bool ValidatePlacement(string startPosition, char placementType, int battleshipLen)
        {
            try
            {
                startPosition = startPosition.ToUpper();
                // replacing column 10 with X to maintain a single char column name
                startPosition = startPosition[0] + startPosition.Substring(1, startPosition.Length - 1).Replace("10", "X");

                // any position on grid is only 2 char length XY, so return false if length > 2
                if (startPosition.Length > 2)
                {
                    return(false);
                }

                GetCoordinates co = new GetCoordinates();
                int            x  = 0;
                int            y  = 0;

                // get the X and Y coordinates
                co.GetXY(startPosition, out x, out y);

                int pos = 0;

                if (placementType == 'H') // if orientation is horizontal
                {
                    pos = y;
                }
                else if (placementType == 'V') // if orientation is vertical
                {
                    pos = x;
                }

                if (pos + battleshipLen <= 11) // check if the battleship is not exceeding the grid length
                {
                    success = true;
                }
                else
                {
                    success = false;
                }

                return(success);
            }
            catch
            {
                throw;
            }
        }
示例#3
0
        // process the firing shot and retuns a hit or miss.
        // updates the firing grid with H/M accordingly
        public bool ProcessFiringShot()
        {
            try
            {
                GetCoordinates co = new GetCoordinates();
                int            x  = 0;
                int            y  = 0;

                // get the X and Y coordinates
                co.GetXY(firingPosition, out x, out y);

                if (DisplayGrid.firingGrid[x, y] != '0') // check if that coordinate is already fired earlier.
                {
                    message = "Already shot at this position";
                    return(false);
                }
                else
                {
                    // check if the battleship is placed at that position
                    if (DisplayGrid.ownGrid[x, y] == 'B') // if its a hit update firing grid position with h
                    {
                        message = "It's a HIT";
                        DisplayGrid.firingGrid[x, y] = 'H';
                        hitCount++;

                        return(true);
                    }
                    else // if its a miss update firing grid position with m
                    {
                        message = "It's a MISS";
                        DisplayGrid.firingGrid[x, y] = 'M';

                        return(false);
                    }
                }
            }
            catch
            {
                throw;
            }
        }