From e510b08d0925629dba304177099ccd3a81585ba9 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 23 Feb 2014 17:52:57 +0100 Subject: Make getExitString() more flexible and add getLongDescription() --- src/esieequest/Game.java | 3 +-- src/esieequest/Room.java | 44 +++++++++++++++++++++----------------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/esieequest/Game.java b/src/esieequest/Game.java index 1be4d15..bad0106 100644 --- a/src/esieequest/Game.java +++ b/src/esieequest/Game.java @@ -180,8 +180,7 @@ public class Game { } private void printLocationInfo() { - System.out.println("You are now " + this.aCurrentRoom.getDescription() + "."); - System.out.println(this.aCurrentRoom.getExitString()); + System.out.println(this.aCurrentRoom.getLongDescription()); } private void printWelcome() { diff --git a/src/esieequest/Room.java b/src/esieequest/Room.java index 1b23afa..9d7f071 100644 --- a/src/esieequest/Room.java +++ b/src/esieequest/Room.java @@ -1,6 +1,7 @@ package esieequest; import java.util.HashMap; +import java.util.Set; /** * A room. @@ -25,8 +26,19 @@ public class Room { } - public String getDescription() { - return this.aDescription; + /** + * Return a long description of this room, of the form: + * + * You are in the kitchen. + * + * Exits: north west + * + * @return A description of the room, including exits. + */ + public String getLongDescription() { + String vLongDescription = "You are now " + this.aDescription + ".\n"; + vLongDescription += getExitString(); + return vLongDescription; } /** @@ -50,33 +62,19 @@ public class Room { } /** - * Return a description of the room’s exits, for example, + * Return a description of the room’s exits, for example * "Exits: north west". * * @return A description of the available exits. */ public String getExitString() { - String vExitsString = "Available exits:"; - if (this.aExits.get("north") != null) { - vExitsString += " north"; - } - if (this.aExits.get("south") != null) { - vExitsString += " south"; - } - if (this.aExits.get("east") != null) { - vExitsString += " east"; - } - if (this.aExits.get("west") != null) { - vExitsString += " west"; - } - if (this.aExits.get("up") != null) { - vExitsString += " up"; - } - if (this.aExits.get("down") != null) { - vExitsString += " down"; + String vExitString = "Available exits:"; + Set keys = this.aExits.keySet(); + for (String exit : keys) { + vExitString += " " + exit; } - vExitsString += "."; - return vExitsString; + vExitString += "."; + return vExitString; } } -- cgit v1.2.3