move userInput to method arguments and add some final
This commit is contained in:
parent
42732e0fdd
commit
214bed9a13
29 changed files with 59 additions and 64 deletions
|
@ -5,7 +5,6 @@
|
|||
|
||||
package com.github.dtschust.zork;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.parser.ZorkReader;
|
||||
import com.github.dtschust.zork.repl.ActionDispatcher;
|
||||
import com.github.dtschust.zork.types.ZorkContainer;
|
||||
|
@ -19,9 +18,8 @@ import static com.github.dtschust.zork.Zork.Type.*;
|
|||
/* And away we go*/
|
||||
public class Zork {
|
||||
public enum Type {ROOM, ITEM, CONTAINER, CREATURE}
|
||||
public String userInput;
|
||||
|
||||
public ZorkGame game;
|
||||
ZorkGame game;
|
||||
Scanner source = new Scanner(System.in);
|
||||
|
||||
public Zork(String filename) {
|
||||
|
@ -36,16 +34,15 @@ public class Zork {
|
|||
|
||||
/* There is no stopping in Zork, until we're done!!*/
|
||||
while (game.isRunning()) {
|
||||
userInput = source.nextLine();
|
||||
String userInput = source.nextLine();
|
||||
|
||||
/*Now that we have the user command, check the input*/
|
||||
if (!executeTriggers()) {
|
||||
if (!executeTriggers(userInput)) {
|
||||
/* If we haven't skipped, perform the user action*/
|
||||
d.dispatch(userInput);
|
||||
|
||||
/* Clear the user input, and check the triggers again (various states have changed, gnomes need to be found!*/
|
||||
userInput = "";
|
||||
executeTriggers();
|
||||
executeTriggers("");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,81 +61,81 @@ public class Zork {
|
|||
}
|
||||
|
||||
/* Check triggers */
|
||||
public boolean executeTriggers() {
|
||||
public boolean executeTriggers(String input) {
|
||||
|
||||
/*Variable initialization*/
|
||||
boolean skip;
|
||||
|
||||
/*Check Room triggers*/
|
||||
skip = doTriggersRoom();
|
||||
skip = doTriggersRoom(input);
|
||||
/* Check items in the containers in the room */
|
||||
skip = skip || doTriggersItemsInContainersInRoom();
|
||||
skip = skip || doTriggersItemsInContainersInRoom(input);
|
||||
/* Check all containers in the room*/
|
||||
skip = skip || doTriggersContainersInRoom();
|
||||
skip = skip || doTriggersContainersInRoom(input);
|
||||
/* Check all creatures in the room */
|
||||
skip = skip || doTriggersCreaturesInRoom();
|
||||
skip = skip || doTriggersCreaturesInRoom(input);
|
||||
/* Check items in inventory */
|
||||
skip = skip || doTriggersItemsInInventory();
|
||||
skip = skip || doTriggersItemsInInventory(input);
|
||||
/* Check items in room */
|
||||
skip = skip || doTriggersItemsInRoom();
|
||||
skip = skip || doTriggersItemsInRoom(input);
|
||||
|
||||
return skip;
|
||||
}
|
||||
|
||||
private boolean doTriggersContainersInRoom() {
|
||||
private boolean doTriggersContainersInRoom(String input) {
|
||||
boolean skip = false;
|
||||
for (String key : game.getCurrentRoom().container) {
|
||||
skip = skip || doZorkTriggers(game.get(CONTAINER, key));
|
||||
skip = skip || doZorkTriggers(game.get(CONTAINER, key), input);
|
||||
}
|
||||
return skip;
|
||||
}
|
||||
|
||||
private boolean doTriggersItemsInContainersInRoom() {
|
||||
private boolean doTriggersItemsInContainersInRoom(String input) {
|
||||
boolean skip = false;
|
||||
for (String key : game.getCurrentRoom().container) {
|
||||
ZorkContainer tempContainer = (ZorkContainer) game.get(CONTAINER, key);
|
||||
for (String key2 : tempContainer.item) {
|
||||
skip = skip || doZorkTriggers(game.get(ITEM, key2));
|
||||
skip = skip || doZorkTriggers(game.get(ITEM, key2), input);
|
||||
}
|
||||
}
|
||||
return skip;
|
||||
}
|
||||
|
||||
private boolean doTriggersItemsInRoom() {
|
||||
private boolean doTriggersItemsInRoom(String input) {
|
||||
boolean skip = false;
|
||||
for (String key : game.getCurrentRoom().item) {
|
||||
skip = skip || doZorkTriggers(game.get(ITEM, key));
|
||||
skip = skip || doZorkTriggers(game.get(ITEM, key), input);
|
||||
}
|
||||
return skip;
|
||||
}
|
||||
|
||||
private boolean doTriggersItemsInInventory() {
|
||||
private boolean doTriggersItemsInInventory(String input) {
|
||||
boolean skip = false;
|
||||
for (String key : game.inventory) {
|
||||
skip = skip || doZorkTriggers(game.get(ITEM, key));
|
||||
skip = skip || doZorkTriggers(game.get(ITEM, key), input);
|
||||
}
|
||||
return skip;
|
||||
}
|
||||
|
||||
private boolean doTriggersCreaturesInRoom() {
|
||||
private boolean doTriggersCreaturesInRoom(String input) {
|
||||
boolean skip = false;
|
||||
for (String key : game.getCurrentRoom().creature) {
|
||||
skip = skip || doZorkTriggers(game.get(CREATURE, key));
|
||||
skip = skip || doZorkTriggers(game.get(CREATURE, key), input);
|
||||
}
|
||||
return skip;
|
||||
}
|
||||
|
||||
private boolean doTriggersRoom() {
|
||||
return doZorkTriggers(game.getCurrentRoom());
|
||||
private boolean doTriggersRoom(String input) {
|
||||
return doZorkTriggers(game.getCurrentRoom(), input);
|
||||
}
|
||||
|
||||
|
||||
private boolean doZorkTriggers(ZorkObject zorkObject) {
|
||||
private boolean doZorkTriggers(ZorkObject zorkObject, String input) {
|
||||
boolean skip = false;
|
||||
Iterator<ZorkTrigger> iterator = zorkObject.trigger.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
ZorkTrigger tempTrigger = iterator.next();
|
||||
if (tempTrigger.evaluate(this)) {
|
||||
if (tempTrigger.evaluate(game, input)) {
|
||||
for (String print : tempTrigger.print) {
|
||||
System.out.println(print);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
|
||||
/* Generic condition*/
|
||||
public abstract class ZorkCondition {
|
||||
public final String object;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.types.ZorkContainer;
|
||||
import com.github.dtschust.zork.types.ZorkRoom;
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.github.dtschust.zork;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.types.ZorkObject;
|
||||
|
||||
/* Status conditions*/
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.github.dtschust.zork.parser;
|
||||
package com.github.dtschust.zork;
|
||||
|
||||
import com.github.dtschust.zork.Zork.Type;
|
||||
import com.github.dtschust.zork.types.*;
|
|
@ -27,10 +27,10 @@ public class ZorkTrigger {
|
|||
this.type = type;
|
||||
}
|
||||
|
||||
public boolean evaluate(Zork zork) {
|
||||
if (!commands.stream().allMatch(c -> c.matchesInput(zork.userInput))) {
|
||||
public boolean evaluate(ZorkGame game, String input) {
|
||||
if (!commands.stream().allMatch(c -> c.matchesInput(input))) {
|
||||
return false;
|
||||
}
|
||||
return conditions.stream().allMatch(c -> c.evaluate(zork.game));
|
||||
return conditions.stream().allMatch(c -> c.evaluate(game));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.github.dtschust.zork.parser;
|
||||
|
||||
import com.github.dtschust.zork.ZorkCondition;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkTrigger;
|
||||
import com.github.dtschust.zork.parser.builders.Parsers;
|
||||
import com.github.dtschust.zork.parser.dom.Elements;
|
||||
|
@ -208,7 +209,7 @@ public class ZorkReader {
|
|||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
// e.printStackTrace();
|
||||
System.out.println("Invalid XML file, exiting");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
|
|
@ -30,11 +30,8 @@ public class ZorkContainerParseStrategy implements ZorkParseStrategy<ZorkContain
|
|||
|
||||
final List<String> accepts = Elements.innerTextByTagName(element, "accept");
|
||||
|
||||
// If a container has an accepts attribute, then it is always open
|
||||
final boolean open = !accepts.isEmpty();
|
||||
|
||||
final List<String> items = Elements.innerTextByTagName(element, "item");
|
||||
|
||||
return new ZorkContainer(name, description, open, status, items, accepts, triggers);
|
||||
return new ZorkContainer(name, description, status, items, accepts, triggers);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,8 +27,6 @@ public class ZorkTriggerListParseStrategy implements ZorkParseStrategy<ZorkTrigg
|
|||
.map(ZorkCommand::new)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
final boolean hasCommand = !commands.isEmpty();
|
||||
|
||||
final List<ZorkCondition> conditions = Elements.byTagName(trigger, "condition").stream()
|
||||
.map(conditionStrategy::parse)
|
||||
.collect(Collectors.toList());
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.actions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.Zork.Type;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.HasSetOfCollectable;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.ZorkCreature;
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.Zork.Type;
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.HasSetOfCollectable;
|
||||
import com.github.dtschust.zork.types.ZorkObject;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.ZorkRoom;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
|
||||
import java.util.List;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
|
||||
import java.util.List;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
|
||||
import java.util.List;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.ZorkContainer;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.ZorkContainer;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.ZorkItem;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.ZorkContainer;
|
||||
import com.github.dtschust.zork.types.ZorkRoom;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.ZorkItem;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.repl.actions;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.Action;
|
||||
import com.github.dtschust.zork.types.ZorkMap;
|
||||
import com.github.dtschust.zork.types.ZorkObject;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.dtschust.zork.types;
|
||||
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
import com.github.dtschust.zork.repl.ActionDispatcher;
|
||||
|
||||
import java.util.List;
|
||||
|
|
|
@ -15,13 +15,13 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
|
|||
|
||||
public ZorkContainer(final String name,
|
||||
final String description,
|
||||
final boolean open,
|
||||
final String status,
|
||||
final Collection<String> items,
|
||||
final Collection<String> accepts,
|
||||
final Collection<ZorkTrigger> triggers) {
|
||||
super(name, description, status, triggers);
|
||||
this.open = open;
|
||||
// If a container has an accepts attribute, then it is always open
|
||||
this.open = !accepts.isEmpty();
|
||||
this.item = new HashSet<>(items);
|
||||
this.accepts = new ArrayList<>(accepts);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.github.dtschust.zork.types;
|
||||
|
||||
import com.github.dtschust.zork.ZorkCondition;
|
||||
import com.github.dtschust.zork.parser.ZorkGame;
|
||||
import com.github.dtschust.zork.ZorkGame;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
|
|
@ -10,7 +10,11 @@ public class ZorkItem extends ZorkObject implements HasPrintsAndActions {
|
|||
private final List<String> turnOnPrint;
|
||||
private final List<String> turnOnAction;
|
||||
|
||||
public ZorkItem(String name, String description, String writing, List<String> turnOnPrint, List<String> turnOnAction) {
|
||||
public ZorkItem(final String name,
|
||||
final String description,
|
||||
final String writing,
|
||||
final List<String> turnOnPrint,
|
||||
final List<String> turnOnAction) {
|
||||
super(name, description);
|
||||
this.writing = writing;
|
||||
this.turnOnPrint = new ArrayList<>(turnOnPrint);
|
||||
|
|
|
@ -15,7 +15,9 @@ public class ZorkRoom extends ZorkObject implements HasSetOfCollectable {
|
|||
public final Set<String> item = new HashSet<>();
|
||||
public final Set<String> creature = new HashSet<>();
|
||||
|
||||
public ZorkRoom(String name, String description, String type) {
|
||||
public ZorkRoom(final String name,
|
||||
final String description,
|
||||
final String type) {
|
||||
super(name, description);
|
||||
this.type = type;
|
||||
}
|
||||
|
|
Reference in a new issue