refactor: initial IDEA-aided refactors on syntax
This commit is contained in:
parent
2406dac418
commit
6e374918b5
12 changed files with 44 additions and 78 deletions
23
README
23
README
|
@ -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
|
8
REFACTORS.md
Normal file
8
REFACTORS.md
Normal file
|
@ -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`
|
|
@ -19,13 +19,13 @@ import java.util.Scanner;
|
|||
public class Zork {
|
||||
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 HashMap<String, ZorkRoom> Rooms = new HashMap<>();
|
||||
public HashMap<String, ZorkItem> Items = new HashMap<>();
|
||||
public HashMap<String, ZorkContainer> Containers = new HashMap<>();
|
||||
public HashMap<String, ZorkObject> Objects = new HashMap<>();
|
||||
public HashMap<String, ZorkCreature> Creatures = new HashMap<>();
|
||||
public HashMap<String, String> Inventory = new HashMap<>();
|
||||
public HashMap<String, String> 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 */
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,9 +6,9 @@ import java.util.HashMap;
|
|||
/* Container*/
|
||||
class ZorkContainer extends ZorkObject {
|
||||
public String name;
|
||||
public HashMap<String, String> item = new HashMap<String, String>();
|
||||
public HashMap<String, String> item = new HashMap<>();
|
||||
public String description;
|
||||
public ArrayList<String> accept = new ArrayList<String>();
|
||||
public ArrayList<String> accept = new ArrayList<>();
|
||||
boolean isOpen;
|
||||
|
||||
public ZorkContainer() {
|
||||
|
|
|
@ -7,10 +7,10 @@ import java.util.HashMap;
|
|||
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 HashMap<String, String> vulnerability = new HashMap<>();
|
||||
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
|
||||
public ArrayList<String> print = new ArrayList<>();
|
||||
public ArrayList<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ 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 ArrayList<String> turnOnPrint = new ArrayList<>();
|
||||
public ArrayList<String> turnOnAction = new ArrayList<>();
|
||||
|
||||
public ZorkItem() {
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.util.ArrayList;
|
|||
/* Generic object, everything inherits from this*/
|
||||
class ZorkObject {
|
||||
public String status;
|
||||
public ArrayList<ZorkTrigger> trigger = new ArrayList<ZorkTrigger>();
|
||||
public ArrayList<ZorkTrigger> trigger = new ArrayList<>();
|
||||
|
||||
public ZorkObject() {
|
||||
}
|
||||
|
|
|
@ -7,10 +7,10 @@ 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 HashMap<String, String> border = new HashMap<>();
|
||||
public HashMap<String, String> container = new HashMap<>();
|
||||
public HashMap<String, String> item = new HashMap<>();
|
||||
public HashMap<String, String> creature = new HashMap<>();
|
||||
|
||||
public ZorkRoom() {
|
||||
}
|
||||
|
|
|
@ -4,15 +4,15 @@ import java.util.ArrayList;
|
|||
|
||||
/*Trigger*/
|
||||
class ZorkTrigger {
|
||||
public ArrayList<ZorkCondition> conditions = new ArrayList<ZorkCondition>();
|
||||
public ArrayList<String> print = new ArrayList<String>();
|
||||
public ArrayList<String> action = new ArrayList<String>();
|
||||
public ArrayList<ZorkCondition> conditions = new ArrayList<>();
|
||||
public ArrayList<String> print = new ArrayList<>();
|
||||
public ArrayList<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue