From 04f03d12815161b81444626ac9fd9a596b59a544 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 5 Mar 2014 11:38:59 +0100 Subject: Add multiple item support --- src/esieequest/controller/Performer.java | 41 +++++++++++++++++++++++--------- src/esieequest/model/Game.java | 24 +++++++++++++------ src/esieequest/model/Room.java | 18 +++++++++----- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index b7044f2..29b5fc8 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -3,6 +3,9 @@ */ package esieequest.controller; +import java.util.Iterator; +import java.util.Set; + import esieequest.model.Game; import esieequest.model.Room; import esieequest.view.View; @@ -21,53 +24,69 @@ public class Performer { this.view = view; } + private String buildListString(Set elements) { + StringBuilder list = new StringBuilder(); + for (Iterator iterator = elements.iterator(); iterator.hasNext();) { + list.append(" "); + list.append(iterator.next()); + } + return list.toString(); + } + public void echo(String message) { this.view.echo(message); } public void newGame() { // this.loadGame(default game); - this.view.echo(this.game.getWelcomeMessage() + "\n" + this.game.getLocationInfo()); + this.echo(this.game.getWelcomeMessage() + "\n" + this.game.getLocationInfo()); } public void loadGame() { - this.view.echo("Not implemented."); + this.echo("Not implemented."); } public void saveGame() { - this.view.echo("Not implemented."); + this.echo("Not implemented."); } public void toggleSound() { - this.view.echo("Not implemented."); + this.echo("Not implemented."); } public void quitGame() { - this.view.echo(this.game.getQuitMessage()); + this.echo(this.game.getQuitMessage()); this.game.setRunning(false); } public void showHelp() { - this.view.echo(this.game.getHelpMessage()); + this.echo(this.game.getHelpMessage()); + // TODO: list commands } public void goTo(String direction) { Room nextRoom = this.game.getRoomExit(direction); if (nextRoom != null) { this.game.goToRoom(nextRoom); - this.view.echo(this.game.getLocationInfo()); + this.echo(this.game.getLocationInfo()); } else { - this.view.echo(this.game.getNoExitMessage()); + this.echo(this.game.getNoExitMessage()); } } public void look() { - this.view.echo(this.game.getLocationInfo()); - this.view.echo(this.game.getItemDescription()); + this.echo(this.game.getLocationInfo()); + this.listItems(); + } + + private void listItems() { + if (!this.game.getItemList().isEmpty()) { + this.echo(this.game.getItemListPrefix() + this.buildListString(this.game.getItemList())); + } } public void eat() { - this.view.echo(this.game.getEatMessage()); + this.echo(this.game.getEatMessage()); } } diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 59b4a32..c9d7933 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -1,6 +1,7 @@ package esieequest.model; import java.util.HashMap; +import java.util.Set; /** * @@ -166,7 +167,8 @@ public class Game { } private void createItems() { - this.rooms.get("Cafeteria").setItem(new Item("Banana", 12)); + this.rooms.get("Cafeteria").addItem("Banana", new Item("A yellow banana", 12)); + this.rooms.get("Cafeteria").addItem("Orange", new Item("An orange orange", 15)); } public void setRunning(boolean state) { @@ -197,6 +199,14 @@ public class Game { return this.currentRoom.getImageName(); } + public String getItemDescription(String itemName) { + return this.currentRoom.getItem(itemName).getDescription(); + } + + public Set getItemList() { + return this.currentRoom.getItemList(); + } + public String getEatMessage() { return "oNommNommNomm..."; } @@ -217,12 +227,12 @@ public class Game { return "There is no exit."; } - public String getItemDescription() { - if (this.currentRoom.getItem() != null) { - return "This room contain : " + this.currentRoom.getItem().getDescription(); - } else { - return ""; - } + public String getCommandListPrefix() { + return "Available commands:"; + } + + public String getItemListPrefix() { + return "This room contains:"; } } diff --git a/src/esieequest/model/Room.java b/src/esieequest/model/Room.java index 0ab0a48..80b5ccd 100644 --- a/src/esieequest/model/Room.java +++ b/src/esieequest/model/Room.java @@ -22,7 +22,7 @@ public class Room { private String description; private String imageName; private HashMap exits; - private Item item; + private HashMap items; /** * Create a room described "description". Initially, it has no exits. @@ -37,6 +37,7 @@ public class Room { this.description = description; this.imageName = imageName; this.exits = new HashMap(); + this.items = new HashMap(); } public Room(String description) { @@ -107,18 +108,23 @@ public class Room { this.exits.put(direction, neighbor); } + public Set getItemList() { + return this.items.keySet(); + } + /** * @return the item */ - public Item getItem() { - return item; + public Item getItem(String itemName) { + return this.items.get(itemName); } /** - * @param item the item to set + * @param item + * the item to set */ - public void setItem(Item item) { - this.item = item; + public void addItem(String itemName, Item item) { + this.items.put(itemName, item); } } -- cgit v1.2.3