public IEnumerable <IShipPosition> GetShipPositions()
        {
            // First, clear all state and re-initialise
            ClearState();

            // Now return some ships
            List <ShipPosition> ships = new List <ShipPosition>();

            bool[,] shipPositions = new bool[width, height];  // Indicates which board squares are occupied

            // Initialise it to all false
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    shipPositions[i, j] = false;
                }
            }

            // Add each ship
            foreach (EShipType ship in Enum.GetValues(typeof(EShipType)))
            {
                int length;

                switch (ship)
                {
                case EShipType.aircraftCarrier:
                    length = 5;
                    break;

                case EShipType.battleship:
                    length = 4;
                    break;

                case EShipType.destroyer:
                    length = 3;
                    break;

                case EShipType.submarine:
                    length = 3;
                    break;

                case EShipType.patrolBoat:
                    length = 2;
                    break;

                default:
                    length = 0;      // Shouldn't happen \ Won't happen.
                    break;
                }

                bool vertical = (rand.Next(2) == 1);  // Decide whether the ship should be placed horizontally or vertically

                int x, y;

                // Effective width and height of the board (allowing for ship sizes)
                int effectiveWidth  = width;
                int effectiveHeight = height;
                if (vertical)
                {
                    effectiveHeight -= (length - 1);
                }
                else
                {
                    effectiveWidth -= (length - 1);
                }

                while (true)
                {
                    // Pick x and y
                    x = rand.Next(effectiveWidth);
                    y = rand.Next(effectiveHeight);

                    bool clear = true;

                    if (vertical)
                    {
                        // Are the spaces clear?
                        for (int i = y; i <= y + (length - 1); i++)
                        {
                            if (shipPositions[x, i])
                            {
                                clear = false;
                            }
                            ;                                          // There's a ship in the way!
                        }
                    }
                    else
                    {
                        // Are the spaces clear?
                        for (int i = x; i <= x + (length - 1); i++)
                        {
                            if (shipPositions[i, y])
                            {
                                clear = false;
                            }
                            ;                                          // There's a ship in the way!
                        }
                    }

                    if (clear)
                    {
                        break;         // No ships were found in the way, all is good :)
                    }
                    // Otherwise, looping again...
                }

                // Calculate the end position of the ship and erase around them on the shipPositions grid
                int endX, endY;

                if (vertical)
                {
                    endX = x;
                    endY = y + (length - 1);
                    if (endY >= 10)
                    {
                        throw new IndexOutOfRangeException("Y :/");
                    }

                    for (int i = x - 1; i <= x + 1; i++)
                    {
                        for (int j = y - 1; j <= endY + 1; j++)
                        {
                            if (i >= 0 && i < width && j >= 0 && j < height)
                            {
                                shipPositions[i, j] = true;
                            }
                        }
                    }
                }
                else
                {
                    endX = x + (length - 1);
                    if (endX >= 10)
                    {
                        throw new IndexOutOfRangeException("X :/");
                    }
                    endY = y;

                    for (int i = y - 1; i <= y + 1; i++)
                    {
                        for (int j = x - 1; j <= endX + 1; j++)
                        {
                            if (i >= 0 && i < height && j >= 0 && j < width)
                            {
                                shipPositions[j, i] = true;
                            }
                        }
                    }
                }

                // Add the new ship to the list
                ShipPosition newShip = new ShipPosition(ship, y, x, endY, endX);
                ships.Add(newShip);
            }

            return(ships);
        }
Exemplo n.º 2
0
        public IEnumerable <IShipPosition> GetShipPositions()
        {
            // First, clear all state and re-initialise
            ClearState();

            // Now return some ships
            List <ShipPosition> ships = new List <ShipPosition>();

            bool[,] shipPositions = new bool[width, height];  // Indicates which board squares are occupied

            // Initialise shipPositions to all false as none have been placed
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    shipPositions[i, j] = false;
                }
            }

            // Add each ship
            foreach (EShipType ship in Enum.GetValues(typeof(EShipType)))
            {
                int length;

                // Get the appropriate length for this ship type
                switch (ship)
                {
                case EShipType.aircraftCarrier:
                    length = 5;
                    break;

                case EShipType.battleship:
                    length = 4;
                    break;

                case EShipType.destroyer:
                    length = 3;
                    break;

                case EShipType.submarine:
                    length = 3;
                    break;

                case EShipType.patrolBoat:
                    length = 2;
                    break;

                default:
                    length = 0;      // Shouldn't happen \ Won't happen.
                    break;
                }

                bool vertical = (rand.Next(2) == 1);  // Decide whether the ship should be placed horizontally or vertically

                int x, y;

                // Effective width and height of the board
                // (Treating it as being smaller so that a ship cannot be accidentally placed off the side)
                int effectiveWidth  = width;
                int effectiveHeight = height;
                if (vertical)
                {
                    effectiveHeight -= (length - 1);
                }
                else
                {
                    effectiveWidth -= (length - 1);
                }

                while (true)
                {
                    // Pick x and y
                    x = rand.Next(effectiveWidth);
                    y = rand.Next(effectiveHeight);

                    bool clear = true;

                    if (vertical)
                    {
                        // Are the spaces clear?
                        for (int i = y; i <= y + (length - 1); i++)
                        {
                            if (shipPositions[x, i])
                            {
                                clear = false;                       // There's a ship in the way!
                            }
                        }
                    }
                    else
                    {
                        // Are the spaces clear?
                        for (int i = x; i <= x + (length - 1); i++)
                        {
                            if (shipPositions[i, y])
                            {
                                clear = false;                       // There's a ship in the way!
                            }
                        }
                    }

                    if (clear)
                    {
                        break;         // No ships were found in the way, all is good :)
                    }
                    // Otherwise, looping again...
                }

                // Calculate the end position of the ship
                int endX, endY;

                if (vertical)
                {
                    endX = x;
                    endY = y + (length - 1);

                    // Erase all tiles around the ship, ie:
                    // ........
                    // .XXXXXX.
                    // .XSHIPX.
                    // .XXXXXX.
                    // ........
                    for (int i = x - 1; i <= x + 1; i++)
                    {
                        for (int j = y - 1; j <= endY + 1; j++)
                        {
                            if (i >= 0 && i < width && j >= 0 && j < height)
                            {
                                shipPositions[i, j] = true;
                            }
                        }
                    }
                }
                else
                {
                    endX = x + (length - 1);
                    endY = y;

                    for (int i = y - 1; i <= y + 1; i++)
                    {
                        for (int j = x - 1; j <= endX + 1; j++)
                        {
                            if (i >= 0 && i < height && j >= 0 && j < width)
                            {
                                shipPositions[j, i] = true;
                            }
                        }
                    }
                }

                // Add the new ship to the list
                ShipPosition newShip = new ShipPosition(ship, y, x, endY, endX);
                ships.Add(newShip);
            }

            return(ships);
        }
        public IEnumerable<IShipPosition> GetShipPositions()
        {
            // First, clear all state and re-initialise
            ClearState();

            // Now return some ships
            List<ShipPosition> ships = new List<ShipPosition>();

            bool[,] shipPositions = new bool[width, height];  // Indicates which board squares are occupied

            // Initialise it to all false
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    shipPositions[i, j] = false;
                }
            }

            // Add each ship
            foreach (EShipType ship in Enum.GetValues(typeof(EShipType)))
            {
                int length;

                switch (ship)
                {
                    case EShipType.aircraftCarrier:
                        length = 5;
                        break;
                    case EShipType.battleship:
                        length = 4;
                        break;
                    case EShipType.destroyer:
                        length = 3;
                        break;
                    case EShipType.submarine:
                        length = 3;
                        break;
                    case EShipType.patrolBoat:
                        length = 2;
                        break;
                    default:
                        length = 0;  // Shouldn't happen \ Won't happen.
                        break;
                }

                bool vertical = (rand.Next(2) == 1);  // Decide whether the ship should be placed horizontally or vertically

                int x, y;

                // Effective width and height of the board (allowing for ship sizes)
                int effectiveWidth = width;
                int effectiveHeight = height;
                if (vertical) effectiveHeight -= (length - 1);
                else effectiveWidth -= (length - 1);

                while (true)
                {
                    // Pick x and y
                    x = rand.Next(effectiveWidth);
                    y = rand.Next(effectiveHeight);

                    bool clear = true;

                    if (vertical)
                    {
                        // Are the spaces clear?
                        for (int i = y; i <= y + (length - 1); i++)
                        {
                            if (shipPositions[x, i]) clear = false; ;  // There's a ship in the way!
                        }
                    }
                    else
                    {
                        // Are the spaces clear?
                        for (int i = x; i <= x + (length - 1); i++)
                        {
                            if (shipPositions[i, y]) clear = false; ;  // There's a ship in the way!
                        }
                    }

                    if (clear) break;  // No ships were found in the way, all is good :)
                    // Otherwise, looping again...
                }

                // Calculate the end position of the ship and erase around them on the shipPositions grid
                int endX, endY;

                if (vertical)
                {
                    endX = x;
                    endY = y + (length - 1);
                    if (endY >= 10) throw new IndexOutOfRangeException("Y :/");

                    for (int i = x - 1; i <= x + 1; i++)
                    {
                        for (int j = y - 1; j <= endY + 1; j++)
                        {
                            if (i >= 0 && i < width && j >= 0 && j < height)
                            {
                                shipPositions[i, j] = true;
                            }
                        }
                    }
                }
                else
                {
                    endX = x + (length - 1);
                    if (endX >= 10) throw new IndexOutOfRangeException("X :/");
                    endY = y;

                    for (int i = y - 1; i <= y + 1; i++)
                    {
                        for (int j = x - 1; j <= endX + 1; j++)
                        {
                            if (i >= 0 && i < height && j >= 0 && j < width)
                            {
                                shipPositions[j, i] = true;
                            }
                        }
                    }
                }

                // Add the new ship to the list
                ShipPosition newShip = new ShipPosition(ship, y, x, endY, endX);
                ships.Add(newShip);
            }

            return ships;
        }