Exemple #1
0
        public bool SpaceAvaiable(ShipPlacementOrientations dir, IShip ship, int x, int y)
        {
            Point p;

            if (dir == ShipPlacementOrientations.Horizontal)
            {
                for (int i = 0; i < ship.Size; i++)
                {
                    p.x = x + i;
                    p.y = y;
                    if (!points.Contains(p))
                    {
                        return(false);
                    }
                }
            }
            else
            {
                for (int i = 0; i < ship.Size; i++)
                {
                    p.x = x;
                    p.y = y + i;
                    if (!points.Contains(p))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemple #2
0
        private void ApplyPlacement(IShip ship, ShipPlacementOrientations orientation, int x, int y)
        {
            for (int i = 0; i < ship.Size; i++)
            {
                // get x,y coordinates based on ship orientation
                int xi = x + (orientation == ShipPlacementOrientations.Horizontal ? i : 0);
                int yi = y + (orientation == ShipPlacementOrientations.Vertical ? i : 0);


                _grid[xi, yi] = ship;
            }
        }
Exemple #3
0
        public bool PlaceShip(IShip ship, ShipPlacementOrientations orientation, int x, int y)
        {
            if (!CheckValidPlacement(ship, orientation, x, y))
            {
                return(false);
            }

            ApplyPlacement(ship, orientation, x, y);
            ship.Orientation = orientation;
            ship.X           = x;
            ship.Y           = y;

            return(true);
        }
Exemple #4
0
 private void RemoveShipSpace(ShipPlacementOrientations dir, IShip ship, int x, int y)
 {
     if (dir == ShipPlacementOrientations.Horizontal)
     {
         for (int i = 0; i < ship.Size; i++)
         {
             RemovePointSpace(x + i, y);
         }
     }
     else
     {
         for (int i = 0; i < ship.Size; i++)
         {
             RemovePointSpace(x, y + i);
         }
     }
 }
Exemple #5
0
        private bool CheckValidPlacement(IShip ship, ShipPlacementOrientations orientation, int x, int y)
        {
            for (int i = 0; i < ship.Size; i++)
            {
                // get x,y coordinates based on ship orientation
                int xi = x + (orientation == ShipPlacementOrientations.Horizontal ? i : 0);
                int yi = y + (orientation == ShipPlacementOrientations.Vertical ? i : 0);

                // check if out of bounds
                if (xi < 0 || xi > _grid.GetLength(0) || yi < 0 || yi > _grid.GetLength(1))
                {
                    return(false);
                }

                // check for collision with another ship
                if (_grid[xi, yi] is IShip)
                {
                    return(false);
                }
            }

            return(true);
        }