Пример #1
0
        /// <summary>
        /// Places the ship going up.
        /// </summary>
        /// <param name="startPosition">Start position.</param>
        /// <param name="ship">Ship.</param>
        /// <param name="row">Row.</param>
        /// <param name="col">Col.</param>
        private void placeShipGoingUp(int startPosition, Ship ship, int row, int col)
        {
            for (int i = startPosition; i >= (startPosition - ((ship.getSize() - 1) * this.xSize)); i = i - xSize)
            {
                values[i] = ship.getMarker();

                if (avoidAdjacentShips)
                {
                    //Mark area to left of ship
                    if (col != 0)
                    {
                        values[i - 1] = '!';
                    }

                    //Mark area to right of ship
                    if (col != this.xSize - 1)
                    {
                        values[i + 1] = '!';
                    }
                }
            }

            if (avoidAdjacentShips)
            {
                //Mark area below ship (start position is at bottom of ship)
                if (row < this.ySize - 1)
                {
                    values[startPosition + this.xSize] = '!';
                }

                //Mark area above top of ship
                if (row - ship.getSize() > 0)
                {
                    values[startPosition - (ship.getSize() * this.xSize)] = '!';
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Places the ship going down.
        /// </summary>
        /// <param name="startPosition">Start position.</param>
        /// <param name="ship">Ship.</param>
        /// <param name="row">Row.</param>
        /// <param name="col">Col.</param>
        private void placeShipGoingDown(int startPosition, Ship ship, int row, int col)
        {
            for (int i = startPosition; i <= (startPosition + ((ship.getSize() - 1) * this.xSize)); i = i + xSize)
            {
                values[i] = ship.getMarker();

                if (avoidAdjacentShips)
                {
                    //Mark area to left of ship
                    if (col != 0)
                    {
                        values[i - 1] = '!';
                    }

                    //Mark area to right of ship
                    if (col != this.xSize - 1)
                    {
                        values[i + 1] = '!';
                    }
                }
            }

            if (avoidAdjacentShips)
            {
                //Mark area above ship
                if (row > 0)
                {
                    values[startPosition - this.xSize] = '!';
                }

                //Mark area below ship
                if (row != this.ySize - 1)
                {
                    values[startPosition + (ship.getSize() * this.xSize)] = '!';
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Places the ship going left.
        /// </summary>
        /// <param name="startPosition">Start position.</param>
        /// <param name="ship">Ship.</param>
        /// <param name="row">Row.</param>
        /// <param name="col">Col.</param>
        private void placeShipGoingLeft(int startPosition, Ship ship, int row, int col)
        {
            for (int i = startPosition; i > (startPosition - ship.getSize()); i--)
            {
                values[i] = ship.getMarker();

                if (avoidAdjacentShips)
                {
                    //Mark area above ship
                    if (row != 0)
                    {
                        values[i - this.xSize] = '!';
                    }

                    //Mark area below ship
                    if (row != this.ySize - 1)
                    {
                        values[i + this.xSize] = '!';
                    }
                }
            }

            if (avoidAdjacentShips)
            {
                //Mark area to left of ship
                if (col - ship.getSize() >= 0)
                {
                    values[startPosition - ship.getSize()] = '!';
                }

                //Mark area to right of ship
                if (col < this.xSize - 1)
                {
                    values[startPosition + 1] = '!';
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Cans the ship go up.
        /// </summary>
        /// <returns><c>true</c>, if ship go up was caned, <c>false</c> otherwise.</returns>
        /// <param name="startPosition">Start position.</param>
        /// <param name="ship">Ship.</param>
        private bool canShipGoUp(int startPosition, Ship ship)
        {
            bool shipCanGoUp = true;

            //Because its horizontal we just go incrementally through grid array
            for (int i = startPosition; i >= (startPosition - ((ship.getSize() - 1) * this.xSize)); i = i - xSize)
            {
                if (!values[i].Equals('.'))
                {
                    shipCanGoUp = false;
                    Debug.WriteLine("Ship {0} had collision while trying to place", ship.getName());
                    break;
                }
            }

            return(shipCanGoUp);
        }
Пример #5
0
        /// <summary>
        /// Cans the ship go down.
        /// </summary>
        /// <returns><c>true</c>, if ship go down was caned, <c>false</c> otherwise.</returns>
        /// <param name="startPosition">Start position.</param>
        /// <param name="ship">Ship.</param>
        private bool canShipGoDown(int startPosition, Ship ship)
        {
            bool shipCanGoDown = true;

            //Because its vertical we check every position in column by adding row size
            for (int i = startPosition; i <= (startPosition + ((ship.getSize() - 1) * this.xSize)); i = i + xSize)
            {
                if (!values[i].Equals('.'))
                {
                    shipCanGoDown = false;
                    Debug.WriteLine("Ship {0} had collision while trying to place", ship.getName());
                    break;
                }
            }

            return(shipCanGoDown);
        }
Пример #6
0
        /// <summary>
        /// Places the ship.
        /// This algorithm gets a random starting coordinate and orientation.
        /// Then, it checks which direction will not run off the board and picks that direction.  Preference is
        /// right or down.  Alternitive if those run off board is left or up.
        /// It then attemps to occupy all the spaces for the ship.  If it collides with another ship
        /// (or its surrounding area when configured as such), it will make recursive call to try again.
        /// If avoidAdjacentShips is set, it will mark area around ship so that other ships will not occupy its surrounding
        /// area
        /// </summary>
        /// <returns><c>true</c>, if ship was placed, <c>false</c> otherwise.</returns>
        /// <param name="ship">Ship.</param>
        private bool placeShip(Ship ship)
        {
            bool spotTaken = false;

            int         startPosition   = getRandomCoordinate();
            Orientation shipOrientation = getRandomOrientation();

            int row = startPosition / this.xSize;
            int col = startPosition % this.xSize;

            Debug.WriteLine("Start Position is " + startPosition);
            Debug.WriteLine("Ship Direction is " + shipOrientation.ToString());
            Debug.WriteLine("Row={0}, Col={1}", row, col);

            if (shipOrientation.Equals(Orientation.Horizontal))
            {
                //See if ship fits going right
                if (ship.getSize() + col <= this.xSize)
                {
                    //See if spots already taken
                    if (canShipGoRight(startPosition, ship))
                    {
                        //Place ship here as it fits and no spots already occupied by another ship
                        placeShipGoingRight(startPosition, ship, row, col);
                    }
                    else
                    {
                        spotTaken = true;
                    }
                }
                //otherwise, it should always fit going left
                else
                {
                    //See if spots already taken
                    if (canShipGoLeft(startPosition, ship))
                    {
                        //Place ship here as it fits and no spots already occupied by another ship
                        placeShipGoingLeft(startPosition, ship, row, col);
                    }
                    else
                    {
                        spotTaken = true;
                    }
                }
            }
            else
            {
                //See if ship fits going down
                if (ship.getSize() + row < this.ySize)
                {
                    //See if spots already taken
                    if (canShipGoDown(startPosition, ship))
                    {
                        //Place ship here as it fits and no spots already occupied by another ship
                        placeShipGoingDown(startPosition, ship, row, col);
                    }
                    else
                    {
                        spotTaken = true;
                    }
                }
                //otherwise, it should always fit going up
                else
                {
                    //See if spots already taken
                    if (canShipGoUp(startPosition, ship))
                    {
                        //Place ship here as it fits and no spots already occupied by another ship
                        placeShipGoingUp(startPosition, ship, row, col);
                    }
                    else
                    {
                        spotTaken = true;
                    }
                }
            }

            //If ship had collision, try again
            //With too many ships, too large ship size, or too small a board,
            //this could loop forever.  If parameters of game were to change,
            //this would need some logic to quit, or else validation on acceptable
            //game parameters.
            if (spotTaken)
            {
                return(placeShip(ship));
            }
            else
            {
                return(true);
            }
        }