Minor cleanup

This commit is contained in:
Claudio Maggioni 2022-11-22 18:36:17 +01:00
parent 4c70ba2800
commit 6df81726d1
18 changed files with 18 additions and 74 deletions

View file

@ -1,8 +0,0 @@
# 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

@ -2,7 +2,6 @@ package com.github.dtschust.zork;
import com.github.dtschust.zork.types.HasPrintsAndActions;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

View file

@ -13,10 +13,6 @@ public abstract class ZorkObject {
private final List<ZorkTrigger> trigger;
private String status;
protected ZorkObject(String name, String description) {
this(name, description, null, Collections.emptyList());
}
protected ZorkObject(final String name,
final String description,
final String status,

View file

@ -1,21 +0,0 @@
package com.github.dtschust.zork.objects;
import java.util.EnumSet;
import java.util.Optional;
public enum ZorkObjectTypes {
ROOM("room"),
ITEM("item"),
CONTAINER("container"),
CREATURE("creature");
private final String propertyName;
ZorkObjectTypes(final String propertyName) {
this.propertyName = propertyName;
}
public static Optional<ZorkObjectTypes> fromPropertyName(final String propertyName) {
return EnumSet.allOf(ZorkObjectTypes.class).stream().filter(e -> e.propertyName.equals(propertyName)).findFirst();
}
}

View file

@ -8,8 +8,6 @@ import com.github.dtschust.zork.types.ZorkDirection;
import java.util.*;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.*;
/* Room*/
public class ZorkRoom extends ZorkObject implements ObjectCollector {
private final String type;

View file

@ -1,11 +1,11 @@
package com.github.dtschust.zork.parser;
import com.github.dtschust.zork.ZorkCondition;
import com.github.dtschust.zork.parser.strategies.*;
import com.github.dtschust.zork.objects.ZorkContainer;
import com.github.dtschust.zork.objects.ZorkCreature;
import com.github.dtschust.zork.objects.ZorkItem;
import com.github.dtschust.zork.objects.ZorkRoom;
import com.github.dtschust.zork.parser.strategies.*;
/**
* Inversion of control for Zork parse strategies

View file

@ -1,10 +1,10 @@
package com.github.dtschust.zork.parser;
import com.github.dtschust.zork.parser.dom.DOMElement;
import com.github.dtschust.zork.objects.ZorkContainer;
import com.github.dtschust.zork.objects.ZorkCreature;
import com.github.dtschust.zork.objects.ZorkItem;
import com.github.dtschust.zork.objects.ZorkRoom;
import com.github.dtschust.zork.parser.dom.DOMElement;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;

View file

@ -1,10 +1,10 @@
package com.github.dtschust.zork.parser.strategies;
import com.github.dtschust.zork.ZorkTrigger;
import com.github.dtschust.zork.objects.ZorkContainer;
import com.github.dtschust.zork.parser.Property;
import com.github.dtschust.zork.parser.PropertyParseStrategy;
import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy;
import com.github.dtschust.zork.objects.ZorkContainer;
import java.util.List;
import java.util.stream.Collectors;

View file

@ -2,10 +2,10 @@ package com.github.dtschust.zork.parser.strategies;
import com.github.dtschust.zork.ZorkCondition;
import com.github.dtschust.zork.ZorkTrigger;
import com.github.dtschust.zork.objects.ZorkCreature;
import com.github.dtschust.zork.parser.Property;
import com.github.dtschust.zork.parser.PropertyParseStrategy;
import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy;
import com.github.dtschust.zork.objects.ZorkCreature;
import java.util.List;
import java.util.stream.Collectors;

View file

@ -1,10 +1,10 @@
package com.github.dtschust.zork.parser.strategies;
import com.github.dtschust.zork.ZorkTrigger;
import com.github.dtschust.zork.objects.ZorkItem;
import com.github.dtschust.zork.parser.Property;
import com.github.dtschust.zork.parser.PropertyParseStrategy;
import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy;
import com.github.dtschust.zork.objects.ZorkItem;
import java.util.List;
import java.util.stream.Collectors;

View file

@ -1,11 +1,11 @@
package com.github.dtschust.zork.parser.strategies;
import com.github.dtschust.zork.ZorkTrigger;
import com.github.dtschust.zork.objects.ZorkRoom;
import com.github.dtschust.zork.parser.Property;
import com.github.dtschust.zork.parser.PropertyParseStrategy;
import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy;
import com.github.dtschust.zork.types.ZorkDirection;
import com.github.dtschust.zork.objects.ZorkRoom;
import java.util.List;
import java.util.Map;

View file

@ -1,17 +1,10 @@
package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.objects.ZorkItem;
import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.types.ObjectCollector;
import com.github.dtschust.zork.objects.ZorkObjectTypes;
import com.github.dtschust.zork.objects.ZorkObject;
import com.github.dtschust.zork.objects.ZorkRoom;
import com.github.dtschust.zork.repl.Action;
import java.util.List;
import java.util.Set;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.*;
/**
* Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky

View file

@ -20,12 +20,12 @@ public class PutAction implements Action {
public boolean run(ZorkGame game, List<String> arguments) {
final String what = arguments.get(1);
return game.getContainer(arguments.get(3))
.filter(c -> c.isOpen() && game.inventory.contains(what))
.map(tempContainer -> {
tempContainer.addItem(what);
game.inventory.remove(what);
game.stream.println("Item " + what + " added to " + tempContainer.getName() + ".");
return true;
}).orElse(false);
.filter(c -> c.isOpen() && game.inventory.contains(what))
.map(tempContainer -> {
tempContainer.addItem(what);
game.inventory.remove(what);
game.stream.println("Item " + what + " added to " + tempContainer.getName() + ".");
return true;
}).orElse(false);
}
}

View file

@ -2,12 +2,9 @@ package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.objects.ZorkItem;
import java.util.List;
import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM;
public class ReadAction implements Action {
@Override
public boolean matchesInput(List<String> arguments) {

View file

@ -24,8 +24,8 @@ public class StartGameAction implements Action {
public boolean run(ZorkGame game, List<String> arguments) {
final String room = arguments.get(2);
if(!game.isRunning()){
if(game.changeRoom(room)){
if (!game.isRunning()) {
if (game.changeRoom(room)) {
game.stream.println(game.getCurrentRoom().getDescription());
return true;
}

View file

@ -2,11 +2,9 @@ package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.objects.ZorkContainer;
import com.github.dtschust.zork.objects.ZorkItem;
import com.github.dtschust.zork.repl.Action;
import java.util.List;
import java.util.Optional;
public class TakeAction implements Action {
@Override

View file

@ -1,11 +1,9 @@
package com.github.dtschust.zork.types;
import com.github.dtschust.zork.objects.ZorkObject;
import com.github.dtschust.zork.objects.ZorkObjectTypes;
import java.util.Set;
public interface ObjectCollector {
void addObject(final ZorkObject object);
void removeObject(final ZorkObject object);
}

View file

@ -4,8 +4,6 @@ import com.github.dtschust.zork.utils.CommandReader;
import com.github.dtschust.zork.utils.IOWrapper;
import org.junit.jupiter.api.Test;
import java.io.PrintStream;
import static com.github.stefanbirkner.systemlambda.SystemLambda.catchSystemExit;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -19,16 +17,12 @@ class ZorkTest {
String gameConfig = "sampleGame.xml";
String gameExecution = "RunThroughResults.txt";
PrintStream out = System.out;
CommandReader run = new CommandReader(gameExecution);
IOWrapper io = new IOWrapper(true);
new Thread(() -> {
try {
catchSystemExit(() -> new Zork(gameConfig));
} catch (Exception ignored) {
ignored.printStackTrace(out);
}
} catch (Exception ignored) {}
}).start();
while(true){
switch(run.getInstructionType()) {