Пример #1
0
        //function returns TRUE if there is a ship in that part of ocean or reached end of ocean
        public bool CanPlaceShip(Ship shipToPlace, PlaceShipDirection direction, int startx, int starty)
        {
            //Make sure there is not already ship in that part of ocean
            //Also can't place a ship out of a ocean
            for (int i = 0; i < shipToPlace.length; i++)
            {
                if (Ocean[startx, starty].status == PointStatus.Ship)
                {
                    return(false);
                }

                if (direction == PlaceShipDirection.Vertical)
                {
                    starty++;
                    if (starty > 9)
                    {
                        return(false);
                    }
                }
                else if (direction == PlaceShipDirection.Horizontal)
                {
                    startx++;
                    if (startx > 9)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Пример #2
0
        // Placing ships randomly
        public void PlaceShipsRandomly()
        {
            int x = 0;
            int y = 0;

            for (int i = 0; i < ListOfShips.Count; i++)
            {
                bool placeThatStuff = false;
                while (!placeThatStuff)
                {
                    bool rightPoint = false;
                    while (!rightPoint)
                    {
                        x = rnd.Next(1, 11) - 1;
                        y = rnd.Next(1, 11) - 1;
                        if (Ocean[x, y].PointStatus == PointStatus.Empty)
                        {
                            rightPoint = true;
                        }
                    }

                    int incrementer         = 0;
                    PlaceShipDirection pdir = (PlaceShipDirection)rnd.Next(0, 2);
                    if (rnd.Next(0, 2) == 0)
                    {
                        incrementer = -1;
                    }
                    else
                    {
                        incrementer = 1;
                    }
                    if (canPlaceShip(ListOfShips[i], pdir, incrementer, x, y))
                    {
                        if (pdir == PlaceShipDirection.Horizontal)
                        {
                            if (incrementer < 0)
                            {
                                x -= ListOfShips[i].Length;
                            }
                        }
                        else if
                        (pdir == PlaceShipDirection.Vertical)
                        {
                            if (incrementer < 0)
                            {
                                y -= ListOfShips[i].Length;
                            }
                        }
                        PlaceShip(ListOfShips[i], pdir, x, y);
                        placeThatStuff = true;
                    }
                }
            }
        }
Пример #3
0
        //Methods
        //Place Ship

        public void PlaceShip(Ship shipToPlace, PlaceShipDirection direction, int startX, int startY)
        {
            for (int i = 0; i <= shipToPlace.length; i++)
            {
                Ocean[startX, startY].Status = Point.PointStatus.Ship;
                shipToPlace.OccupiedPoints.Add(Ocean[startX, startY]);
                if (direction == PlaceShipDirection.Horizontal)
                {
                    startX++;
                }
                else
                {
                    startY++;
                }
            }
        }
Пример #4
0
 public void PlaceShip(Ship shipPlace, PlaceShipDirection direction, int startOfX, int startOfY)
 {
     for (int i = 0; i < shipPlace.Length; i++)
     {
         Ocean[startOfX, startOfY].Status = PointStatus.Ship;
         shipPlace.OccupiedPoints.Add(Ocean[startOfX, startOfY]);
         if (direction == PlaceShipDirection.Horizontal)
         {
             startOfX++;
         }
         else
         {
             startOfY++;
         }
     }
     this.ListOfShips.Add(shipPlace);
 }
Пример #5
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);
                }
            }
        }
Пример #6
0
 //place each ship in the list of ships using placeship()
 public void PlaceShip(Ship shipToPlace, PlaceShipDirection direction, int startX, int startY)
 {
     for (int i = 1; i < shipToPlace.Length; i++)
     {
         var startingPoint = Ocean[startX, startY];
         startingPoint.Status = Point.PointStatus.Ship;
         shipToPlace.OccupiedPoints.Add(startingPoint);
         if (direction == PlaceShipDirection.Horizontal)
         {
             startX++;
         }
         else
         {
             startY++;
         }
     }
 }
Пример #7
0
        // return true if there is enough space for ship
        public bool canPlaceShip(Ship shipToPlace, PlaceShipDirection direction, int incrementer, int start_x, int start_y)
        {
            bool value = true;
            int  x     = start_x;
            int  y     = start_y;

            for (int i = 0; i < shipToPlace.Length; i++)
            {
                if (x < 0 || x > 9)
                {
                    value = false;
                }
                else if (y < 0 || y > 9)
                {
                    value = false;
                }
                else if (Ocean[x, y].PointStatus == PointStatus.Ship)
                {
                    value = false;
                }


                if (direction == PlaceShipDirection.Horizontal)
                {
                    x += incrementer;
                }
                else if (direction == PlaceShipDirection.Vertical)
                {
                    y += incrementer;
                }

                if (x < 0 || x > 9)
                {
                    value = false;
                }
                else if (y < 0 || y > 9)
                {
                    value = false;
                }
            }

            return(value);
        }
Пример #8
0
        public void PlaceShip(Ship shipToPlace, PlaceShipDirection direction, int startX, int startY)
        {
            for (int i = 0; i < shipToPlace.Length - 1; i++)
            {
                Point thePoint = Ocean[startX, startY];
                // Point.Pointstatus = Ship.ShipType;
                thePoint.Status = Point.Pointstatus.Ship;

                shipToPlace.OccupiedPoints.Add(thePoint);

                if (direction == PlaceShipDirection.Horizontal)
                {
                    startX++;
                }
                else
                {
                    startY++;
                }
            }
        }
Пример #9
0
        // Place one ship
        public void PlaceShip(Ship shipToPlace, PlaceShipDirection direction, int start_x, int start_y)
        {
            int x = start_x;
            int y = start_y;

            for (int i = 0; i < shipToPlace.Length; i++)
            {
                Ocean[x, y].PointStatus = PointStatus.Ship;
                shipToPlace.OccupiedPoints.Add(Ocean[x, y]);

                if (direction == PlaceShipDirection.Horizontal)
                {
                    x++;
                }
                else if (direction == PlaceShipDirection.Vertical)
                {
                    y++;
                }
            }
        }
Пример #10
0
 //Methods and Functions
 public void PlaceShip(Ship shipToPlace, PlaceShipDirection direction, int startX, int startY)
 {
     //loop for the length of the ship being currently placed
     for (int i = 0; i < shipToPlace.Length; i++)
     {
         //add the shipToPlace to the occupied points
         this.Ocean[startX, startY].Status = Point.PointStatus.Ship;
         shipToPlace.OccupiedPoints.Add(this.Ocean[startX, startY]);
         //if the direction is horizontal
         if (direction == PlaceShipDirection.Horizontal)
         {
             //add ship points to the x axis for the length of the ship
             startX++;
         }
         //the direction is vertical
         else
         {
             //add ship points to the y axis for the length of the ship
             startY++;
         }
     }
 }
Пример #11
0
        //Methods and Functions
        private void PlaceShip(Ship shipToPlace, PlaceShipDirection direction, int startX, int startY)
        {
            for (int i = 0; i <= shipToPlace.Length; i++)
            {
                Point startPoint = Ocean[startX, startY];
                startPoint.Status = Point.PointStatus.Ship;
                shipToPlace.OccupiedPoints.Add(startPoint);

                if (direction == PlaceShipDirection.Horizontal)
                {
                    //if placing horizonal 

                    startX++;
                }
                else
                {
                    //if placing vertical
                    startY++;
                }

            }
        }
Пример #12
0
 private void PlaceShip(Ship ship, PlaceShipDirection dir, int x, int y)
 {
     for (int i = 0; i < ship.Length; i++)
     {
         var p = this.Ocean[x, y];
         p.Status = PointStatus.Ship;
         ship.Position.Add(p);
         if (dir == PlaceShipDirection.Horizontally) { x++; } else { y++; }
     }
 }