Minor cleanup
This commit is contained in:
parent
69072097bb
commit
aafbf94434
8 changed files with 24 additions and 62 deletions
|
@ -7,16 +7,13 @@ package com.github.dtschust.zork;
|
||||||
|
|
||||||
import com.github.dtschust.zork.parser.ParserIOC;
|
import com.github.dtschust.zork.parser.ParserIOC;
|
||||||
import com.github.dtschust.zork.repl.ActionDispatcher;
|
import com.github.dtschust.zork.repl.ActionDispatcher;
|
||||||
import com.github.dtschust.zork.objects.ZorkContainer;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import static com.github.dtschust.zork.objects.ZorkObjectTypes.*;
|
|
||||||
|
|
||||||
/* And away we go*/
|
/* And away we go*/
|
||||||
public class Zork {
|
public class Zork {
|
||||||
ZorkGame game;
|
final ZorkGame game;
|
||||||
Scanner source = new Scanner(System.in);
|
final Scanner source = new Scanner(System.in);
|
||||||
|
|
||||||
public Zork(final String filename) {
|
public Zork(final String filename) {
|
||||||
game = ParserIOC.xmlParser().parse(filename);
|
game = ParserIOC.xmlParser().parse(filename);
|
||||||
|
@ -36,7 +33,7 @@ public class Zork {
|
||||||
/* 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!*/
|
// check the triggers again (various states have changed, gnomes need to be found!)
|
||||||
game.evaluateTriggers("");
|
game.evaluateTriggers("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
import com.github.dtschust.zork.objects.*;
|
import com.github.dtschust.zork.objects.*;
|
||||||
import com.github.dtschust.zork.types.Triggerable;
|
|
||||||
import com.github.dtschust.zork.types.ZorkMap;
|
import com.github.dtschust.zork.types.ZorkMap;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -10,7 +9,7 @@ import java.util.Set;
|
||||||
|
|
||||||
import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
|
import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
|
||||||
|
|
||||||
public class ZorkGame implements Triggerable {
|
public class ZorkGame {
|
||||||
|
|
||||||
public final Set<String> inventory = new HashSet<>();
|
public final Set<String> inventory = new HashSet<>();
|
||||||
private final ZorkMap<ZorkRoom> rooms = new ZorkMap<>();
|
private final ZorkMap<ZorkRoom> rooms = new ZorkMap<>();
|
||||||
|
@ -102,10 +101,9 @@ public class ZorkGame implements Triggerable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private boolean evaluateTriggers(final ZorkGame game, final String currentCommand) {
|
||||||
public boolean evaluateTriggers(final ZorkGame game, final String currentCommand) {
|
|
||||||
final boolean currentRoom = getCurrentRoom().evaluateTriggers(game, currentCommand);
|
final boolean currentRoom = getCurrentRoom().evaluateTriggers(game, currentCommand);
|
||||||
final boolean itemsInInventory = Triggerable.evaluateTriggerCollection(inventory, ITEM, game, currentCommand);
|
final boolean itemsInInventory = ZorkTrigger.evaluateTriggerCollection(inventory, ITEM, game, currentCommand);
|
||||||
|
|
||||||
return currentRoom || itemsInInventory;
|
return currentRoom || itemsInInventory;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package com.github.dtschust.zork;
|
package com.github.dtschust.zork;
|
||||||
|
|
||||||
|
import com.github.dtschust.zork.objects.ZorkObjectTypes;
|
||||||
import com.github.dtschust.zork.types.HasPrintsAndActions;
|
import com.github.dtschust.zork.types.HasPrintsAndActions;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -27,6 +29,16 @@ public class ZorkTrigger implements HasPrintsAndActions {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean evaluateTriggerCollection(final Collection<String> collection,
|
||||||
|
final ZorkObjectTypes type,
|
||||||
|
final ZorkGame game,
|
||||||
|
final String input) {
|
||||||
|
// non short-circuited to execute all side effects of evaluateTriggers
|
||||||
|
return collection.stream()
|
||||||
|
.map(i -> game.get(type, i).evaluateTriggers(game, input))
|
||||||
|
.reduce(false, (a, b) -> a || b);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean hasCommand() {
|
public boolean hasCommand() {
|
||||||
return !this.commands.isEmpty();
|
return !this.commands.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package com.github.dtschust.zork.objects;
|
package com.github.dtschust.zork.objects;
|
||||||
|
|
||||||
import com.github.dtschust.zork.ZorkGame;
|
|
||||||
import com.github.dtschust.zork.ZorkTrigger;
|
import com.github.dtschust.zork.ZorkTrigger;
|
||||||
import com.github.dtschust.zork.types.HasSetOfCollectable;
|
import com.github.dtschust.zork.types.HasSetOfCollectable;
|
||||||
import com.github.dtschust.zork.types.Triggerable;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -12,7 +10,6 @@ import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
|
||||||
/* Container*/
|
/* Container*/
|
||||||
public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
|
public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
|
||||||
private final Set<String> items;
|
private final Set<String> items;
|
||||||
private final List<String> accepts;
|
|
||||||
private boolean open;
|
private boolean open;
|
||||||
|
|
||||||
public ZorkContainer(final String name,
|
public ZorkContainer(final String name,
|
||||||
|
@ -25,7 +22,6 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
|
||||||
// If a container has an accepts attribute, then it is always open
|
// If a container has an accepts attribute, then it is always open
|
||||||
this.open = !accepts.isEmpty();
|
this.open = !accepts.isEmpty();
|
||||||
this.items = new HashSet<>(items);
|
this.items = new HashSet<>(items);
|
||||||
this.accepts = new ArrayList<>(accepts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContents() {
|
public String getContents() {
|
||||||
|
@ -48,10 +44,6 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
|
||||||
return this.items.contains(item);
|
return this.items.contains(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterable<String> items() {
|
|
||||||
return Collections.unmodifiableSet(items);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isOpen() {
|
public boolean isOpen() {
|
||||||
return open;
|
return open;
|
||||||
}
|
}
|
||||||
|
@ -67,13 +59,4 @@ public class ZorkContainer extends ZorkObject implements HasSetOfCollectable {
|
||||||
throw new IllegalStateException("Unexpected value: " + type);
|
throw new IllegalStateException("Unexpected value: " + type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getAccepts() {
|
|
||||||
return Collections.unmodifiableList(accepts);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean evaluateTriggers(ZorkGame game, String input) {
|
|
||||||
final boolean itemsEvaluation = Triggerable.evaluateTriggerCollection(items, ITEM, game, input);
|
|
||||||
return super.evaluateTriggers(game, input) || itemsEvaluation;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,11 @@ package com.github.dtschust.zork.objects;
|
||||||
import com.github.dtschust.zork.ZorkGame;
|
import com.github.dtschust.zork.ZorkGame;
|
||||||
import com.github.dtschust.zork.ZorkTrigger;
|
import com.github.dtschust.zork.ZorkTrigger;
|
||||||
import com.github.dtschust.zork.ZorkTriggerType;
|
import com.github.dtschust.zork.ZorkTriggerType;
|
||||||
import com.github.dtschust.zork.types.Triggerable;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/* Generic object, everything inherits from this*/
|
/* Generic object, everything inherits from this*/
|
||||||
public abstract class ZorkObject implements Triggerable {
|
public abstract class ZorkObject {
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String description;
|
private final String description;
|
||||||
private final List<ZorkTrigger> trigger;
|
private final List<ZorkTrigger> trigger;
|
||||||
|
@ -45,10 +44,6 @@ public abstract class ZorkObject implements Triggerable {
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ZorkTrigger> getTrigger() {
|
|
||||||
return trigger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean evaluateTriggers(final ZorkGame game, final String input) {
|
public boolean evaluateTriggers(final ZorkGame game, final String input) {
|
||||||
boolean skip = false;
|
boolean skip = false;
|
||||||
final Iterator<ZorkTrigger> iterator = trigger.iterator();
|
final Iterator<ZorkTrigger> iterator = trigger.iterator();
|
||||||
|
|
|
@ -4,7 +4,6 @@ package com.github.dtschust.zork.objects;
|
||||||
import com.github.dtschust.zork.ZorkGame;
|
import com.github.dtschust.zork.ZorkGame;
|
||||||
import com.github.dtschust.zork.ZorkTrigger;
|
import com.github.dtschust.zork.ZorkTrigger;
|
||||||
import com.github.dtschust.zork.types.HasSetOfCollectable;
|
import com.github.dtschust.zork.types.HasSetOfCollectable;
|
||||||
import com.github.dtschust.zork.types.Triggerable;
|
|
||||||
import com.github.dtschust.zork.types.ZorkDirection;
|
import com.github.dtschust.zork.types.ZorkDirection;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@ -66,11 +65,10 @@ public class ZorkRoom extends ZorkObject implements HasSetOfCollectable {
|
||||||
return Optional.ofNullable(this.border.get(border));
|
return Optional.ofNullable(this.border.get(border));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean evaluateTriggers(ZorkGame game, String input) {
|
public boolean evaluateTriggers(ZorkGame game, String input) {
|
||||||
final boolean items = Triggerable.evaluateTriggerCollection(item, ITEM, game, input);
|
final boolean items = ZorkTrigger.evaluateTriggerCollection(item, ITEM, game, input);
|
||||||
final boolean creatures = Triggerable.evaluateTriggerCollection(creature, CREATURE, game, input);
|
final boolean creatures = ZorkTrigger.evaluateTriggerCollection(creature, CREATURE, game, input);
|
||||||
final boolean containers = Triggerable.evaluateTriggerCollection(container, CONTAINER, game, input);
|
final boolean containers = ZorkTrigger.evaluateTriggerCollection(container, CONTAINER, game, input);
|
||||||
return super.evaluateTriggers(game, input) || items || creatures || containers;
|
return super.evaluateTriggers(game, input) || items || creatures || containers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
package com.github.dtschust.zork.types;
|
|
||||||
|
|
||||||
import com.github.dtschust.zork.ZorkGame;
|
|
||||||
import com.github.dtschust.zork.objects.ZorkObjectTypes;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
public interface Triggerable {
|
|
||||||
|
|
||||||
boolean evaluateTriggers(final ZorkGame game, final String userCommand);
|
|
||||||
|
|
||||||
static boolean evaluateTriggerCollection(final Collection<String> collection,
|
|
||||||
final ZorkObjectTypes type,
|
|
||||||
final ZorkGame game,
|
|
||||||
final String input) {
|
|
||||||
// non short-circuited to execute all side effects of evaluateTriggers
|
|
||||||
return collection.stream()
|
|
||||||
.map(i -> game.get(type, i).evaluateTriggers(game, input))
|
|
||||||
.reduce(false, (a, b) -> a || b);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,8 +7,8 @@ import java.util.Iterator;
|
||||||
|
|
||||||
public class ZorkMap<T extends ZorkObject> extends HashMap<String, T> implements Iterable<T> {
|
public class ZorkMap<T extends ZorkObject> extends HashMap<String, T> implements Iterable<T> {
|
||||||
|
|
||||||
public T put(T object) {
|
public void put(T object) {
|
||||||
return put(object.getName(), object);
|
put(object.getName(), object);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Reference in a new issue