Exemple #1
0
        // Checks if the point shares one axis with the ship and whether the other
        //  axis is between the bow and stern
        public bool IsPointOnShip(Point p)
        {
            // Same vertical axis
            if (p.GetX() == _bow.GetX())
            {
                // Checking if point lies between Y values
                if (_bow.GetY() <= _stern.GetY())
                {
                    return(p.GetY() >= _bow.GetY() && p.GetY() <= _stern.GetY());
                }
                else
                {
                    return(p.GetY() >= _stern.GetY() && p.GetY() <= _bow.GetY());
                }
            }

            // Same horizontal axis
            if (p.GetY() == _bow.GetY())
            {
                // Checking if point lies between X values
                if (_bow.GetX() <= _stern.GetX())
                {
                    return(p.GetX() >= _bow.GetX() && p.GetX() <= _stern.GetX());
                }
                else
                {
                    return(p.GetX() >= _stern.GetX() && p.GetX() <= _bow.GetX());
                }
            }

            return(false);
        }
Exemple #2
0
        // Checks if point lies on the game's grid
        private bool IsValidBoardPoint(Point p)
        {
            bool xValid = p.GetX() >= 0 && p.GetX() < _gridSize;
            bool yValid = p.GetY() >= 0 && p.GetY() < _gridSize;

            return(xValid && yValid);
        }
Exemple #3
0
 // Returns the absolutel value of the difference of the non shared axis
 private int CalcShipLength()
 {
     if (_bow.GetX() == _stern.GetX())
     {
         return(Math.Abs(_bow.GetY() - _stern.GetY()) + 1);
     }
     else
     {
         return(Math.Abs(_bow.GetX() - _stern.GetX()) + 1);
     }
 }