Ejemplo n.º 1
0
        public void IsPlateauCreate(string plateauSizeText)
        {
            var plateauServiceResult = PlateauService.Current.Create(plateauSizeText);

            plateauDto = plateauServiceResult.ResponseObject;
            Assert.IsTrue(plateauServiceResult.IsSuccess, plateauServiceResult.Message);
        }
Ejemplo n.º 2
0
        public TransactionResult <IRoverDto> Create(IPlateauDto plateau, string roverPositionText)
        {
            var result = new TransactionResult <IRoverDto>();

            try
            {
                result.ResponseObject = _roverBusiness.Create(plateau, roverPositionText);
                result.SetStatusSucceeded("Transaction succeed.");
            }
            catch (ValidationException ve)
            {
                result.SetStatusValidationException(ve.Message);
            }
            catch (Exception ex)
            {
                result.SetStatusUnhandledException(ex);
            }

            return(result);
        }
Ejemplo n.º 3
0
        public IRoverDto Create(IPlateauDto plateau, string roverPositionText)
        {
            if (plateau is null)
            {
                throw new ValidationException("Plateau is null. Please try again.");
            }
            if (roverPositionText is null)
            {
                throw new ValidationException("Position is null. Please try again.");
            }

            var roverMatch = Regex.Match(roverPositionText, RegexPatterns.RoverPosition, RegexOptions.Singleline);

            if (!roverMatch.Success)
            {
                throw new ValidationException($"{roverPositionText} is not matched.");
            }

            var rover = new RoverDto();

            rover.Land(plateau, new Position(int.Parse(roverMatch.Groups[1].Value), int.Parse(roverMatch.Groups[2].Value), Directions.Items.FirstOrDefault(a => a.Key.Equals(roverMatch.Groups[3].Value.ToUpper()))));

            return(rover);
        }
Ejemplo n.º 4
0
 public void Initialize()
 {
     plateauDto = null;
 }
Ejemplo n.º 5
0
        public TransactionResult <string> DisplayText(IPlateauDto plateau)
        {
            var           result      = new TransactionResult <string>();
            StringBuilder textBuilder = new StringBuilder();

            var dimentionX = plateau.Width + 1;
            var dimentionY = plateau.Heigth;
            var cellWidth  = plateau.Width.ToString().Length + 2;

            if (cellWidth < 7)
            {
                cellWidth = 7;
            }
            var startHeight = plateau.Heigth.ToString().Length + 1;
            var whiteSpaces = string.Join(" ", new string[startHeight + 1]);

            for (int i = dimentionY; i >= 0; i--)
            {
                textBuilder.Append(whiteSpaces);
                for (int h = 0; h < dimentionX; h++)
                {
                    textBuilder.Append($"+{string.Join("-", new string[cellWidth + 1])}");
                }

                textBuilder.Append("+\n");
                textBuilder.Append($"{whiteSpaces.Substring(0, whiteSpaces.Length - i.ToString().Length)}{i}");

                for (int v = 0; v < dimentionX; v++)
                {
                    var rover     = plateau.Rovers.Where(w => w.Position.X == v && w.Position.Y == i).Select((s, k) => new { s, k }).FirstOrDefault();
                    var roverText = $@"{rover?.k}{rover?.s.Symbol}{rover?.s.Position.Direction.Key}";
                    var lengthX   = roverText.ToString().Length;
                    var startX    = (int)Math.Floor((double)(cellWidth - lengthX) / 2);
                    var finishX   = cellWidth - (startX + lengthX);

                    roverText = $"{string.Join(" ", new string[startX + 1])}{roverText}{string.Join(" ", new string[finishX + 1])}";

                    textBuilder.Append($"|{roverText}");
                }
                textBuilder.Append("|\n");
            }

            textBuilder.Append(whiteSpaces);
            for (int h = 0; h < dimentionX; h++)
            {
                textBuilder.Append($"+{string.Join("-", new string[cellWidth + 1])}");
            }
            textBuilder.Append("+\n");

            string footerX = whiteSpaces;

            for (int i = 0; i < dimentionX; i++)
            {
                var lengthX = i.ToString().Length;
                var startX  = (int)Math.Floor((double)(cellWidth - lengthX) / 2);
                var finishX = cellWidth - (startX + lengthX);

                footerX += $"{string.Join(" ", new string[startX + 2])}{i}{string.Join(" ", new string[finishX + 1])}";
            }
            textBuilder.AppendLine(footerX);

            textBuilder.Append("\n\n");

            foreach (var rover in plateau.Rovers)
            {
                textBuilder.AppendLine(rover.Position.ToString());
            }


            result.ResponseObject = textBuilder.ToString();
            textBuilder           = null;
            result.SetStatusSucceeded("Transaction succeed.");
            return(result);
        }