/** * Parses the String returning a TurnInstruction instance or throwing a * WrongInstructionFormatException() * * @param cad text String to parse * @return Instruction Reference pointing to an instance of a Instruction subclass, if it is * corresponding to the String cad * @throws WrongInstructionFormatException When the String is not TURN LEFT or RIGHT or GIRAR LEFT * or RIGHT */ public Instruction parse(String cad) throws WrongInstructionFormatException { String[] words = cad.split(" "); int length = words.length; Rotation rot; if (length == 2 && (words[0].equalsIgnoreCase("TURN") || words[0].equalsIgnoreCase("GIRAR"))) { rot = Rotation.parseRotation(words[1]); if (rot != Rotation.UNKNOWN) return new TurnInstruction(rot); else throw new WrongInstructionFormatException("Incorrect Format for instruction 'TURN'"); } else throw new WrongInstructionFormatException("Incorrect Format for instruction 'TURN'"); }
/** * Undo turn instruction, this implies rotate in the opposite direction and restore the energy * consumed in the rotation. */ public void undo() { navi.rotate(rotation.opposite()); // First, we perform an opposite rotation robot.addFuel(+5); // Later we restore the energy inverted in the rotation }