/// <summary> /// Checks valid Place command /// </summary> /// <param name="input">The X,Y coordinates with the Cardinal</param> /// <returns>Commandvalidation</returns> private CommandResult PlaceCommand(string input) { var command = Commands.Place; string[] texts = input.Split(','); if (texts.Length != 3) { return(CommandResult.CreateFromFailure(CommandErrors.InvalidParameters, _invalidParameters)); } var xStringValue = texts[0].Trim(); var yStringValue = texts[1].Trim(); var facingValue = StringExtentions.AllToUppercase(texts[2]).Trim(); if (!xStringValue.All(char.IsDigit) || !yStringValue.All(char.IsDigit)) { return(CommandResult.CreateFromFailure(CommandErrors.InvalidCoordinates, _invalidCoordinates)); } if (!Enum.IsDefined(typeof(Facing), facingValue)) { return(CommandResult.CreateFromFailure(CommandErrors.InvalidCarinal, _invalidCardinal)); } return(CommandResult.CreateFromSuccess(command, input)); }
/// <inheritdoc /> public PlayerServiceResult PlacePlayer(string parameters) { string[] texts = parameters.Split(','); var xPlayerPosition = Convert.ToInt32(texts[0].Trim()); var yPlayerPosition = Convert.ToInt32(texts[1].Trim()); var playerFacing = Enum.Parse <Facing>(StringExtentions.AllToUppercase(texts[2]).Trim()); var isValid = CheckValidPlacement(xPlayerPosition, yPlayerPosition); if (!isValid) { return(PlayerServiceResult.CreateFromFailure(_invalidPlacing + $"\n({_gameConfig.MinWidth},{_gameConfig.MinHeight}) ({_gameConfig.MaxWidth},{_gameConfig.MaxHeight})")); } SetPlayerPlacement(xPlayerPosition, yPlayerPosition, playerFacing); _gameConfig.IsPlayerPlaced = true; return(PlayerServiceResult.CreateFromSuccess()); }