From 54e1024637b14c2aad4bc5d276741babb408b15b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 24 Feb 2014 20:10:50 +0100 Subject: Add GUI and image support --- src/esieequest/Command.java | 6 +- src/esieequest/Game.java | 261 +----------------------------------- src/esieequest/GameEngine.java | 270 ++++++++++++++++++++++++++++++++++++++ src/esieequest/Parser.java | 21 ++- src/esieequest/Room.java | 16 ++- src/esieequest/UserInterface.java | 135 +++++++++++++++++++ 6 files changed, 436 insertions(+), 273 deletions(-) create mode 100644 src/esieequest/GameEngine.java create mode 100644 src/esieequest/UserInterface.java (limited to 'src') diff --git a/src/esieequest/Command.java b/src/esieequest/Command.java index fd05930..14c3d32 100644 --- a/src/esieequest/Command.java +++ b/src/esieequest/Command.java @@ -1,8 +1,6 @@ package esieequest; /** - * A text command that triggers an action in the game. - * * This class holds information about a command that was issued by the user. A * command currently consists of two strings: a command word and a second word * (for example, if the command was "take map", then the two strings obviously @@ -33,8 +31,8 @@ public class Command { * @param secondWord * The second word of the command. */ - public Command(final String pCommandWord, final String pSecondWord) { - this.aCommandWord = pCommandWord; + public Command(final String pFirstWord, final String pSecondWord) { + this.aCommandWord = pFirstWord; this.aSecondWord = pSecondWord; } diff --git a/src/esieequest/Game.java b/src/esieequest/Game.java index 43db8f7..925f067 100644 --- a/src/esieequest/Game.java +++ b/src/esieequest/Game.java @@ -1,7 +1,5 @@ package esieequest; -import java.util.HashMap; - /** * The game engine. * @@ -15,262 +13,15 @@ import java.util.HashMap; * @version February 2014 */ public class Game { - private HashMap aRooms; - private Room aCurrentRoom; - private Parser aParser; + private UserInterface aGui; + private GameEngine aEngine; /** - * Create the game and initialize its internal map. + * Create the game and initialise its internal map. */ public Game() { - this.aRooms = new HashMap(); - this.createRooms(); - this.aParser = new Parser(); - this.play(); - } - - /** - * Main play routine. Loops until end of play. - */ - private void play() { - this.printWelcome(); - boolean vFinished = false; - while (!vFinished) { - Command vCommand = this.aParser.getCommand(); - vFinished = this.processCommand(vCommand); - } - - System.out.println("Thank you for playing. Good bye."); - } - - /** - * Create all the rooms and link their exits together. - */ - private void createRooms() { - // create rooms - this.aRooms.put("AmphitheaterSeat", new Room("in the amphitheater")); - this.aRooms.put("AmphitheaterStage", new Room("on the amphitheater stage")); - - this.aRooms.put("CafeteriaStreet", new Room("in the main corridor, in front of the cafeteria")); - this.aRooms.put("Cafeteria", new Room("at the cafeteria")); - - this.aRooms.put("EsieespaceStreet", new Room("in the main corridor, in front of the ESIEEspace HQ")); - this.aRooms.put("EsieespaceFront", new Room("in front of the ESIEEspace HQ")); - this.aRooms.put("EsieespaceEntrance", new Room("at the ESIEEspace HQ entrance")); - this.aRooms.put("Esieespace", new Room("in the ESIEEspace HQ")); - - this.aRooms.put("ClubnixStreet", new Room("in the main corridor, in front of the Club*Nix")); - this.aRooms.put("ClubnixFront", new Room("in front of the Club*Nix")); - this.aRooms.put("ClubnixEntrance", new Room("at the Club*Nix entrance")); - this.aRooms.put("Clubnix", new Room("in the Club*Nix")); - - this.aRooms.put("EntranceStreet", new Room("in the main corridor, at the reception")); - this.aRooms.put("EntranceStairs", new Room("on the main entrance stairs")); - this.aRooms.put("EntranceRoundabout", new Room("on the roundabout")); - - this.aRooms.put("WingStreet", new Room("in font of wing #3")); - this.aRooms.put("WingCorridorOne", new Room("in the corridor in wing #3, on the ground floor")); - this.aRooms.put("WingStairsOne", new Room("in the stairwell on the ground floor")); - this.aRooms.put("WingStairsTwo", new Room("in the stairwell on the first floor")); - this.aRooms.put("WingCorridorTwo", new Room("in the corridor in wind #3, on the first floor")); - this.aRooms.put("WingCorridorTwoOffice", new Room("in front of the office #3254")); - this.aRooms.put("WingOffice", new Room("in the office #3254")); - - this.aRooms.put("OffscriptEat", new Room("somewhere implementing hunger")); - this.aRooms.put("OffscriptEatPantry", new Room("in the pantry")); - this.aRooms.put("OffscriptTake", new Room("somewhere implementing weight")); - this.aRooms.put("OffscriptTakeStorageroom", new Room("in a storage room")); - this.aRooms.put("OffscriptTimeout", new Room("somewhere implementing time")); - this.aRooms.put("OffscriptTimeoutCountdownroom", new Room("in a dangerous room")); - this.aRooms.put("OffscriptTrapdoor", new Room("somewhere implementing a trap")); - this.aRooms.put("OffscriptTrapdoorDeadend", new Room("trapped")); - this.aRooms.put("OffscriptBeamer", new Room("somewhere implementing teleportation")); - this.aRooms.put("OffscriptBeamerAnchor", new Room("on a checkpoint")); - this.aRooms.put("OffscriptLock", new Room("somewhere implementing a doorlock")); - this.aRooms.put("OffscriptLockLockedroom", new Room("in a locked room that is not anymore")); - this.aRooms.put("OffscriptAlea", new Room("somewhere implementing alea")); - this.aRooms.put("OffscriptAleaRoomrandomizer", new Room("in a weird room that will transport you somewhere else")); - this.aRooms.put("OffscriptMovingcharacter", new Room("somewhere implementing a moving character")); - this.aRooms.put("OffscriptMovingcharacterMo", new Room("in M-O's room")); - - // connect rooms - this.aRooms.get("AmphitheaterSeat").setExit("north", this.aRooms.get("AmphitheaterStage")); - this.aRooms.get("AmphitheaterStage").setExit("west", this.aRooms.get("Cafeteria")); - - this.aRooms.get("CafeteriaStreet").setExit("south", this.aRooms.get("Cafeteria")); - this.aRooms.get("CafeteriaStreet").setExit("east", this.aRooms.get("EsieespaceStreet")); - this.aRooms.get("Cafeteria").setExit("north", this.aRooms.get("CafeteriaStreet")); - this.aRooms.get("Cafeteria").setExit("east", this.aRooms.get("AmphitheaterStage")); - - this.aRooms.get("EsieespaceStreet").setExit("west", this.aRooms.get("Cafeteria")); - this.aRooms.get("EsieespaceStreet").setExit("south", this.aRooms.get("EsieespaceFront")); - this.aRooms.get("EsieespaceStreet").setExit("east", this.aRooms.get("EntranceStreet")); - this.aRooms.get("EsieespaceFront").setExit("north", this.aRooms.get("EsieespaceStreet")); - this.aRooms.get("EsieespaceFront").setExit("east", this.aRooms.get("EsieespaceEntrance")); - this.aRooms.get("EsieespaceEntrance").setExit("north", this.aRooms.get("Esieespace")); - this.aRooms.get("EsieespaceEntrance").setExit("west", this.aRooms.get("EsieespaceFront")); - this.aRooms.get("Esieespace").setExit("south", this.aRooms.get("EsieespaceEntrance")); - - this.aRooms.get("ClubnixStreet").setExit("west", this.aRooms.get("WingStreet")); - this.aRooms.get("ClubnixStreet").setExit("south", this.aRooms.get("ClubnixFront")); - this.aRooms.get("ClubnixFront").setExit("north", this.aRooms.get("ClubnixStreet")); - this.aRooms.get("ClubnixFront").setExit("east", this.aRooms.get("ClubnixEntrance")); - this.aRooms.get("ClubnixEntrance").setExit("north", this.aRooms.get("Clubnix")); - this.aRooms.get("ClubnixEntrance").setExit("west", this.aRooms.get("ClubnixFront")); - this.aRooms.get("Clubnix").setExit("south", this.aRooms.get("ClubnixEntrance")); - - this.aRooms.get("EntranceStreet").setExit("west", this.aRooms.get("EsieespaceStreet")); - this.aRooms.get("EntranceStreet").setExit("south", this.aRooms.get("EntranceStairs")); - this.aRooms.get("EntranceStreet").setExit("east", this.aRooms.get("WingStreet")); - this.aRooms.get("EntranceStairs").setExit("north", this.aRooms.get("EntranceStreet")); - this.aRooms.get("EntranceStairs").setExit("south", this.aRooms.get("EntranceRoundabout")); - this.aRooms.get("EntranceRoundabout").setExit("north", this.aRooms.get("EntranceStairs")); - - this.aRooms.get("WingStreet").setExit("north", this.aRooms.get("WingCorridorOne")); - this.aRooms.get("WingStreet").setExit("west", this.aRooms.get("EntranceStreet")); - this.aRooms.get("WingStreet").setExit("east", this.aRooms.get("ClubnixStreet")); - this.aRooms.get("WingCorridorOne").setExit("west", this.aRooms.get("WingStairsOne")); - this.aRooms.get("WingCorridorOne").setExit("south", this.aRooms.get("WingStreet")); - this.aRooms.get("WingCorridorOne").setExit("east", this.aRooms.get("OffscriptEat")); - this.aRooms.get("WingStairsOne").setExit("south", this.aRooms.get("WingStairsTwo")); - this.aRooms.get("WingStairsOne").setExit("up", this.aRooms.get("WingStairsTwo")); - this.aRooms.get("WingStairsOne").setExit("east", this.aRooms.get("WingCorridorOne")); - this.aRooms.get("WingStairsTwo").setExit("south", this.aRooms.get("WingStairsOne")); - this.aRooms.get("WingStairsTwo").setExit("down", this.aRooms.get("WingStairsOne")); - this.aRooms.get("WingStairsTwo").setExit("east", this.aRooms.get("WingCorridorTwo")); - this.aRooms.get("WingCorridorTwo").setExit("north", this.aRooms.get("WingCorridorTwoOffice")); - this.aRooms.get("WingCorridorTwoOffice").setExit("south", this.aRooms.get("WingCorridorTwo")); - this.aRooms.get("WingCorridorTwoOffice").setExit("east", this.aRooms.get("WingOffice")); - this.aRooms.get("WingOffice").setExit("west", this.aRooms.get("WingCorridorTwoOffice")); - - this.aRooms.get("OffscriptEat").setExit("north", this.aRooms.get("OffscriptEatPantry")); - this.aRooms.get("OffscriptEat").setExit("west", this.aRooms.get("WingCorridorOne")); - this.aRooms.get("OffscriptEat").setExit("east", this.aRooms.get("OffscriptTake")); - this.aRooms.get("OffscriptEatPantry").setExit("south", this.aRooms.get("OffscriptEat")); - this.aRooms.get("OffscriptTake").setExit("north", this.aRooms.get("OffscriptTakeStorageroom")); - this.aRooms.get("OffscriptTake").setExit("west", this.aRooms.get("OffscriptEat")); - this.aRooms.get("OffscriptTake").setExit("east", this.aRooms.get("OffscriptTimeout")); - this.aRooms.get("OffscriptTakeStorageroom").setExit("south", this.aRooms.get("OffscriptTake")); - this.aRooms.get("OffscriptTimeout").setExit("north", this.aRooms.get("OffscriptTimeoutCountdownroom")); - this.aRooms.get("OffscriptTimeout").setExit("west", this.aRooms.get("OffscriptTakeStorageroom")); - this.aRooms.get("OffscriptTimeout").setExit("east", this.aRooms.get("OffscriptTrapdoor")); - this.aRooms.get("OffscriptTimeoutCountdownroom").setExit("south", this.aRooms.get("OffscriptTimeout")); - this.aRooms.get("OffscriptTrapdoor").setExit("north", this.aRooms.get("OffscriptTrapdoorDeadend")); - this.aRooms.get("OffscriptTrapdoor").setExit("west", this.aRooms.get("OffscriptTimeout")); - this.aRooms.get("OffscriptTrapdoor").setExit("east", this.aRooms.get("OffscriptBeamer")); - this.aRooms.get("OffscriptTrapdoorDeadend").setExit("south", this.aRooms.get("OffscriptTrapdoor")); - this.aRooms.get("OffscriptBeamer").setExit("north", this.aRooms.get("OffscriptBeamerAnchor")); - this.aRooms.get("OffscriptBeamer").setExit("west", this.aRooms.get("OffscriptTrapdoor")); - this.aRooms.get("OffscriptBeamer").setExit("east", this.aRooms.get("OffscriptLock")); - this.aRooms.get("OffscriptBeamerAnchor").setExit("south", this.aRooms.get("OffscriptBeamer")); - this.aRooms.get("OffscriptLock").setExit("north", this.aRooms.get("OffscriptLockLockedroom")); - this.aRooms.get("OffscriptLock").setExit("west", this.aRooms.get("OffscriptBeamer")); - this.aRooms.get("OffscriptLock").setExit("east", this.aRooms.get("OffscriptAlea")); - this.aRooms.get("OffscriptLockLockedroom").setExit("south", this.aRooms.get("OffscriptLock")); - this.aRooms.get("OffscriptAlea").setExit("north", this.aRooms.get("OffscriptAleaRoomrandomizer")); - this.aRooms.get("OffscriptAlea").setExit("west", this.aRooms.get("OffscriptLock")); - this.aRooms.get("OffscriptAlea").setExit("east", this.aRooms.get("OffscriptMovingcharacter")); - this.aRooms.get("OffscriptAleaRoomrandomizer").setExit("south", this.aRooms.get("OffscriptAlea")); - this.aRooms.get("OffscriptMovingcharacter").setExit("north", this.aRooms.get("OffscriptMovingcharacterMo")); - this.aRooms.get("OffscriptMovingcharacter").setExit("west", this.aRooms.get("OffscriptAlea")); - - // set the starting room - this.aCurrentRoom = this.aRooms.get("AmphitheaterSeat"); - } - - /** - * Try to go in one direction. If there is an exit, enter the new room, - * otherwise print an error message. - */ - public void goRoom(final Command pCommand) { - if (!pCommand.hasSecondWord()) { - System.out.println("Go where?"); - return; - } - - Room vNextRoom = aCurrentRoom.getExit(pCommand.getSecondWord()); - - if (vNextRoom == null) { - System.out.println("There is no door!"); - return; - } - - this.aCurrentRoom = vNextRoom; - System.out.println(this.aCurrentRoom.getLongDescription()); - } - - /** - * Print out the opening message for the player. - */ - private void printWelcome() { - System.out.println("Welcome to ESIEEquest"); - System.out.println("ESIEEquest is a new, incredibly surprising adventure game."); - System.out.println("Type 'help' if you need help."); - System.out.println(""); - System.out.println(this.aCurrentRoom.getLongDescription()); - } - - /** - * Print out some help information. Here we print some stupid, cryptic - * message and a list of the command words. - */ - private void printHelp() { - System.out.println("You are lost. You are alone. "); - System.out.println("You wander around at the university."); - System.out.println(""); - System.out.println("Your command words are:"); - System.out.println(this.aParser.showCommands()); - } - - private void look() { - System.out.println(this.aCurrentRoom.getLongDescription()); - } - - private void eat() { - System.out.println("You have eaten now and you are not hungry any more."); - } - - /** - * "Quit" was entered. Check the rest of the command to see whether we - * really quit the game. - * - * @return true, if this command quits the game, false otherwise. - */ - private boolean quit(final Command pCommand) { - if (pCommand.hasSecondWord()) { - System.out.println("Quit what?"); - return false; - } - return true; - } - - /** - * Given a command, process (that is: execute) the command. - * - * @param command - * The command to be processed. - * @return true If the command ends the game, false otherwise. - */ - private boolean processCommand(final Command pCommand) { - if (pCommand.getCommandWord() != null) { - switch (pCommand.getCommandWord()) { - case "go": - this.goRoom(pCommand); - return false; - case "look": - this.look(); - return false; - case "eat": - this.eat(); - return false; - case "help": - this.printHelp(); - return false; - case "quit": - return this.quit(pCommand); - } - } - System.out.println("I don't know what you mean..."); - return false; + this.aEngine = new GameEngine(); + this.aGui = new UserInterface(this.aEngine); + this.aEngine.setGUI(this.aGui); } } diff --git a/src/esieequest/GameEngine.java b/src/esieequest/GameEngine.java new file mode 100644 index 0000000..3a89413 --- /dev/null +++ b/src/esieequest/GameEngine.java @@ -0,0 +1,270 @@ +package esieequest; + +import java.util.HashMap; + +/** + * This class creates all rooms, creates the parser and starts the game. It also + * evaluates and executes the commands that the parser returns. + * + * @author Pacien TRAN-GIRARD + * @author Benoit LUBRANO DI SBARAGLIONE + * + * @version February 2014 + */ +public class GameEngine { + private HashMap aRooms; + private Room aCurrentRoom; + private Parser aParser; + private UserInterface aGui; + + /** + * Create the game and initialize its internal map. + */ + public GameEngine() { + this.aRooms = new HashMap(); + this.aParser = new Parser(); + this.createRooms(); + } + + public void setGUI(UserInterface pUserInterface) { + this.aGui = pUserInterface; + this.printWelcome(); + } + + /** + * Create all the rooms and link their exits together. + */ + private void createRooms() { + // create rooms + this.aRooms.put("AmphitheaterSeat", new Room("in the amphitheater")); + this.aRooms.put("AmphitheaterStage", new Room("on the amphitheater stage")); + + this.aRooms.put("CafeteriaStreet", new Room("in the main corridor, in front of the cafeteria")); + this.aRooms.put("Cafeteria", new Room("at the cafeteria")); + + this.aRooms.put("EsieespaceStreet", new Room("in the main corridor, in front of the ESIEEspace HQ")); + this.aRooms.put("EsieespaceFront", new Room("in front of the ESIEEspace HQ")); + this.aRooms.put("EsieespaceEntrance", new Room("at the ESIEEspace HQ entrance")); + this.aRooms.put("Esieespace", new Room("in the ESIEEspace HQ")); + + this.aRooms.put("ClubnixStreet", new Room("in the main corridor, in front of the Club*Nix")); + this.aRooms.put("ClubnixFront", new Room("in front of the Club*Nix")); + this.aRooms.put("ClubnixEntrance", new Room("at the Club*Nix entrance")); + this.aRooms.put("Clubnix", new Room("in the Club*Nix")); + + this.aRooms.put("EntranceStreet", new Room("in the main corridor, at the reception")); + this.aRooms.put("EntranceStairs", new Room("on the main entrance stairs")); + this.aRooms.put("EntranceRoundabout", new Room("on the roundabout")); + + this.aRooms.put("WingStreet", new Room("in font of wing #3")); + this.aRooms.put("WingCorridorOne", new Room("in the corridor in wing #3, on the ground floor")); + this.aRooms.put("WingStairsOne", new Room("in the stairwell on the ground floor")); + this.aRooms.put("WingStairsTwo", new Room("in the stairwell on the first floor")); + this.aRooms.put("WingCorridorTwo", new Room("in the corridor in wind #3, on the first floor")); + this.aRooms.put("WingCorridorTwoOffice", new Room("in front of the office #3254")); + this.aRooms.put("WingOffice", new Room("in the office #3254")); + + this.aRooms.put("OffscriptEat", new Room("somewhere implementing hunger")); + this.aRooms.put("OffscriptEatPantry", new Room("in the pantry")); + this.aRooms.put("OffscriptTake", new Room("somewhere implementing weight")); + this.aRooms.put("OffscriptTakeStorageroom", new Room("in a storage room")); + this.aRooms.put("OffscriptTimeout", new Room("somewhere implementing time")); + this.aRooms.put("OffscriptTimeoutCountdownroom", new Room("in a dangerous room")); + this.aRooms.put("OffscriptTrapdoor", new Room("somewhere implementing a trap")); + this.aRooms.put("OffscriptTrapdoorDeadend", new Room("trapped")); + this.aRooms.put("OffscriptBeamer", new Room("somewhere implementing teleportation")); + this.aRooms.put("OffscriptBeamerAnchor", new Room("on a checkpoint")); + this.aRooms.put("OffscriptLock", new Room("somewhere implementing a doorlock")); + this.aRooms.put("OffscriptLockLockedroom", new Room("in a locked room that is not anymore")); + this.aRooms.put("OffscriptAlea", new Room("somewhere implementing alea")); + this.aRooms.put("OffscriptAleaRoomrandomizer", new Room("in a weird room that will transport you somewhere else")); + this.aRooms.put("OffscriptMovingcharacter", new Room("somewhere implementing a moving character")); + this.aRooms.put("OffscriptMovingcharacterMo", new Room("in M-O's room")); + + // connect rooms + this.aRooms.get("AmphitheaterSeat").setExit("north", this.aRooms.get("AmphitheaterStage")); + this.aRooms.get("AmphitheaterStage").setExit("west", this.aRooms.get("Cafeteria")); + + this.aRooms.get("CafeteriaStreet").setExit("south", this.aRooms.get("Cafeteria")); + this.aRooms.get("CafeteriaStreet").setExit("east", this.aRooms.get("EsieespaceStreet")); + this.aRooms.get("Cafeteria").setExit("north", this.aRooms.get("CafeteriaStreet")); + this.aRooms.get("Cafeteria").setExit("east", this.aRooms.get("AmphitheaterStage")); + + this.aRooms.get("EsieespaceStreet").setExit("west", this.aRooms.get("Cafeteria")); + this.aRooms.get("EsieespaceStreet").setExit("south", this.aRooms.get("EsieespaceFront")); + this.aRooms.get("EsieespaceStreet").setExit("east", this.aRooms.get("EntranceStreet")); + this.aRooms.get("EsieespaceFront").setExit("north", this.aRooms.get("EsieespaceStreet")); + this.aRooms.get("EsieespaceFront").setExit("east", this.aRooms.get("EsieespaceEntrance")); + this.aRooms.get("EsieespaceEntrance").setExit("north", this.aRooms.get("Esieespace")); + this.aRooms.get("EsieespaceEntrance").setExit("west", this.aRooms.get("EsieespaceFront")); + this.aRooms.get("Esieespace").setExit("south", this.aRooms.get("EsieespaceEntrance")); + + this.aRooms.get("ClubnixStreet").setExit("west", this.aRooms.get("WingStreet")); + this.aRooms.get("ClubnixStreet").setExit("south", this.aRooms.get("ClubnixFront")); + this.aRooms.get("ClubnixFront").setExit("north", this.aRooms.get("ClubnixStreet")); + this.aRooms.get("ClubnixFront").setExit("east", this.aRooms.get("ClubnixEntrance")); + this.aRooms.get("ClubnixEntrance").setExit("north", this.aRooms.get("Clubnix")); + this.aRooms.get("ClubnixEntrance").setExit("west", this.aRooms.get("ClubnixFront")); + this.aRooms.get("Clubnix").setExit("south", this.aRooms.get("ClubnixEntrance")); + + this.aRooms.get("EntranceStreet").setExit("west", this.aRooms.get("EsieespaceStreet")); + this.aRooms.get("EntranceStreet").setExit("south", this.aRooms.get("EntranceStairs")); + this.aRooms.get("EntranceStreet").setExit("east", this.aRooms.get("WingStreet")); + this.aRooms.get("EntranceStairs").setExit("north", this.aRooms.get("EntranceStreet")); + this.aRooms.get("EntranceStairs").setExit("south", this.aRooms.get("EntranceRoundabout")); + this.aRooms.get("EntranceRoundabout").setExit("north", this.aRooms.get("EntranceStairs")); + + this.aRooms.get("WingStreet").setExit("north", this.aRooms.get("WingCorridorOne")); + this.aRooms.get("WingStreet").setExit("west", this.aRooms.get("EntranceStreet")); + this.aRooms.get("WingStreet").setExit("east", this.aRooms.get("ClubnixStreet")); + this.aRooms.get("WingCorridorOne").setExit("west", this.aRooms.get("WingStairsOne")); + this.aRooms.get("WingCorridorOne").setExit("south", this.aRooms.get("WingStreet")); + this.aRooms.get("WingCorridorOne").setExit("east", this.aRooms.get("OffscriptEat")); + this.aRooms.get("WingStairsOne").setExit("south", this.aRooms.get("WingStairsTwo")); + this.aRooms.get("WingStairsOne").setExit("up", this.aRooms.get("WingStairsTwo")); + this.aRooms.get("WingStairsOne").setExit("east", this.aRooms.get("WingCorridorOne")); + this.aRooms.get("WingStairsTwo").setExit("south", this.aRooms.get("WingStairsOne")); + this.aRooms.get("WingStairsTwo").setExit("down", this.aRooms.get("WingStairsOne")); + this.aRooms.get("WingStairsTwo").setExit("east", this.aRooms.get("WingCorridorTwo")); + this.aRooms.get("WingCorridorTwo").setExit("north", this.aRooms.get("WingCorridorTwoOffice")); + this.aRooms.get("WingCorridorTwoOffice").setExit("south", this.aRooms.get("WingCorridorTwo")); + this.aRooms.get("WingCorridorTwoOffice").setExit("east", this.aRooms.get("WingOffice")); + this.aRooms.get("WingOffice").setExit("west", this.aRooms.get("WingCorridorTwoOffice")); + + this.aRooms.get("OffscriptEat").setExit("north", this.aRooms.get("OffscriptEatPantry")); + this.aRooms.get("OffscriptEat").setExit("west", this.aRooms.get("WingCorridorOne")); + this.aRooms.get("OffscriptEat").setExit("east", this.aRooms.get("OffscriptTake")); + this.aRooms.get("OffscriptEatPantry").setExit("south", this.aRooms.get("OffscriptEat")); + this.aRooms.get("OffscriptTake").setExit("north", this.aRooms.get("OffscriptTakeStorageroom")); + this.aRooms.get("OffscriptTake").setExit("west", this.aRooms.get("OffscriptEat")); + this.aRooms.get("OffscriptTake").setExit("east", this.aRooms.get("OffscriptTimeout")); + this.aRooms.get("OffscriptTakeStorageroom").setExit("south", this.aRooms.get("OffscriptTake")); + this.aRooms.get("OffscriptTimeout").setExit("north", this.aRooms.get("OffscriptTimeoutCountdownroom")); + this.aRooms.get("OffscriptTimeout").setExit("west", this.aRooms.get("OffscriptTakeStorageroom")); + this.aRooms.get("OffscriptTimeout").setExit("east", this.aRooms.get("OffscriptTrapdoor")); + this.aRooms.get("OffscriptTimeoutCountdownroom").setExit("south", this.aRooms.get("OffscriptTimeout")); + this.aRooms.get("OffscriptTrapdoor").setExit("north", this.aRooms.get("OffscriptTrapdoorDeadend")); + this.aRooms.get("OffscriptTrapdoor").setExit("west", this.aRooms.get("OffscriptTimeout")); + this.aRooms.get("OffscriptTrapdoor").setExit("east", this.aRooms.get("OffscriptBeamer")); + this.aRooms.get("OffscriptTrapdoorDeadend").setExit("south", this.aRooms.get("OffscriptTrapdoor")); + this.aRooms.get("OffscriptBeamer").setExit("north", this.aRooms.get("OffscriptBeamerAnchor")); + this.aRooms.get("OffscriptBeamer").setExit("west", this.aRooms.get("OffscriptTrapdoor")); + this.aRooms.get("OffscriptBeamer").setExit("east", this.aRooms.get("OffscriptLock")); + this.aRooms.get("OffscriptBeamerAnchor").setExit("south", this.aRooms.get("OffscriptBeamer")); + this.aRooms.get("OffscriptLock").setExit("north", this.aRooms.get("OffscriptLockLockedroom")); + this.aRooms.get("OffscriptLock").setExit("west", this.aRooms.get("OffscriptBeamer")); + this.aRooms.get("OffscriptLock").setExit("east", this.aRooms.get("OffscriptAlea")); + this.aRooms.get("OffscriptLockLockedroom").setExit("south", this.aRooms.get("OffscriptLock")); + this.aRooms.get("OffscriptAlea").setExit("north", this.aRooms.get("OffscriptAleaRoomrandomizer")); + this.aRooms.get("OffscriptAlea").setExit("west", this.aRooms.get("OffscriptLock")); + this.aRooms.get("OffscriptAlea").setExit("east", this.aRooms.get("OffscriptMovingcharacter")); + this.aRooms.get("OffscriptAleaRoomrandomizer").setExit("south", this.aRooms.get("OffscriptAlea")); + this.aRooms.get("OffscriptMovingcharacter").setExit("north", this.aRooms.get("OffscriptMovingcharacterMo")); + this.aRooms.get("OffscriptMovingcharacter").setExit("west", this.aRooms.get("OffscriptAlea")); + + // set the starting room + this.aCurrentRoom = this.aRooms.get("AmphitheaterSeat"); + } + + /** + * Try to go in one direction. If there is an exit, enter the new room, + * otherwise print an error message. + */ + public void goRoom(final Command pCommand) { + if (!pCommand.hasSecondWord()) { + this.aGui.println("Go where?"); + return; + } + + Room vNextRoom = aCurrentRoom.getExit(pCommand.getSecondWord()); + + if (vNextRoom == null) { + this.aGui.println("There is no door!"); + return; + } + + this.aCurrentRoom = vNextRoom; + this.aGui.println(this.aCurrentRoom.getLongDescription()); + + if (this.aCurrentRoom.getImageName() != null) { + this.aGui.showImage(this.aCurrentRoom.getImageName()); + } + } + + /** + * Print out the opening message for the player. + */ + private void printWelcome() { + this.aGui.println("Welcome to ESIEEquest"); + this.aGui.println("ESIEEquest is a new, incredibly surprising adventure game."); + this.aGui.println("Type 'help' if you need help."); + this.aGui.println(""); + this.look(); + this.aGui.showImage(this.aCurrentRoom.getImageName()); + } + + /** + * Print out some help information. Here we print some stupid, cryptic + * message and a list of the command words. + */ + private void printHelp() { + this.aGui.println("You are lost. You are alone. "); + this.aGui.println("You wander around at the university."); + this.aGui.println(""); + this.aGui.println("Your command words are:"); + this.aGui.println(this.aParser.showCommands()); + } + + private void look() { + this.aGui.println(this.aCurrentRoom.getLongDescription()); + } + + private void eat() { + this.aGui.println("You have eaten now and you are not hungry any more."); + } + + /** + * "Quit" was entered.F + */ + private void endGame() { + this.aGui.println("Thank you for playing. Good bye."); + this.aGui.enable(false); + } + + /** + * Given a command, process (that is: execute) the command. + * + * @param command + * The command to be processed. + * @return true If the command ends the game, false otherwise. + */ + private void processCommand(final Command pCommand) { + if (pCommand.getCommandWord() != null) { + switch (pCommand.getCommandWord()) { + case "go": + this.goRoom(pCommand); + return; + case "look": + this.look(); + return; + case "eat": + this.eat(); + return; + case "help": + this.printHelp(); + return; + case "quit": + this.endGame(); + return; + } + } + this.aGui.println("I don't know what you mean..."); + return; + } + + public void interpretCommand(final String pCommandLine) { + this.aGui.println(pCommandLine); + Command vCommand = this.aParser.getCommand(pCommandLine); + this.processCommand(vCommand); + } +} diff --git a/src/esieequest/Parser.java b/src/esieequest/Parser.java index d270f1e..a061abf 100644 --- a/src/esieequest/Parser.java +++ b/src/esieequest/Parser.java @@ -1,6 +1,7 @@ package esieequest; import java.util.Scanner; +import java.util.StringTokenizer; /** * This parser reads user input and tries to interpret it as an "Adventure" @@ -19,40 +20,34 @@ import java.util.Scanner; */ public class Parser { private CommandWords aValidCommands; - private Scanner aReader; /** * Create a parser to read from the terminal window. */ public Parser() { this.aValidCommands = new CommandWords(); - this.aReader = new Scanner(System.in); } /** * @return The next command from the user. */ - public Command getCommand() { - String vInputLine = null; + public Command getCommand(String pInputLine) { String vWord1 = null; String vWord2 = null; - System.out.print("> "); + StringTokenizer vTokenizer = new StringTokenizer(pInputLine); - vInputLine = this.aReader.nextLine(); - - Scanner vTokenizer = new Scanner(vInputLine); - if (vTokenizer.hasNext()) { - vWord1 = vTokenizer.next(); - if (vTokenizer.hasNext()) { - vWord2 = vTokenizer.next(); + if (vTokenizer.hasMoreTokens()) { + vWord1 = vTokenizer.nextToken(); + if (vTokenizer.hasMoreTokens()) { + vWord2 = vTokenizer.nextToken(); } } if (this.aValidCommands.isCommand(vWord1)) { return new Command(vWord1, vWord2); } else { - return new Command(null, null); + return new Command(null, vWord2); } } diff --git a/src/esieequest/Room.java b/src/esieequest/Room.java index 6d05b58..096463e 100644 --- a/src/esieequest/Room.java +++ b/src/esieequest/Room.java @@ -21,6 +21,7 @@ public class Room { private String aDescription; private HashMap aExits; + private String aImageName; /** * Create a room described "description". Initially, it has no exits. @@ -28,11 +29,17 @@ public class Room { * * @param description * The room's description. + * @param image + * name The room's image */ - public Room(final String pDescription) { + public Room(final String pDescription, final String pImageName) { this.aDescription = pDescription; this.aExits = new HashMap(); + this.aImageName = pImageName; + } + public Room(final String pDescription) { + this(pDescription, null); } /** @@ -94,4 +101,11 @@ public class Room { return vStrBuilder.toString(); } + /** + * Return a string describing the room's image name + */ + public String getImageName() { + return this.aImageName; + } + } diff --git a/src/esieequest/UserInterface.java b/src/esieequest/UserInterface.java new file mode 100644 index 0000000..1621bc5 --- /dev/null +++ b/src/esieequest/UserInterface.java @@ -0,0 +1,135 @@ +package esieequest; + +import javax.swing.*; + +import java.awt.*; +import java.awt.event.*; +import java.io.IOException; +import java.net.URL; +import java.awt.image.*; + +/** + * This class implements a simple graphical user interface with a text entry + * area, a text output area and an optional aImage. + * + * @author Pacien TRAN-GIRARD + * @author Benoit LUBRANO DI SBARAGLIONE + * + * @version February 2014 + */ +public class UserInterface implements ActionListener { + private GameEngine aEngine; + private JFrame aMyFrame; + private JTextField aEntryField; + private JTextArea aLog; + private JLabel aImage; + + /** + * Construct a UserInterface. As a parameter, a Game Engine (an object + * processing and executing the game commands) is needed. + * + * @param gameEngine + * The GameEngine object implementing the game logic. + */ + public UserInterface(GameEngine pGameEngine) { + this.aEngine = pGameEngine; + this.createGUI(); + } + + /** + * Print out some text into the text area. + */ + public void print(String pText) { + this.aLog.append(pText); + this.aLog.setCaretPosition(this.aLog.getDocument().getLength()); + } + + /** + * Print out some text into the text area, followed by a line break. + */ + public void println(String pText) { + this.print(pText + "\n"); + } + + /** + * Show an image file in the interface. + */ + public void showImage(String pImageName) { + URL vImageURL = this.getClass().getClassLoader().getResource(pImageName); + if (vImageURL == null) + System.out.println("image not found"); + else { + ImageIcon vIcon = new ImageIcon(vImageURL); + this.aImage.setIcon(vIcon); + this.aMyFrame.pack(); + } + } + + /** + * Enable or disable input in the input field. + */ + public void enable(boolean pOn) { + this.aEntryField.setEditable(pOn); + if (!pOn) { + this.aEntryField.getCaret().setBlinkRate(0); + } + } + + /** + * Set up graphical user interface. + */ + private void createGUI() { + this.aMyFrame = new JFrame("ESIEEquest"); + this.aEntryField = new JTextField(34); + + this.aLog = new JTextArea(); + this.aLog.setEditable(false); + JScrollPane vListScroller = new JScrollPane(this.aLog); + vListScroller.setPreferredSize(new Dimension(200, 200)); + vListScroller.setMinimumSize(new Dimension(100, 100)); + + JPanel vPanel = new JPanel(); + this.aImage = new JLabel(); + + vPanel.setLayout(new BorderLayout()); + vPanel.add(this.aImage, BorderLayout.NORTH); + vPanel.add(vListScroller, BorderLayout.CENTER); + vPanel.add(this.aEntryField, BorderLayout.SOUTH); + + this.aMyFrame.getContentPane().add(vPanel, BorderLayout.CENTER); + + // add some event listeners to some components + this.aMyFrame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent pEvent) { + System.exit(0); + } + }); + + this.aEntryField.addActionListener(this); + + this.aMyFrame.pack(); + this.aMyFrame.setVisible(true); + this.aEntryField.requestFocus(); + } + + /** + * Actionlistener interface for entry textfield. + */ + public void actionPerformed(ActionEvent pEvent) { + // no need to check the type of action at the moment. + // there is only one possible action: text entry + processCommand(); + } + + /** + * A command has been entered. Read the command and do whatever is necessary + * to process it. + */ + private void processCommand() { + boolean vFinished = false; + String vInput = this.aEntryField.getText(); + this.aEntryField.setText(""); + + this.aEngine.interpretCommand(vInput); + } +} -- cgit v1.2.3