-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
330 lines (296 loc) · 9.21 KB
/
Game.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import java.util.HashMap;
import java.util.ArrayList;
import java.util.HashSet;
/**
* This class is the main class of the "Detective Zuul" application.
* "Detective Zuul" is a game based on "World of Zuul", created by Michael Kölling and David J. Barnes.
* Users can explore and interact with the scenery
*
* To play this game, create an instance of this class and call the "play"
* method.
*
* This main class creates and initialises all the others: it creates all
* rooms, creates the parser and starts the game. It also evaluates and
* executes the commands that the parser returns.
*
*/
public class Game
{
private Parser parser;
private Stage currentStage;
/**
* Create the game and initialise its internal map.
*/
public Game()
{
parser = new Parser();
currentStage=null;
}
/**
* Load the game
*
*/
public void loadGame(){
this.currentStage = LoadSave.loadZuul();
}
/**
* Print everything on the player's view
*/
private void printLocationInfo(){
System.out.println("\t[ " + currentStage.getPlayerCurrentRoomName()+" ]"+currentStage.getPlayerDirection());
System.out.println(currentStage.getPlayerCurrentRoomDescription());
System.out.println(currentStage.getPlayerViewSideDescription());
System.out.println("Objects:");
for(String s : currentStage.getPlayerViewObjects()){
System.out.print(s);
System.out.print("\t");
}
System.out.println("");
}
/**
* Main play routine. Loops until end of play.
*/
public void play()
{
if(this.currentStage.equals(null)){
System.out.println("OOPS, its wrong.. do not do this again");
}else{
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over.
boolean finished = false;
while (! finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Game Over");
}
}
/**
* Print out the opening message for the player.
*/
private void printWelcome()
{
System.out.println();
System.out.println("Detective Zuul");
System.out.println("You are Zuul. A detective in somewhereland city.");
System.out.println("Police departament calls you when the crime commited is unaswered.");
System.out.println("Type 'help' if you need help.");
System.out.println();
printLocationInfo();
}
/**
* Given a command, process (that is: execute) the command.
* @param command The command to be processed.
* @return true If the command ends the game, false otherwise.
*/
private boolean processCommand(Command command)
{
boolean wantToQuit = false;
if(command.isUnknown()) {
System.out.println("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
// Print help
if (commandWord.equals("help")) {
printHelp();
}
// Go somewhere
else if (commandWord.equals("go")) {
goRoom(command);
}
// Exit the game
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
else if (commandWord.equals("look")) {
look(command);
}
else if (commandWord.equals("examine")){
examine(command);
}
else if(commandWord.equals("pick")){
pick(command);
}
else if(commandWord.equals("bag")){
bag();
}
else if (commandWord.equals("interact")){
interact(command);
}
else if (commandWord.equals("loot")){
loot(command);
}
else if (commandWord.equals("use")){
use(command);
}
else if (commandWord.equals("combine")){
combine(command);
}
return wantToQuit;
}
//TODO: change everything from this point
// implementations of user commands:
/**
* Print out some help information.
* Here we print some stupid, cryptic message and a list of the
* command words.
*/
private void printHelp()
{
System.out.println("You are at the crime scene.");
System.out.println("Find out how the murder was done (or not. this is not functional yet)");
System.out.println();
System.out.println(parser.showCommands());
}
/**
* Try to go trhough the door. If there is open, enter
* the new room, stay in the same room.
*/
private void goRoom(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Go which door?");
return;
}
String door = command.getSecondWord();
currentStage.playerGoRoom(door);
this.printLocationInfo();
}
/**
* Change where player is looking
*/
private void look(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
printLocationInfo();
return;
}
String direction = command.getSecondWord();
currentStage.setPlayerDirection(direction);
this.printLocationInfo();
}
/**
* Examine objects, returning the description of the Object
*
* @param name of the Object
*
* @return the description of this thing
*/
private void examine(Command command){
if(!command.hasSecondWord()){
System.out.println("examine what?");
return;
}
String thing = command.getSecondWord();
System.out.println(currentStage.getPlayerViewThingDescription(thing));
}
/**
* Pick up an Item
*
* @param name of the Thing
*/
private void pick(Command command){
if(!command.hasSecondWord()){
System.out.println("Pick up what?");
return;
}
String thing = command.getSecondWord();
if ( currentStage.getPlayerViewObjects().contains(thing)){
currentStage.playerPickItem(thing);
}
this.printLocationInfo();
}
/**
* Print Player's bag
*
*/
private void bag(){
System.out.println("\t [ Bag ]\t");
for (String s: currentStage.showPlayerBag()){
System.out.print(s);
System.out.print("\t");
}
System.out.println("");
}
/**
* tries to interact with object
*
*/
private void interact(Command command){
if(!command.hasSecondWord()){
System.out.println("Interact with what?");
return;
}
String thing = command.getSecondWord();
if (!currentStage.playerInteract(thing)){
System.out.println("not succeed to interact with "+ thing);
}
}
/**
* Loot a furniture, picking up everything inside
*
*/
private void loot(Command command){
if(!command.hasSecondWord()){
System.out.println("Loot what?");
return;
}
String furniture = command.getSecondWord();
currentStage.playerGetFurnitureItems(furniture);
}
/**
* Use bag item into other objectin visible(not in bag)
*/
private void use(Command command){
if(!command.hasSecondWord()){
System.out.println("Use what?");
return;
}
if(!command.hasThirdWord()){
System.out.println("Use "+command.getSecondWord()+" where?");
return;
}
String item = command.getSecondWord();
String object = command.getThirdWord();
if(currentStage.playerUseItem(item,object)){
System.out.println("It worked!");
}else{
System.out.println("Well... that's not how i should use it");
}
}
/**
* Combine Items
*/
private void combine(Command command){
if(!command.hasSecondWord()){
System.out.println("Combine what?");
return;
}
if(!command.hasThirdWord()){
System.out.println("Combine "+command.getSecondWord()+" with what?");
return;
}
String item1 = command.getSecondWord();
String item2 = command.getThirdWord();
currentStage.playerCombineItem(item1,item2);
}
/**
* "Quit" was entered. Check the rest of the command to see
* whether we really quit the game.
* @return true, if this command quits the game, false otherwise.
*/
private boolean quit(Command command)
{
if(command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
}
else {
return true; // signal that we want to quit
}
}
}