コード例 #1
0
ファイル: Ship.cs プロジェクト: Rudeka/KottansHomeWork
        public bool OverlapsWith(Ship otherShip)
        {
            var shipLastX = X + Size - 1;
            var otherShipLastX = otherShip.X + otherShip.Size - 1;
            var shipAllX = new List<int>();
            var otherShipAllX = new List<int>();

            var shipLastY = Y;
            var otherShipLastY = otherShip.Y;
            var shipAllY = new List<int> { Y };
            var otherShipAllY = new List<int> { otherShip.Y };

            var xCoordinatesForGap = new List<int>();
            var yCoordinatesForGap = new List<int>();

            if (Direction == Direction.Vertical)
            {
                shipLastX = X;
                shipLastY = Y + Size - 1;
                for (var i = Y+1; i < shipLastY; i++)
                {
                    shipAllY.Add(i);
                }

            }

            if (otherShip.Direction == Direction.Vertical)
            {
                otherShipLastX = otherShip.X;
                otherShipLastY = otherShip.Y + otherShip.Size - 1;
                for (var i = Y+1; i < otherShipLastY; i++)
                {
                    otherShipAllY.Add(i);
                }

            }

            for (var i = X; i <= shipLastX; i++)
            {
                shipAllX.Add(i);
            }

            for (var i = otherShip.X; i <= otherShipLastX; i++)
            {
                otherShipAllX.Add(i);
            }

            foreach (var shipX in shipAllX)
            {
                xCoordinatesForGap.Add(shipX);
                xCoordinatesForGap.Add(shipX + 1);
                xCoordinatesForGap.Add(shipX - 1);
            }

            foreach (var shipY in shipAllY)
            {
                yCoordinatesForGap.Add(shipY);
                yCoordinatesForGap.Add(shipY + 1);
                yCoordinatesForGap.Add(shipY - 1);
            }

            if (X == otherShip.X && Y == otherShip.Y) return true;

            if (otherShipAllX.Any(t => otherShipAllY.Any(t1 => shipAllX.Any(a => a == t)
                                                               && shipAllY.Any(a => a == t1))))
            {
                return true;
            }

            return otherShipAllX.Any(t => otherShipAllY.Any(t1 => xCoordinatesForGap.Any(a => a == t)
                                                                  && yCoordinatesForGap.Any(a => a == t1)));
        }
コード例 #2
0
ファイル: Ship.cs プロジェクト: Rudeka/KottansHomeWork
 public static bool TryParse(string notation, out Ship pos)
 {
     pos = default(Ship);
     try
     {
         pos = (Ship)Parse(notation);
         return true;
     }
     catch
     {
         return false;
     }
 }