/// <summary> /// Run the Report command. Can be overridden. /// </summary> /// <returns>CommandResult</returns> protected virtual GenericResult DoReport(ParseCommandResult parseCommandResult) { GenericResult result = new GenericResult(); result.Output = _currentState.ToString(); result.Success = true; return(result); }
/// <summary> /// Run the Right command. Can be overridden. /// </summary> /// <param name="parseCommandResult"></param> /// <returns>CommandResult</returns> protected virtual GenericResult DoRight(ParseCommandResult parseCommandResult) { GenericResult result = new GenericResult(); var newDirection = RotateDirection(90); var newState = CloneGridItem(_currentState); newState.Facing = newDirection; AddItemStateToHistory(newState); result.Success = true; return(result); }
/// <summary> /// Run the Place command. Can be overridden. /// </summary> /// <param name="parseCommandResult"></param> /// <returns>CommandResult</returns> protected virtual GenericResult DoPlace(ParseCommandResult parseCommandResult) { GenericResult result = new GenericResult(); // Check if the new coords are within the boundary of the grid. if (_taskGrid.IsInGridBounds(parseCommandResult.Coordinates)) { var newState = new GridItemState() { Coords = parseCommandResult.Coordinates, Facing = parseCommandResult.FacingDirection }; AddItemStateToHistory(newState); result.Success = true; } else { result.Comment = $"That command would position the {GRID_ITEM_TYPE_NAME} off the grid. Try again."; } return(result); }
/// <summary> /// Run the Move command. Can be overridden. /// </summary> /// <returns>CommandResult</returns> protected virtual GenericResult DoMove(ParseCommandResult parseCommandResult) { GenericResult result = new GenericResult(); // Get direction of item, and how to calculate the move. var newCoordsForMove = _currentState.GetCoordinatesForNextMove(); // check if the new coords are within the boundary of the grid. if (_taskGrid.IsInGridBounds(newCoordsForMove)) { var newState = CloneGridItem(_currentState); newState.Coords = newCoordsForMove; AddItemStateToHistory(newState); result.Success = true; } else { result.Comment = $"That move would position the {GRID_ITEM_TYPE_NAME} off the grid. Try another command. Current position: {_currentState.ToString()}"; } return(result); }
/// <summary> /// Parse the command string to ParseCommandResult /// </summary> /// <param name="command"></param> /// <returns>ParseCommandResult</returns> protected ParseCommandResult ParseCommand(string command) { ParseCommandResult result = new ParseCommandResult(); // Regex match for at least 1 whole word. Group the first word, and the rest (if there is any other text) in another group. Match match = Regex.Match(command.ToUpper(), @"([A-Z]+)(.*)"); if (match.Success) { // The first group is the base command. var commandWord = match.Groups[1].Value; // Any parameters will be in the 2nd group, if present. var parameters = string.Empty; if (match.Groups.Count == 3) { parameters = match.Groups[2].Value; } if (Enum.TryParse(commandWord, out CommandType thisCommand)) { // Command found. result.Command = thisCommand; switch (thisCommand) { case CommandType.PLACE: // Regex match for '[digit],[digit],[WHOLEWORD]' i.e. 1,2, NORTH - ignoring any white spaces. Match matchParams = Regex.Match(parameters, @"\W+(\d+)\W*,\W*(\d+)\W*,\W*([A-Z]+)\w*$"); if (matchParams.Success) { if (matchParams.Groups.Count == 4) { var x = Convert.ToInt32(matchParams.Groups[1].Value); var y = Convert.ToInt32(matchParams.Groups[2].Value); var direction = matchParams.Groups[3].Value; // Try and parse the direction to FacingDirection enum if (Enum.TryParse(direction, out FacingDirection facingDirection)) { result.Coordinates = new Coordinates(x, y); result.FacingDirection = facingDirection; result.Success = true; } } } break; default: // Check if any additioanl parameters have been given. // Note: We could decide to ignore these if we wanted to. if (string.IsNullOrEmpty(parameters)) { result.Success = true; } else { result.Comment = $"The {thisCommand.ToString()} command does not have any parameters. Please try again."; return(result); } break; } } else { result.Comment = "No valid command received"; } } else { result.Comment = "No commands found. Please try again."; } return(result); }