Esempio n. 1
0
        //define constructor
        public Grid()
        {
            Random rng = new Random();

            //itialize the ocean
            this.Ocean = new Point [10, 10];
            for (int x = 0; x <= 9; x++)
            {
                for (int y = 0; y <= 9; y++)
                {
                    this.Ocean[x, y] = new Point(x, y, Point.Pointstatus.Empty);
                }
            }

            PlaceShipDirection direction = (PlaceShipDirection)rng.Next(0, 2);

            this.ListOfShips = new List <Ship>();

            Ship shipCarrier = new Ship(Ship.ShipType.Carrier);

            ListOfShips.Add(shipCarrier);
            Ship shipBattleship = new Ship(Ship.ShipType.Battleship);

            ListOfShips.Add(shipBattleship);
            Ship shipCruiser = new Ship(Ship.ShipType.Cruiser);

            ListOfShips.Add(shipCruiser);
            Ship shipSubmarine = new Ship(Ship.ShipType.Submarine);

            ListOfShips.Add(shipSubmarine);
            Ship shipMinesweeper = new Ship(Ship.ShipType.Minesweeper);

            ListOfShips.Add(shipMinesweeper);


            for (int i = 0; i < ListOfShips.Count - 1; i++)
            {
                int xPoint = rng.Next(0, 10);
                int yPoint = rng.Next(0, 10);
                PlaceShipDirection placeDir = (PlaceShipDirection)rng.Next(0, 2);
                if (Ocean[xPoint, yPoint].Status == Point.Pointstatus.Empty)
                {
                    PlaceShip(ListOfShips[rng.Next(0, ListOfShips.Count - 1)], placeDir, xPoint, yPoint);
                }
            }
        }
Esempio n. 2
0
        //method to place ships
        public void PlaceShip(Ship shipToPlace)
        {
            bool yesShip = false;
            int  startx = 0, starty = 0;
            int  direction = 0;

            //need to generate a set of x-coordinate and y-coordinate, so that they are valid
            while (!yesShip)
            {
                Random rng = new Random();

                //random coordinates
                startx = rng.Next(0, 10);
                starty = rng.Next(0, 10);

                //random direction
                direction = rng.Next(1, 3);
                yesShip   = CanPlaceShip(shipToPlace, (PlaceShipDirection)direction, startx, starty);
            }

            //make sure there is no ship in that part of ocean
            for (int i = 0; i < shipToPlace.length; i++)
            {
                //change the status of that point in ocean (from empty) to ship
                Ocean[startx, starty].status = PointStatus.Ship;
                //add that point to ship's occupaid points
                shipToPlace.occupiedPoint.Add(Ocean[startx, starty]);

                if ((PlaceShipDirection)direction == PlaceShipDirection.Horizontal)
                {
                    startx++;
                }
                else
                {
                    starty++;
                }
            }
            //add that ship to list of ships in the ocean
            ListOfShips.Add(shipToPlace);
        }