refactor: initial IDEA-aided refactors on syntax

This commit is contained in:
Claudio Maggioni 2022-11-16 16:36:08 +01:00
parent 2406dac418
commit 6e374918b5
12 changed files with 44 additions and 78 deletions

23
README
View file

@ -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
View 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`

View file

@ -19,13 +19,13 @@ import java.util.Scanner;
public class Zork { public class Zork {
public String userInput = ""; public String userInput = "";
/*Hashmaps to store the instance of the game*/ /*Hashmaps to store the instance of the game*/
public HashMap<String, ZorkRoom> Rooms = new HashMap<String, ZorkRoom>(); public HashMap<String, ZorkRoom> Rooms = new HashMap<>();
public HashMap<String, ZorkItem> Items = new HashMap<String, ZorkItem>(); public HashMap<String, ZorkItem> Items = new HashMap<>();
public HashMap<String, ZorkContainer> Containers = new HashMap<String, ZorkContainer>(); public HashMap<String, ZorkContainer> Containers = new HashMap<>();
public HashMap<String, ZorkObject> Objects = new HashMap<String, ZorkObject>(); public HashMap<String, ZorkObject> Objects = new HashMap<>();
public HashMap<String, ZorkCreature> Creatures = new HashMap<String, ZorkCreature>(); public HashMap<String, ZorkCreature> Creatures = new HashMap<>();
public HashMap<String, String> Inventory = new HashMap<String, String>(); public HashMap<String, String> Inventory = new HashMap<>();
public HashMap<String, String> ObjectLookup = new HashMap<String, String>(); public HashMap<String, String> ObjectLookup = new HashMap<>();
public String currentRoom; public String currentRoom;
public File file; public File file;
Scanner source = new Scanner(System.in); 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*/ /*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); Rooms.put(tempRoom.name, tempRoom);
Objects.put(tempRoom.name, (ZorkObject) tempRoom); Objects.put(tempRoom.name, tempRoom);
ObjectLookup.put(tempRoom.name, "room"); 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*/ /* Put each item in the items hashmap, the generic objects hashmap, and store its type in objectlookup*/
Items.put(tempItem.name, tempItem); Items.put(tempItem.name, tempItem);
Objects.put(tempItem.name, (ZorkObject) tempItem); Objects.put(tempItem.name, tempItem);
ObjectLookup.put(tempItem.name, "item"); 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*/ /* Put each container in the containers hashmap, the generic object hashmap, and the objectlookup hashmap*/
Containers.put(tempCont.name, tempCont); Containers.put(tempCont.name, tempCont);
Objects.put(tempCont.name, (ZorkObject) tempCont); Objects.put(tempCont.name, tempCont);
ObjectLookup.put(tempCont.name, "container"); 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*/ /* Put each creature in the creatures hashmap, the generic object hashmap, and the objectlookup hashmap*/
Creatures.put(tempCreature.name, tempCreature); Creatures.put(tempCreature.name, tempCreature);
Objects.put(tempCreature.name, (ZorkObject) tempCreature); Objects.put(tempCreature.name, tempCreature);
ObjectLookup.put(tempCreature.name, "creature"); ObjectLookup.put(tempCreature.name, "creature");
} }
@ -698,7 +698,6 @@ public class Zork {
else { else {
System.out.println("Error"); System.out.println("Error");
} }
return;
} }
/* Check triggers */ /* Check triggers */

View file

@ -5,9 +5,6 @@ class ZorkCommand extends ZorkCondition {
String command; String command;
public boolean evaluate(Zork zork) { public boolean evaluate(Zork zork) {
if (command.equals(zork.userInput)) return command.equals(zork.userInput);
return true;
else
return false;
} }
} }

View file

@ -8,30 +8,18 @@ class ZorkConditionHas extends ZorkCondition {
public boolean evaluate(Zork zork) { 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*/ /*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 (owner.equals("inventory")) {
if (zork.Inventory.get(object) != null && has.equals("yes") || zork.Inventory.get(object) == null && has.equals("no")) { return zork.Inventory.get(object) != null && has.equals("yes") || zork.Inventory.get(object) == null && has.equals("no");
return true;
} else {
return false;
}
} else { } else {
/* is it a room?*/ /* is it a room?*/
ZorkRoom roomObject = zork.Rooms.get(owner); ZorkRoom roomObject = zork.Rooms.get(owner);
if (roomObject != null) { if (roomObject != null) {
if ((roomObject).item.get(object) != null && has.equals("yes") || (roomObject).item.get(object) == null && has.equals("no")) { return (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?*/ /* is it a container?*/
else { else {
ZorkContainer containerObject = zork.Containers.get(owner); ZorkContainer containerObject = zork.Containers.get(owner);
if (containerObject != null) { if (containerObject != null) {
if ((containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no")) { return (containerObject).item.get(object) != null && has.equals("yes") || (containerObject).item.get(object) == null && has.equals("no");
return true;
} else {
return false;
}
} }
} }

View file

@ -6,9 +6,6 @@ class ZorkConditionStatus extends ZorkCondition {
public boolean evaluate(Zork zork) { public boolean evaluate(Zork zork) {
ZorkObject tested = zork.Objects.get(object); ZorkObject tested = zork.Objects.get(object);
if (tested != null && tested.status.equals(status)) return tested != null && tested.status.equals(status);
return true;
else
return false;
} }
} }

View file

@ -6,9 +6,9 @@ import java.util.HashMap;
/* Container*/ /* Container*/
class ZorkContainer extends ZorkObject { class ZorkContainer extends ZorkObject {
public String name; public String name;
public HashMap<String, String> item = new HashMap<String, String>(); public HashMap<String, String> item = new HashMap<>();
public String description; public String description;
public ArrayList<String> accept = new ArrayList<String>(); public ArrayList<String> accept = new ArrayList<>();
boolean isOpen; boolean isOpen;
public ZorkContainer() { public ZorkContainer() {

View file

@ -7,10 +7,10 @@ import java.util.HashMap;
class ZorkCreature extends ZorkObject { class ZorkCreature extends ZorkObject {
public String name; public String name;
public String description; public String description;
public HashMap<String, String> vulnerability = new HashMap<String, String>(); public HashMap<String, String> vulnerability = new HashMap<>();
public ArrayList<ZorkCondition> conditions = new ArrayList<ZorkCondition>(); public ArrayList<ZorkCondition> conditions = new ArrayList<>();
public ArrayList<String> print = new ArrayList<String>(); public ArrayList<String> print = new ArrayList<>();
public ArrayList<String> action = new ArrayList<String>(); public ArrayList<String> action = new ArrayList<>();
public ZorkCreature() { public ZorkCreature() {
} }
@ -20,8 +20,8 @@ class ZorkCreature extends ZorkObject {
if (vulnerability.get(weapon) == null) { if (vulnerability.get(weapon) == null) {
return false; return false;
} }
for (int i = 0; i < conditions.size(); i++) { for (ZorkCondition condition : conditions) {
if (!conditions.get(i).evaluate(zork)) { if (!condition.evaluate(zork)) {
return false; return false;
} }
} }

View file

@ -7,8 +7,8 @@ class ZorkItem extends ZorkObject {
public String name; public String name;
public String description; public String description;
public String writing; public String writing;
public ArrayList<String> turnOnPrint = new ArrayList<String>(); public ArrayList<String> turnOnPrint = new ArrayList<>();
public ArrayList<String> turnOnAction = new ArrayList<String>(); public ArrayList<String> turnOnAction = new ArrayList<>();
public ZorkItem() { public ZorkItem() {
} }

View file

@ -5,7 +5,7 @@ import java.util.ArrayList;
/* Generic object, everything inherits from this*/ /* Generic object, everything inherits from this*/
class ZorkObject { class ZorkObject {
public String status; public String status;
public ArrayList<ZorkTrigger> trigger = new ArrayList<ZorkTrigger>(); public ArrayList<ZorkTrigger> trigger = new ArrayList<>();
public ZorkObject() { public ZorkObject() {
} }

View file

@ -7,10 +7,10 @@ class ZorkRoom extends ZorkObject {
public String name; public String name;
public String type = "regular"; public String type = "regular";
public String description; public String description;
public HashMap<String, String> border = new HashMap<String, String>(); public HashMap<String, String> border = new HashMap<>();
public HashMap<String, String> container = new HashMap<String, String>(); public HashMap<String, String> container = new HashMap<>();
public HashMap<String, String> item = new HashMap<String, String>(); public HashMap<String, String> item = new HashMap<>();
public HashMap<String, String> creature = new HashMap<String, String>(); public HashMap<String, String> creature = new HashMap<>();
public ZorkRoom() { public ZorkRoom() {
} }

View file

@ -4,15 +4,15 @@ import java.util.ArrayList;
/*Trigger*/ /*Trigger*/
class ZorkTrigger { class ZorkTrigger {
public ArrayList<ZorkCondition> conditions = new ArrayList<ZorkCondition>(); public ArrayList<ZorkCondition> conditions = new ArrayList<>();
public ArrayList<String> print = new ArrayList<String>(); public ArrayList<String> print = new ArrayList<>();
public ArrayList<String> action = new ArrayList<String>(); public ArrayList<String> action = new ArrayList<>();
String type = "single"; /*By default, single*/ String type = "single"; /*By default, single*/
boolean hasCommand = false; boolean hasCommand = false;
public boolean evaluate(Zork zork) { public boolean evaluate(Zork zork) {
for (int i = 0; i < conditions.size(); i++) { for (ZorkCondition condition : conditions) {
if (!conditions.get(i).evaluate(zork)) { if (!condition.evaluate(zork)) {
return false; return false;
} }
} }