As Exceptions might be new to you, here is an example of a very basic Text Adventure, that uses Exceptions. It is merely to give you an idea of how to use them in a real context - but of course, you're welcome to use it (you can hardly say "play it") and, if you want to, extend it.
Expand|Select|Wrap|Line Numbers
- import java.io.IOException;
- import java.util.Scanner;
- public class Chapter5 {
- private static Scanner scanner = new Scanner(System.in);
- /**
- * It should be known, if the light is on or off.
- */
- private static boolean lightIsOn = true;
- private static boolean roomHasLightSwitch = true;
- private static boolean canTalk = true;
- public static void main(String[] args)
- {
- while(true)
- {
- try
- {
- checkCommand();
- }
- catch(ActionNotPossibleException anpe)
- {
- System.err.println(anpe);
- if(!anpe.getCause().toString().equals(null))
- System.err.println("\t" + anpe.getCause());
- }
- }
- }
- /**
- * Checks, what command the user has given.
- * @throws ActionNotPossibleException
- */
- private static void checkCommand() throws ActionNotPossibleException
- {
- String command = scanner.nextLine();
- if(command.toLowerCase().startsWith("look at"))
- {
- lookAt(command.substring(8));
- }
- else if(command.toLowerCase().startsWith("move switch"))
- {
- moveSwitch();
- }
- else if(command.toLowerCase().startsWith("say"))
- {
- saySomething(command.substring(4));
- }
- else if(command.toLowerCase().startsWith("shout at"))
- {
- shoutAt(command.substring(9));
- }
- else if(command.toLowerCase().startsWith("quit"))
- System.exit(1);
- else System.out.println("Don't know that command.");
- }
- /**
- * Switch the light on if it's off and off if it's on.
- */
- public static void moveSwitch() throws ActionNotPossibleException
- {
- lightIsOn = !lightIsOn;
- if(roomHasLightSwitch)
- {
- if(lightIsOn) System.out.println("You switched the light on.");
- else System.out.println("You switched the light off.");
- }
- else throw new ActionNotPossibleException("move the light switch","You can't find a light switch.");
- }
- /**
- * Look at the targeted Object and give a comment about it.
- * @param target
- * @throws ActionNotPossibleNowException
- */
- public static void lookAt(String target) throws ActionNotPossibleException
- {
- if(!lightIsOn) throw new ActionNotPossibleException("look at " + target, "It is dark.");
- else
- {
- System.out.println("You look at the " + target + ". It's boring.\n");
- }
- }
- /**
- * Say something.
- * @param sentence
- * @throws UnableToSpeakException
- */
- public static void saySomething(String sentence) throws UnableToSpeakException
- {
- if(canTalk)
- {
- System.out.println(sentence + "\n");
- }
- else throw new UnableToSpeakException("Maybe you should shut up sometimes.");
- }
- /**
- * Shout at someone or something. It is of course one way of saying
- * something (even though a very unfriendly way).
- * @param target
- * @throws UnableToSpeakException
- */
- public static void shoutAt(String target) throws UnableToSpeakException
- {
- try
- {
- lookAt(target);
- System.out.println("You shout: \"SLARTIBARTFAST\" at " + target + ". No reaction.");
- }
- catch(ActionNotPossibleException anpe)
- {
- UnableToSpeakException utse = new UnableToSpeakException("");
- utse.initCause(anpe);
- throw utse;
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- /**
- * Our selfdefined Exception "ActionNotPossibleNowException".
- */
- class ActionNotPossibleException extends IOException
- {
- // This is just to make sure, the compiler compiles without giving a warning.
- // However, if you should continue developing this game, you might want to change
- // the Version UID.
- private static final long serialVersionUID = 1L;
- public ActionNotPossibleException(String action, String reason)
- {
- super("Could not " + action + " now. Reason: " + reason);
- }
- }
- /**
- * A second selfdefined Exception "UnableToSpeakException", which is an "ActionNotPossibleException".
- */
- class UnableToSpeakException extends ActionNotPossibleException
- {
- private static final long serialVersionUID = 1L;
- public UnableToSpeakException(String reason)
- {
- super("speak",reason);
- }
- }
Back to chapter 4