Ejemplo n.º 1
0
        public void Add(Boats boat)
        {
            switch (boat.Direction)
            {
                case Direction.Horizontal:
                    if (Enumerable.Range((int)boat.X, (int)boat.Length).Any(x => x < 1 || x > 10) || (boat.Y < 1 || boat.Y > 10))
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                    break;
                case Direction.Vertical:
                    if (Enumerable.Range((int)boat.Y, (int)boat.Length).Any(y => y < 1 || y > 10) || (boat.X < 1 || boat.X > 10))
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            foreach (var b in boats)
            {
                if (b.Equals(boat))
                {
                    throw new BoatsOverlapException("Ship " + b + " overlaps with " + boat);
                }
            }

            boats.Add(boat);
        }
Ejemplo n.º 2
0
 public static bool TryParse(string notation, out Boats pos)
 {
     try
     {
         pos = Parse(notation);
         return true;
     }
     catch (NotABoatException)
     {
         pos = null;
         return false;
     }
 }
Ejemplo n.º 3
0
        public bool OverlapsWith(Boats pos2)
        {
            switch (Direction)
            {
                case Direction.Horizontal:
                    if (Math.Abs((int)Y - (int)pos2.Y) > 1)
                    {
                        return false;
                    }
                    return Enumerable.Range((int)X, (int)Length)
                            .Any(
                                thisX =>
                                    Enumerable.Range((int)pos2.X, (int)pos2.Length)
                                        .Any(otherX => Math.Abs(thisX - otherX) < 2));
                case Direction.Vertical:
                    if (Math.Abs((int)X - (int)pos2.X) > 1)
                    {
                        return false;
                    }
                    return Enumerable.Range((int)Y, (int)Length)
                            .Any(
                                thisY =>
                                    Enumerable.Range((int)pos2.Y, (int)pos2.Length)
                                        .Any(otherY => Math.Abs(thisY - otherY) < 2));
                default:
                    throw new ArgumentOutOfRangeException();
            }

            //IEnumerable<int> num1;
            //IEnumerable<int> num2;

            //var val1 = "";
            //var val2 = "";

            //switch (Direction)
            //{
            //    case Direction.Horizontal:
            //        num1 = Enumerable.Range((int)X, (int)Length);
            //        val1 = EnumerableToString(num1, Direction, (int)X, (int)Y);
            //        break;
            //    case Direction.Vertical:
            //        num1 = Enumerable.Range((int)Y, (int)Length);
            //        val1 = EnumerableToString(num1, Direction, (int)X, (int)Y);
            //        break;
            //    default:
            //        throw new ArgumentOutOfRangeException();
            //}

            //switch (pos2.Direction)
            //{
            //    case Direction.Horizontal:
            //        num2 = Enumerable.Range((int)pos2.X, (int)pos2.Length);
            //        val2 = EnumerableToString(num2, pos2.Direction, (int)pos2.X, (int)pos2.Y);
            //        break;
            //    case Direction.Vertical:
            //        num2 = Enumerable.Range((int)pos2.Y, (int)pos2.Length);
            //        val2 = EnumerableToString(num2, pos2.Direction, (int)pos2.X, (int)pos2.Y);
            //        break;
            //    default:
            //        throw new ArgumentOutOfRangeException();
            //}
            //var result = SameValueBool(val1, val2) != true ? XandY(pos2) : SameValueBool(val1, val2);
        }