Exemplo n.º 1
0
        /// <summary>
        /// This function create list of destroyers with random positions and add them to the battlfield
        /// </summary>
        /// <param name="battlefield"></param>
        /// <returns>New battlefield object with added destroyers and also list of the ships</returns>
        public static (IBattleField battlefield, List <Battleship> destroyers) CreateAndLoadDestroyersToBattlefield(IBattleField battlefield)
        {
            List <Battleship> destroyers     = new List <Battleship>();
            IBattleField      newBattlefield = (IBattleField)battlefield.Clone();

            (int x, int y, Direction direction)coordinates;
            bool isSucceed;

            for (int i = 1; i <= 2; i++)
            {
                Battleship destroyer = new Battleship(4);
                do
                {
                    coordinates = RandomInitialCoordinates();
                    IBattleField battlefieldWihtNewShips;
                    (battlefieldWihtNewShips, isSucceed) = destroyer.TryLoadBattleshipToTheBattlefield(newBattlefield, coordinates.x, coordinates.y, coordinates.direction);
                    if (isSucceed == true)
                    {
                        newBattlefield = battlefieldWihtNewShips;
                    }
                } while (isSucceed == false);

                destroyers.Add(destroyer);
            }
            return(battlefield : newBattlefield, destroyers : destroyers);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The function load the battleship to the battlefield
        /// </summary>
        /// <param name="battlefield"></param>
        /// <returns>Returns new battlfield object with new battlship</returns>
        private IBattleField LoadShipToBattlefield(IBattleField battlefield)
        {
            IBattleField newBattleField = (IBattleField)battlefield.Clone();

            int xOfTheBattlefield = battlefield.GetArray().GetLength(0) - 1;
            int yOfTheBattlefield = battlefield.GetArray().GetLength(1) - 1;

            int?maxXcoordinate = this.CoordinatesOfTheShip.Max(a => a.X);
            int?maxYcoordinate = this.CoordinatesOfTheShip.Max(a => a.Y);

            int?minXcoordinate = this.CoordinatesOfTheShip.Min(a => a.X);
            int?minYcoordinate = this.CoordinatesOfTheShip.Min(a => a.Y);

            foreach (Coordinates mast in this.CoordinatesOfTheShip)
            {
                newBattleField = newBattleField.AddMastOfTheShip(this.id, Convert.ToInt32(mast.X), Convert.ToInt32(mast.Y));
            }

            return(newBattleField);
        }
Exemplo n.º 3
0
        /// <summary>
        /// This function create list of battleships with random positions and add them to the battlfields.
        /// </summary>
        /// <param name="battlefield"></param>
        /// <returns>New battlefield object with added destroyers and also list of the ships.</returns>

        public static (IBattleField battlefield, List <Battleship> ships) CreateAndLoadBattleshipsToBattlefield(IBattleField battlefield)
        {
            List <Battleship> battleships = new List <Battleship>();
            Battleship        battleship  = new Battleship(5);

            (int x, int y, Direction direction)coordinates;
            IBattleField NewBattleField = (IBattleField)battlefield.Clone();
            bool         isSucceed;

            do
            {
                coordinates = RandomInitialCoordinates();
                IBattleField battlefieldWihtNewShips;
                (battlefieldWihtNewShips, isSucceed) = battleship.TryLoadBattleshipToTheBattlefield(NewBattleField, coordinates.x, coordinates.y, coordinates.direction);
                if (isSucceed == true)
                {
                    NewBattleField = battlefieldWihtNewShips;
                }
            } while (isSucceed == false);

            battleships.Add(battleship);
            return(battlefield : NewBattleField, ships : battleships);
        }