From 45f05292b429622d9becbdd8a8546de372c832f1 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Wed, 11 Mar 2020 10:51:29 +0100 Subject: [PATCH] Update game logic to changed requirements and refactor command parsing --- game1_output.txt | 3 +- game2_output.txt | 2 - src/edu/kit/informatik/Terminal.java | 10 ++++- src/edu/kit/informatik/model/Card.java | 17 +++++++ src/edu/kit/informatik/model/CardGame.java | 45 +++++++------------ src/edu/kit/informatik/model/Item.java | 11 ++++- src/edu/kit/informatik/ui/CommandLine.java | 21 +++++---- src/edu/kit/informatik/ui/command/Build.java | 21 +++------ .../kit/informatik/ui/command/Buildable.java | 18 ++------ .../kit/informatik/ui/command/Command.java | 13 +++++- .../informatik/ui/command/CommandFactory.java | 32 ++++++------- src/edu/kit/informatik/ui/command/Draw.java | 18 ++------ .../informatik/ui/command/ListBuildings.java | 18 ++------ .../informatik/ui/command/ListResources.java | 18 ++------ src/edu/kit/informatik/ui/command/Reset.java | 18 ++------ .../kit/informatik/ui/command/RollDice.java | 27 ++++------- src/edu/kit/informatik/ui/command/Start.java | 24 +++------- 17 files changed, 134 insertions(+), 182 deletions(-) diff --git a/game1_output.txt b/game1_output.txt index 3a0f466..e0a44d0 100644 --- a/game1_output.txt +++ b/game1_output.txt @@ -107,8 +107,7 @@ Error, need fireplace to build OK Error, already built OK -OK -axe +Error, already built axe fireplace win diff --git a/game2_output.txt b/game2_output.txt index bfc6500..b04a1af 100644 --- a/game2_output.txt +++ b/game2_output.txt @@ -56,9 +56,7 @@ shack OK OK OK -axe ballon -club hangglider sailingraft shack diff --git a/src/edu/kit/informatik/Terminal.java b/src/edu/kit/informatik/Terminal.java index 2936dd0..86e51aa 100644 --- a/src/edu/kit/informatik/Terminal.java +++ b/src/edu/kit/informatik/Terminal.java @@ -97,7 +97,15 @@ public final class Terminal { */ public static String readLine() { try { - return IN.readLine(); + String line = IN.readLine(); + while (line != null && line.startsWith("#")) { + line = IN.readLine(); + } + if (line == null) { + return "quit"; + } else { + return line; + } } catch (final IOException e) { /* * The IOException will not occur during tests executed by the praktomat, therefore the diff --git a/src/edu/kit/informatik/model/Card.java b/src/edu/kit/informatik/model/Card.java index ad7bd00..0d116eb 100644 --- a/src/edu/kit/informatik/model/Card.java +++ b/src/edu/kit/informatik/model/Card.java @@ -47,6 +47,23 @@ public enum Card { return this.equals(THUNDERSTORM); } + public int requiredAmount() { + switch (this) { + case WOOD: + case METAL: + case PLASTIC: + return 16; + case SPIDER: + case SNAKE: + case TIGER: + return 5; + case THUNDERSTORM: + return 1; + default: + return 0; + } + } + public static Card parse(String input) { switch (input) { case "wood": diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java index cbf5b95..f9be260 100644 --- a/src/edu/kit/informatik/model/CardGame.java +++ b/src/edu/kit/informatik/model/CardGame.java @@ -1,7 +1,5 @@ package edu.kit.informatik.model; -import edu.kit.informatik.ui.InvalidInputException; - import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -12,16 +10,7 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; -import static edu.kit.informatik.model.Card.METAL; -import static edu.kit.informatik.model.Card.PLASTIC; -import static edu.kit.informatik.model.Card.SNAKE; -import static edu.kit.informatik.model.Card.SPIDER; -import static edu.kit.informatik.model.Card.THUNDERSTORM; -import static edu.kit.informatik.model.Card.TIGER; -import static edu.kit.informatik.model.Card.WOOD; -import static edu.kit.informatik.model.Item.AXE; import static edu.kit.informatik.model.Item.BALLON; -import static edu.kit.informatik.model.Item.CLUB; import static edu.kit.informatik.model.Item.FIREPLACE; import static edu.kit.informatik.model.Item.HANG_GLIDER; import static edu.kit.informatik.model.Item.SAILING_RAFT; @@ -31,8 +20,8 @@ import static edu.kit.informatik.model.Item.STEAMBOAT; public class CardGame { private Deque cardStack; private Deque currentCardStack; - private Deque resources = new ArrayDeque<>(); - private List items = new ArrayList<>(); + private final Deque resources = new ArrayDeque<>(); + private final List items = new ArrayList<>(); private boolean fightingAnimal = false; private Integer awaitedDiceSize = null; private Integer minimumDiceRoll = null; @@ -44,19 +33,19 @@ public class CardGame { * @return whether the game could be successfully started */ public boolean start(Deque cardStack) throws LogicException { - if (cardStack == null || currentCardStack == null) { - if (Collections.frequency(cardStack, WOOD) != 16 || Collections.frequency(cardStack, PLASTIC) != 16 - || Collections.frequency(cardStack, METAL) != 16 || Collections.frequency(cardStack, SPIDER) != 5 - || Collections.frequency(cardStack, SNAKE) != 5 || Collections.frequency(cardStack, TIGER) != 5 - || Collections.frequency(cardStack, THUNDERSTORM) != 1) { - throw new LogicException("invalid deck: missing or surplus cards"); - } - this.cardStack = new ArrayDeque<>(cardStack); - reset(); - return true; - } else { - return false; // game already started + if (cardStack == null) { + throw new LogicException("can not initialize game with null cards"); } + if (gameStarted()) { + return false; + } + if (!Arrays.stream(Card.values()) + .allMatch(card -> Collections.frequency(cardStack, card) == card.requiredAmount())) { + throw new LogicException("invalid deck: missing or surplus cards"); + } + this.cardStack = new ArrayDeque<>(cardStack); + reset(); + return true; } public Card draw() throws LogicException { @@ -94,7 +83,7 @@ public class CardGame { final int minimumNeeded = minimumDiceRoll; minimumDiceRoll = null; if (fightingAnimal) { - final int bonus = items.contains(AXE) ? 2 : items.contains(CLUB) ? 1 : 0; + final int bonus = items.stream().mapToInt(Item::fightingBonus).max().orElse(0); if (roll + bonus >= minimumNeeded) { return "survived"; } else { @@ -125,7 +114,7 @@ public class CardGame { throw new LogicException("can not build item"); } else if (item.requiresFireplace() && !items.contains(FIREPLACE)) { throw new LogicException("need fireplace to build"); - } else if (items.contains(item) && !item.canHaveMoreThanOne()) { + } else if (items.contains(item)) { throw new LogicException("already built"); } else if (canBuild(item)) { // remove used resources @@ -156,7 +145,7 @@ public class CardGame { if (item.requiresFireplace() && !items.contains(FIREPLACE)) { return false; } - if (!item.canHaveMoreThanOne() && items.contains(item)) { + if (items.contains(item)) { return false; } final Card[] resourcesNeeded = item.resourcesNeeded(); diff --git a/src/edu/kit/informatik/model/Item.java b/src/edu/kit/informatik/model/Item.java index 3b3d06c..b6c335a 100644 --- a/src/edu/kit/informatik/model/Item.java +++ b/src/edu/kit/informatik/model/Item.java @@ -41,8 +41,15 @@ public enum Item { return this.equals(STEAMBOAT) || this.equals(BALLON); } - public boolean canHaveMoreThanOne() { - return this.equals(AXE) || this.equals(CLUB); + public int fightingBonus() { + switch (this) { + case AXE: + return 2; + case CLUB: + return 1; + default: + return 0; + } } public static Item parse(String input) { diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java index fe3eaea..7f014d1 100644 --- a/src/edu/kit/informatik/ui/CommandLine.java +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -2,6 +2,7 @@ package edu.kit.informatik.ui; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.Terminal; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.command.Command; import edu.kit.informatik.ui.command.CommandFactory; @@ -36,12 +37,6 @@ public final class CommandLine { while (true) { String input = Terminal.readLine(); - if (input == null) { - break; // TODO remove - } - if (input.startsWith("#")) { - continue; // TODO remove - } if (input.startsWith("startn")) { // TODO remove StringJoiner j = new StringJoiner(","); @@ -54,15 +49,23 @@ public final class CommandLine { Terminal.printError("input after quit command"); continue; } else { - break; + return; } } + final Command command; + try { - Command command = CommandFactory.getCommand(input); - command.apply(simulation); + command = CommandFactory.getCommand(input); } catch (NumberFormatException | InvalidInputException e) { Terminal.printError(e.getMessage()); + continue; + } + + try { + command.apply(simulation); + } catch (LogicException e) { + Terminal.printError(e.getMessage()); } } } diff --git a/src/edu/kit/informatik/ui/command/Build.java b/src/edu/kit/informatik/ui/command/Build.java index 5aec171..a71a643 100644 --- a/src/edu/kit/informatik/ui/command/Build.java +++ b/src/edu/kit/informatik/ui/command/Build.java @@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.Item; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; import java.util.regex.Matcher; @@ -13,14 +14,10 @@ import static edu.kit.informatik.ui.command.CommandFactory.BUILD; public final class Build extends Command { private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)"); - private final Item item; - - private Build(Item item) { - this.item = item; - } + private Item item; @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) throws LogicException { final String result = game.build(item); if (result == null) { Terminal.printError("could not build item"); @@ -29,14 +26,8 @@ public final class Build extends Command { } } - /** - * Parse user input into this command object. - * - * @param input one line of user input - * @return fully initialized command - * @throws InvalidInputException if user input is in any way invalid - */ - public static Command parse(String input) throws InvalidInputException { + @Override + public void parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(BUILD)) { throw new InvalidInputException("unknown command"); } @@ -44,6 +35,6 @@ public final class Build extends Command { if (!matcher.matches()) { throw new InvalidInputException("invalid build command"); } - return new Build(Item.parse(matcher.group(1))); + item = Item.parse(matcher.group(1)); } } diff --git a/src/edu/kit/informatik/ui/command/Buildable.java b/src/edu/kit/informatik/ui/command/Buildable.java index 1040f51..b3ba6b5 100644 --- a/src/edu/kit/informatik/ui/command/Buildable.java +++ b/src/edu/kit/informatik/ui/command/Buildable.java @@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.Item; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; import java.util.Comparator; @@ -11,12 +12,8 @@ import java.util.Set; import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE; public final class Buildable extends Command { - private Buildable() { - - } - @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) throws LogicException { final Set buildable = game.getBuildableItems(); if (buildable.isEmpty()) { Terminal.printLine("EMPTY"); @@ -25,20 +22,13 @@ public final class Buildable extends Command { } } - /** - * Parse user input into this command object. - * - * @param input one line of user input - * @return fully initialized command - * @throws InvalidInputException if user input is in any way invalid - */ - public static Command parse(String input) throws InvalidInputException { + @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"); } - return new Buildable(); } } diff --git a/src/edu/kit/informatik/ui/command/Command.java b/src/edu/kit/informatik/ui/command/Command.java index c9ae2ce..f592199 100644 --- a/src/edu/kit/informatik/ui/command/Command.java +++ b/src/edu/kit/informatik/ui/command/Command.java @@ -1,6 +1,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; /** @@ -14,7 +15,15 @@ public abstract class Command { /** * Apply this command to a game. * @param game game to use - * @throws InvalidInputException on invalid user input + * @throws LogicException on invalid user input */ - public abstract void apply(CardGame game) throws InvalidInputException; + public abstract void apply(CardGame game) throws LogicException; + + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @throws InvalidInputException if user input is in any way invalid + */ + public abstract void parse(String input) throws InvalidInputException; } diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index baa6b91..3d71b3b 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -5,6 +5,7 @@ import edu.kit.informatik.ui.InvalidInputException; import java.util.HashMap; import java.util.Map; import java.util.function.Function; +import java.util.function.Supplier; /** * Factory used to parse user input into commands. @@ -13,8 +14,6 @@ import java.util.function.Function; * @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"; @@ -24,18 +23,18 @@ public final class CommandFactory { public static final String ROLL_DICE = "rollD"; public static final String RESET = "reset"; - private static final Map> COMMANDS = new HashMap<>(); + private static final Map> COMMANDS = new HashMap<>(); static { // initialize registered commands - COMMANDS.put(START, Start::parse); - COMMANDS.put(DRAW, Draw::parse); - COMMANDS.put(LIST_RESOURCES, ListResources::parse); - COMMANDS.put(BUILD, Build::parse); - COMMANDS.put(LIST_BUILDINGS, ListBuildings::parse); - COMMANDS.put(BUILDABLE, Buildable::parse); - COMMANDS.put(ROLL_DICE, RollDice::parse); - COMMANDS.put(RESET, Reset::parse); + COMMANDS.put(START, Start::new); + COMMANDS.put(DRAW, Draw::new); + COMMANDS.put(LIST_RESOURCES, ListResources::new); + COMMANDS.put(BUILD, Build::new); + COMMANDS.put(LIST_BUILDINGS, ListBuildings::new); + COMMANDS.put(BUILDABLE, Buildable::new); + COMMANDS.put(ROLL_DICE, RollDice::new); + COMMANDS.put(RESET, Reset::new); } /** @@ -52,12 +51,15 @@ public final class CommandFactory { * @throws InvalidInputException if user input is invalid */ public static Command getCommand(String input) throws InvalidInputException { - Function parser = COMMANDS.entrySet().stream() + Supplier commandSupplier = COMMANDS.entrySet().stream() .filter(entry -> input.startsWith(entry.getKey())).map(Map.Entry::getValue) .findFirst().orElse(null); - if (parser != null) { - return parser.apply(input); + if (commandSupplier != null) { + Command command = commandSupplier.get(); + command.parse(input); + return command; + } else { + throw new InvalidInputException("unknown command"); } - throw new InvalidInputException("unknown command"); } } diff --git a/src/edu/kit/informatik/ui/command/Draw.java b/src/edu/kit/informatik/ui/command/Draw.java index 4629882..7f7c7b3 100644 --- a/src/edu/kit/informatik/ui/command/Draw.java +++ b/src/edu/kit/informatik/ui/command/Draw.java @@ -2,34 +2,24 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; import static edu.kit.informatik.ui.command.CommandFactory.DRAW; public final class Draw extends Command { - private Draw() { - - } - @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) throws LogicException { Terminal.printLine(game.draw()); } - /** - * Parse user input into this command object. - * - * @param input one line of user input - * @return fully initialized command - * @throws InvalidInputException if user input is in any way invalid - */ - public static Command parse(String input) throws InvalidInputException { + @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"); } - return new Draw(); } } diff --git a/src/edu/kit/informatik/ui/command/ListBuildings.java b/src/edu/kit/informatik/ui/command/ListBuildings.java index bcdeb7e..5db6695 100644 --- a/src/edu/kit/informatik/ui/command/ListBuildings.java +++ b/src/edu/kit/informatik/ui/command/ListBuildings.java @@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.model.Item; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; import java.util.List; @@ -10,12 +11,8 @@ import java.util.List; import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS; public final class ListBuildings extends Command { - private ListBuildings() { - - } - @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) throws LogicException { List items = game.getItems(); if (items.isEmpty()) { Terminal.printLine("EMPTY"); @@ -26,20 +23,13 @@ public final class ListBuildings extends Command { } } - /** - * Parse user input into this command object. - * - * @param input one line of user input - * @return fully initialized command - * @throws InvalidInputException if user input is in any way invalid - */ - public static Command parse(String input) throws InvalidInputException { + @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"); } - return new ListBuildings(); } } diff --git a/src/edu/kit/informatik/ui/command/ListResources.java b/src/edu/kit/informatik/ui/command/ListResources.java index b73bde8..14dfc3d 100644 --- a/src/edu/kit/informatik/ui/command/ListResources.java +++ b/src/edu/kit/informatik/ui/command/ListResources.java @@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.Card; import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; import java.util.Deque; @@ -10,12 +11,8 @@ import java.util.Deque; import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES; public final class ListResources extends Command { - private ListResources() { - - } - @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) throws LogicException { Deque resources = game.getResources(); if (resources.isEmpty()) { Terminal.printLine("EMPTY"); @@ -24,20 +21,13 @@ public final class ListResources extends Command { } } - /** - * Parse user input into this command object. - * - * @param input one line of user input - * @return fully initialized command - * @throws InvalidInputException if user input is in any way invalid - */ - public static Command parse(String input) throws InvalidInputException { + @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"); } - return new ListResources(); } } diff --git a/src/edu/kit/informatik/ui/command/Reset.java b/src/edu/kit/informatik/ui/command/Reset.java index a2c6bf4..aa2ca04 100644 --- a/src/edu/kit/informatik/ui/command/Reset.java +++ b/src/edu/kit/informatik/ui/command/Reset.java @@ -2,35 +2,25 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; import static edu.kit.informatik.ui.command.CommandFactory.RESET; public final class Reset extends Command { - private Reset() { - - } - @Override - public void apply(CardGame game) { + public void apply(CardGame game) throws LogicException { game.reset(); Terminal.printLine("OK"); } - /** - * Parse user input into this command object. - * - * @param input one line of user input - * @return fully initialized command - * @throws InvalidInputException if user input is in any way invalid - */ - public static Command parse(String input) throws InvalidInputException { + @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"); } - return new Reset(); } } diff --git a/src/edu/kit/informatik/ui/command/RollDice.java b/src/edu/kit/informatik/ui/command/RollDice.java index f5b8739..b4f5770 100644 --- a/src/edu/kit/informatik/ui/command/RollDice.java +++ b/src/edu/kit/informatik/ui/command/RollDice.java @@ -2,6 +2,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; import java.util.regex.Matcher; @@ -12,28 +13,17 @@ import static edu.kit.informatik.ui.command.CommandFactory.ROLL_DICE; public final class RollDice extends Command { private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(ROLL_DICE + "(\\d) (\\d)"); - private final int size; + private int size; - private final int rolledNumber; - - private RollDice(int size, int rolledNumber) { - this.size = size; - this.rolledNumber = rolledNumber; - } + private int rolledNumber; @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) throws LogicException { Terminal.printLine(game.rollDice(size, rolledNumber)); } - /** - * Parse user input into this command object. - * - * @param input one line of user input - * @return fully initialized command - * @throws InvalidInputException if user input is in any way invalid - */ - public static Command parse(String input) throws InvalidInputException { + @Override + public void parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(ROLL_DICE)) { throw new InvalidInputException("unknown command"); } @@ -41,8 +31,7 @@ public final class RollDice extends Command { if (!matcher.matches()) { throw new InvalidInputException("invalid roll dice syntax"); } - final int size = Integer.parseInt(matcher.group(1)); - final int rolledNumber = Integer.parseInt(matcher.group(2)); - return new RollDice(size, rolledNumber); + 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 8ff0058..087c358 100644 --- a/src/edu/kit/informatik/ui/command/Start.java +++ b/src/edu/kit/informatik/ui/command/Start.java @@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.Card; import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.model.LogicException; import edu.kit.informatik.ui.InvalidInputException; import java.util.ArrayDeque; @@ -13,29 +14,19 @@ import java.util.regex.Pattern; public final class Start extends Command { private static final Pattern START_ARGUMENTS = Pattern.compile("start ((\\w+,){63}(\\w+))"); - private final Deque cards; - - private Start(Deque cards) { - this.cards = cards; - } + private Deque cards; @Override - public void apply(CardGame game) throws InvalidInputException { + public void apply(CardGame game) throws LogicException { if (game.start(cards)) { Terminal.printLine("OK"); } else { - throw new InvalidInputException("could not start game"); + throw new LogicException("could not start game"); } } - /** - * Parse user input into this command object. - * - * @param input one line of user input - * @return fully initialized command - * @throws InvalidInputException if user input is in any way invalid - */ - public static Command parse(String input) throws InvalidInputException { + @Override + public void parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(CommandFactory.START)) { throw new InvalidInputException("unknown command"); } @@ -43,7 +34,7 @@ public final class Start extends Command { if (!matcher.matches()) { throw new InvalidInputException("invalid start arguments"); } - final Deque cards = new ArrayDeque<>(); + cards = new ArrayDeque<>(); for (final String s : matcher.group(1).split(",")) { final Card card = Card.parse(s); if (card == null) { @@ -51,7 +42,6 @@ public final class Start extends Command { } cards.add(card); } - return new Start(cards); } /* Dieser Befehl ermöglicht es dem Benutzer, ein neues Spiel zu starten. Dieser Befehl kann nur