Exemple #1
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;
        }
Exemple #2
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;
        }