aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-03-05 11:38:59 +0100
committerPacien TRAN-GIRARD2014-03-05 11:38:59 +0100
commit04f03d12815161b81444626ac9fd9a596b59a544 (patch)
tree75454fd62903f7ee42b171f504d4a088d788b8d7 /src
parent499ca98547b7ac5a3d2b37bde5678c7cb5a33d97 (diff)
downloadesieequest-04f03d12815161b81444626ac9fd9a596b59a544.tar.gz
Add multiple item support
Diffstat (limited to 'src')
-rw-r--r--src/esieequest/controller/Performer.java41
-rw-r--r--src/esieequest/model/Game.java24
-rw-r--r--src/esieequest/model/Room.java18
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 @@
3 */ 3 */
4package esieequest.controller; 4package esieequest.controller;
5 5
6import java.util.Iterator;
7import java.util.Set;
8
6import esieequest.model.Game; 9import esieequest.model.Game;
7import esieequest.model.Room; 10import esieequest.model.Room;
8import esieequest.view.View; 11import esieequest.view.View;
@@ -21,53 +24,69 @@ public class Performer {
21 this.view = view; 24 this.view = view;
22 } 25 }
23 26
27 private String buildListString(Set<String> elements) {
28 StringBuilder list = new StringBuilder();
29 for (Iterator<String> iterator = elements.iterator(); iterator.hasNext();) {
30 list.append(" ");
31 list.append(iterator.next());
32 }
33 return list.toString();
34 }
35
24 public void echo(String message) { 36 public void echo(String message) {
25 this.view.echo(message); 37 this.view.echo(message);
26 } 38 }
27 39
28 public void newGame() { 40 public void newGame() {
29 // this.loadGame(default game); 41 // this.loadGame(default game);
30 this.view.echo(this.game.getWelcomeMessage() + "\n" + this.game.getLocationInfo()); 42 this.echo(this.game.getWelcomeMessage() + "\n" + this.game.getLocationInfo());
31 } 43 }
32 44
33 public void loadGame() { 45 public void loadGame() {
34 this.view.echo("Not implemented."); 46 this.echo("Not implemented.");
35 } 47 }
36 48
37 public void saveGame() { 49 public void saveGame() {
38 this.view.echo("Not implemented."); 50 this.echo("Not implemented.");
39 } 51 }
40 52
41 public void toggleSound() { 53 public void toggleSound() {
42 this.view.echo("Not implemented."); 54 this.echo("Not implemented.");
43 } 55 }
44 56
45 public void quitGame() { 57 public void quitGame() {
46 this.view.echo(this.game.getQuitMessage()); 58 this.echo(this.game.getQuitMessage());
47 this.game.setRunning(false); 59 this.game.setRunning(false);
48 } 60 }
49 61
50 public void showHelp() { 62 public void showHelp() {
51 this.view.echo(this.game.getHelpMessage()); 63 this.echo(this.game.getHelpMessage());
64 // TODO: list commands
52 } 65 }
53 66
54 public void goTo(String direction) { 67 public void goTo(String direction) {
55 Room nextRoom = this.game.getRoomExit(direction); 68 Room nextRoom = this.game.getRoomExit(direction);
56 if (nextRoom != null) { 69 if (nextRoom != null) {
57 this.game.goToRoom(nextRoom); 70 this.game.goToRoom(nextRoom);
58 this.view.echo(this.game.getLocationInfo()); 71 this.echo(this.game.getLocationInfo());
59 } else { 72 } else {
60 this.view.echo(this.game.getNoExitMessage()); 73 this.echo(this.game.getNoExitMessage());
61 } 74 }
62 } 75 }
63 76
64 public void look() { 77 public void look() {
65 this.view.echo(this.game.getLocationInfo()); 78 this.echo(this.game.getLocationInfo());
66 this.view.echo(this.game.getItemDescription()); 79 this.listItems();
80 }
81
82 private void listItems() {
83 if (!this.game.getItemList().isEmpty()) {
84 this.echo(this.game.getItemListPrefix() + this.buildListString(this.game.getItemList()));
85 }
67 } 86 }
68 87
69 public void eat() { 88 public void eat() {
70 this.view.echo(this.game.getEatMessage()); 89 this.echo(this.game.getEatMessage());
71 } 90 }
72 91
73} 92}
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 @@
1package esieequest.model; 1package esieequest.model;
2 2
3import java.util.HashMap; 3import java.util.HashMap;
4import java.util.Set;
4 5
5/** 6/**
6 * 7 *
@@ -166,7 +167,8 @@ public class Game {
166 } 167 }
167 168
168 private void createItems() { 169 private void createItems() {
169 this.rooms.get("Cafeteria").setItem(new Item("Banana", 12)); 170 this.rooms.get("Cafeteria").addItem("Banana", new Item("A yellow banana", 12));
171 this.rooms.get("Cafeteria").addItem("Orange", new Item("An orange orange", 15));
170 } 172 }
171 173
172 public void setRunning(boolean state) { 174 public void setRunning(boolean state) {
@@ -197,6 +199,14 @@ public class Game {
197 return this.currentRoom.getImageName(); 199 return this.currentRoom.getImageName();
198 } 200 }
199 201
202 public String getItemDescription(String itemName) {
203 return this.currentRoom.getItem(itemName).getDescription();
204 }
205
206 public Set<String> getItemList() {
207 return this.currentRoom.getItemList();
208 }
209
200 public String getEatMessage() { 210 public String getEatMessage() {
201 return "oNommNommNomm..."; 211 return "oNommNommNomm...";
202 } 212 }
@@ -217,12 +227,12 @@ public class Game {
217 return "There is no exit."; 227 return "There is no exit.";
218 } 228 }
219 229
220 public String getItemDescription() { 230 public String getCommandListPrefix() {
221 if (this.currentRoom.getItem() != null) { 231 return "Available commands:";
222 return "This room contain : " + this.currentRoom.getItem().getDescription(); 232 }
223 } else { 233
224 return ""; 234 public String getItemListPrefix() {
225 } 235 return "This room contains:";
226 } 236 }
227 237
228} 238}
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 {
22 private String description; 22 private String description;
23 private String imageName; 23 private String imageName;
24 private HashMap<String, Room> exits; 24 private HashMap<String, Room> exits;
25 private Item item; 25 private HashMap<String, Item> items;
26 26
27 /** 27 /**
28 * Create a room described "description". Initially, it has no exits. 28 * Create a room described "description". Initially, it has no exits.
@@ -37,6 +37,7 @@ public class Room {
37 this.description = description; 37 this.description = description;
38 this.imageName = imageName; 38 this.imageName = imageName;
39 this.exits = new HashMap<String, Room>(); 39 this.exits = new HashMap<String, Room>();
40 this.items = new HashMap<String, Item>();
40 } 41 }
41 42
42 public Room(String description) { 43 public Room(String description) {
@@ -107,18 +108,23 @@ public class Room {
107 this.exits.put(direction, neighbor); 108 this.exits.put(direction, neighbor);
108 } 109 }
109 110
111 public Set<String> getItemList() {
112 return this.items.keySet();
113 }
114
110 /** 115 /**
111 * @return the item 116 * @return the item
112 */ 117 */
113 public Item getItem() { 118 public Item getItem(String itemName) {
114 return item; 119 return this.items.get(itemName);
115 } 120 }
116 121
117 /** 122 /**
118 * @param item the item to set 123 * @param item
124 * the item to set
119 */ 125 */
120 public void setItem(Item item) { 126 public void addItem(String itemName, Item item) {