/// <summary> /// Rotates the robot in the specified direction by the specified amount /// </summary> /// <param name="direction">Direction in which to turn, positive number indicates clockwise, negative number indicates anticlockwise, magnitude is number of turns to rotate</param> public void Turn(Int32 direction) { if (this.Placed == false) { //Specialise exception to be TurnException etc depending on needs //Would normally throw exception, in this case, the command will just be ignored //throw new Exception("The robot has not been placed."); return; } var currentDirection = Robot.Directions.IndexOf(this.Direction); var newDirection = (currentDirection + direction) % Robot.Directions.Count; if (newDirection < 0) { newDirection += Robot.Directions.Count; } this.Direction = Robot.Directions[newDirection]; }
public Boolean Move(MoveVector vector) { if (this.Placed == false) { //Specialise exception to be MoveException etc depending on needs //Would normally throw exception, in this case, the command will just be ignored //throw new Exception("The robot has not been placed."); return(false); } var newPosition = new TablePosition { X = this.Position.X, Y = this.Position.Y }; newPosition.X += vector.X; newPosition.Y += vector.Y; return(this.Table.Move(this, newPosition)); //Check with the table that the position is valid }
public String Command(String command) { if (command.IndexOf(Commands.Place) == 0) { if (command.Length < Commands.Place.Length + 1) { throw new Exception("No parameters were passed."); } String[] placeParams = command.Substring(Commands.Place.Length + 1).Split(','); var placePosition = new TablePosition(); if (placeParams.Length < 3) { throw new Exception("Not enough parameters. Three parameters are required."); } try { placePosition.X = Int32.Parse(placeParams[0]); } catch { throw new Exception(placeParams[0] + " is not a valid number."); } try { placePosition.Y = Int32.Parse(placeParams[1]); } catch { throw new Exception(placeParams[1] + " is not a valid number."); } var direction = Robot.Directions.FirstOrDefault(d => d.Name == placeParams[2]); if (direction == null) { throw new Exception("Could not find direction " + placeParams[2] + "."); } if (this.Table.Move(this, placePosition)) { this.Placed = true; this.Direction = direction; } } else if (command == Commands.Move) { Move(this.Direction); } else if (command == Commands.Left) { Turn(-1); } else if (command == Commands.Right) { Turn(1); } else if (command == Commands.Report) { if (this.Placed == false) { throw new Exception("The robot has not been placed."); } return(this.Position.X + "," + this.Position.Y + "," + this.Direction.Name); } else { //Specialise exception to be CommandException etc depending on needs throw new UnknownCommandException("Command not recognised."); } return(""); }