From 5ddb6df63ebb643a0e179f21f2895950be6d83da Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 14:16:57 +0200 Subject: Reorganise the model package and implement items actions --- src/esieequest/controller/Interpreter.java | 9 +- src/esieequest/controller/Parser.java | 2 +- src/esieequest/controller/Performer.java | 41 ++++++- src/esieequest/controller/Utils.java | 40 +++++++ src/esieequest/model/Game.java | 8 +- src/esieequest/model/Item.java | 52 -------- src/esieequest/model/Player.java | 44 ------- src/esieequest/model/Quest.java | 22 ---- src/esieequest/model/Room.java | 153 ------------------------ src/esieequest/model/Side.java | 5 - src/esieequest/model/command/Command.java | 62 ---------- src/esieequest/model/command/CommandWord.java | 71 ----------- src/esieequest/model/command/package-info.java | 5 - src/esieequest/model/commands/Command.java | 62 ++++++++++ src/esieequest/model/commands/CommandWord.java | 71 +++++++++++ src/esieequest/model/commands/package-info.java | 5 + src/esieequest/model/entities/Player.java | 83 +++++++++++++ src/esieequest/model/entities/package-info.java | 5 + src/esieequest/model/events/Quest.java | 22 ++++ src/esieequest/model/events/package-info.java | 5 + src/esieequest/model/items/Inventory.java | 57 +++++++++ src/esieequest/model/items/Item.java | 45 +++++++ src/esieequest/model/items/package-info.java | 5 + src/esieequest/model/map/Room.java | 137 +++++++++++++++++++++ src/esieequest/model/map/Side.java | 5 + src/esieequest/model/map/package-info.java | 5 + src/esieequest/view/View.java | 11 +- src/esieequest/view/app/UserInterface.java | 8 +- src/esieequest/view/text/TextInterface.java | 17 ++- src/esieequest/view/web/WebInterface.java | 8 +- 30 files changed, 622 insertions(+), 443 deletions(-) create mode 100644 src/esieequest/controller/Utils.java delete mode 100644 src/esieequest/model/Item.java delete mode 100644 src/esieequest/model/Player.java delete mode 100644 src/esieequest/model/Quest.java delete mode 100644 src/esieequest/model/Room.java delete mode 100644 src/esieequest/model/Side.java delete mode 100644 src/esieequest/model/command/Command.java delete mode 100644 src/esieequest/model/command/CommandWord.java delete mode 100644 src/esieequest/model/command/package-info.java create mode 100644 src/esieequest/model/commands/Command.java create mode 100644 src/esieequest/model/commands/CommandWord.java create mode 100644 src/esieequest/model/commands/package-info.java create mode 100644 src/esieequest/model/entities/Player.java create mode 100644 src/esieequest/model/entities/package-info.java create mode 100644 src/esieequest/model/events/Quest.java create mode 100644 src/esieequest/model/events/package-info.java create mode 100644 src/esieequest/model/items/Inventory.java create mode 100644 src/esieequest/model/items/Item.java create mode 100644 src/esieequest/model/items/package-info.java create mode 100644 src/esieequest/model/map/Room.java create mode 100644 src/esieequest/model/map/Side.java create mode 100644 src/esieequest/model/map/package-info.java (limited to 'src') diff --git a/src/esieequest/controller/Interpreter.java b/src/esieequest/controller/Interpreter.java index 586b329..8d0f703 100644 --- a/src/esieequest/controller/Interpreter.java +++ b/src/esieequest/controller/Interpreter.java @@ -1,7 +1,7 @@ package esieequest.controller; import esieequest.model.Game; -import esieequest.model.command.Command; +import esieequest.model.commands.Command; import esieequest.view.View; /** @@ -72,6 +72,12 @@ class Interpreter { case "eat": this.performer.eat(); return; + case "take": + this.performer.take(command.getOption()); + return; + case "drop": + this.performer.drop(command.getOption()); + return; case "help": this.performer.showHelp(); return; @@ -83,4 +89,5 @@ class Interpreter { this.performer.echo("Unknown command."); return; } + } diff --git a/src/esieequest/controller/Parser.java b/src/esieequest/controller/Parser.java index 0f97fd1..452c27c 100644 --- a/src/esieequest/controller/Parser.java +++ b/src/esieequest/controller/Parser.java @@ -1,6 +1,6 @@ package esieequest.controller; -import esieequest.model.command.Command; +import esieequest.model.commands.Command; /** * The command parser. diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 96a95df..1abc887 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -1,7 +1,8 @@ package esieequest.controller; import esieequest.model.Game; -import esieequest.model.Room; +import esieequest.model.items.Inventory; +import esieequest.model.map.Room; import esieequest.view.View; /** @@ -130,4 +131,42 @@ class Performer { this.echo(this.game.getEatMessage()); } + /** + * Moves an item from the current Room to the Player's inventory. + * + * @param itemName + * the item's name + */ + public void take(final String itemName) { + this.moveItem(this.game.getPlayer().getCurrentRoom(), this.game.getPlayer(), itemName); + } + + /** + * Moves an item from the Player's inventory to the current Room. + * + * @param itemName + * the item's name + */ + public void drop(final String itemName) { + this.moveItem(this.game.getPlayer(), this.game.getPlayer().getCurrentRoom(), itemName); + } + + /** + * Moves a given item referred by its name from an inventory to another. + * + * @param source + * the source inventory + * @param dest + * the destination inventory + * @param itemName + * the item's name + */ + private void moveItem(final Inventory source, final Inventory dest, final String itemName) { + if (!source.hasItem(itemName)) { + this.echo("No such item."); + return; + } + dest.putItem(itemName, source.takeItem(itemName)); + } + } diff --git a/src/esieequest/controller/Utils.java b/src/esieequest/controller/Utils.java new file mode 100644 index 0000000..139798c --- /dev/null +++ b/src/esieequest/controller/Utils.java @@ -0,0 +1,40 @@ +package esieequest.controller; + +import java.util.Set; + +public class Utils { + + /** + * + * @param elements + * the elements + * @param prefix + * the prefix of the list + * @param emptyText + * the text that will be returned if the Set is empty + * @return the list + */ + public static String list(final Set elements, final String prefix, final String emptyText) { + if (!elements.isEmpty()) { + return prefix + Utils.buildListString(elements) + "."; + } + return emptyText; + } + + /** + * Builds a list in the form of a String from a given Set. + * + * @param elements + * the Set of Strings + * + * @return the list + */ + private static String buildListString(final Set elements) { + final StringBuilder list = new StringBuilder(); + for (final String string : elements) { + list.append(" ").append(string); + } + return list.toString(); + } + +} diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 0a9aa01..5ef4c22 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -2,6 +2,10 @@ package esieequest.model; import java.util.HashMap; +import esieequest.model.entities.Player; +import esieequest.model.items.Item; +import esieequest.model.map.Room; + /** * Represents the game. * @@ -187,8 +191,8 @@ public class Game { * Creates and adds items into rooms. */ private void createItems() { - this.rooms.get("Cafeteria").addItem("Banana", new Item("A yellow banana", 12)); - this.rooms.get("Cafeteria").addItem("Orange", new Item("An orange orange", 15)); + this.rooms.get("Cafeteria").putItem("banana", new Item("A yellow banana", 12)); + this.rooms.get("Cafeteria").putItem("orange", new Item("An orange orange", 15)); } /** diff --git a/src/esieequest/model/Item.java b/src/esieequest/model/Item.java deleted file mode 100644 index d1659ab..0000000 --- a/src/esieequest/model/Item.java +++ /dev/null @@ -1,52 +0,0 @@ -package esieequest.model; - -/** - * Represents an item with a description and a weight, stored in a room. Can be - * picked, used and dropped. - * - * @author Pacien TRAN-GIRARD - */ -public class Item { - - private final String description; - private final int weight; - - /** - * The default constructor. - */ - public Item() { - this(null, 0); - } - - /** - * Creates an item with the given characteristics. - * - * @param description - * the description of the item - * @param weight - * the weight of the item - */ - public Item(final String description, final int weight) { - this.description = description; - this.weight = weight; - } - - /** - * Returns the description of the item. - * - * @return the description - */ - public String getDescription() { - return this.description; - } - - /** - * Returns the weight of the item. - * - * @return the weight - */ - public int getWeight() { - return this.weight; - } - -} diff --git a/src/esieequest/model/Player.java b/src/esieequest/model/Player.java deleted file mode 100644 index e28edaf..0000000 --- a/src/esieequest/model/Player.java +++ /dev/null @@ -1,44 +0,0 @@ -package esieequest.model; - -import java.util.Stack; - -public class Player { - - private Room currentRoom; - private final Stack previousRooms; - - public Player() { - this.currentRoom = null; - this.previousRooms = new Stack(); - } - - /** - * Returns the current room. - * - * @return the current room - */ - public Room getCurrentRoom() { - return this.currentRoom; - } - - /** - * Sets the current room and stacks previous rooms. - * - * @param room - * the destination room - */ - public void goToRoom(final Room room) { - this.previousRooms.push(this.currentRoom); - this.currentRoom = room; - } - - /** - * Sets the current room to the previous room. - */ - public void goToPreviousRoom() { - if (!this.previousRooms.empty()) { - this.currentRoom = this.previousRooms.pop(); - } - } - -} diff --git a/src/esieequest/model/Quest.java b/src/esieequest/model/Quest.java deleted file mode 100644 index 1f4a9cc..0000000 --- a/src/esieequest/model/Quest.java +++ /dev/null @@ -1,22 +0,0 @@ -package esieequest.model; - -public class Quest { - - private String title; - - /** - * @return the title - */ - public String getTitle() { - return this.title; - } - - /** - * @param title - * the title to set - */ - public void setTitle(final String title) { - this.title = title; - } - -} diff --git a/src/esieequest/model/Room.java b/src/esieequest/model/Room.java deleted file mode 100644 index 2260a3f..0000000 --- a/src/esieequest/model/Room.java +++ /dev/null @@ -1,153 +0,0 @@ -package esieequest.model; - -import java.util.HashMap; -import java.util.Set; - -/** - * Represents one location in the scenery of the game, connected to others via - * exits (labelled north, east, south, west, up, down). For each direction, the - * room stores a reference to the neighbouring room, or null if there is no exit - * in that direction. - * - * @author Pacien TRAN-GIRARD - * @author Benoît LUBRANO DI SBARAGLIONE - */ -public class Room { - - private final String description; - private final String imageName; - private final HashMap exits; - private final HashMap items; - - /** - * Creates a room with a description and an associated image. - * - * @param description - * the description of the room - * @param image - * the image URL of the room - */ - public Room(final String description, final String imageName) { - this.description = description; - this.imageName = imageName; - this.exits = new HashMap(); - this.items = new HashMap(); - } - - public Room(final String description) { - this(description, null); - } - - /** - * Returns the description of the room. - * - * @return the description of the room - */ - public String getDescription() { - return this.description; - } - - /** - * Returns the informations about the room (its description, exits and - * items). - * - * @return the informations about the room, including exits and items - */ - public String getInformations() { - return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.listItems(); - } - - /** - * Returns the room's image name. - * - * @return the room's image name - */ - public String getImageName() { - return this.imageName; - } - - /** - * Returns the room in the relative given direction or null if there is no - * associated exit. - * - * @return the exit room - */ - public Room getExit(final String direction) { - return this.exits.get(direction); - } - - /** - * Returns an item of the room - * - * @param itemName - * the item name - * - * @return the item - */ - public Item getItem(final String itemName) { - return this.items.get(itemName); - } - - /** - * Defines an exit for the room. - * - * @param direction - * the direction of the exit - * @param neighbor - * the room in the given direction - */ - public void addExit(final String direction, final Room neighbor) { - this.exits.put(direction, neighbor); - } - - /** - * Adds an item to the room. - * - * @param item - * the item to set - */ - public void addItem(final String itemName, final Item item) { - this.items.put(itemName, item); - } - - /** - * Lists a description of the room's possible exits. - * - * @return the list of the available exits - */ - private String listExits() { - if (!this.exits.isEmpty()) { - return "Available exits:" + this.buildListString(this.exits.keySet()) + "."; - } - return "This room has no exit."; - } - - /** - * Lists the items contained in the room. - * - * @return the list of the items - */ - private String listItems() { - if (!this.items.isEmpty()) { - return "This room contains:" + this.buildListString(this.items.keySet()) + "."; - } - return "This room does not contain any item."; - } - - /** - * Builds a list in the form of a String from given a Set. - * - * @param elements - * the Set of Strings - * - * @return the list - */ - private String buildListString(final Set elements) { - final StringBuilder list = new StringBuilder(); - for (final String string : elements) { - list.append(" ").append(string); - } - return list.toString(); - } - -} diff --git a/src/esieequest/model/Side.java b/src/esieequest/model/Side.java deleted file mode 100644 index cb4e545..0000000 --- a/src/esieequest/model/Side.java +++ /dev/null @@ -1,5 +0,0 @@ -package esieequest.model; - -public class Side { - -} diff --git a/src/esieequest/model/command/Command.java b/src/esieequest/model/command/Command.java deleted file mode 100644 index 3d76b06..0000000 --- a/src/esieequest/model/command/Command.java +++ /dev/null @@ -1,62 +0,0 @@ -package esieequest.model.command; - -/** - * Represents a command constituted by an action and a parameter. - * - * @author Pacien TRAN-GIRARD - */ -public class Command { - - private final String action; - private final String option; - - /** - * The constructor. - * - * @param action - * the action - * @param option - * the option - */ - public Command(final String action, final String option) { - this.action = action; - this.option = option; - } - - /** - * Returns the action. - * - * @return the action - */ - public String getAction() { - return this.action; - } - - /** - * Tells if the action is unknown. - * - * @return - */ - public boolean isUnknown() { - return this.action == null; - } - - /** - * Returns the option. - * - * @return the option - */ - public String getOption() { - return this.option; - } - - /** - * Tells if the command contains an option - * - * @return - */ - public boolean hasOption() { - return this.option != null; - } - -} diff --git a/src/esieequest/model/command/CommandWord.java b/src/esieequest/model/command/CommandWord.java deleted file mode 100644 index 270cc49..0000000 --- a/src/esieequest/model/command/CommandWord.java +++ /dev/null @@ -1,71 +0,0 @@ -package esieequest.model.command; - -/** - * Contains the commands that the user can enter - * - * @author Pacien TRAN-GIRARD - */ -public enum CommandWord { - - // @formatter:off - // COMMAND ARGUMENTS ("" = no one ; null = any) - NEW ( "new", new String[] { "" } ), - LOAD ( "load", new String[] { "" } ), - SAVE ( "save", new String[] { "" } ), - SOUND ( "sound", new String[] { "" } ), - HELP ( "help", new String[] { "" } ), - QUIT ( "quit", new String[] { "" } ), - LOOK ( "look", new String[] { "" } ), - EAT ( "eat", new String[] { "" } ), - FORWARD ( "forward", new String[] { "" } ), - BACK ( "back", new String[] { "" } ), - GO ( "go", new String[] { "north", "south", "east", "west", "up", "down" } ), - TURN ( "turn", new String[] { "left", "right" } ), - DO ( "do", new String[] { "" } ), - TAKE ( "take", new String[] { "" } ), - TALK ( "talk", new String[] { "" } ), - INVENTORY ( "inventory", new String[] { "" } ), - USE ( "use", null ), - UNKNOWN ( "?", new String[] { "" } ); - // @formatter:on - - private String action; - private String[] validOptions; - - /** - * The constructor. - * - * @param action - * the action command - * @param validOption - * the list of the valid parameters - */ - CommandWord(final String action, final String[] validOption) { - this.action = action; - this.validOptions = validOption; - } - - @Override - public String toString() { - return this.action; - } - - /** - * Returns the action. - * - * @return the action - */ - public String getAction() { - return this.action; - } - - /** - * Returns the valid options. - * - * @return the valid options list - */ - public String[] getValidOptions() { - return this.validOptions; - } - -} diff --git a/src/esieequest/model/command/package-info.java b/src/esieequest/model/command/package-info.java deleted file mode 100644 index 9cbeaba..0000000 --- a/src/esieequest/model/command/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * The command model package. Contains the models associated with commands. - */ -package esieequest.model.command; - diff --git a/src/esieequest/model/commands/Command.java b/src/esieequest/model/commands/Command.java new file mode 100644 index 0000000..ea33c35 --- /dev/null +++ b/src/esieequest/model/commands/Command.java @@ -0,0 +1,62 @@ +package esieequest.model.commands; + +/** + * Represents a command constituted by an action and a parameter. + * + * @author Pacien TRAN-GIRARD + */ +public class Command { + + private final String action; + private final String option; + + /** + * The constructor. + * + * @param action + * the action + * @param option + * the option + */ + public Command(final String action, final String option) { + this.action = action; + this.option = option; + } + + /** + * Returns the action. + * + * @return the action + */ + public String getAction() { + return this.action; + } + + /** + * Tells if the action is unknown. + * + * @return + */ + public boolean isUnknown() { + return this.action == null; + } + + /** + * Returns the option. + * + * @return the option + */ + public String getOption() { + return this.option; + } + + /** + * Tells if the command contains an option + * + * @return + */ + public boolean hasOption() { + return this.option != null; + } + +} diff --git a/src/esieequest/model/commands/CommandWord.java b/src/esieequest/model/commands/CommandWord.java new file mode 100644 index 0000000..2afa372 --- /dev/null +++ b/src/esieequest/model/commands/CommandWord.java @@ -0,0 +1,71 @@ +package esieequest.model.commands; + +/** + * Contains the commands that the user can enter + * + * @author Pacien TRAN-GIRARD + */ +public enum CommandWord { + + // @formatter:off + // COMMAND ARGUMENTS ("" = no one ; null = any) + NEW ( "new", new String[] { "" } ), + LOAD ( "load", new String[] { "" } ), + SAVE ( "save", new String[] { "" } ), + SOUND ( "sound", new String[] { "" } ), + HELP ( "help", new String[] { "" } ), + QUIT ( "quit", new String[] { "" } ), + LOOK ( "look", new String[] { "" } ), + EAT ( "eat", new String[] { "" } ), + FORWARD ( "forward", new String[] { "" } ), + BACK ( "back", new String[] { "" } ), + GO ( "go", new String[] { "north", "south", "east", "west", "up", "down" } ), + TURN ( "turn", new String[] { "left", "right" } ), + DO ( "do", new String[] { "" } ), + TAKE ( "take", new String[] { "" } ), + TALK ( "talk", new String[] { "" } ), + INVENTORY ( "inventory", new String[] { "" } ), + USE ( "use", null ), + UNKNOWN ( "?", new String[] { "" } ); + // @formatter:on + + private String action; + private String[] validOptions; + + /** + * The constructor. + * + * @param action + * the action command + * @param validOption + * the list of the valid parameters + */ + CommandWord(final String action, final String[] validOption) { + this.action = action; + this.validOptions = validOption; + } + + @Override + public String toString() { + return this.action; + } + + /** + * Returns the action. + * + * @return the action + */ + public String getAction() { + return this.action; + } + + /** + * Returns the valid options. + * + * @return the valid options list + */ + public String[] getValidOptions() { + return this.validOptions; + } + +} diff --git a/src/esieequest/model/commands/package-info.java b/src/esieequest/model/commands/package-info.java new file mode 100644 index 0000000..5ce6bed --- /dev/null +++ b/src/esieequest/model/commands/package-info.java @@ -0,0 +1,5 @@ +/** + * The command model package. Contains the models related to the game commands. + */ +package esieequest.model.commands; + diff --git a/src/esieequest/model/entities/Player.java b/src/esieequest/model/entities/Player.java new file mode 100644 index 0000000..ef47d3f --- /dev/null +++ b/src/esieequest/model/entities/Player.java @@ -0,0 +1,83 @@ +package esieequest.model.entities; + +import java.util.HashMap; +import java.util.Set; +import java.util.Stack; + +import esieequest.model.items.Inventory; +import esieequest.model.items.Item; +import esieequest.model.map.Room; + +public class Player implements Inventory { + + private Room currentRoom; + private final Stack previousRooms; + + private final HashMap inventory; + + public Player() { + this.currentRoom = null; + this.previousRooms = new Stack(); + + this.inventory = new HashMap(); + } + + /** + * Returns the current room. + * + * @return the current room + */ + public Room getCurrentRoom() { + return this.currentRoom; + } + + /** + * Sets the current room and stacks previous rooms. + * + * @param room + * the destination room + */ + public void goToRoom(final Room room) { + this.previousRooms.push(this.currentRoom); + this.currentRoom = room; + } + + /** + * Sets the current room to the previous room. + */ + public void goToPreviousRoom() { + if (!this.previousRooms.empty()) { + this.currentRoom = this.previousRooms.pop(); + } + } + + @Override + public Item takeItem(final String itemName) { + final Item item = this.inventory.get(itemName); + this.inventory.remove(itemName); + return item; + } + + @Override + public boolean hasItem(final String itemName) { + return this.inventory.get(itemName) != null; + } + + @Override + public void putItem(final String itemName, final Item item) { + this.inventory.put(itemName, item); + } + + @Override + public Set getItemList() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String listItems() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/esieequest/model/entities/package-info.java b/src/esieequest/model/entities/package-info.java new file mode 100644 index 0000000..f0070cd --- /dev/null +++ b/src/esieequest/model/entities/package-info.java @@ -0,0 +1,5 @@ +/** + * The entities package. Contains classes related to the game entities. + */ +package esieequest.model.entities; + diff --git a/src/esieequest/model/events/Quest.java b/src/esieequest/model/events/Quest.java new file mode 100644 index 0000000..352d764 --- /dev/null +++ b/src/esieequest/model/events/Quest.java @@ -0,0 +1,22 @@ +package esieequest.model.events; + +public class Quest { + + private String title; + + /** + * @return the title + */ + public String getTitle() { + return this.title; + } + + /** + * @param title + * the title to set + */ + public void setTitle(final String title) { + this.title = title; + } + +} diff --git a/src/esieequest/model/events/package-info.java b/src/esieequest/model/events/package-info.java new file mode 100644 index 0000000..3ef3407 --- /dev/null +++ b/src/esieequest/model/events/package-info.java @@ -0,0 +1,5 @@ +/** + * The events package. Contains classes related to the game events. + */ +package esieequest.model.events; + diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java new file mode 100644 index 0000000..d78b682 --- /dev/null +++ b/src/esieequest/model/items/Inventory.java @@ -0,0 +1,57 @@ +package esieequest.model.items; + +import java.util.Set; + +/** + * The inventory interface that describes an inventory containing Items refered + * by their names. + * + * @author Pacien TRAN-GIRARD + */ +public interface Inventory { + + /** + * Takes (returns and removes) an item from the inventory. + * + * @param itemName + * the item's name + * + * @return the item + */ + public Item takeItem(String itemName); + + /** + * Tells is an inventory contains a given item by its name. + * + * @param itemName + * the item's name + * + * @return the existence of the item + */ + public boolean hasItem(String itemName); + + /** + * Puts an item in the inventory. + * + * @param itemName + * the item's name + * @param item + * the item + */ + public void putItem(String itemName, Item item); + + /** + * Returns a set of item names contained in the inventory. + * + * @return a set of item names + */ + public Set getItemList(); + + /** + * Lists all items names contained in the inventory in a String. + * + * @return the list of items + */ + public String listItems(); + +} diff --git a/src/esieequest/model/items/Item.java b/src/esieequest/model/items/Item.java new file mode 100644 index 0000000..01bf3f3 --- /dev/null +++ b/src/esieequest/model/items/Item.java @@ -0,0 +1,45 @@ +package esieequest.model.items; + +/** + * Represents an item with a description and a weight, stored in a room. Can be + * picked, used and dropped. + * + * @author Pacien TRAN-GIRARD + */ +public class Item { + + private final String description; + private final int weight; + + /** + * Creates an item with the given characteristics. + * + * @param description + * the description of the item + * @param weight + * the weight of the item + */ + public Item(final String description, final int weight) { + this.description = description; + this.weight = weight; + } + + /** + * Returns the description of the item. + * + * @return the description + */ + public String getDescription() { + return this.description; + } + + /** + * Returns the weight of the item. + * + * @return the weight + */ + public int getWeight() { + return this.weight; + } + +} diff --git a/src/esieequest/model/items/package-info.java b/src/esieequest/model/items/package-info.java new file mode 100644 index 0000000..b35497f --- /dev/null +++ b/src/esieequest/model/items/package-info.java @@ -0,0 +1,5 @@ +/** + * The items package. Contains classes related to the game items. + */ +package esieequest.model.items; + diff --git a/src/esieequest/model/map/Room.java b/src/esieequest/model/map/Room.java new file mode 100644 index 0000000..a5aa6d9 --- /dev/null +++ b/src/esieequest/model/map/Room.java @@ -0,0 +1,137 @@ +package esieequest.model.map; + +import java.util.HashMap; +import java.util.Set; + +import esieequest.controller.Utils; +import esieequest.model.items.Inventory; +import esieequest.model.items.Item; + +/** + * Represents one location in the scenery of the game, connected to others via + * exits (labelled north, east, south, west, up, down). For each direction, the + * room stores a reference to the neighbouring room, or null if there is no exit + * in that direction. + * + * @author Pacien TRAN-GIRARD + * @author Benoît LUBRANO DI SBARAGLIONE + */ +public class Room implements Inventory { + + private final String description; + private final String imageName; + private final HashMap exits; + private final HashMap items; + + /** + * Creates a room with a description and an associated image. + * + * @param description + * the description of the room + * @param image + * the image URL of the room + */ + public Room(final String description, final String imageName) { + this.description = description; + this.imageName = imageName; + this.exits = new HashMap(); + this.items = new HashMap(); + } + + public Room(final String description) { + this(description, null); + } + + /** + * Returns the description of the room. + * + * @return the description of the room + */ + public String getDescription() { + return this.description; + } + + /** + * Returns the informations about the room (its description, exits and + * items). + * + * @return the informations about the room, including exits and items + */ + public String getInformations() { + return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.listItems(); + } + + /** + * Returns the room's image name. + * + * @return the room's image name + */ + public String getImageName() { + return this.imageName; + } + + /** + * Returns the room in the relative given direction or null if there is no + * associated exit. + * + * @return the exit room + */ + public Room getExit(final String direction) { + return this.exits.get(direction); + } + + /** + * Lists a description of the room's possible exits. + * + * @return the list of the available exits + */ + private String listExits() { + return Utils.list(this.exits.keySet(), "Available exits:", "This room has no exit."); + } + + /** + * Defines an exit for the room. + * + * @param direction + * the direction of the exit + * @param neighbor + * the room in the given direction + */ + public void addExit(final String direction, final Room neighbor) { + this.exits.put(direction, neighbor); + } + + @Override + public Item takeItem(final String itemName) { + final Item item = this.items.get(itemName); + this.items.remove(itemName); + return item; + } + + @Override + public boolean hasItem(final String itemName) { + return this.items.get(itemName) != null; + } + + @Override + public void putItem(final String itemName, final Item item) { + this.items.put(itemName, item); + } + + @Override + public Set getItemList() { + // TODO Auto-generated method stub + return null; + } + + /** + * Lists the items contained in the room. + * + * @return the list of the items + */ + @Override + public String listItems() { + return Utils.list(this.items.keySet(), "This room contains:", "This room does not contain any item."); + } + +} diff --git a/src/esieequest/model/map/Side.java b/src/esieequest/model/map/Side.java new file mode 100644 index 0000000..8584fdc --- /dev/null +++ b/src/esieequest/model/map/Side.java @@ -0,0 +1,5 @@ +package esieequest.model.map; + +public class Side { + +} diff --git a/src/esieequest/model/map/package-info.java b/src/esieequest/model/map/package-info.java new file mode 100644 index 0000000..b7fee79 --- /dev/null +++ b/src/esieequest/model/map/package-info.java @@ -0,0 +1,5 @@ +/** + * The map package. Contains classes related to the game map. + */ +package esieequest.model.map; + diff --git a/src/esieequest/view/View.java b/src/esieequest/view/View.java index 9ffd7ec..9b68f00 100644 --- a/src/esieequest/view/View.java +++ b/src/esieequest/view/View.java @@ -1,15 +1,12 @@ -/** - * - */ package esieequest.view; import java.util.HashMap; import esieequest.controller.GameEngine; -import esieequest.model.Item; -import esieequest.model.Quest; -import esieequest.model.Room; -import esieequest.model.Side; +import esieequest.model.events.Quest; +import esieequest.model.items.Item; +import esieequest.model.map.Room; +import esieequest.model.map.Side; /** * The view interface that describes an user interface. diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index 28b2f37..e2fbc9a 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -18,10 +18,10 @@ import javax.swing.border.EmptyBorder; import com.wordpress.tipsforjava.swing.StretchIcon; import esieequest.controller.GameEngine; -import esieequest.model.Item; -import esieequest.model.Quest; -import esieequest.model.Room; -import esieequest.model.Side; +import esieequest.model.events.Quest; +import esieequest.model.items.Item; +import esieequest.model.map.Room; +import esieequest.model.map.Side; import esieequest.view.View; /** diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java index 0361631..09553d5 100644 --- a/src/esieequest/view/text/TextInterface.java +++ b/src/esieequest/view/text/TextInterface.java @@ -3,10 +3,10 @@ package esieequest.view.text; import java.util.HashMap; import esieequest.controller.GameEngine; -import esieequest.model.Item; -import esieequest.model.Quest; -import esieequest.model.Room; -import esieequest.model.Side; +import esieequest.model.events.Quest; +import esieequest.model.items.Item; +import esieequest.model.map.Room; +import esieequest.model.map.Side; import esieequest.view.View; /** @@ -44,12 +44,13 @@ abstract class TextInterface implements View { } /** - * @param running the running to set + * @param running + * the running to set */ - public void setRunning(boolean running) { + public void setRunning(final boolean running) { this.running = running; } - + @Override public void setController(final GameEngine gameEngine) { this.gameEngine = gameEngine; @@ -98,6 +99,4 @@ abstract class TextInterface implements View { // TODO Auto-generated method stub } - - } diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 97265cb..000c794 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -23,10 +23,10 @@ import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Widget; import esieequest.controller.GameEngine; -import esieequest.model.Item; -import esieequest.model.Quest; -import esieequest.model.Room; -import esieequest.model.Side; +import esieequest.model.events.Quest; +import esieequest.model.items.Item; +import esieequest.model.map.Room; +import esieequest.model.map.Side; import esieequest.view.View; /** -- cgit v1.2.3