diff options
author | Pacien TRAN-GIRARD | 2014-04-06 14:16:57 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-04-06 14:17:39 +0200 |
commit | 5ddb6df63ebb643a0e179f21f2895950be6d83da (patch) | |
tree | d760d8e8fe2fca20de33d582f31d2bae9cf402a7 /src | |
parent | b19e6711834472ee561831dc1e4a529382af20f5 (diff) | |
download | esieequest-5ddb6df63ebb643a0e179f21f2895950be6d83da.tar.gz |
Reorganise the model package and implement items actions
Diffstat (limited to 'src')
25 files changed, 320 insertions, 141 deletions
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 @@ | |||
1 | package esieequest.controller; | 1 | package esieequest.controller; |
2 | 2 | ||
3 | import esieequest.model.Game; | 3 | import esieequest.model.Game; |
4 | import esieequest.model.command.Command; | 4 | import esieequest.model.commands.Command; |
5 | import esieequest.view.View; | 5 | import esieequest.view.View; |
6 | 6 | ||
7 | /** | 7 | /** |
@@ -72,6 +72,12 @@ class Interpreter { | |||
72 | case "eat": | 72 | case "eat": |
73 | this.performer.eat(); | 73 | this.performer.eat(); |
74 | return; | 74 | return; |
75 | case "take": | ||
76 | this.performer.take(command.getOption()); | ||
77 | return; | ||
78 | case "drop": | ||
79 | this.performer.drop(command.getOption()); | ||
80 | return; | ||
75 | case "help": | 81 | case "help": |
76 | this.performer.showHelp(); | 82 | this.performer.showHelp(); |
77 | return; | 83 | return; |
@@ -83,4 +89,5 @@ class Interpreter { | |||
83 | this.performer.echo("Unknown command."); | 89 | this.performer.echo("Unknown command."); |
84 | return; | 90 | return; |
85 | } | 91 | } |
92 | |||
86 | } | 93 | } |
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 @@ | |||
1 | package esieequest.controller; | 1 | package esieequest.controller; |
2 | 2 | ||
3 | import esieequest.model.command.Command; | 3 | import esieequest.model.commands.Command; |
4 | 4 | ||
5 | /** | 5 | /** |
6 | * The command parser. | 6 | * 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 @@ | |||
1 | package esieequest.controller; | 1 | package esieequest.controller; |
2 | 2 | ||
3 | import esieequest.model.Game; | 3 | import esieequest.model.Game; |
4 | import esieequest.model.Room; | 4 | import esieequest.model.items.Inventory; |
5 | import esieequest.model.map.Room; | ||
5 | import esieequest.view.View; | 6 | import esieequest.view.View; |
6 | 7 | ||
7 | /** | 8 | /** |
@@ -130,4 +131,42 @@ class Performer { | |||
130 | this.echo(this.game.getEatMessage()); | 131 | this.echo(this.game.getEatMessage()); |
131 | } | 132 | } |
132 | 133 | ||
134 | /** | ||
135 | * Moves an item from the current Room to the Player's inventory. | ||
136 | * | ||
137 | * @param itemName | ||
138 | * the item's name | ||
139 | */ | ||
140 | public void take(final String itemName) { | ||
141 | this.moveItem(this.game.getPlayer().getCurrentRoom(), this.game.getPlayer(), itemName); | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * Moves an item from the Player's inventory to the current Room. | ||
146 | * | ||
147 | * @param itemName | ||
148 | * the item's name | ||
149 | */ | ||
150 | public void drop(final String itemName) { | ||
151 | this.moveItem(this.game.getPlayer(), this.game.getPlayer().getCurrentRoom(), itemName); | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * Moves a given item referred by its name from an inventory to another. | ||
156 | * | ||
157 | * @param source | ||
158 | * the source inventory | ||
159 | * @param dest | ||
160 | * the destination inventory | ||
161 | * @param itemName | ||
162 | * the item's name | ||
163 | */ | ||
164 | private void moveItem(final Inventory source, final Inventory dest, final String itemName) { | ||
165 | if (!source.hasItem(itemName)) { | ||
166 | this.echo("No such item."); | ||
167 | return; | ||
168 | } | ||
169 | dest.putItem(itemName, source.takeItem(itemName)); | ||
170 | } | ||
171 | |||
133 | } | 172 | } |
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 @@ | |||
1 | package esieequest.controller; | ||
2 | |||
3 | import java.util.Set; | ||
4 | |||
5 | public class Utils { | ||
6 | |||
7 | /** | ||
8 | * | ||
9 | * @param elements | ||
10 | * the elements | ||
11 | * @param prefix | ||
12 | * the prefix of the list | ||
13 | * @param emptyText | ||
14 | * the text that will be returned if the Set is empty | ||
15 | * @return the list | ||
16 | */ | ||
17 | public static String list(final Set<String> elements, final String prefix, final String emptyText) { | ||
18 | if (!elements.isEmpty()) { | ||
19 | return prefix + Utils.buildListString(elements) + "."; | ||
20 | } | ||
21 | return emptyText; | ||
22 | } | ||
23 | |||
24 | /** | ||
25 | * Builds a list in the form of a String from a given Set. | ||
26 | * | ||
27 | * @param elements | ||
28 | * the Set of Strings | ||
29 | * | ||
30 | * @return the list | ||
31 | */ | ||
32 | private static String buildListString(final Set<String> elements) { | ||
33 | final StringBuilder list = new StringBuilder(); | ||
34 | for (final String string : elements) { | ||
35 | list.append(" ").append(string); | ||
36 | } | ||
37 | return list.toString(); | ||
38 | } | ||
39 | |||
40 | } | ||
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; | |||
2 | 2 | ||
3 | import java.util.HashMap; | 3 | import java.util.HashMap; |
4 | 4 | ||
5 | import esieequest.model.entities.Player; | ||
6 | import esieequest.model.items.Item; | ||
7 | import esieequest.model.map.Room; | ||
8 | |||
5 | /** | 9 | /** |
6 | * Represents the game. | 10 | * Represents the game. |
7 | * | 11 | * |
@@ -187,8 +191,8 @@ public class Game { | |||
187 | * Creates and adds items into rooms. | 191 | * Creates and adds items into rooms. |
188 | */ | 192 | */ |
189 | private void createItems() { | 193 | private void createItems() { |
190 | this.rooms.get("Cafeteria").addItem("Banana", new Item("A yellow banana", 12)); | 194 | this.rooms.get("Cafeteria").putItem("banana", new Item("A yellow banana", 12)); |
191 | this.rooms.get("Cafeteria").addItem("Orange", new Item("An orange orange", 15)); | 195 | this.rooms.get("Cafeteria").putItem("orange", new Item("An orange orange", 15)); |
192 | } | 196 | } |
193 | 197 | ||
194 | /** | 198 | /** |
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 @@ | |||
1 | package esieequest.model; | ||
2 | |||
3 | import java.util.Stack; | ||
4 | |||
5 | public class Player { | ||
6 | |||
7 | private Room currentRoom; | ||
8 | private final Stack<Room> previousRooms; | ||
9 | |||
10 | public Player() { | ||
11 |