From 343ae03421a4fd0640fe40e55332f94d2cd3d521 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 24 Feb 2020 17:02:48 +0100 Subject: [PATCH] Initial command parser --- src/edu/kit/informatik/model/Card.java | 32 +++++++++++++++++++ src/edu/kit/informatik/ui/command/Build.java | 18 ++++++++++- .../kit/informatik/ui/command/Buildable.java | 11 +++++-- .../informatik/ui/command/CommandFactory.java | 2 ++ src/edu/kit/informatik/ui/command/Draw.java | 9 +++++- .../informatik/ui/command/ListBuildings.java | 9 +++++- .../informatik/ui/command/ListResources.java | 11 +++++-- src/edu/kit/informatik/ui/command/Quit.java | 16 ---------- src/edu/kit/informatik/ui/command/Reset.java | 11 +++++-- .../kit/informatik/ui/command/RollDice.java | 21 +++++++++++- src/edu/kit/informatik/ui/command/Start.java | 32 +++++++++++++++++-- 11 files changed, 144 insertions(+), 28 deletions(-) create mode 100644 src/edu/kit/informatik/model/Card.java delete mode 100644 src/edu/kit/informatik/ui/command/Quit.java diff --git a/src/edu/kit/informatik/model/Card.java b/src/edu/kit/informatik/model/Card.java new file mode 100644 index 0000000..9a42964 --- /dev/null +++ b/src/edu/kit/informatik/model/Card.java @@ -0,0 +1,32 @@ +package edu.kit.informatik.model; + +public enum Card { + WOOD, + METAL, + PLASTIC, + SPIDER, + SNAKE, + TIGER, + THUNDERSTORM; + + public static Card parse(String input) { + switch (input) { + case "wood": + return WOOD; + case "metal": + return METAL; + case "plastic": + return PLASTIC; + case "spider": + return SPIDER; + case "snake": + return SNAKE; + case "tiger": + return TIGER; + case "thunderstorm": + return THUNDERSTORM; + default: + return null; + } + } +} diff --git a/src/edu/kit/informatik/ui/command/Build.java b/src/edu/kit/informatik/ui/command/Build.java index 45abf9f..7d8cfbb 100644 --- a/src/edu/kit/informatik/ui/command/Build.java +++ b/src/edu/kit/informatik/ui/command/Build.java @@ -3,7 +3,16 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static edu.kit.informatik.ui.command.CommandFactory.BUILD; + public class Build extends Command { + private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)"); + + private String item; + @Override public void apply(CardGame game) throws InvalidInputException { @@ -11,6 +20,13 @@ public class Build extends Command { @Override public void parse(String input) throws InvalidInputException { - + if (input == null || !input.startsWith(BUILD)) { + throw new InvalidInputException("unknown command"); + } + Matcher matcher = BUILD_ARGUMENT.matcher(input); + if (!matcher.matches()) { + throw new InvalidInputException("invalid build command"); + } + item = matcher.group(1); } } diff --git a/src/edu/kit/informatik/ui/command/Buildable.java b/src/edu/kit/informatik/ui/command/Buildable.java index 83043f2..43de44b 100644 --- a/src/edu/kit/informatik/ui/command/Buildable.java +++ b/src/edu/kit/informatik/ui/command/Buildable.java @@ -3,14 +3,21 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; +import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE; + public class Buildable extends Command { @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) { } @Override public void parse(String input) throws InvalidInputException { - + if (input == null || !input.startsWith(BUILDABLE)) { + throw new InvalidInputException("unknown command"); + } + if (!input.equals(BUILDABLE)) { + throw new InvalidInputException("invalid build? argument: none expected"); + } } } diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index 3097034..84c0a25 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -13,6 +13,8 @@ import java.util.function.Supplier; * @version 1.0 */ public final class CommandFactory { + public static final String CARD = "wood|metal|plastic|spider|snake|tiger|thunderstorm"; + public static final String START = "start"; public static final String DRAW = "draw"; public static final String LIST_RESOURCES = "list-resources"; diff --git a/src/edu/kit/informatik/ui/command/Draw.java b/src/edu/kit/informatik/ui/command/Draw.java index 1494e07..a798e37 100644 --- a/src/edu/kit/informatik/ui/command/Draw.java +++ b/src/edu/kit/informatik/ui/command/Draw.java @@ -3,6 +3,8 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; +import static edu.kit.informatik.ui.command.CommandFactory.DRAW; + public class Draw extends Command { @Override public void apply(CardGame game) throws InvalidInputException { @@ -11,7 +13,12 @@ public class Draw extends Command { @Override public void parse(String input) throws InvalidInputException { - + if (input == null || !input.startsWith(DRAW)) { + throw new InvalidInputException("unknown command"); + } + if (!input.equals(DRAW)) { + throw new InvalidInputException("invalid draw argument: none expected"); + } } /* Es existieren folgende Fallunterscheidungen: diff --git a/src/edu/kit/informatik/ui/command/ListBuildings.java b/src/edu/kit/informatik/ui/command/ListBuildings.java index c1cf511..1b390fe 100644 --- a/src/edu/kit/informatik/ui/command/ListBuildings.java +++ b/src/edu/kit/informatik/ui/command/ListBuildings.java @@ -3,6 +3,8 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; +import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS; + public class ListBuildings extends Command { @Override public void apply(CardGame game) throws InvalidInputException { @@ -11,6 +13,11 @@ public class ListBuildings extends Command { @Override public void parse(String input) throws InvalidInputException { - + if (input == null || !input.startsWith(LIST_BUILDINGS)) { + throw new InvalidInputException("unknown command"); + } + if (!input.equals(LIST_BUILDINGS)) { + throw new InvalidInputException("invalid list-buildings argument: none expected"); + } } } diff --git a/src/edu/kit/informatik/ui/command/ListResources.java b/src/edu/kit/informatik/ui/command/ListResources.java index f7aa599..c9e9417 100644 --- a/src/edu/kit/informatik/ui/command/ListResources.java +++ b/src/edu/kit/informatik/ui/command/ListResources.java @@ -3,15 +3,22 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; +import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES; + public class ListResources extends Command { @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) { } @Override public void parse(String input) throws InvalidInputException { - + if (input == null || !input.startsWith(LIST_RESOURCES)) { + throw new InvalidInputException("unknown command"); + } + if (!input.equals(LIST_RESOURCES)) { + throw new InvalidInputException("invalid list-resources argument: none expected"); + } } /* diff --git a/src/edu/kit/informatik/ui/command/Quit.java b/src/edu/kit/informatik/ui/command/Quit.java deleted file mode 100644 index 4148d95..0000000 --- a/src/edu/kit/informatik/ui/command/Quit.java +++ /dev/null @@ -1,16 +0,0 @@ -package edu.kit.informatik.ui.command; - -import edu.kit.informatik.model.CardGame; -import edu.kit.informatik.ui.InvalidInputException; - -public class Quit extends Command { - @Override - public void apply(CardGame game) throws InvalidInputException { - - } - - @Override - public void parse(String input) throws InvalidInputException { - - } -} diff --git a/src/edu/kit/informatik/ui/command/Reset.java b/src/edu/kit/informatik/ui/command/Reset.java index 30730cd..3053c57 100644 --- a/src/edu/kit/informatik/ui/command/Reset.java +++ b/src/edu/kit/informatik/ui/command/Reset.java @@ -3,14 +3,21 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; +import static edu.kit.informatik.ui.command.CommandFactory.RESET; + public class Reset extends Command { @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) { } @Override public void parse(String input) throws InvalidInputException { - + if (input == null || !input.startsWith(RESET)) { + throw new InvalidInputException("unknown command"); + } + if (!input.equals(RESET)) { + throw new InvalidInputException("invalid reset argument: none expected"); + } } } diff --git a/src/edu/kit/informatik/ui/command/RollDice.java b/src/edu/kit/informatik/ui/command/RollDice.java index 1355bff..75287e0 100644 --- a/src/edu/kit/informatik/ui/command/RollDice.java +++ b/src/edu/kit/informatik/ui/command/RollDice.java @@ -3,7 +3,18 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static edu.kit.informatik.ui.command.CommandFactory.ROLL_DICE; + public class RollDice extends Command { + private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(ROLL_DICE + "(\\d) (\\d)"); + + private int size; + + private int rolledNumber; + @Override public void apply(CardGame game) throws InvalidInputException { @@ -11,6 +22,14 @@ public class RollDice extends Command { @Override public void parse(String input) throws InvalidInputException { - + if (input == null || !input.startsWith(ROLL_DICE)) { + throw new InvalidInputException("unknown command"); + } + Matcher matcher = ROLL_DICE_ARGUMENTS.matcher(input); + if (!matcher.matches()) { + throw new InvalidInputException("invalid roll dice syntax"); + } + size = Integer.parseInt(matcher.group(1)); + rolledNumber = Integer.parseInt(matcher.group(2)); } } diff --git a/src/edu/kit/informatik/ui/command/Start.java b/src/edu/kit/informatik/ui/command/Start.java index 486b13e..9ddc2c4 100644 --- a/src/edu/kit/informatik/ui/command/Start.java +++ b/src/edu/kit/informatik/ui/command/Start.java @@ -1,17 +1,45 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.model.Card; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; +import java.util.List; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static edu.kit.informatik.ui.command.CommandFactory.CARD; + public class Start extends Command { + private static final Pattern START_ARGUMENTS = Pattern.compile("start (?:(" + CARD + "),){63},(" + CARD + ")"); + + private List cards; + @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) { } @Override public void parse(String input) throws InvalidInputException { - + if (input == null || !input.startsWith(CommandFactory.START)) { + throw new InvalidInputException("unknown command"); + } + Matcher matcher = START_ARGUMENTS.matcher(input); + if (!matcher.matches()) { + throw new InvalidInputException("invalid start arguments"); + } + cards = IntStream.rangeClosed(1, matcher.groupCount()) + .mapToObj(matcher::group) + .map(Card::parse) + .collect(Collectors.toList()); + if (cards.stream().anyMatch(Objects::isNull)) { + cards = null; + throw new InvalidInputException("invalid start argument value(s)"); + } } /* Dieser Befehl ermöglicht es dem Benutzer, ein neues Spiel zu starten. Dieser Befehl kann nur