Beispiel #1
0
        public void Launch(Map map)
        {
            //TODO: Implementierung der Abschusssequenz

            map.fireShot(1, 1);

        }
Beispiel #2
0
        /// <summary>
        /// returns rows with shots, hits, and ship
        /// </summary>
        /// <param name="mapOfCurrentPlayer">map of current player</param>
        /// <param name="mapOfOpponent">map of opponent</param>
        /// <returns>processed string</returns>
        private static String drawResultRows(Map mapOfCurrentPlayer, Map mapOfOpponent)
        {
            String line = "";
            int mapDim = mapOfCurrentPlayer.Dimension;

            for (int y = 0; y < mapDim; y++)
            {
                // opponent Field: Show hits and none Hits
                line += " ";
                line += (y < 10) ? " " : "";
                line += y + "|";
                for (int x = 0; x < mapDim; x++)
                    if (mapOfOpponent.getField(x, y).shot)
                        line += (mapOfOpponent.getField(x, y).ship) ? " " + CHAR_SHIP_SHOT : " " + CHAR_WATER_SHOT;
                    else
                        line += "  ";
                line += " | ";

                line += (y < 10) ? " " : "";
                line += y + "|";
                for (int x = 0; x < mapDim; x++)
                {
                    var field = mapOfCurrentPlayer.getField(x, y);
                    if (field.ship)
                        line += (field.shot) ? " " + CHAR_SHIP_SHOT : " " + CHAR_SHIP;
                    else
                        line += (field.shot) ? " " + CHAR_WATER_SHOT : " " + CHAR_WATER;
                }
                line += " | \n";
            }

            return line;
        }
Beispiel #3
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 #4
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>
 /// <returns>map with ships</returns>
 public static Map GenerateShipsOnMap(Map map)
 {
     return GenerateShipsOnMap(map, (int)Math.Floor(Math.Pow(map.Dimension, 2) / 10));
 }
Beispiel #5
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;
        }
 public SequenceExecuter(Map map) {
     this.map = map;
 }
Beispiel #7
0
 /// <summary>
 /// creates player instance  
 /// </summary>
 /// <param name="name">player name</param>
 /// <param name="map">player map</param>
 public Player(String name, Map map)
 {
     this.name = name;
     this.map = map;
 }