move userInput to method arguments and add some final

This commit is contained in:
RaffaeleMorganti 2022-11-22 09:55:26 +01:00
parent 42732e0fdd
commit 214bed9a13
29 changed files with 59 additions and 64 deletions

View file

@ -5,7 +5,6 @@
package com.github.dtschust.zork; package com.github.dtschust.zork;
import com.github.dtschust.zork.parser.ZorkGame;
import com.github.dtschust.zork.parser.ZorkReader; import com.github.dtschust.zork.parser.ZorkReader;
import com.github.dtschust.zork.repl.ActionDispatcher; import com.github.dtschust.zork.repl.ActionDispatcher;
import com.github.dtschust.zork.types.ZorkContainer; import com.github.dtschust.zork.types.ZorkContainer;
@ -19,9 +18,8 @@ import static com.github.dtschust.zork.Zork.Type.*;
/* And away we go*/ /* And away we go*/
public class Zork { public class Zork {
public enum Type {ROOM, ITEM, CONTAINER, CREATURE} public enum Type {ROOM, ITEM, CONTAINER, CREATURE}
public String userInput;
public ZorkGame game; ZorkGame game;
Scanner source = new Scanner(System.in); Scanner source = new Scanner(System.in);
public Zork(String filename) { public Zork(String filename) {
@ -36,16 +34,15 @@ public class Zork {
/* There is no stopping in Zork, until we're done!!*/ /* There is no stopping in Zork, until we're done!!*/
while (game.isRunning()) { while (game.isRunning()) {
userInput = source.nextLine(); String userInput = source.nextLine();
/*Now that we have the user command, check the input*/ /*Now that we have the user command, check the input*/
if (!executeTriggers()) { if (!executeTriggers(userInput)) {
/* If we haven't skipped, perform the user action*/ /* If we haven't skipped, perform the user action*/
d.dispatch(userInput); d.dispatch(userInput);
/* Clear the user input, and check the triggers again (various states have changed, gnomes need to be found!*/ /* 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 */ /* Check triggers */
public boolean executeTriggers() { public boolean executeTriggers(String input) {
/*Variable initialization*/ /*Variable initialization*/
boolean skip; boolean skip;
/*Check Room triggers*/ /*Check Room triggers*/
skip = doTriggersRoom(); skip = doTriggersRoom(input);
/* Check items in the containers in the room */ /* Check items in the containers in the room */
skip = skip || doTriggersItemsInContainersInRoom(); skip = skip || doTriggersItemsInContainersInRoom(input);
/* Check all containers in the room*/ /* Check all containers in the room*/
skip = skip || doTriggersContainersInRoom(); skip = skip || doTriggersContainersInRoom(input);
/* Check all creatures in the room */ /* Check all creatures in the room */
skip = skip || doTriggersCreaturesInRoom(); skip = skip || doTriggersCreaturesInRoom(input);
/* Check items in inventory */ /* Check items in inventory */
skip = skip || doTriggersItemsInInventory(); skip = skip || doTriggersItemsInInventory(input);
/* Check items in room */ /* Check items in room */
skip = skip || doTriggersItemsInRoom(); skip = skip || doTriggersItemsInRoom(input);
return skip; return skip;
} }
private boolean doTriggersContainersInRoom() { private boolean doTriggersContainersInRoom(String input) {
boolean skip = false; boolean skip = false;
for (String key : game.getCurrentRoom().container) { for (String key : game.getCurrentRoom().container) {
skip = skip || doZorkTriggers(game.get(CONTAINER, key)); skip = skip || doZorkTriggers(game.get(CONTAINER, key), input);
} }
return skip; return skip;
} }
private boolean doTriggersItemsInContainersInRoom() { private boolean doTriggersItemsInContainersInRoom(String input) {
boolean skip = false; boolean skip = false;
for (String key : game.getCurrentRoom().container) { for (String key : game.getCurrentRoom().container) {
ZorkContainer tempContainer = (ZorkContainer) game.get(CONTAINER, key); ZorkContainer tempContainer = (ZorkContainer) game.get(CONTAINER, key);
for (String key2 : tempContainer.item) { for (String key2 : tempContainer.item) {
skip = skip || doZorkTriggers(game.get(ITEM, key2)); skip = skip || doZorkTriggers(game.get(ITEM, key2), input);
} }
} }
return skip; return skip;
} }
private boolean doTriggersItemsInRoom() { private boolean doTriggersItemsInRoom(String input) {
boolean skip = false; boolean skip = false;
for (String key : game.getCurrentRoom().item) { for (String key : game.getCurrentRoom().item) {
skip = skip || doZorkTriggers(game.get(ITEM, key)); skip = skip || doZorkTriggers(game.get(ITEM, key), input);
} }
return skip; return skip;
} }
private boolean doTriggersItemsInInventory() { private boolean doTriggersItemsInInventory(String input) {
boolean skip = false; boolean skip = false;
for (String key : game.inventory) { for (String key : game.inventory) {
skip = skip || doZorkTriggers(game.get(ITEM, key)); skip = skip || doZorkTriggers(game.get(ITEM, key), input);
} }
return skip; return skip;
} }
private boolean doTriggersCreaturesInRoom() { private boolean doTriggersCreaturesInRoom(String input) {
boolean skip = false; boolean skip = false;
for (String key : game.getCurrentRoom().creature) { for (String key : game.getCurrentRoom().creature) {
skip = skip || doZorkTriggers(game.get(CREATURE, key)); skip = skip || doZorkTriggers(game.get(CREATURE, key), input);
} }
return skip; return skip;
} }
private boolean doTriggersRoom() { private boolean doTriggersRoom(String input) {
return doZorkTriggers(game.getCurrentRoom()); return doZorkTriggers(game.getCurrentRoom(), input);
} }
private boolean doZorkTriggers(ZorkObject zorkObject) { private boolean doZorkTriggers(ZorkObject zorkObject, String input) {
boolean skip = false; boolean skip = false;
Iterator<ZorkTrigger> iterator = zorkObject.trigger.iterator(); Iterator<ZorkTrigger> iterator = zorkObject.trigger.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
ZorkTrigger tempTrigger = iterator.next(); ZorkTrigger tempTrigger = iterator.next();
if (tempTrigger.evaluate(this)) { if (tempTrigger.evaluate(game, input)) {
for (String print : tempTrigger.print) { for (String print : tempTrigger.print) {
System.out.println(print); System.out.println(print);
} }

View file

@ -1,7 +1,5 @@
package com.github.dtschust.zork; package com.github.dtschust.zork;
import com.github.dtschust.zork.parser.ZorkGame;
/* Generic condition*/ /* Generic condition*/
public abstract class ZorkCondition { public abstract class ZorkCondition {
public final String object; public final String object;

View file

@ -1,6 +1,5 @@
package com.github.dtschust.zork; 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.ZorkContainer;
import com.github.dtschust.zork.types.ZorkRoom; import com.github.dtschust.zork.types.ZorkRoom;

View file

@ -1,6 +1,5 @@
package com.github.dtschust.zork; package com.github.dtschust.zork;
import com.github.dtschust.zork.parser.ZorkGame;
import com.github.dtschust.zork.types.ZorkObject; import com.github.dtschust.zork.types.ZorkObject;
/* Status conditions*/ /* Status conditions*/

View file

@ -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.Zork.Type;
import com.github.dtschust.zork.types.*; import com.github.dtschust.zork.types.*;

View file

@ -27,10 +27,10 @@ public class ZorkTrigger {
this.type = type; this.type = type;
} }
public boolean evaluate(Zork zork) { public boolean evaluate(ZorkGame game, String input) {
if (!commands.stream().allMatch(c -> c.matchesInput(zork.userInput))) { if (!commands.stream().allMatch(c -> c.matchesInput(input))) {
return false; return false;
} }
return conditions.stream().allMatch(c -> c.evaluate(zork.game)); return conditions.stream().allMatch(c -> c.evaluate(game));
} }
} }

View file

@ -1,6 +1,7 @@
package com.github.dtschust.zork.parser; package com.github.dtschust.zork.parser;
import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.ZorkCondition;
import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.ZorkTrigger; import com.github.dtschust.zork.ZorkTrigger;
import com.github.dtschust.zork.parser.builders.Parsers; import com.github.dtschust.zork.parser.builders.Parsers;
import com.github.dtschust.zork.parser.dom.Elements; import com.github.dtschust.zork.parser.dom.Elements;
@ -208,7 +209,7 @@ public class ZorkReader {
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
System.out.println("Invalid XML file, exiting"); System.out.println("Invalid XML file, exiting");
System.exit(-1); System.exit(-1);
} }

View file

@ -30,11 +30,8 @@ public class ZorkContainerParseStrategy implements ZorkParseStrategy<ZorkContain
final List<String> accepts = Elements.innerTextByTagName(element, "accept"); 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"); 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);
} }
} }

View file

@ -27,8 +27,6 @@ public class ZorkTriggerListParseStrategy implements ZorkParseStrategy<ZorkTrigg
.map(ZorkCommand::new) .map(ZorkCommand::new)
.collect(Collectors.toList()); .collect(Collectors.toList());
final boolean hasCommand = !commands.isEmpty();
final List<ZorkCondition> conditions = Elements.byTagName(trigger, "condition").stream() final List<ZorkCondition> conditions = Elements.byTagName(trigger, "condition").stream()
.map(conditionStrategy::parse) .map(conditionStrategy::parse)
.collect(Collectors.toList()); .collect(Collectors.toList());

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl; package com.github.dtschust.zork.repl;
import com.github.dtschust.zork.parser.ZorkGame; import com.github.dtschust.zork.ZorkGame;
import java.util.List; import java.util.List;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl; 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 com.github.dtschust.zork.repl.actions.*;
import java.util.Arrays; import java.util.Arrays;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.Zork.Type;
import com.github.dtschust.zork.repl.Action; import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.types.HasSetOfCollectable; import com.github.dtschust.zork.types.HasSetOfCollectable;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import com.github.dtschust.zork.types.ZorkCreature; import com.github.dtschust.zork.types.ZorkCreature;

View file

@ -1,7 +1,7 @@
package com.github.dtschust.zork.repl.actions; package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.Zork.Type; 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.repl.Action;
import com.github.dtschust.zork.types.HasSetOfCollectable; import com.github.dtschust.zork.types.HasSetOfCollectable;
import com.github.dtschust.zork.types.ZorkObject; import com.github.dtschust.zork.types.ZorkObject;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import com.github.dtschust.zork.types.ZorkRoom; import com.github.dtschust.zork.types.ZorkRoom;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import java.util.List; import java.util.List;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import java.util.List; import java.util.List;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import java.util.List; import java.util.List;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import com.github.dtschust.zork.types.ZorkContainer; import com.github.dtschust.zork.types.ZorkContainer;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import com.github.dtschust.zork.types.ZorkContainer; import com.github.dtschust.zork.types.ZorkContainer;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import com.github.dtschust.zork.types.ZorkItem; import com.github.dtschust.zork.types.ZorkItem;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import com.github.dtschust.zork.types.ZorkContainer; import com.github.dtschust.zork.types.ZorkContainer;
import com.github.dtschust.zork.types.ZorkRoom; import com.github.dtschust.zork.types.ZorkRoom;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import com.github.dtschust.zork.types.ZorkItem; import com.github.dtschust.zork.types.ZorkItem;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.repl.actions; 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.repl.Action;
import com.github.dtschust.zork.types.ZorkMap; import com.github.dtschust.zork.types.ZorkMap;
import com.github.dtschust.zork.types.ZorkObject; import com.github.dtschust.zork.types.ZorkObject;

View file

@ -1,6 +1,6 @@
package com.github.dtschust.zork.types; 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 com.github.dtschust.zork.repl.ActionDispatcher;
import java.util.List; import java.util.List;

View file

@ -15,13 +15,13 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
public ZorkContainer(final String name, public ZorkContainer(final String name,
final String description, final String description,
final boolean open,
final String status, final String status,
final Collection<String> items, final Collection<String> items,
final Collection<String> accepts, final Collection<String> accepts,
final Collection<ZorkTrigger> triggers) { final Collection<ZorkTrigger> triggers) {
super(name, description, status, 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.item = new HashSet<>(items);
this.accepts = new ArrayList<>(accepts); this.accepts = new ArrayList<>(accepts);
} }

View file

@ -1,7 +1,7 @@
package com.github.dtschust.zork.types; package com.github.dtschust.zork.types;
import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.ZorkCondition;
import com.github.dtschust.zork.parser.ZorkGame; import com.github.dtschust.zork.ZorkGame;
import java.util.*; import java.util.*;

View file

@ -10,7 +10,11 @@ public class ZorkItem extends ZorkObject implements HasPrintsAndActions {
private final List<String> turnOnPrint; private final List<String> turnOnPrint;
private final List<String> turnOnAction; 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); super(name, description);
this.writing = writing; this.writing = writing;
this.turnOnPrint = new ArrayList<>(turnOnPrint); this.turnOnPrint = new ArrayList<>(turnOnPrint);

View file

@ -15,7 +15,9 @@ public class ZorkRoom extends ZorkObject implements HasSetOfCollectable {
public final Set<String> item = new HashSet<>(); public final Set<String> item = new HashSet<>();
public final Set<String> creature = 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); super(name, description);
this.type = type; this.type = type;
} }