-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
549 lines (484 loc) · 15.7 KB
/
Game.java
File metadata and controls
549 lines (484 loc) · 15.7 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import java.util.Stack;
import java.util.ArrayList;
/**
* This class is the main class of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game. Users
* can walk around some scenery. That's all. It should really be extended
* to make it more interesting!
*
* 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.
*
* @author Michael Kolling and David J. Barnes
* @version 2006.03.30
*
* @author Lynn Marshall
* @version October 21, 2012
*
* @author Erin Mccombe
* @version March 20, 2026
*/
public class Game
{
private Parser parser;
private Room currentRoom;
private Room prevRoom;
private Stack<Room> roomBackStack;
private Item item;
private boolean hungry;
private int numItemsHeld;
/**
* Create the game and initialise its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
roomBackStack = new Stack<Room>();
prevRoom = null;
item = null;
hungry = true;
numItemsHeld = 0;
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, theatre, pub, lab, office, transporter;
Item chair, book, table, bowl, popcorn, tree, cookie, beamer1, beamer2;
// create the rooms
outside = new Room("outside the main entrance of the university");
theatre = new Room("in a lecture theatre");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
transporter = new TransporterRoom("in the transporting room");
// create new items
chair = new Item("wooden chair", 5.8, "chair");
book = new Item("fiction book", 1.1, "book");
table = new Item("large table", 7.2, "table");
bowl = new Item("ceramic bowl", 0.8, "bowl");
popcorn = new Item("buttered popcorn", 2.1, "popcorn");
tree = new Item("old oak tree", 23.8, "tree");
cookie = new Item("chocolate chip cookie", 0.5, "cookie");
beamer1 = new Beamer();
beamer2 = new Beamer();
// add items to rooms
pub.addItem(chair);
pub.addItem(table);
pub.addItem(bowl);
office.addItem(chair);
office.addItem(table);
lab.addItem(book);
theatre.addItem(popcorn);
outside.addItem(tree);
pub.addItem(cookie);
theatre.addItem(cookie);
office.addItem(cookie);
outside.addItem(beamer1);
pub.addItem(beamer2);
transporter.addItem(cookie);
// initialise room exits
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
theatre.setExit("west", outside);
theatre.setExit("north", transporter);
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
office.setExit("west", lab);
transporter.setExit("south", theatre);
currentRoom = outside; // start game outside
}
/**
* Main play routine. Loops until end of play.
*/
public void play()
{
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("Thank you for playing. Good bye.");
}
/**
* Print out the opening message for the player.
*/
private void printWelcome()
{
System.out.println();
System.out.println("Welcome to the World of Zuul!");
System.out.println("World of Zuul is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println();
System.out.println(currentRoom.getLongDescription());
System.out.println(currItemHeld());
}
/**
* Prints a description if player is currently holding an item and if so what
*
* @return a string of the current item
*/
private String currItemHeld()
{
if(item == null)
{
return "You are not currently holding anything";
}
return "You are holding a " + item.getName();
}
/**
* 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();
if (commandWord.equals("help")) {
printHelp();
}
else if (commandWord.equals("go")) {
goRoom(command);
}
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
else if (commandWord.equals("look")) {
look(command);
}
else if (commandWord.equals("eat")){
eat(command);
}
else if (commandWord.equals("back")){
back(command);
}
else if (commandWord.equals("stackBack")){
stackBack(command);
}
else if (commandWord.equals("take")){
take(command);
}
else if (commandWord.equals("drop")){
drop(command);
}
else if (commandWord.equals("charge")){
charge(command);
}
else if (commandWord.equals("fire")){
fire(command);
}
// else command not recognised.
return wantToQuit;
}
// implementations of user commands:
/**
* Print out some help information.
* Here we print a cryptic message and a list of the
* command words.
*/
private void printHelp()
{
System.out.println("You are lost. You are alone. You wander");
System.out.println("around at the university.");
System.out.println();
System.out.println("Your command words are:");
System.out.println(parser.getCommands());
}
/**
* Try to go to one direction. If there is an exit, enter the new
* room, otherwise print an error message.
*
* @param command The command to be processed
*/
private void goRoom(Command command)
{
if(!(currentRoom instanceof TransporterRoom) && (!command.hasSecondWord())) {
// if there is no second word, we don't know where to go...
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
System.out.println("There is no door!");
}
else {
// get previous room before moving and add it to stack back
prevRoom = currentRoom;
roomBackStack.push(prevRoom);
currentRoom = nextRoom;
System.out.println(currentRoom.getLongDescription());
System.out.println(currItemHeld());
}
}
/**
* "Quit" was entered. Check the rest of the command to see
* whether we really quit the game.
*
* @param command The command to be processed
* @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
}
}
/**
* "Look" was entered. Look around the room
*
* @param command The command to be processed
*/
private void look(Command command)
{
if(command.hasSecondWord()) {
// if there is a second word
System.out.println("Look what?");
return;
}
System.out.println(currentRoom.getLongDescription());
System.out.println(currItemHeld());
}
/**
* "Eat" was entered. Print eat line
*
* @param command The command to be processed
*/
private void eat(Command command)
{
if(command.hasSecondWord()) {
// if there is a second word
System.out.println("Eat what?");
return;
}
// eat only if holding a cookie
if(item == null)
{
System.out.println("You have no food to eat.");
return;
}
else if(!(item.getName().equals("cookie")))
{
System.out.println("You dont currently have a cookie to eat");
return;
}
else
{
System.out.println("You have eaten a cookie and are now full");
hungry = false;
item = null;
numItemsHeld = 0;
return;
}
}
/**
* "Back" was entered. Go to past room
*
* @param command The command to be processed
*/
private void back(Command command)
{
if(command.hasSecondWord()) {
// if there is a second word
System.out.println("Back what?");
return;
}
if (prevRoom == null)
{
System.out.println("You are at the beginning of the game, cannot go back yet.");
return;
}
// add past room to back stack
roomBackStack.push(currentRoom);
// swap current and prev room
Room temp = currentRoom;
currentRoom = prevRoom;
prevRoom = temp;
// print current room description
System.out.println("You have gone back");
System.out.println(currentRoom.getLongDescription());
System.out.println(currItemHeld());
return;
}
/**
* "stackBack" was entered. go to past rooms in stack form
*
* @param command The command to be processed
*/
private void stackBack (Command command)
{
if(command.hasSecondWord()) {
// if there is a second word
System.out.println("StackBack what?");
return;
}
if (roomBackStack.empty())
{
System.out.println("You are at the beginning of the game, cannot go back yet.");
return;
}
// get prev room to go back to and go back
prevRoom = currentRoom;
currentRoom = roomBackStack.pop();
System.out.println("You have gone stackBack");
System.out.println(currentRoom.getLongDescription());
System.out.println(currItemHeld());
return;
}
/**
* "Take" was entered. Take item if available in room and if not currently
* holding something
*
* @param command The command to be processed
*/
private void take(Command command)
{
if(!command.hasSecondWord()) {
// if there is a second word
System.out.println("Take what?");
return;
}
// if already holding something cant pick an item up
if(item != null)
{
System.out.println("You are already holding something, please drop it.");
return;
}
// get item to be taken
String itemToTake = command.getSecondWord();
// if hungry can only pick up a cookie
if(hungry && !(itemToTake.equals("cookie")))
{
System.out.println("You are hungry and must eat a cookie before getting a new item");
return;
}
// loop through each item in room, if in room take it
ArrayList<Item> currItems = currentRoom.getItems();
for (Item itemInRoom: currItems)
{
if(itemInRoom.getName().equals(itemToTake))
{
item = itemInRoom;
// remove item taken from room
currentRoom.removeItem(itemInRoom);
System.out.println("You picked up a " + itemInRoom.getName());
numItemsHeld ++;
// if picked up 5 items, change to hungry
if(numItemsHeld == 5)
{
hungry = true;
}
return;
}
}
// if item not in room print error message
System.out.println("That item is not in the room");
}
/**
* "drop" was entered. Drop current item if holding one
*
* @param command The command to be processed
*/
private void drop(Command command)
{
if(command.hasSecondWord()) {
// if there is a second word
System.out.println("Drop what?");
return;
}
// if not currently holding anything print error message
if(item == null)
{
System.out.println("You have nothing to drop.");
return;
}
// drop item currently held
// add item dropped to room
currentRoom.addItem(item);
System.out.println("You have dropped a " + item.getName());
item = null;
}
/**
* "charge" was entered. Charge current Beamer if holding one.
*
* @param command The command to be processed
*/
private void charge(Command command)
{
if(command.hasSecondWord()) {
// if there is a second word
System.out.println("Charge what?");
return;
}
// make sure holding a beamer currently
if(!(item instanceof Beamer))
{
System.out.println("Must be holding a Beamer to charge it");
return;
}
// cast the beamer so can use its methods
Beamer b = (Beamer) item;
if(b.isCharged())
{
System.out.println("Beamer is already charged. Please fire before charging");
return;
}
b.charge(currentRoom);
System.out.println("beamer has been charged");
}
/**
* "fire" was entered. Fire current Beamer if holding one.
*
* @param command The command to be processed
*/
private void fire(Command command)
{
if(command.hasSecondWord()) {
// if there is a second word
System.out.println("Fire what?");
return;
}
// make sure holding a beamer currently
if(!(item instanceof Beamer))
{
System.out.println("Must be holding a Beamer to charge it");
return;
}
// cast the beamer so can use its methods
Beamer b = (Beamer) item;
// make sure its charged before firing
if(!(b.isCharged()))
{
System.out.println("Beamer is not charged yet. Please charge first");
return;
}
// fire beamer
currentRoom = b.fire();
System.out.println("Beamer fired");
System.out.println(currentRoom.getLongDescription());
System.out.println(currItemHeld());
return;
}
}