-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
536 lines (444 loc) · 17.7 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
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
import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashMap;
import java.io.*;
import java.util.Iterator;
/**
* This is the main class of the Magical Realm Land program.
* Users are to walk around rooms, interacting with items until they reach the end of the game.
*
* 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, items and creatures, creates the parser and player and starts the game. It also evaluates and
* executes the commands that the parser returns.
*
* @author 162224
* @version 15/11
*
*/
public class Game
{
public Parser parser;
public Player player;
public Room currentRoom;
private ArrayList<Room> allrooms; //A store of all rooms for use by the Portal
private ArrayList<String> userInputs;
private Random random;
boolean finished = false; //A boolean to determine the end of game
boolean gameOver = false; //Another boolean to determine an early termination of the game through user actions
/**
* Create the game and initialise its internal map.
*/
public Game()
{
Logger.setLogger(null);
createInstances();
parser = new Parser();
player = new Player();
userInputs = new ArrayList<String>();
}
/**
* A method which retrieves a random room from the array of available rooms
*
*/
public Room getRandomRoom() {
random = new Random();
Room portal = allrooms.get(random.nextInt(allrooms.size()-1));
return portal;
}
/**
* Create all the rooms and link their exits together.
* Also initialises items and creatures and places them
*
*/
private void createInstances()
{
Room EnchantedForest, IdyllicGlade, GumdropHill, DenseWoods, VolcanoHell, SecretHotSpring, MagmaChamber, MysteriousCloud, SkyParadise, Cirrostratus, Nimbostratus, Stratocumulus, WaterTemple, Whirlpool, EyeoftheStorm, BossQuarters, Portal;
random = new Random();
allrooms = new ArrayList<Room>();
// create the rooms providing an ID to be passed into the room constructor
EnchantedForest = new EnchantedForestRoom();
allrooms.add(EnchantedForest);
IdyllicGlade = new IdyllicGladeRoom();
allrooms.add(IdyllicGlade);
GumdropHill = new GumdropHillRoom();
allrooms.add(GumdropHill);
DenseWoods = new DenseWoodsRoom();
allrooms.add(DenseWoods);
VolcanoHell = new VolcanoHellRoom();
allrooms.add(VolcanoHell);
SecretHotSpring = new SecretHotSpringRoom();
allrooms.add(SecretHotSpring);
MagmaChamber = new MagmaChamberRoom();
allrooms.add(MagmaChamber);
MysteriousCloud = new MysteriousCloudRoom();
allrooms.add(MysteriousCloud);
SkyParadise = new SkyParadiseRoom();
allrooms.add(SkyParadise);
Cirrostratus = new CirrostratusRoom();
allrooms.add(Cirrostratus);
Nimbostratus = new NimbostratusRoom();
allrooms.add(Nimbostratus);
Stratocumulus = new StratocumulusRoom();
allrooms.add(Stratocumulus);
WaterTemple = new WaterTempleRoom();
allrooms.add(WaterTemple);
Whirlpool = new WhirlpoolRoom();
allrooms.add(Whirlpool);
EyeoftheStorm = new EyeoftheStormRoom();
allrooms.add(EyeoftheStorm);
BossQuarters = new BossQuartersRoom();
allrooms.add(BossQuarters);
Portal = new PortalRoom();
// initialise room exits, items and creatures
EnchantedForest.setExit("east", IdyllicGlade);
EnchantedForest.setExit("west", GumdropHill);
EnchantedForest.setExit("south", DenseWoods);
IdyllicGlade.setExit("west", EnchantedForest);
GumdropHill.setExit("down", EnchantedForest);
GumdropHill.setExit("up", MysteriousCloud);
DenseWoods.setExit("north", EnchantedForest);
DenseWoods.setExit("south", VolcanoHell);
MagmaChamber.setExit("north",VolcanoHell);
VolcanoHell.setExit("east", SecretHotSpring);
VolcanoHell.setExit("north", DenseWoods);
VolcanoHell.setExit("west", MysteriousCloud);
VolcanoHell.setExit("south", MagmaChamber);
SecretHotSpring.setExit("west", VolcanoHell);
MysteriousCloud.setExit("west", VolcanoHell);
MysteriousCloud.setExit("in", Portal);
MysteriousCloud.setExit("up", SkyParadise);
SkyParadise.setExit("down", MysteriousCloud);
SkyParadise.setExit("up", Cirrostratus);
SkyParadise.setExit("east", Nimbostratus);
Cirrostratus.setExit("north", SkyParadise);
Cirrostratus.setExit("down", WaterTemple);
Nimbostratus.setExit("west", SkyParadise);
Nimbostratus.setExit("east", Stratocumulus);
Stratocumulus.setExit("west", Nimbostratus);
Stratocumulus.setExit("down", WaterTemple);
WaterTemple.setExit("up",Stratocumulus);
WaterTemple.setExit("high", Cirrostratus);
WaterTemple.setExit("down", Whirlpool);
WaterTemple.setExit("in", BossQuarters);
Whirlpool.setExit("up", WaterTemple);
Whirlpool.setExit("down", EyeoftheStorm);
EyeoftheStorm.setExit("up", Whirlpool);
EyeoftheStorm.setExit("in", BossQuarters);
BossQuarters.setExit("out", WaterTemple);
currentRoom = EnchantedForest;
}
public void guiCommand(String inputText){
if (finished) return;
inputText = inputText.trim().toLowerCase();
Logger.Log("> " + inputText);
userInputs.add(inputText);
if (inputText.equals("quit")){
Logger.Log("Thank you for playing. Good bye.");
finished = true;
quitGame();
return;
}
String[] words = inputText.split(" ");
if (words.length != 2) {
Logger.Log(inputText + " is not a valid command.");
return;
}
String word1 = words[0];
String word2 = words[1];
Command command = new Command(parser.commands.getCommandWord(word1), word2);
finished = processCommand(command);
if (gameOver){
Logger.Log("Your adventure is over...");
finished = true;
quitGame();
return;
}
if (finished){
Logger.Log("Thank you for playing. Good bye.");
quitGame();
return;
}
}
public void quitGame(){
try{
PrintWriter writer = new PrintWriter("userlog.txt", "UTF-8");
for(String s : userInputs){
writer.println(s);
}
writer.close();
}
catch (Exception e){
Logger.Log("Unable to write to file");
}
}
/**
* Main play routine for console version. Loops until end of play.
*/
public void playConsole(){
consoleWelcome();
while (! finished) {
if (gameOver) {
Logger.Log("Your adventure is over...");
break;
}
Command command = parser.getCommand();
finished = processCommand(command);
}
if (!gameOver) {
Logger.Log("Thank you for playing. Good bye.");
}
}
/**
* Print out the opening message for the player.
*/
private void consoleWelcome()
{
welcomePartOne();
Scanner reader = new Scanner(System.in);
String playerName = reader.nextLine();
player.playerName = playerName;
welcomePartTwo();
}
public void welcomePartOne(){
Logger.Log("");
Logger.Log("~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~");
Logger.Log("Welcome to the Magical Realm Land");
Logger.Log("~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~");
Logger.Log("Magical Realm Land is a wonderous place full of secrets and adventure!");
Logger.Log("");
Logger.Log("What is your name, daring explorer?");
}
public void welcomePartTwo(){
Logger.Log("");
Logger.Log("");
Logger.Log("Well hello there, " + player.playerName);
Logger.Log("");
Logger.Log("You seem to have found yourself in a land far away from home with no idea how you arrived.");
Logger.Log("");
Logger.Log("Your task is to get yourself home!");
Logger.Log("");
Logger.Log("There are a few things you can do to get yourself back.");
Logger.Log("Try typing 'look' in new areas to see what you can find!");
Logger.Log("You may be able to take items, or use them to recruit companions who you can use against bosses.");
Logger.Log("");
Logger.Log("Otherwise type'" + CommandWord.HELP + "' if you need help. Have a lovely time :)");
Logger.Log("");
Logger.Log(currentRoom.getShortDescription());
Logger.Log("");
Logger.Log(currentRoom.getLongDescription());
}
/**
* 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;
CommandWord commandWord = command.getCommandWord();
if(commandWord == CommandWord.UNKNOWN) {
Logger.Log("I don't know what you mean...");
return false;
}
if (commandWord == CommandWord.HELP) {
printHelp();
}
else if (commandWord == CommandWord.GO) {
goRoom(command);
}
else if (commandWord == CommandWord.TAKE) {
takeItem(command);
}
else if (commandWord == CommandWord.USE) {
useItem(command);
}
else if (commandWord == CommandWord.LOOK) {
Logger.Log(currentRoom.getLongDescription());
}
else if (commandWord == CommandWord.VIEW) {
viewPlayer(command);
}
else if (commandWord == CommandWord.QUIT) {
wantToQuit = quit(command);
}
// else command not recognised.
return wantToQuit;
}
// 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()
{
Logger.Log("You have somehow ended up in this strange, magical land. But really you just want to go home.");
Logger.Log("");
Logger.Log("Your command words are:");
parser.showCommands();
Logger.Log("You may view your inventory or your companions");
}
/**
* Try to go to one direction. If there is an exit, enter the new
* room, otherwise print an error message.
*
* If entering the portal, take user to a random room which is generated every time the portal is entered
*
*/
private void goRoom(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
Logger.Log("Where are you trying to go?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
Logger.Log("You walk repeatedly into a wall... Please try another direction");
}
// If the portal is entered, call the random room method
else {
if (nextRoom.RoomID == 17){
//System.out.println("????????"); //An unusable string which may be implemented later
Logger.Log("~*~*~PoRtAl~*~*~");
nextRoom = getRandomRoom();
}
currentRoom = nextRoom;
Logger.Log(currentRoom.getShortDescription());
}
}
/**
* Takes an item from the room and places it into the inventory.
* Items in rooms are unlimited quantity so they can be re-looted.
*
*/
private void takeItem(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
Logger.Log("What are you trying to take?");
return;
}
String itemName = command.getSecondWord();
String secondName = itemName + "2";
Item thisItem = currentRoom.getItem(itemName);
Item secondItem = currentRoom.getItem(secondName);
if (thisItem == null) {
Logger.Log("There isn't a " + itemName + "in here!");
}
else if (player.inventory.containsKey(itemName)){
Logger.Log("You take another " + itemName + " and put it in your backpack.");
player.addItem(secondName, secondItem);
}
else if (player.inventory.containsKey(itemName)&&(player.inventory.containsKey(secondName))){
Logger.Log("You don't need any more " + itemName + "s!");
}
else {
Logger.Log("You take the " + itemName + " and put it in your backpack.");
player.addItem(itemName, thisItem);
}
}
/**
* Determines which item the player wishes to use an calls the appropriate method.
* In this case, the user string calls upon a creature OR item to be used.
* Occasionally, if a creature is used, it may flee.
*
*/
private void useItem(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
Logger.Log("What are you trying to use?");
return;
}
String itemUsed = command.getSecondWord();
String secondUsed = itemUsed + "2";
Item thisItem = player.getItem(itemUsed);
Item secondItem = player.getItem(secondUsed);
Creature thisFriend = player.getCompanion(itemUsed);
//Tries to retrieve which item or creature to use. If not, an error message is returned
if (thisItem == null&&secondItem == null&&thisFriend == null) {
Logger.Log("You don't have a " + itemUsed);
}
else if (player.inventory.containsKey(itemUsed)) {
player.removeItem(itemUsed); //The item is removed from inventory
thisItem.UseItem(this);
}
else if (player.inventory.containsKey(secondUsed)) {
player.removeItem(secondUsed); //The item is removed from inventory
secondItem.UseItem(this);
}
else if (player.friends.containsKey(itemUsed)){
thisFriend.UseCompanion(this);
if (thisFriend.flees){
player.removeCompanion(itemUsed); //Companions do not get removed from inventory unless the flees boolean has been set to true.
}
}
}
/**
* The command to view either inventory or current companions.
* If an incompatible command is entered, return an error message.
* If that particular inventory is empty, return a relevant statement.
*
*/
private void viewPlayer(Command command)
{ if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
Logger.Log("What do you want to view?");
return;
}
String thingToView = command.getSecondWord();
String inventory = "inventory";
String companions = "companions";
if (!thingToView.equals(inventory)&&!thingToView.equals(companions)){
Logger.Log("You can only view your inventory or your current companions");
}
else if (thingToView.equals(inventory)&&!player.inventory.isEmpty()) {
Logger.Log("~*" + player.playerName + "'s Backpack *~");
player.viewInventory();
Logger.Log("~*~*~*~*~*~");
}
else if (thingToView.equals(inventory)&&player.inventory.isEmpty()){
Logger.Log("There is nothing in your backpack...");
}
else if (thingToView.equals(companions)&&!player.friends.isEmpty()) {
Logger.Log("~*" + player.playerName + "'s Companions *~");
player.viewCompanions();
Logger.Log("~*~*~*~*~*~");
}
else if (thingToView.equals(companions)&&player.friends.isEmpty()) {
Logger.Log("You don't have any companions at the moment :(");
}
}
/**
* "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()) {
Logger.Log("Quit what?");
return false;
}
else {
return true; // signal that we want to quit
}
}
public HashMap<String, Item> getInventory(){
return player.inventory;
}
public HashMap<String, Creature> getCreatures(){
return player.friends;
}
public HashMap<String, Creature> getBosses(){
return player.defeated;
}
// public ArrayList<
}