From 92f671b84b9c2a2c2319aac2a52e6dffa8bc2195 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 2 Apr 2014 10:50:28 +0200 Subject: Move some attributes to the Player class --- src/esieequest/controller/Performer.java | 12 +++--- src/esieequest/model/Game.java | 60 +++++++---------------------- src/esieequest/model/Player.java | 39 +++++++++++++++++++ src/esieequest/view/text/Console.java | 4 +- src/esieequest/view/text/FileReader.java | 4 +- src/esieequest/view/text/TextInterface.java | 27 ++++++++++++- 6 files changed, 87 insertions(+), 59 deletions(-) diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 27cbf14..96a95df 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -99,10 +99,10 @@ class Performer { * the direction of the new room */ public void goTo(final String direction) { - final Room nextRoom = this.game.getRoomExit(direction); + final Room nextRoom = this.game.getPlayer().getCurrentRoom().getExit(direction); if (nextRoom != null) { - this.game.goToRoom(nextRoom); - this.view.updateRoom(this.game.getCurrentRoom()); + this.game.getPlayer().goToRoom(nextRoom); + this.view.updateRoom(this.game.getPlayer().getCurrentRoom()); } else { this.echo(this.game.getNoExitMessage()); } @@ -112,15 +112,15 @@ class Performer { * Changes the current room to the previous one. */ public void goBack() { - this.game.goToPreviousRoom(); - this.view.updateRoom(this.game.getCurrentRoom()); + this.game.getPlayer().goToPreviousRoom(); + this.view.updateRoom(this.game.getPlayer().getCurrentRoom()); } /** * Displays informations about the current place. */ public void look() { - this.echo(this.game.getCurrentRoom().getInformations()); + this.echo(this.game.getPlayer().getCurrentRoom().getInformations()); } /** diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 62dfc94..0a9aa01 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -1,7 +1,6 @@ package esieequest.model; import java.util.HashMap; -import java.util.Stack; /** * Represents the game. @@ -12,17 +11,16 @@ import java.util.Stack; public class Game { private final HashMap rooms; - private Room currentRoom; - private final Stack previousRooms; + private final Player player; /** * The default constructor. */ public Game() { this.rooms = new HashMap(); - this.currentRoom = null; - this.previousRooms = new Stack(); + + this.player = new Player(); this.createRooms(); this.linkRooms(); @@ -30,6 +28,15 @@ public class Game { this.goToRoom("AmphitheaterSeat"); } + /** + * Returns the player + * + * @return the player + */ + public Player getPlayer() { + return this.player; + } + /** * Creates a new room. * @@ -184,17 +191,6 @@ public class Game { this.rooms.get("Cafeteria").addItem("Orange", new Item("An orange orange", 15)); } - /** - * 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 designated by its name. * @@ -202,37 +198,7 @@ public class Game { * the destination room name */ public void goToRoom(final String roomName) { - this.currentRoom = this.rooms.get(roomName); - } - - /** - * Sets the current room to the previous room. - */ - public void goToPreviousRoom() { - if (!this.previousRooms.empty()) { - this.currentRoom = this.previousRooms.pop(); - } - } - - /** - * Returns the current room. - * - * @return the current room - */ - public Room getCurrentRoom() { - return this.currentRoom; - } - - /** - * Returns the current room's exit located in the given direction. - * - * @param direction - * the direction of the exit - * - * @return the exit room - */ - public Room getRoomExit(final String direction) { - return this.currentRoom.getExit(direction); + this.player.goToRoom(this.rooms.get(roomName)); } /** diff --git a/src/esieequest/model/Player.java b/src/esieequest/model/Player.java index 0252da3..e28edaf 100644 --- a/src/esieequest/model/Player.java +++ b/src/esieequest/model/Player.java @@ -1,5 +1,44 @@ 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/view/text/Console.java b/src/esieequest/view/text/Console.java index e7a71dd..455db81 100644 --- a/src/esieequest/view/text/Console.java +++ b/src/esieequest/view/text/Console.java @@ -15,9 +15,9 @@ public class Console extends TextInterface { @Override protected void run() { final Scanner scanner = new Scanner(System.in); - while (this.running) { + while (this.isRunning()) { System.out.print("> "); - this.gameEngine.interpret(scanner.nextLine()); + this.getController().interpret(scanner.nextLine()); } scanner.close(); } diff --git a/src/esieequest/view/text/FileReader.java b/src/esieequest/view/text/FileReader.java index 392a691..11ee895 100644 --- a/src/esieequest/view/text/FileReader.java +++ b/src/esieequest/view/text/FileReader.java @@ -31,11 +31,11 @@ public class FileReader extends TextInterface { protected void run() { try { final Scanner scanner = new Scanner(this.file); - while (this.running && scanner.hasNextLine()) { + while (this.isRunning() && scanner.hasNextLine()) { final String line = scanner.nextLine(); System.out.println("> " + line); if (!line.startsWith("//")) { - this.gameEngine.interpret(line); + this.getController().interpret(line); } } scanner.close(); diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java index 133b03b..0361631 100644 --- a/src/esieequest/view/text/TextInterface.java +++ b/src/esieequest/view/text/TextInterface.java @@ -16,9 +16,9 @@ import esieequest.view.View; */ abstract class TextInterface implements View { - protected GameEngine gameEngine; + private GameEngine gameEngine; - protected boolean running; + private boolean running; /** * The default constructor. @@ -29,6 +29,27 @@ abstract class TextInterface implements View { protected abstract void run(); + /** + * @return the gameEngine + */ + public GameEngine getController() { + return this.gameEngine; + } + + /** + * @return the running state + */ + public boolean isRunning() { + return this.running; + } + + /** + * @param running the running to set + */ + public void setRunning(boolean running) { + this.running = running; + } + @Override public void setController(final GameEngine gameEngine) { this.gameEngine = gameEngine; @@ -77,4 +98,6 @@ abstract class TextInterface implements View { // TODO Auto-generated method stub } + + } -- cgit v1.2.3 From b19e6711834472ee561831dc1e4a529382af20f5 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 2 Apr 2014 11:00:13 +0200 Subject: Update report --- report/progression.tex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/report/progression.tex b/report/progression.tex index 24c5d7a..1cc10ac 100644 --- a/report/progression.tex +++ b/report/progression.tex @@ -266,6 +266,9 @@ to win the game. \subsection{Player} +The current room and the previous rooms were moved from Game to Player with +commit 92f671b84. + \subsection{take, drop} \subsection{Carry several items} -- cgit v1.2.3 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 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 From 484c4c1e06c060f0185126ddd00d3a905dd2f05f Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 14:22:21 +0200 Subject: Update report --- report/progression.tex | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/report/progression.tex b/report/progression.tex index 1cc10ac..f8c05c4 100644 --- a/report/progression.tex +++ b/report/progression.tex @@ -271,8 +271,15 @@ commit 92f671b84. \subsection{take, drop} +The take and drop commands were implemented as part of the commit number +5ddb6df63. An Inventory interface, used by the Room and Player classes, was +added. Items are stored using HashMaps, and a moveItem procedure was added to +the Performer. + \subsection{Carry several items} +Multiple items carrying was already implemented in the commit number 5ddb6df63. + \subsection{ItemList} \subsection{Maximum weight} -- cgit v1.2.3 From db2c22c9b4b71a2f1b4221aaf2db5934def41ce3 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 14:34:59 +0200 Subject: Implements "ItemList" in Inventory, that is now a class --- src/esieequest/controller/Performer.java | 4 +-- src/esieequest/model/Game.java | 4 +-- src/esieequest/model/entities/Player.java | 43 +++++++------------------------ src/esieequest/model/items/Inventory.java | 36 ++++++++++++++++++++------ src/esieequest/model/map/Room.java | 42 ++++++------------------------ 5 files changed, 50 insertions(+), 79 deletions(-) diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 1abc887..d1f1bb6 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -138,7 +138,7 @@ class Performer { * the item's name */ public void take(final String itemName) { - this.moveItem(this.game.getPlayer().getCurrentRoom(), this.game.getPlayer(), itemName); + this.moveItem(this.game.getPlayer().getCurrentRoom().getItems(), this.game.getPlayer().getInventory(), itemName); } /** @@ -148,7 +148,7 @@ class Performer { * the item's name */ public void drop(final String itemName) { - this.moveItem(this.game.getPlayer(), this.game.getPlayer().getCurrentRoom(), itemName); + this.moveItem(this.game.getPlayer().getInventory(), this.game.getPlayer().getCurrentRoom().getItems(), itemName); } /** diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 5ef4c22..63c1eee 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -191,8 +191,8 @@ public class Game { * Creates and adds items into rooms. */ private void createItems() { - this.rooms.get("Cafeteria").putItem("banana", new Item("A yellow banana", 12)); - this.rooms.get("Cafeteria").putItem("orange", new Item("An orange orange", 15)); + this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 12)); + this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 15)); } /** diff --git a/src/esieequest/model/entities/Player.java b/src/esieequest/model/entities/Player.java index ef47d3f..5a97726 100644 --- a/src/esieequest/model/entities/Player.java +++ b/src/esieequest/model/entities/Player.java @@ -1,25 +1,22 @@ 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 { +public class Player { private Room currentRoom; private final Stack previousRooms; - private final HashMap inventory; + private final Inventory inventory; public Player() { this.currentRoom = null; this.previousRooms = new Stack(); - this.inventory = new HashMap(); + this.inventory = new Inventory(); } /** @@ -51,33 +48,13 @@ public class Player implements Inventory { } } - @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; + /** + * Gets the player's inventory. + * + * @return the player's inventory + */ + public Inventory getInventory() { + return this.inventory; } } diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java index d78b682..99b48a9 100644 --- a/src/esieequest/model/items/Inventory.java +++ b/src/esieequest/model/items/Inventory.java @@ -1,14 +1,22 @@ package esieequest.model.items; +import java.util.HashMap; import java.util.Set; +import esieequest.controller.Utils; + /** - * The inventory interface that describes an inventory containing Items refered - * by their names. + * An inventory containing Items referred by their names. * * @author Pacien TRAN-GIRARD */ -public interface Inventory { +public class Inventory { + + private final HashMap items; + + public Inventory() { + this.items = new HashMap(); + } /** * Takes (returns and removes) an item from the inventory. @@ -18,7 +26,11 @@ public interface Inventory { * * @return the item */ - public Item takeItem(String itemName); + public Item takeItem(final String itemName) { + final Item item = this.items.get(itemName); + this.items.remove(itemName); + return item; + } /** * Tells is an inventory contains a given item by its name. @@ -28,7 +40,9 @@ public interface Inventory { * * @return the existence of the item */ - public boolean hasItem(String itemName); + public boolean hasItem(final String itemName) { + return this.items.get(itemName) != null; + } /** * Puts an item in the inventory. @@ -38,20 +52,26 @@ public interface Inventory { * @param item * the item */ - public void putItem(String itemName, Item item); + public void putItem(final String itemName, final Item item) { + this.items.put(itemName, item); + } /** * Returns a set of item names contained in the inventory. * * @return a set of item names */ - public Set getItemList(); + public Set getItemList() { + return this.items.keySet(); + } /** * Lists all items names contained in the inventory in a String. * * @return the list of items */ - public String listItems(); + public String listItems() { + return Utils.list(this.items.keySet(), "Items:", "No items."); + } } diff --git a/src/esieequest/model/map/Room.java b/src/esieequest/model/map/Room.java index a5aa6d9..fc20cc5 100644 --- a/src/esieequest/model/map/Room.java +++ b/src/esieequest/model/map/Room.java @@ -1,11 +1,9 @@ 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 @@ -16,12 +14,12 @@ import esieequest.model.items.Item; * @author Pacien TRAN-GIRARD * @author Benoît LUBRANO DI SBARAGLIONE */ -public class Room implements Inventory { +public class Room { private final String description; private final String imageName; private final HashMap exits; - private final HashMap items; + private final Inventory items; /** * Creates a room with a description and an associated image. @@ -35,7 +33,7 @@ public class Room implements Inventory { this.description = description; this.imageName = imageName; this.exits = new HashMap(); - this.items = new HashMap(); + this.items = new Inventory(); } public Room(final String description) { @@ -58,7 +56,7 @@ public class Room implements Inventory { * @return the informations about the room, including exits and items */ public String getInformations() { - return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.listItems(); + return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.items.listItems(); } /** @@ -101,37 +99,13 @@ public class Room implements Inventory { 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. + * Gets the room's items. * - * @return the list of the items + * @return the room's inventory */ - @Override - public String listItems() { - return Utils.list(this.items.keySet(), "This room contains:", "This room does not contain any item."); + public Inventory getItems() { + return this.items; } } -- cgit v1.2.3 From 4ef10e49ecf6a786c60eda0461871aa9d0012e0b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:05:09 +0200 Subject: Update report --- report/progression.tex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/report/progression.tex b/report/progression.tex index f8c05c4..809cb78 100644 --- a/report/progression.tex +++ b/report/progression.tex @@ -282,6 +282,9 @@ Multiple items carrying was already implemented in the commit number 5ddb6df63. \subsection{ItemList} +The ItemList was implemented as the Inventory class in commit number db2c22c9b. +The Inventory is no longer an interface. + \subsection{Maximum weight} \subsection{Inventory} -- cgit v1.2.3 From b8771ccb25d5d12f39dffd761e6534fe09642bcb Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:05:21 +0200 Subject: Implement maximum inventory weight --- src/esieequest/controller/Performer.java | 9 +++++++++ src/esieequest/model/Game.java | 6 +++--- src/esieequest/model/entities/Player.java | 11 ++++++++++- src/esieequest/model/items/Inventory.java | 12 ++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index d1f1bb6..466f9da 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -138,6 +138,15 @@ class Performer { * the item's name */ public void take(final String itemName) { + int weight = this.game.getPlayer().getInventory().getTotalWeight(); + weight += this.game.getPlayer().getCurrentRoom().getItems().getItemWeight(itemName); + final int maxWeight = this.game.getPlayer().getMaxCarryWeight(); + + if (weight >= maxWeight) { + this.echo("Maximum inventory weight reached. Cannot pick up item."); + return; + } + this.moveItem(this.game.getPlayer().getCurrentRoom().getItems(), this.game.getPlayer().getInventory(), itemName); } diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 63c1eee..f08f264 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -24,7 +24,7 @@ public class Game { public Game() { this.rooms = new HashMap(); - this.player = new Player(); + this.player = new Player(10); this.createRooms(); this.linkRooms(); @@ -191,8 +191,8 @@ public class Game { * Creates and adds items into rooms. */ private void createItems() { - this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 12)); - this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 15)); + this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 5)); + this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 6)); } /** diff --git a/src/esieequest/model/entities/Player.java b/src/esieequest/model/entities/Player.java index 5a97726..91888eb 100644 --- a/src/esieequest/model/entities/Player.java +++ b/src/esieequest/model/entities/Player.java @@ -11,12 +11,14 @@ public class Player { private final Stack previousRooms; private final Inventory inventory; + private final int maxCarryWeight; - public Player() { + public Player(final int maxCarryWeight) { this.currentRoom = null; this.previousRooms = new Stack(); this.inventory = new Inventory(); + this.maxCarryWeight = maxCarryWeight; } /** @@ -57,4 +59,11 @@ public class Player { return this.inventory; } + /** + * @return the maxCarryWeight + */ + public int getMaxCarryWeight() { + return this.maxCarryWeight; + } + } diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java index 99b48a9..86e86b5 100644 --- a/src/esieequest/model/items/Inventory.java +++ b/src/esieequest/model/items/Inventory.java @@ -74,4 +74,16 @@ public class Inventory { return Utils.list(this.items.keySet(), "Items:", "No items."); } + public int getItemWeight(final String itemName) { + return this.items.get(itemName).getWeight(); + } + + public int getTotalWeight() { + int totalWeight = 0; + for (final Item item : this.items.values()) { + totalWeight += item.getWeight(); + } + return totalWeight; + } + } -- cgit v1.2.3 From bfbd481b9da18fb810b5dcb01dd8c8ee61d4341c Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:07:11 +0200 Subject: Update report --- report/progression.tex | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/report/progression.tex b/report/progression.tex index 809cb78..5408d48 100644 --- a/report/progression.tex +++ b/report/progression.tex @@ -287,6 +287,10 @@ The Inventory is no longer an interface. \subsection{Maximum weight} +The maximum carryable weight was implement with the commit number b8771ccb2. +In order to achieve this, a getTotalWeight() function was added to the Inventory +class. + \subsection{Inventory} \subsection{Magic cookie} -- cgit v1.2.3 From a3f6ce16e722e6d26f3ce1809a6a4f4937debe9b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:12:53 +0200 Subject: Implements player's inventory listing --- src/esieequest/controller/Interpreter.java | 3 +++ src/esieequest/controller/Performer.java | 7 +++++++ src/esieequest/model/items/Inventory.java | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/src/esieequest/controller/Interpreter.java b/src/esieequest/controller/Interpreter.java index 8d0f703..ea01fd7 100644 --- a/src/esieequest/controller/Interpreter.java +++ b/src/esieequest/controller/Interpreter.java @@ -72,6 +72,9 @@ class Interpreter { case "eat": this.performer.eat(); return; + case "inventory": + this.performer.listItems(); + return; case "take": this.performer.take(command.getOption()); return; diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 466f9da..7f41109 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -178,4 +178,11 @@ class Performer { dest.putItem(itemName, source.takeItem(itemName)); } + /** + * Lists the items contained in the player's inventory. + */ + public void listItems() { + this.echo(Utils.list(this.game.getPlayer().getInventory().getItemList(), "Items:", "No item in your inventory.")); + } + } diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java index 86e86b5..228386e 100644 --- a/src/esieequest/model/items/Inventory.java +++ b/src/esieequest/model/items/Inventory.java @@ -74,6 +74,13 @@ public class Inventory { return Utils.list(this.items.keySet(), "Items:", "No items."); } + /** + * Returns the weight of the given item referred by its name. + * + * @param itemName + * the item's name + * @return + */ public int getItemWeight(final String itemName) { return this.items.get(itemName).getWeight(); } -- cgit v1.2.3 From ed9e5e3bc1f8861b9c235d3c486cbf750f8f37d2 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:14:16 +0200 Subject: Update report --- report/progression.tex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/report/progression.tex b/report/progression.tex index 5408d48..8bb0b43 100644 --- a/report/progression.tex +++ b/report/progression.tex @@ -293,6 +293,9 @@ class. \subsection{Inventory} +The player's inventory items listing was implemented with the commit number +a3f6ce16e, using the recently added listing utility class. + \subsection{Magic cookie} \subsection{Tests} -- cgit v1.2.3 From 35ee3b1ee2d9d77f605f06e8adb5b69708463ba3 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:17:34 +0200 Subject: Implement carryable weight increasing via negative mass item --- src/esieequest/controller/Interpreter.java | 3 --- src/esieequest/controller/Performer.java | 7 ------- src/esieequest/model/Game.java | 1 + 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/esieequest/controller/Interpreter.java b/src/esieequest/controller/Interpreter.java index ea01fd7..5c60f6d 100644 --- a/src/esieequest/controller/Interpreter.java +++ b/src/esieequest/controller/Interpreter.java @@ -69,9 +69,6 @@ class Interpreter { case "look": this.performer.look(); return; - case "eat": - this.performer.eat(); - return; case "inventory": this.performer.listItems(); return; diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 7f41109..8f8afeb 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -124,13 +124,6 @@ class Performer { this.echo(this.game.getPlayer().getCurrentRoom().getInformations()); } - /** - * Displays a special message. - */ - public void eat() { - this.echo(this.game.getEatMessage()); - } - /** * Moves an item from the current Room to the Player's inventory. * diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index f08f264..cc6157d 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -193,6 +193,7 @@ public class Game { private void createItems() { this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 5)); this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 6)); + this.rooms.get("Cafeteria").getItems().putItem("anti-matter", new Item("A block of anti-matter with a negative mass", -10)); } /** -- cgit v1.2.3 From 8312e144784d0c9a2295980221a5ed5af166a895 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:19:52 +0200 Subject: Update report --- report/progression.tex | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/report/progression.tex b/report/progression.tex index 8bb0b43..d40a8e6 100644 --- a/report/progression.tex +++ b/report/progression.tex @@ -298,6 +298,12 @@ a3f6ce16e, using the recently added listing utility class. \subsection{Magic cookie} +Since there are already too many commands, the carryable weight +expansion was implemented in a smarter way using a negative weight +item in the commit number 35ee3b1ee. +We could have instead increased the maximum carryable weight with a setter on +the Player class, called by the Performer with the ``eat'' command. + \subsection{Tests} -- cgit v1.2.3 From 7b610fc05d85ffba69af1a5cf4d9b94cae8245b2 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 15:21:56 +0200 Subject: Update commands test file --- test/commands.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/commands.txt b/t