From c9d890b9b71efddf4b9b39354c78b5bff3dffac6 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 23 Feb 2014 16:45:19 +0100 Subject: Use HashMap to store exits --- src/esieequest/Game.java | 120 ++++++++++++++++++++++++++++++----------------- src/esieequest/Room.java | 54 +++++++++++---------- 2 files changed, 105 insertions(+), 69 deletions(-) diff --git a/src/esieequest/Game.java b/src/esieequest/Game.java index a00d901..a0e0cd2 100644 --- a/src/esieequest/Game.java +++ b/src/esieequest/Game.java @@ -78,49 +78,83 @@ public class Game { Room vOffscriptMovingcharacterMo = new Room("in M-O's room"); // connect rooms (N, W, S, E) - vAmphitheaterSeat.setExits(vAmphitheaterStage, null, null, null); - vAmphitheaterStage.setExits(null, vCafeteria, null, null); - - vCafeteriaStreet.setExits(null, null, vCafeteria, vEsieespaceStreet); - vCafeteria.setExits(vCafeteriaStreet, null, null, vAmphitheaterStage); - - vEsieespaceStreet.setExits(null, vCafeteria, vEsieespaceFront, vEntranceStreet); - vEsieespaceFront.setExits(vEsieespaceStreet, null, null, vEsieespaceEntrance); - vEsieespaceEntrance.setExits(vEsieespace, vEsieespaceFront, null, null); - vEsieespace.setExits(null, null, vEsieespaceEntrance, null); - - vClubnixStreet.setExits(null, vWingStreet, vClubnixFront, null); - vClubnixFront.setExits(vClubnixStreet, null, null, vClubnixEntrance); - vClubnixEntrance.setExits(vClubnix, vClubnixFront, null, null); - vClubnix.setExits(null, null, vClubnixEntrance, null); - - vEntranceStreet.setExits(null, vEsieespaceStreet, vEntranceStairs, vWingStreet); - vEntranceStairs.setExits(vEntranceStreet, null, vEntranceRoundabout, null); - vEntranceRoundabout.setExits(vEntranceStairs, null, null, null); - - vWingStreet.setExits(vWingCorridorOne, vEntranceStreet, null, vClubnixStreet); - vWingCorridorOne.setExits(null, vWingStairsOne, vWingStreet, vOffscriptEat); - vWingStairsOne.setExits(null, null, vWingStairsTwo, vWingCorridorOne); - vWingStairsTwo.setExits(null, null, vWingStairsOne, vWingCorridorTwo); - vWingCorridorTwo.setExits(vWingCorridorTwoOffice, null, null, null); - vWingCorridorTwoOffice.setExits(null, null, vWingCorridorTwo, vWingOffice); - vWingOffice.setExits(null, vWingCorridorTwoOffice, null, null); - - vOffscriptEat.setExits(vOffscriptEatPantry, vWingCorridorOne, null, vOffscriptTake); - vOffscriptEatPantry.setExits(null, null, vOffscriptEat, null); - vOffscriptTake.setExits(vOffscriptTakeStorageroom, vOffscriptEat, null, vOffscriptTimeout); - vOffscriptTakeStorageroom.setExits(null, null, vOffscriptTake, null); - vOffscriptTimeout.setExits(vOffscriptTimeoutCountdownroom, vOffscriptTakeStorageroom, null, vOffscriptTrapdoor); - vOffscriptTimeoutCountdownroom.setExits(null, null, vOffscriptTimeout, null); - vOffscriptTrapdoor.setExits(vOffscriptTrapdoorDeadend, vOffscriptTimeout, null, vOffscriptBeamer); - vOffscriptTrapdoorDeadend.setExits(null, null, vOffscriptTrapdoor, null); - vOffscriptBeamer.setExits(vOffscriptBeamerAnchor, vOffscriptTrapdoor, null, vOffscriptLock); - vOffscriptBeamerAnchor.setExits(null, null, vOffscriptBeamer, null); - vOffscriptLock.setExits(vOffscriptLockLockedroom, vOffscriptBeamer, null, vOffscriptAlea); - vOffscriptLockLockedroom.setExits(null, null, vOffscriptLock, null); - vOffscriptAlea.setExits(vOffscriptAleaRoomrandomizer, vOffscriptLock, null, vOffscriptMovingcharacter); - vOffscriptAleaRoomrandomizer.setExits(null, null, vOffscriptAlea, null); - vOffscriptMovingcharacter.setExits(vOffscriptMovingcharacterMo, vOffscriptAlea, null, null); + vAmphitheaterSeat.setExit("north", vAmphitheaterStage); + vAmphitheaterStage.setExit("west", vCafeteria); + + vCafeteriaStreet.setExit("south", vCafeteria); + vCafeteriaStreet.setExit("east", vEsieespaceStreet); + vCafeteria.setExit("north", vCafeteriaStreet); + vCafeteria.setExit("east", vAmphitheaterStage); + + vEsieespaceStreet.setExit("west", vCafeteria); + vEsieespaceStreet.setExit("south", vEsieespaceFront); + vEsieespaceStreet.setExit("east", vEntranceStreet); + vEsieespaceFront.setExit("north", vEsieespaceStreet); + vEsieespaceFront.setExit("east", vEsieespaceEntrance); + vEsieespaceEntrance.setExit("north", vEsieespace); + vEsieespaceEntrance.setExit("west", vEsieespaceFront); + vEsieespace.setExit("south", vEsieespaceEntrance); + + vClubnixStreet.setExit("west", vWingStreet); + vClubnixStreet.setExit("south", vClubnixFront); + vClubnixFront.setExit("north", vClubnixStreet); + vClubnixFront.setExit("east", vClubnixEntrance); + vClubnixEntrance.setExit("north", vClubnix); + vClubnixEntrance.setExit("west", vClubnixFront); + vClubnix.setExit("south", vClubnixEntrance); + + vEntranceStreet.setExit("west", vEsieespaceStreet); + vEntranceStreet.setExit("south", vEntranceStairs); + vEntranceStreet.setExit("east", vWingStreet); + vEntranceStairs.setExit("north", vEntranceStreet); + vEntranceStairs.setExit("south", vEntranceRoundabout); + vEntranceRoundabout.setExit("north", vEntranceStairs); + + vWingStreet.setExit("north", vWingCorridorOne); + vWingStreet.setExit("west", vEntranceStreet); + vWingStreet.setExit("east", vClubnixStreet); + vWingCorridorOne.setExit("west", vWingStairsOne); + vWingCorridorOne.setExit("south", vWingStreet); + vWingCorridorOne.setExit("east", vOffscriptEat); + vWingStairsOne.setExit("south", vWingStairsTwo); + vWingStairsOne.setExit("east", vWingCorridorOne); + vWingStairsTwo.setExit("south", vWingStairsOne); + vWingStairsTwo.setExit("east", vWingCorridorTwo); + vWingCorridorTwo.setExit("north", vWingCorridorTwoOffice); + vWingCorridorTwoOffice.setExit("south", vWingCorridorTwo); + vWingCorridorTwoOffice.setExit("east", vWingOffice); + vWingOffice.setExit("west", vWingCorridorTwoOffice); + + vOffscriptEat.setExit("north", vOffscriptEatPantry); + vOffscriptEat.setExit("west", vWingCorridorOne); + vOffscriptEat.setExit("east", vOffscriptTake); + vOffscriptEatPantry.setExit("south", vOffscriptEat); + vOffscriptTake.setExit("north", vOffscriptTakeStorageroom); + vOffscriptTake.setExit("west", vOffscriptEat); + vOffscriptTake.setExit("east", vOffscriptTimeout); + vOffscriptTakeStorageroom.setExit("south", vOffscriptTake); + vOffscriptTimeout.setExit("north", vOffscriptTimeoutCountdownroom); + vOffscriptTimeout.setExit("west", vOffscriptTakeStorageroom); + vOffscriptTimeout.setExit("east", vOffscriptTrapdoor); + vOffscriptTimeoutCountdownroom.setExit("south", vOffscriptTimeout); + vOffscriptTrapdoor.setExit("north", vOffscriptTrapdoorDeadend); + vOffscriptTrapdoor.setExit("west", vOffscriptTimeout); + vOffscriptTrapdoor.setExit("east", vOffscriptBeamer); + vOffscriptTrapdoorDeadend.setExit("south", vOffscriptTrapdoor); + vOffscriptBeamer.setExit("north", vOffscriptBeamerAnchor); + vOffscriptBeamer.setExit("west", vOffscriptTrapdoor); + vOffscriptBeamer.setExit("east", vOffscriptLock); + vOffscriptBeamerAnchor.setExit("south", vOffscriptBeamer); + vOffscriptLock.setExit("north", vOffscriptLockLockedroom); + vOffscriptLock.setExit("west", vOffscriptBeamer); + vOffscriptLock.setExit("east", vOffscriptAlea); + vOffscriptLockLockedroom.setExit("south", vOffscriptLock); + vOffscriptAlea.setExit("north", vOffscriptAleaRoomrandomizer); + vOffscriptAlea.setExit("west", vOffscriptLock); + vOffscriptAlea.setExit("east", vOffscriptMovingcharacter); + vOffscriptAleaRoomrandomizer.setExit("south", vOffscriptAlea); + vOffscriptMovingcharacter.setExit("north", vOffscriptMovingcharacterMo); + vOffscriptMovingcharacter.setExit("west", vOffscriptAlea); // set the starting room this.aCurrentRoom = vAmphitheaterSeat; diff --git a/src/esieequest/Room.java b/src/esieequest/Room.java index 5b4dcfa..780f76a 100644 --- a/src/esieequest/Room.java +++ b/src/esieequest/Room.java @@ -1,5 +1,7 @@ package esieequest; +import java.util.HashMap; + /** * A room. * @@ -9,15 +11,18 @@ package esieequest; * @version February 2014 */ public class Room { - private String aDescription; - private Room aNorthExit; - private Room aSouthExit; - private Room aEastExit; - private Room aWestExit; + private String aDescription; + private HashMap aExits; + /** + * Create a room described "description "Initially, it has no exits. + * "description" is something like "a kitchen" or "an open courtyard". + */ public Room(final String pDescription) { this.aDescription = pDescription; + this.aExits = new HashMap(); + } public String getDescription() { @@ -25,27 +30,23 @@ public class Room { } /** - * Defines the four exits (other rooms) of this room. + * Define an exit from this room. + * + * @param direction + * The direction of the exit. + * @param neighbor + * The room in the given direction. */ - public void setExits(final Room pNorthExit, final Room pWestExit, final Room pSouthExit, final Room pEastExit) { - this.aNorthExit = pNorthExit; - this.aSouthExit = pSouthExit; - this.aEastExit = pEastExit; - this.aWestExit = pWestExit; + public void setExit(String direction, Room neighbor) { + this.aExits.put(direction, neighbor); } + /** + * Return the room that is reached if we go from this room in direction + * "direction". If there is no room in that direction, return null. + */ public Room getExit(String pDirection) { - switch (pDirection) { - case "north": - return this.aNorthExit; - case "south": - return this.aSouthExit; - case "east": - return this.aEastExit; - case "west": - return this.aWestExit; - } - return null; + return this.aExits.get(pDirection); } /** @@ -56,19 +57,20 @@ public class Room { */ public String getExitString() { String vExitsString = "Available exits:"; - if (this.aNorthExit != null) { + if (this.aExits.get("north") != null) { vExitsString += " North"; } - if (this.aSouthExit != null) { + if (this.aExits.get("south") != null) { vExitsString += " South"; } - if (this.aEastExit != null) { + if (this.aExits.get("east") != null) { vExitsString += " East"; } - if (this.aWestExit != null) { + if (this.aExits.get("west") != null) { vExitsString += " West"; } vExitsString += "."; return vExitsString; } + } -- cgit v1.2.3