-
Notifications
You must be signed in to change notification settings - Fork 18
/
Zork.java
1455 lines (1341 loc) · 46.5 KB
/
Zork.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*Drew Schuster
dtschust
ECE462
*/
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.*;
/* Generic object, everything inherits from this*/
class ZorkObject
{
public String status;
public ArrayList<ZorkTrigger> trigger = new ArrayList<ZorkTrigger>();
public ZorkObject()
{
}
}
/* Generic condition*/
abstract class ZorkCondition
{
String object;
public abstract boolean evaluate(Zork zork);
}
/* Status conditions*/
class ZorkConditionStatus extends ZorkCondition
{
String status;
public boolean evaluate(Zork zork)
{
ZorkObject tested = zork.Objects.get(object);
if (tested != null && tested.status.equals(status))
return true;
else
return false;
}
}
/* Has conditions*/
class ZorkConditionHas extends ZorkCondition
{
String has;
String owner;
public boolean evaluate(Zork zork)
{
/*Inventory is a special case as it isn't the name of any object in the game, check for it specifically*/
if (owner.equals("inventory"))
{
if (zork.Inventory.get(object) != null && has.equals("yes") || zork.Inventory.get(object) == null && has.equals("no"))
{
return true;
}
else
{
return false;
}
}
else
{
/* is it a room?*/
ZorkRoom roomObject = zork.Rooms.get(owner);
if ( roomObject != null )
{
if ((roomObject).item.get(object) != null && has.equals("yes") || (roomObject).item.get(object) == null && has.equals("no"))
{
return true;
}
else
{
return false;
}
}
/* is it a container?*/
else
{
ZorkContainer containerObject = zork.Containers.get(owner);
if (containerObject != null )
{
if ((containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no"))
{
return true;
}
else
{
return false;
}
}
}
}
return false;
}
}
/* Special Command condition*/
class ZorkCommand extends ZorkCondition
{
String command;
public boolean evaluate(Zork zork)
{
if (command.equals(zork.userInput))
return true;
else
return false;
}
}
/*Trigger*/
class ZorkTrigger
{
public ArrayList<ZorkCondition> conditions = new ArrayList<ZorkCondition>();
String type="single"; /*By default, single*/
boolean hasCommand = false;
public ArrayList<String> print = new ArrayList<String>();
public ArrayList<String> action = new ArrayList<String>();
public boolean evaluate(Zork zork)
{
for(int i=0;i<conditions.size();i++)
{
if (!conditions.get(i).evaluate(zork))
{
return false;
}
}
return true;
}
}
/* Room*/
class ZorkRoom extends ZorkObject
{
public String name;
public String type="regular";
public String description;
public HashMap<String,String> border = new HashMap<String,String>();
public HashMap<String,String> container = new HashMap<String,String>();
public HashMap<String,String> item = new HashMap<String,String>();
public HashMap<String,String> creature = new HashMap<String,String>();
public ZorkRoom()
{
}
}
/* Item*/
class ZorkItem extends ZorkObject
{
public String name;
public String description;
public String writing;
public ArrayList<String> turnOnPrint = new ArrayList<String>();
public ArrayList<String> turnOnAction = new ArrayList<String>();
public ZorkItem()
{
}
}
/* Container*/
class ZorkContainer extends ZorkObject
{
public String name;
public HashMap<String,String> item = new HashMap<String,String>();
public String description;
public ArrayList<String> accept = new ArrayList<String>();
boolean isOpen;
public ZorkContainer()
{
}
}
/* Creature*/
class ZorkCreature extends ZorkObject
{
public String name;
public String description;
public HashMap<String,String> vulnerability = new HashMap<String,String>();
public ArrayList<ZorkCondition> conditions = new ArrayList<ZorkCondition>();
public ArrayList<String> print = new ArrayList<String>();
public ArrayList<String> action = new ArrayList<String>();
public ZorkCreature()
{
}
/* Evaluate the success of an attack*/
public boolean attack(Zork zork,String weapon)
{
if (vulnerability.get(weapon) == null)
{
return false;
}
for(int i=0;i<conditions.size();i++)
{
if (!conditions.get(i).evaluate(zork))
{
return false;
}
}
return true;
}
}
/* And away we go*/
public class Zork
{
Scanner source = new Scanner(System.in);
public String userInput="";
/*Hashmaps to store the instance of the game*/
public HashMap<String,ZorkRoom> Rooms = new HashMap<String,ZorkRoom>();
public HashMap<String,ZorkItem> Items = new HashMap<String,ZorkItem>();
public HashMap<String,ZorkContainer> Containers = new HashMap<String,ZorkContainer>();
public HashMap<String,ZorkObject> Objects = new HashMap<String,ZorkObject>();
public HashMap<String,ZorkCreature> Creatures = new HashMap<String,ZorkCreature>();
public HashMap<String,String> Inventory = new HashMap<String,String>();
public HashMap<String,String> ObjectLookup = new HashMap<String,String>();
public String currentRoom;
public File file;
public Zork(String filename)
{
int i,j,k,l,x,y,z;
file = new File(filename);
if (!file.canRead())
{
System.out.println("Error opening file. Exiting...");
return;
}
try
{
/* Open the xml file*/
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
Element rootElement = doc.getDocumentElement();
/* Every single first generation child is a room, container, creature, or item. So load them in*/
NodeList nodes = rootElement.getChildNodes();
for (k=0;k<nodes.getLength();k++)
{
Node node = nodes.item(k);
Element element;
if (node instanceof Element)
{
element = (Element)node;
String tagType = element.getTagName();
/* If it's a room ... */
if (tagType.equals("room"))
{
ZorkRoom tempRoom = new ZorkRoom();
/*Get all possible Room attributes*/
NodeList name = element.getElementsByTagName("name");
tempRoom.name = getString((Element)name.item(0));
NodeList type = element.getElementsByTagName("type");
if (type.getLength() > 0)
{
tempRoom.type = getString((Element)type.item(0));
}
else
{
tempRoom.type = "regular";
}
NodeList status = element.getElementsByTagName("status");
if (status.getLength() > 0)
{
tempRoom.status = getString((Element)type.item(0));
}
else
{
tempRoom.status = "";
}
NodeList description = element.getElementsByTagName("description");
tempRoom.description = getString((Element)description.item(0));
NodeList items = element.getElementsByTagName("item");
for (j=0;j<items.getLength();j++)
{
Element item = (Element)items.item(j);
String itemName = getString(item);
tempRoom.item.put(itemName,itemName);
}
NodeList creatures = element.getElementsByTagName("creature");
for (j=0;j<creatures.getLength();j++)
{
Element creature = (Element)creatures.item(j);
String creatureName = getString(creature);
tempRoom.creature.put(creatureName,creatureName);
}
NodeList triggers = element.getElementsByTagName("trigger");
for (j=0;j<triggers.getLength();j++)
{
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element)triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l=0;l<commands.getLength();l++)
{
Element command = (Element)commands.item(l);
ZorkCommand tempCommand = new ZorkCommand();
tempCommand.command = getString(command);
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
NodeList conditions = trigger.getElementsByTagName("condition");
for (l=0;l<conditions.getLength();l++)
{
Element condition = (Element)conditions.item(l);
NodeList object = condition.getElementsByTagName("object");
NodeList has = condition.getElementsByTagName("has");
if (has.getLength() > 0)
{
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
tempConditionHas.has = getString((Element)has.item(0));
tempConditionHas.object = getString((Element)object.item(0));
NodeList owner = condition.getElementsByTagName("owner");
tempConditionHas.owner = getString((Element)owner.item(0));
tempTrigger.conditions.add(tempConditionHas);
}
else
{
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
tempConditionStatus.object = getString((Element)object.item(0));
NodeList sstatus = condition.getElementsByTagName("status");
tempConditionStatus.status = getString((Element)sstatus.item(0));
tempTrigger.conditions.add(tempConditionStatus);
}
}
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0)
{
tempTrigger.type = getString((Element)ttype.item(0));
}
else
{
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l=0;l<prints.getLength();l++)
{
Element print = (Element)prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l=0;l<actions.getLength();l++)
{
Element action = (Element)actions.item(l);
tempTrigger.action.add(getString(action));
}
tempRoom.trigger.add(tempTrigger);
}
NodeList containers = element.getElementsByTagName("container");
for (j=0;j<containers.getLength();j++)
{
Element container = (Element)containers.item(j);
String containerName = getString(container);
tempRoom.container.put(containerName,containerName);
}
NodeList borders = element.getElementsByTagName("border");
for (j=0;j<borders.getLength();j++)
{
Element border = (Element)borders.item(j);
String borderdirection = getString((Element)border.getElementsByTagName("direction").item(0));
String bordername = getString((Element)border.getElementsByTagName("name").item(0));
tempRoom.border.put(borderdirection,bordername);
}
/*Add this room to the rooms hashmap, put it in the generic objects hashmap, and store it's type in the objectlookup hashmap*/
Rooms.put(tempRoom.name,tempRoom);
Objects.put(tempRoom.name,(ZorkObject)tempRoom);
ObjectLookup.put(tempRoom.name,"room");
}
/* If it's an item... */
else if (tagType.equals("item"))
{
ZorkItem tempItem = new ZorkItem();
/* Get all possible item attributes*/
NodeList name = element.getElementsByTagName("name");
if (name.getLength() > 0)
tempItem.name = getString((Element)name.item(0));
NodeList status = element.getElementsByTagName("status");
if (status.getLength() > 0)
{
tempItem.status = getString((Element)status.item(0));
}
else
{
tempItem.status = "";
}
NodeList description = element.getElementsByTagName("description");
if (description.getLength()>0)
tempItem.description = getString((Element)description.item(0));
NodeList writing = element.getElementsByTagName("writing");
if (writing.getLength()>0)
tempItem.writing = getString((Element)writing.item(0));
NodeList turnon = element.getElementsByTagName("turnon");
if (turnon.getLength()>0)
{
NodeList prints = element.getElementsByTagName("print");
for(j=0;j<prints.getLength();j++)
{
tempItem.turnOnPrint.add(getString((Element)prints.item(j)));
}
NodeList actions = element.getElementsByTagName("action");
for(j=0;j<actions.getLength();j++)
{
tempItem.turnOnAction.add(getString((Element)actions.item(j)));
}
}
NodeList triggers = element.getElementsByTagName("trigger");
for (j=0;j<triggers.getLength();j++)
{
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element)triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l=0;l<commands.getLength();l++)
{
Element command = (Element)commands.item(l);
ZorkCommand tempCommand = new ZorkCommand();
tempCommand.command = getString(command);
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
NodeList conditions = trigger.getElementsByTagName("condition");
for (l=0;l<conditions.getLength();l++)
{
Element condition = (Element)conditions.item(l);
NodeList object = condition.getElementsByTagName("object");
NodeList has = condition.getElementsByTagName("has");
if (has.getLength() > 0)
{
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
tempConditionHas.has = getString((Element)has.item(0));
tempConditionHas.object = getString((Element)object.item(0));
NodeList owner = condition.getElementsByTagName("owner");
tempConditionHas.owner = getString((Element)owner.item(0));
tempTrigger.conditions.add(tempConditionHas);
}
else
{
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
tempConditionStatus.object = getString((Element)object.item(0));
NodeList sstatus = condition.getElementsByTagName("status");
tempConditionStatus.status = getString((Element)sstatus.item(0));
tempTrigger.conditions.add(tempConditionStatus);
}
}
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0)
{
tempTrigger.type = getString((Element)ttype.item(0));
}
else
{
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l=0;l<prints.getLength();l++)
{
Element print = (Element)prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l=0;l<actions.getLength();l++)
{
Element action = (Element)actions.item(l);
tempTrigger.action.add(getString(action));
}
tempItem.trigger.add(tempTrigger);
}
/* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
Items.put(tempItem.name,tempItem);
Objects.put(tempItem.name,(ZorkObject)tempItem);
ObjectLookup.put(tempItem.name,"item");
}
/* If it's a container... */
else if (tagType.equals("container"))
{
ZorkContainer tempCont = new ZorkContainer();
/*Get all possible container attributes*/
NodeList name = element.getElementsByTagName("name");
if (name.getLength()>0)
tempCont.name = getString((Element)name.item(0));
NodeList status = element.getElementsByTagName("status");
if (status.getLength()>0)
tempCont.status = getString((Element)status.item(0));
/*Initially assume a closed container*/
tempCont.isOpen = false;
NodeList description = element.getElementsByTagName("description");
if (description.getLength()>0)
tempCont.description = getString((Element)description.item(0));
NodeList accepts = element.getElementsByTagName("accept");
for(j=0;j<accepts.getLength();j++)
{
/* If container has an accepts attribute, then it is always open*/
tempCont.isOpen = true;
tempCont.accept.add(getString((Element)accepts.item(j)));
}
NodeList citems = element.getElementsByTagName("item");
for (j=0;j<citems.getLength();j++)
{
Element item = (Element)citems.item(j);
String itemName = getString(item);
tempCont.item.put(itemName,itemName);
}
NodeList triggers = element.getElementsByTagName("trigger");
for (j=0;j<triggers.getLength();j++)
{
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element)triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l=0;l<commands.getLength();l++)
{
Element command = (Element)commands.item(l);
ZorkCommand tempCommand = new ZorkCommand();
tempCommand.command = getString(command);
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
NodeList conditions = trigger.getElementsByTagName("condition");
for (l=0;l<conditions.getLength();l++)
{
Element condition = (Element)conditions.item(l);
NodeList object = condition.getElementsByTagName("object");
NodeList has = condition.getElementsByTagName("has");
if (has.getLength() > 0)
{
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
tempConditionHas.has = getString((Element)has.item(0));
tempConditionHas.object = getString((Element)object.item(0));
NodeList owner = condition.getElementsByTagName("owner");
tempConditionHas.owner = getString((Element)owner.item(0));
tempTrigger.conditions.add(tempConditionHas);
}
else
{
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
tempConditionStatus.object = getString((Element)object.item(0));
NodeList sstatus = condition.getElementsByTagName("status");
tempConditionStatus.status = getString((Element)sstatus.item(0));
tempTrigger.conditions.add(tempConditionStatus);
}
}
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0)
{
tempTrigger.type = getString((Element)ttype.item(0));
}
else
{
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l=0;l<prints.getLength();l++)
{
Element print = (Element)prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l=0;l<actions.getLength();l++)
{
Element action = (Element)actions.item(l);
tempTrigger.action.add(getString(action));
}
tempCont.trigger.add(tempTrigger);
}
/* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
Containers.put(tempCont.name,tempCont);
Objects.put(tempCont.name,(ZorkObject)tempCont);
ObjectLookup.put(tempCont.name,"container");
}
/* And finally, if it's a creature...*/
else if (tagType.equals("creature"))
{
ZorkCreature tempCreature = new ZorkCreature();
/* Get all possible creature attributes*/
NodeList name = element.getElementsByTagName("name");
if (name.getLength()>0)
tempCreature.name = getString((Element)name.item(0));
NodeList status = element.getElementsByTagName("status");
if (status.getLength()>0)
tempCreature.status = getString((Element)status.item(0));
NodeList description = element.getElementsByTagName("description");
if (description.getLength()>0)
tempCreature.description = getString((Element)description.item(0));
NodeList vulns = element.getElementsByTagName("vulnerability");
for(j=0;j<vulns.getLength();j++)
{
String vulnString = getString((Element)vulns.item(j));
tempCreature.vulnerability.put(vulnString,vulnString);
}
NodeList attacks = element.getElementsByTagName("attack");
for (j=0;j<attacks.getLength();j++)
{
Element attack = (Element)attacks.item(j);
NodeList conditions = attack.getElementsByTagName("condition");
for (l=0;l<conditions.getLength();l++)
{
Element condition = (Element)conditions.item(l);
NodeList object = condition.getElementsByTagName("object");
NodeList has = condition.getElementsByTagName("has");
if (has.getLength() > 0)
{
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
tempConditionHas.has = getString((Element)has.item(0));
tempConditionHas.object = getString((Element)object.item(0));
NodeList owner = condition.getElementsByTagName("owner");
tempConditionHas.owner = getString((Element)owner.item(0));
tempCreature.conditions.add(tempConditionHas);
}
else
{
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
tempConditionStatus.object = getString((Element)object.item(0));
NodeList sstatus = condition.getElementsByTagName("status");
tempConditionStatus.status = getString((Element)sstatus.item(0));
tempCreature.conditions.add(tempConditionStatus);
}
}
NodeList prints = attack.getElementsByTagName("print");
for (l=0;l<prints.getLength();l++)
{
Element print = (Element)prints.item(l);
tempCreature.print.add(getString(print));
}
NodeList actions = attack.getElementsByTagName("action");
for (l=0;l<actions.getLength();l++)
{
Element action = (Element)actions.item(l);
tempCreature.action.add(getString(action));
}
}
NodeList triggers = element.getElementsByTagName("trigger");
for (j=0;j<triggers.getLength();j++)
{
ZorkTrigger tempTrigger = new ZorkTrigger();
Element trigger = (Element)triggers.item(j);
NodeList commands = trigger.getElementsByTagName("command");
for (l=0;l<commands.getLength();l++)
{
Element command = (Element)commands.item(l);
ZorkCommand tempCommand = new ZorkCommand();
tempCommand.command = getString(command);
tempTrigger.conditions.add(tempCommand);
tempTrigger.hasCommand = true;
}
NodeList conditions = trigger.getElementsByTagName("condition");
for (l=0;l<conditions.getLength();l++)
{
Element condition = (Element)conditions.item(l);
NodeList object = condition.getElementsByTagName("object");
NodeList has = condition.getElementsByTagName("has");
if (has.getLength() > 0)
{
ZorkConditionHas tempConditionHas = new ZorkConditionHas();
tempConditionHas.has = getString((Element)has.item(0));
tempConditionHas.object = getString((Element)object.item(0));
NodeList owner = condition.getElementsByTagName("owner");
tempConditionHas.owner = getString((Element)owner.item(0));
tempTrigger.conditions.add(tempConditionHas);
}
else
{
ZorkConditionStatus tempConditionStatus = new ZorkConditionStatus();
tempConditionStatus.object = getString((Element)object.item(0));
NodeList sstatus = condition.getElementsByTagName("status");
tempConditionStatus.status = getString((Element)sstatus.item(0));
tempTrigger.conditions.add(tempConditionStatus);
}
}
NodeList ttype = element.getElementsByTagName("type");
if (ttype.getLength() > 0)
{
tempTrigger.type = getString((Element)ttype.item(0));
}
else
{
tempTrigger.type = "single";
}
NodeList prints = trigger.getElementsByTagName("print");
for (l=0;l<prints.getLength();l++)
{
Element print = (Element)prints.item(l);
tempTrigger.print.add(getString(print));
}
NodeList actions = trigger.getElementsByTagName("action");
for (l=0;l<actions.getLength();l++)
{
Element action = (Element)actions.item(l);
tempTrigger.action.add(getString(action));
}
tempCreature.trigger.add(tempTrigger);
}
/* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
Creatures.put(tempCreature.name,tempCreature);
Objects.put(tempCreature.name,(ZorkObject)tempCreature);
ObjectLookup.put(tempCreature.name,"creature");
}
}
}
}
catch (Exception e)
{
System.out.println("Invalid XML file, exiting");
System.exit(-1);
//e.printStackTrace();
}
/*Phew, we're finally done with that ... let's get playing!!*/
/* Some temporary initialization variables*/
String tempString;
ZorkContainer tempContainer;
ZorkTrigger tempTrigger;
currentRoom = "Entrance";
boolean skip = false;
/* Print out the first entrance description, starting the game!*/
System.out.println(Rooms.get(currentRoom).description);
/* There is no stopping in Zork, until we're done!!*/
while(true)
{
skip=false;
userInput = source.nextLine();
/*Now that we have the user command, check the input*/
skip = checkAllTriggers();
/*We only skip if a matched trigger overwrites the execution of a command*/
if (skip)
{
continue;
}
/* If we haven't skipped, perform the user action*/
action(userInput);
/* Clear the user input, and check the triggers again (various states have changed, gnomes need to be found!*/
userInput = "";
checkAllTriggers();
}
}
/* Execute a user action or an action command from some <action> element that is not one of the "Special Commands"*/
public void action(String input)
{
int i,j,k,l,x,y,z;
String tempString;
ZorkContainer tempContainer;
/* Movement */
if (input.equals("n") || input.equals("s") || input.equals("e") || input.equals("w"))
{
move(input);
}
/* Inventory */
else if (input.equals("i"))
{
inventory();
}
/* Take */
else if (input.startsWith("take") && input.split(" ").length>=1)
{
tempString = input.split(" ")[1];
if ((Rooms.get(currentRoom)).item.get(tempString) != null)
{
Inventory.put(tempString,tempString);
ZorkRoom tempRoom = (Rooms.get(currentRoom));
tempRoom.item.remove(tempString);
Rooms.put(tempRoom.name,tempRoom);
System.out.println("Item "+tempString+" added to inventory.");
}
else
{
/*Search all containers in the current room for the item!*/
boolean found=false;
for (String key : Rooms.get(currentRoom).container.keySet())
{
tempContainer = Containers.get(key);
if (tempContainer != null && tempContainer.isOpen && tempContainer.item.get(tempString) != null)
{
Inventory.put(tempString,tempString);
tempContainer.item.remove(tempString);
Containers.put(tempContainer.name,tempContainer);
System.out.println("Item "+tempString+" added to inventory.");
found=true;
break;
}
}
if (!found)
System.out.println("Error");
}
}
/* Open Exit (you should be so lucky)*/
else if (input.equals("open exit"))
{
if (Rooms.get(currentRoom).type.equals("exit"))
{
System.out.println("Game Over");
System.exit(0);
}
else
{
System.out.println("Error");
}
}
/* Open a container */
else if (input.startsWith("open") && input.split(" ").length>=1)
{
tempString = input.split(" ")[1];
String found = Rooms.get(currentRoom).container.get(tempString);
if (found != null)
{
tempContainer = Containers.get(tempString);
tempContainer.isOpen = true;
containerInventory(tempContainer.item,tempString);
}
else
{
System.out.println("Error");
}
}
/* Read an object */
else if (input.startsWith("read") && input.split(" ").length>=1)
{
tempString = input.split(" ")[1];
ZorkItem tempItem;
if (Inventory.get(tempString) != null)
{
tempItem = Items.get(tempString);
if (tempItem.writing != null && tempItem.writing != "")
{
System.out.println(tempItem.writing);
}
else
{
System.out.println("Nothing written.");
}
}
else
{
System.out.println("Error");
}
}
/* Drop an item*/
else if (input.startsWith("drop") && input.split(" ").length>=1)
{
tempString = input.split(" ")[1];
if (Inventory.get(tempString) != null)
{
ZorkRoom tempRoom = Rooms.get(currentRoom);
tempRoom.item.put(tempString,tempString);
Rooms.put(tempRoom.name,tempRoom);
Inventory.remove(tempString);
System.out.println(tempString+" dropped.");
}
else
{
System.out.println("Error");
}
}
/* Put an item somewhere */
else if (input.startsWith("put") && input.split(" ").length>=4)
{
tempString = input.split(" ")[1];
String destination = input.split(" ")[3];
if (Rooms.get(currentRoom).container.get(destination) != null && Containers.get(destination).isOpen && Inventory.get(tempString) != null)
{
tempContainer = Containers.get(destination);
tempContainer.item.put(tempString,tempString);
Inventory.remove(tempString);
System.out.println("Item "+tempString+" added to "+destination+".");
}
else
{
System.out.println("Error");
}
}
/* Turn on an item*/
else if (input.startsWith("turn on") && input.split(" ").length>=3)
{
tempString = input.split(" ")[2];
ZorkItem tempItem;
if (Inventory.get(tempString) != null)
{
tempItem = Items.get(tempString);
System.out.println("You activate the "+tempString+".");
if (tempItem != null)
{
for (y=0;y<tempItem.turnOnPrint.size();y++)
{
System.out.println(tempItem.turnOnPrint.get(y));
}
for (y=0;y<tempItem.turnOnAction.size();y++)
{
performAction(tempItem.turnOnAction.get(y));
}
}
else
{
System.out.println("Error");
}
}
else
{
System.out.println("Error");
}
}
/* Attempt an attack, you feeling lucky?*/
else if (input.startsWith("attack") && input.split(" ").length>=4)
{
tempString = input.split(" ")[1];
ZorkCreature tempCreature;
String weapon = input.split(" ")[3];
if (Rooms.get(currentRoom).creature.get(tempString) != null)
{
tempCreature = Creatures.get(tempString);
if (tempCreature != null && Inventory.get(weapon)!= null)
{
if (tempCreature.attack(this,weapon))
{
System.out.println("You assault the "+tempString+" with the "+weapon+".");
for (y=0;y<tempCreature.print.size();y++)
{
System.out.println(tempCreature.print.get(y));
}
for (y=0;y<tempCreature.action.size();y++)
{
performAction(tempCreature.action.get(y));
}
}
else
{
System.out.println("Error");
}
}
else
{
System.out.println("Error");
}