Esempio n. 1
0
        public static Ship Parse(string notation)
        {
            notation = notation.ToUpper();

            int positionX      = 0;
            int positionY      = 0;
            var positionParsed = ParsePosition(notation, ref positionX, ref positionY);

            int length = ParseLength(notation);

            Direction direction = ParseDirection(notation);

            if (positionParsed &&
                positionX >= Board.POSITION_MIN && positionX <= Board.POSITION_MAX &&
                positionY >= Board.POSITION_MIN && positionY <= Board.POSITION_MAX &&
                length >= (int)Types.PatrolBoat && length <= (int)Types.AircraftCarrier)
            {
                Ship ship = null;

                switch (length)
                {
                case (int)Types.PatrolBoat:
                    ship = new PatrolBoat(positionX, positionY);
                    break;

                case (int)Types.Cruiser:
                    ship = new Cruiser(positionX, positionY, direction);
                    break;

                case (int)Types.Submarine:
                    ship = new Submarine(positionX, positionY, direction);
                    break;

                case (int)Types.AircraftCarrier:
                    ship = new AircraftCarrier(positionX, positionY, direction);
                    break;
                }

                return(ship);
            }
            else
            {
                throw new NotAShipException();
            }
        }
Esempio n. 2
0
        public static Ship Parse(string notation)
        {
            notation = notation.ToUpper();

            int positionX = 0;
            int positionY = 0;
            var positionParsed = ParsePosition(notation, ref positionX, ref positionY);

            int length = ParseLength(notation);

            Direction direction = ParseDirection(notation);

            if (positionParsed &&
                positionX >= Board.POSITION_MIN && positionX <= Board.POSITION_MAX &&
                positionY >= Board.POSITION_MIN && positionY <= Board.POSITION_MAX &&
                length >= (int)Types.PatrolBoat && length <= (int)Types.AircraftCarrier)
            {
                Ship ship = null;

                switch (length)
                {
                    case (int)Types.PatrolBoat:
                        ship = new PatrolBoat(positionX, positionY);
                        break;
                    case (int)Types.Cruiser:
                        ship = new Cruiser(positionX, positionY, direction);
                        break;
                    case (int)Types.Submarine:
                        ship = new Submarine(positionX, positionY, direction);
                        break;
                    case (int)Types.AircraftCarrier:
                        ship = new AircraftCarrier(positionX, positionY, direction);
                        break;
                }

                return ship;
            }
            else
            {
                throw new NotAShipException();
            }
        }