Beispiel #1
0
        /// <summary>
        /// adds a ship to given coordinates if possible       
        /// </summary>
        /// <param name="map">player map</param>
        /// <param name="ship">ship to be placed</param>
        /// <param name="posX">position x where ship maybe placed</param>
        /// <param name="posY">position y where ship maybe placed</param>
        /// <returns>true, if ship could be placed</returns>
        public static Boolean AddShip(Map map, Ship ship, int posX, int posY)
        {
            int sizeX;
            int sizeY;

            if (ship.horizontal)
            {
                sizeX = ship.size;
                sizeY = 1;
            }
            else
            {
                sizeX = 1;
                sizeY = ship.size;
            }

            for (int y = posY; y < (posY + sizeY); y++)
                for (int x = posX; x < (posX + sizeX); x++)
                    if (x >= map.Dimension || y >= map.Dimension || map.getField(x, y).ship)
                        return false;

            for (int y = posY; y < (posY + sizeY); y++)
                for (int x = posX; x < (posX + sizeX); x++)
                    map.getField(x, y).ship = true;

            return true;
        }
Beispiel #2
0
        /// <summary>
        /// generates ships of size 5, 4, 3, and 2 and places them on map                   
        /// </summary>
        /// <param name="map">map on which ships are placed on</param>
        /// <param name="numberOfShipFields">max number of ship fields</param>
        /// <returns>map with ships</returns>
        public static Map GenerateShipsOnMap(Map map, int numberOfShipFields)
        {
            int numberOfFiver = (int)Math.Floor((double)(((5.0 / 24) * numberOfShipFields) / 5));
            int numberOfFoursomes = (int)Math.Floor((double)(((8.0 / 24) * numberOfShipFields) / 4));
            int numberOfThreesomes = (int)Math.Floor((double)(((9.0 / 24) * numberOfShipFields) / 3));
            int numberOfTwoSomes = (int)Math.Floor((double)(((4.0 / 24) * numberOfShipFields) / 2));

            Ship[] ships = new Ship[numberOfFiver + numberOfFoursomes + numberOfThreesomes + numberOfTwoSomes];
            Random random = new Random();
            int counter = 0;

            for (int i = 0; i < numberOfFiver; i++)
                ships[counter++] = new Ship(5, (random.NextDouble() > 0.5));
            for (int i = 0; i < numberOfFoursomes; i++)
                ships[counter++] = new Ship(4, (random.NextDouble() > 0.5));
            for (int i = 0; i < numberOfThreesomes; i++)
                ships[counter++] = new Ship(3, (random.NextDouble() > 0.5));
            for (int i = 0; i < numberOfTwoSomes; i++)
                ships[counter++] = new Ship(2, (random.NextDouble() > 0.5));

            if (map.Dimension * map.Dimension < numberOfShipFields)
                return null;

            foreach (Ship ship in ships)
            {
                Boolean found = false;
                int randomX;
                int randomY;

                while (!found)
                {
                    randomX = random.Next(map.Dimension);
                    randomY = random.Next(map.Dimension);
                    found = AddShip(map, ship, randomX, randomY);
                }
            }
            return map;
        }