diff --git a/README b/README deleted file mode 100644 index e901bed..0000000 --- a/README +++ /dev/null @@ -1,23 +0,0 @@ -################### -# To compile code # -################### - -make all - -################### -# To cleanup code # -################### - -make clean - -################### -# To execute code # -################### - -java Zork [game xml] - -###################### -# Sample Walkthrough # -###################### -(Example inputs can be found in RunThroughResults.txt as to how to beat the sample game) -java Zork sampleGame.xml diff --git a/REFACTORS.md b/REFACTORS.md new file mode 100644 index 0000000..41f6703 --- /dev/null +++ b/REFACTORS.md @@ -0,0 +1,8 @@ +# Refactors done + +- chore: move to package `com.github.dtschust.zork` + - default package is bad +- remove 5 redundant `if` statements from `ZorkCommand`, `ZorkCommandHas`, `ZorkCondtionStatus' + - e.g. `if (blah) return true; else return false;` can be simplified to `return blah;` +- replaced 2 `for` with foreach loops in `ZorkCreature` and `ZorkTrigger` +- 23 use of diamond operator in `Zork`, `ZorkContainer`, `ZorkCreature`, `ZorkItem`, `ZorkObject`, `ZorkRoom`, `ZorkTrigger` \ No newline at end of file diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index 6439edf..2228cd8 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -19,13 +19,13 @@ import java.util.Scanner; public class Zork { public String userInput = ""; /*Hashmaps to store the instance of the game*/ - public HashMap Rooms = new HashMap(); - public HashMap Items = new HashMap(); - public HashMap Containers = new HashMap(); - public HashMap Objects = new HashMap(); - public HashMap Creatures = new HashMap(); - public HashMap Inventory = new HashMap(); - public HashMap ObjectLookup = new HashMap(); + public HashMap Rooms = new HashMap<>(); + public HashMap Items = new HashMap<>(); + public HashMap Containers = new HashMap<>(); + public HashMap Objects = new HashMap<>(); + public HashMap Creatures = new HashMap<>(); + public HashMap Inventory = new HashMap<>(); + public HashMap ObjectLookup = new HashMap<>(); public String currentRoom; public File file; Scanner source = new Scanner(System.in); @@ -164,7 +164,7 @@ public class Zork { /*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); + Objects.put(tempRoom.name, tempRoom); ObjectLookup.put(tempRoom.name, "room"); } @@ -261,7 +261,7 @@ public class Zork { /* 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); + Objects.put(tempItem.name, tempItem); ObjectLookup.put(tempItem.name, "item"); } @@ -356,7 +356,7 @@ public class Zork { /* 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); + Objects.put(tempCont.name, tempCont); ObjectLookup.put(tempCont.name, "container"); } @@ -478,7 +478,7 @@ public class Zork { /* 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); + Objects.put(tempCreature.name, tempCreature); ObjectLookup.put(tempCreature.name, "creature"); } @@ -698,7 +698,6 @@ public class Zork { else { System.out.println("Error"); } - return; } /* Check triggers */ diff --git a/src/main/java/com/github/dtschust/zork/ZorkCommand.java b/src/main/java/com/github/dtschust/zork/ZorkCommand.java index 9b4d0ed..a58efdc 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkCommand.java +++ b/src/main/java/com/github/dtschust/zork/ZorkCommand.java @@ -5,9 +5,6 @@ class ZorkCommand extends ZorkCondition { String command; public boolean evaluate(Zork zork) { - if (command.equals(zork.userInput)) - return true; - else - return false; + return command.equals(zork.userInput); } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java b/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java index f8e7bc6..7bf5559 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java +++ b/src/main/java/com/github/dtschust/zork/ZorkConditionHas.java @@ -8,30 +8,18 @@ class ZorkConditionHas extends ZorkCondition { 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; - } + return zork.Inventory.get(object) != null && has.equals("yes") || zork.Inventory.get(object) == null && has.equals("no"); } 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; - } + return (roomObject).item.get(object) != null && has.equals("yes") || (roomObject).item.get(object) == null && has.equals("no"); } /* 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 (containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no"); } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkConditionStatus.java b/src/main/java/com/github/dtschust/zork/ZorkConditionStatus.java index ca32228..25e9eab 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkConditionStatus.java +++ b/src/main/java/com/github/dtschust/zork/ZorkConditionStatus.java @@ -6,9 +6,6 @@ class ZorkConditionStatus extends ZorkCondition { public boolean evaluate(Zork zork) { ZorkObject tested = zork.Objects.get(object); - if (tested != null && tested.status.equals(status)) - return true; - else - return false; + return tested != null && tested.status.equals(status); } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkContainer.java b/src/main/java/com/github/dtschust/zork/ZorkContainer.java index e055c1c..6797aee 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkContainer.java +++ b/src/main/java/com/github/dtschust/zork/ZorkContainer.java @@ -6,9 +6,9 @@ import java.util.HashMap; /* Container*/ class ZorkContainer extends ZorkObject { public String name; - public HashMap item = new HashMap(); + public HashMap item = new HashMap<>(); public String description; - public ArrayList accept = new ArrayList(); + public ArrayList accept = new ArrayList<>(); boolean isOpen; public ZorkContainer() { diff --git a/src/main/java/com/github/dtschust/zork/ZorkCreature.java b/src/main/java/com/github/dtschust/zork/ZorkCreature.java index 950a701..eab8a21 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkCreature.java +++ b/src/main/java/com/github/dtschust/zork/ZorkCreature.java @@ -7,10 +7,10 @@ import java.util.HashMap; class ZorkCreature extends ZorkObject { public String name; public String description; - public HashMap vulnerability = new HashMap(); - public ArrayList conditions = new ArrayList(); - public ArrayList print = new ArrayList(); - public ArrayList action = new ArrayList(); + public HashMap vulnerability = new HashMap<>(); + public ArrayList conditions = new ArrayList<>(); + public ArrayList print = new ArrayList<>(); + public ArrayList action = new ArrayList<>(); public ZorkCreature() { } @@ -20,8 +20,8 @@ class ZorkCreature extends ZorkObject { if (vulnerability.get(weapon) == null) { return false; } - for (int i = 0; i < conditions.size(); i++) { - if (!conditions.get(i).evaluate(zork)) { + for (ZorkCondition condition : conditions) { + if (!condition.evaluate(zork)) { return false; } } diff --git a/src/main/java/com/github/dtschust/zork/ZorkItem.java b/src/main/java/com/github/dtschust/zork/ZorkItem.java index 53c480f..a354395 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkItem.java +++ b/src/main/java/com/github/dtschust/zork/ZorkItem.java @@ -7,8 +7,8 @@ class ZorkItem extends ZorkObject { public String name; public String description; public String writing; - public ArrayList turnOnPrint = new ArrayList(); - public ArrayList turnOnAction = new ArrayList(); + public ArrayList turnOnPrint = new ArrayList<>(); + public ArrayList turnOnAction = new ArrayList<>(); public ZorkItem() { } diff --git a/src/main/java/com/github/dtschust/zork/ZorkObject.java b/src/main/java/com/github/dtschust/zork/ZorkObject.java index 10a69c5..f5abc90 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkObject.java +++ b/src/main/java/com/github/dtschust/zork/ZorkObject.java @@ -5,7 +5,7 @@ import java.util.ArrayList; /* Generic object, everything inherits from this*/ class ZorkObject { public String status; - public ArrayList trigger = new ArrayList(); + public ArrayList trigger = new ArrayList<>(); public ZorkObject() { } diff --git a/src/main/java/com/github/dtschust/zork/ZorkRoom.java b/src/main/java/com/github/dtschust/zork/ZorkRoom.java index 18b6443..898d7e2 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkRoom.java +++ b/src/main/java/com/github/dtschust/zork/ZorkRoom.java @@ -7,10 +7,10 @@ class ZorkRoom extends ZorkObject { public String name; public String type = "regular"; public String description; - public HashMap border = new HashMap(); - public HashMap container = new HashMap(); - public HashMap item = new HashMap(); - public HashMap creature = new HashMap(); + public HashMap border = new HashMap<>(); + public HashMap container = new HashMap<>(); + public HashMap item = new HashMap<>(); + public HashMap creature = new HashMap<>(); public ZorkRoom() { } diff --git a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java index f175044..5fb5f75 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java +++ b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java @@ -4,15 +4,15 @@ import java.util.ArrayList; /*Trigger*/ class ZorkTrigger { - public ArrayList conditions = new ArrayList(); - public ArrayList print = new ArrayList(); - public ArrayList action = new ArrayList(); + public ArrayList conditions = new ArrayList<>(); + public ArrayList print = new ArrayList<>(); + public ArrayList action = new ArrayList<>(); String type = "single"; /*By default, single*/ boolean hasCommand = false; public boolean evaluate(Zork zork) { - for (int i = 0; i < conditions.size(); i++) { - if (!conditions.get(i).evaluate(zork)) { + for (ZorkCondition condition : conditions) { + if (!condition.evaluate(zork)) { return false; } }