Пример #1
0
        public BattleshipConfiguration(string configurationAsString)
        {
            var configurationAsArray = configurationAsString
                                       .Split(",")
                                       .Select(configuration => configuration.Trim())
                                       .ToArray();

            Position = new XYPosition(
                x: int.Parse(configurationAsArray[0]),
                y: int.Parse(configurationAsArray[1])
                );

            switch (configurationAsArray[2])
            {
            case "h":
                Orientation = Orientation.Horizontal;
                break;

            case "v":
                Orientation = Orientation.Vertical;
                break;

            default:
                throw new ArgumentException("Invalid orientation");
            }

            Size = int.Parse(configurationAsArray[3]);
        }
Пример #2
0
        public override XYBattleship Build(XYPosition position, int size)
        {
            var occupiedPositions = new List <XYOccupiedPosition>();

            for (var i = 0; i < size; i++)
            {
                var occupiedPosition = new XYOccupiedPosition(new XYPosition(position.X + i, position.Y));
                occupiedPositions.Add(occupiedPosition);
            }

            return(new XYBattleship(occupiedPositions));
        }
Пример #3
0
        public static IPosition Convert(string positionAsString)
        {
            var positionAsArray = positionAsString
                                  .Split(",")
                                  .Select(configuration => configuration.Trim())
                                  .ToArray();

            var position = new XYPosition(
                x: int.Parse(positionAsArray[0]),
                y: int.Parse(positionAsArray[1])
                );

            return(position);
        }
Пример #4
0
        bool ValidateOrientationAndSize(XYPosition xyPosition, out string message)
        {
            message = "";
            switch (orientation)
            {
            case Orientation.Horizontal:
                if (xyPosition.X + size >= width)
                {
                    message = $"X + size ({xyPosition.X} + {size}) is bigger than width bound '{width}', ";
                    return(false);
                }
                break;

            case Orientation.Vertical:
                if (xyPosition.Y + size >= height)
                {
                    message = $"Y + size ({xyPosition.Y} + {size}) is bigger than height bound '{height}', ";
                    return(false);
                }
                break;
            }

            return(true);
        }
Пример #5
0
 public XYOccupiedPosition(XYPosition position)
 {
     this.Position = position;
 }
Пример #6
0
 bool ValidateExistingBattleshipConfigurations(XYPosition xyPosition, out string message)
 {
     message = "";
     return(true);
 }
Пример #7
0
 abstract public XYBattleship Build(XYPosition position, int size);