diff --git a/src/edu/kit/informatik/cardgame/Main.java b/src/edu/kit/informatik/cardgame/Main.java index 381f758..f84e404 100644 --- a/src/edu/kit/informatik/cardgame/Main.java +++ b/src/edu/kit/informatik/cardgame/Main.java @@ -2,6 +2,12 @@ package edu.kit.informatik.cardgame; import edu.kit.informatik.cardgame.ui.CommandLine; +/** + * Main program class. + * + * @author Arne Keller + * @version 1.0 + */ public final class Main { /** * Utility class -> private constructor. @@ -13,7 +19,7 @@ public final class Main { /** * Program entry point. * - * @param args command-line arguments + * @param args command-line arguments (will be ignored) */ public static void main(String[] args) { CommandLine.startInteractive(); diff --git a/src/edu/kit/informatik/cardgame/model/Card.java b/src/edu/kit/informatik/cardgame/model/Card.java index 234bf68..76dd84f 100644 --- a/src/edu/kit/informatik/cardgame/model/Card.java +++ b/src/edu/kit/informatik/cardgame/model/Card.java @@ -39,6 +39,11 @@ public enum Card implements RequireDice { */ THUNDERSTORM; + /** + * Get the category of this card. + * + * @return category of this card + */ public CardCategory category() { switch (this) { case WOOD: @@ -84,6 +89,11 @@ public enum Card implements RequireDice { } } + /** + * Get the number of cards of this type needed in a stack. + * + * @return amount of cards needed in a card deck + */ public int requiredAmount() { switch (this) { case WOOD: @@ -101,6 +111,13 @@ public enum Card implements RequireDice { } } + /** + * Parse a single word and return the card represented by the input. + * Fun fact: Card.parse(card.toString()) == card is true for all cards. + * + * @param input text + * @return card object (null if input invalid) + */ public static Card parse(String input) { switch (input) { case "wood": diff --git a/src/edu/kit/informatik/cardgame/model/CardGame.java b/src/edu/kit/informatik/cardgame/model/CardGame.java index 8b0cd9d..d5031ee 100644 --- a/src/edu/kit/informatik/cardgame/model/CardGame.java +++ b/src/edu/kit/informatik/cardgame/model/CardGame.java @@ -33,11 +33,14 @@ public class CardGame { * Items built by the player. */ private final List items = new ArrayList<>(); - private RequireDice requireDice = null; /** * Current game phase. */ - private Phase phase = null; + private Phase phase; + /** + * Object currently requiring dice roll. + */ + private RequireDice requireDice; /** * Start a new game with the specified stack of cards. diff --git a/src/edu/kit/informatik/cardgame/model/RequireDice.java b/src/edu/kit/informatik/cardgame/model/RequireDice.java index dc2bdc8..80d7c5f 100644 --- a/src/edu/kit/informatik/cardgame/model/RequireDice.java +++ b/src/edu/kit/informatik/cardgame/model/RequireDice.java @@ -2,14 +2,25 @@ package edu.kit.informatik.cardgame.model; import java.util.Optional; +/** + * Objects of classes implementing this optionally require a dice to be rolled. + * + * @author Arne Keller + * @version 1.0 + */ public interface RequireDice { + /** + * Get the size of the dice needed to activate this object. + * + * @return dice size needed to use this item (empty if dice not required) + */ Optional diceSizeNeeded(); /** - * Get the minimum dice roll needed to activate this object if it needs a dice roll. + * Get the minimum dice roll needed to activate this object. * - * @return minimum dice roll needed to use this item + * @return minimum dice roll needed to use this item (empty if dice not required) */ Optional minimumDiceRollNeeded(); } diff --git a/src/edu/kit/informatik/cardgame/ui/CommandLine.java b/src/edu/kit/informatik/cardgame/ui/CommandLine.java index e0ba216..c654050 100644 --- a/src/edu/kit/informatik/cardgame/ui/CommandLine.java +++ b/src/edu/kit/informatik/cardgame/ui/CommandLine.java @@ -6,9 +6,6 @@ import edu.kit.informatik.cardgame.ui.command.Command; import edu.kit.informatik.cardgame.ui.command.CommandFactory; import edu.kit.informatik.Terminal; -import java.util.StringJoiner; -import java.util.stream.IntStream; - /** * Interactive game runner, gets user inputs and processes commands specified. * @@ -35,10 +32,11 @@ public final class CommandLine { // create a new simulation CardGame simulation = new CardGame(); boolean lost = false; - + // accept user input indefinitely while (true) { String input = Terminal.readLine(); + // handle quit command if (input.startsWith(QUIT)) { if (input.length() != QUIT.length()) { Terminal.printError("input after quit command"); @@ -49,19 +47,20 @@ public final class CommandLine { } final Command command; - + // attempt to parse user input try { command = CommandFactory.getCommand(input); } catch (NumberFormatException | InvalidInputException e) { Terminal.printError(e.getMessage()); continue; } - + // attempt to execute command try { command.apply(simulation); } catch (LogicException e) { Terminal.printError(e.getMessage()); } + // inform the player if he lost if (lost != simulation.gameLost() && simulation.gameLost()) { Terminal.printLine("lost"); } diff --git a/src/edu/kit/informatik/cardgame/ui/command/Build.java b/src/edu/kit/informatik/cardgame/ui/command/Build.java index 09d52c7..0e550d0 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/Build.java +++ b/src/edu/kit/informatik/cardgame/ui/command/Build.java @@ -9,9 +9,8 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static edu.kit.informatik.cardgame.ui.command.CommandFactory.BUILD; - public final class Build extends Command { + public static final String NAME = "build"; private static final Pattern BUILD_ARGUMENT = Pattern.compile(" (\\w+)"); private Item item; @@ -28,10 +27,10 @@ public final class Build extends Command { @Override public void parse(String input) throws InvalidInputException { - if (input == null || !input.startsWith(BUILD)) { + if (input == null || !input.startsWith(NAME)) { throw new InvalidInputException("unknown command"); } - final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input.substring(BUILD.length())); + final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input.substring(NAME.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid build command"); } diff --git a/src/edu/kit/informatik/cardgame/ui/command/Buildable.java b/src/edu/kit/informatik/cardgame/ui/command/Buildable.java index 833bf4a..1f539cc 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/Buildable.java +++ b/src/edu/kit/informatik/cardgame/ui/command/Buildable.java @@ -9,9 +9,9 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException; import java.util.Comparator; import java.util.Set; -import static edu.kit.informatik.cardgame.ui.command.CommandFactory.BUILDABLE; - public final class Buildable extends Command { + public static final String NAME = "build?"; + @Override public void apply(CardGame game) throws LogicException { final Set buildable = game.getBuildableItems(); @@ -24,10 +24,10 @@ public final class Buildable extends Command { @Override public void parse(String input) throws InvalidInputException { - if (input == null || !input.startsWith(BUILDABLE)) { + if (input == null || !input.startsWith(NAME)) { throw new InvalidInputException("unknown command"); } - if (!input.equals(BUILDABLE)) { + if (!input.equals(NAME)) { throw new InvalidInputException("invalid build? argument: none expected"); } } diff --git a/src/edu/kit/informatik/cardgame/ui/command/CommandFactory.java b/src/edu/kit/informatik/cardgame/ui/command/CommandFactory.java index d542d52..fbad20a 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/cardgame/ui/command/CommandFactory.java @@ -2,7 +2,6 @@ package edu.kit.informatik.cardgame.ui.command; import edu.kit.informatik.cardgame.ui.InvalidInputException; -import java.util.Map; import java.util.TreeMap; import java.util.function.Supplier; @@ -10,30 +9,24 @@ import java.util.function.Supplier; * Factory used to parse user input into commands. * * @author Arne Keller - * @version 1.1 + * @version 1.2 */ public final class CommandFactory { - public static final String START = "start"; - public static final String DRAW = "draw"; - public static final String LIST_RESOURCES = "list-resources"; - public static final String BUILD = "build"; - public static final String LIST_BUILDINGS = "list-buildings"; - public static final String BUILDABLE = "build?"; - public static final String ROLL_DICE = "rollD"; - public static final String RESET = "reset"; - + /** + * Collection of command suppliers keyed by their respective command name. + */ private static final TreeMap> COMMANDS = new TreeMap<>(); static { // initialize registered commands - 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); + COMMANDS.put(Start.NAME, Start::new); + COMMANDS.put(Draw.NAME, Draw::new); + COMMANDS.put(ListResources.NAME, ListResources::new); + COMMANDS.put(Build.NAME, Build::new); + COMMANDS.put(ListBuildings.NAME, ListBuildings::new); + COMMANDS.put(Buildable.NAME, Buildable::new); + COMMANDS.put(RollDice.NAME, RollDice::new); + COMMANDS.put(Reset.NAME, Reset::new); } /** @@ -50,13 +43,14 @@ public final class CommandFactory { * @throws InvalidInputException if user input is invalid */ public static Command getCommand(String input) throws InvalidInputException { - Supplier commandSupplier = COMMANDS + final Supplier commandSupplier = COMMANDS + // match longest prefix .descendingKeySet().stream() .filter(input::startsWith) .map(COMMANDS::get) .findFirst().orElse(null); if (commandSupplier != null) { - Command command = commandSupplier.get(); + final Command command = commandSupplier.get(); command.parse(input); return command; } else { diff --git a/src/edu/kit/informatik/cardgame/ui/command/Draw.java b/src/edu/kit/informatik/cardgame/ui/command/Draw.java index 6a5a37e..b3f4dda 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/Draw.java +++ b/src/edu/kit/informatik/cardgame/ui/command/Draw.java @@ -5,9 +5,9 @@ import edu.kit.informatik.cardgame.model.CardGame; import edu.kit.informatik.cardgame.model.LogicException; import edu.kit.informatik.cardgame.ui.InvalidInputException; -import static edu.kit.informatik.cardgame.ui.command.CommandFactory.DRAW; - public final class Draw extends Command { + public static final String NAME = "draw"; + @Override public void apply(CardGame game) throws LogicException { Terminal.printLine(game.draw()); @@ -15,10 +15,10 @@ public final class Draw extends Command { @Override public void parse(String input) throws InvalidInputException { - if (input == null || !input.startsWith(DRAW)) { + if (input == null || !input.startsWith(NAME)) { throw new InvalidInputException("unknown command"); } - if (!input.equals(DRAW)) { + if (!input.equals(NAME)) { throw new InvalidInputException("invalid draw argument: none expected"); } } diff --git a/src/edu/kit/informatik/cardgame/ui/command/ListBuildings.java b/src/edu/kit/informatik/cardgame/ui/command/ListBuildings.java index e9b4cf5..8e20c35 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/ListBuildings.java +++ b/src/edu/kit/informatik/cardgame/ui/command/ListBuildings.java @@ -8,9 +8,9 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException; import java.util.List; -import static edu.kit.informatik.cardgame.ui.command.CommandFactory.LIST_BUILDINGS; - public final class ListBuildings extends Command { + public static final String NAME = "list-buildings"; + @Override public void apply(CardGame game) throws LogicException { List items = game.getItems(); @@ -25,10 +25,10 @@ public final class ListBuildings extends Command { @Override public void parse(String input) throws InvalidInputException { - if (input == null || !input.startsWith(LIST_BUILDINGS)) { + if (input == null || !input.startsWith(NAME)) { throw new InvalidInputException("unknown command"); } - if (!input.equals(LIST_BUILDINGS)) { + if (!input.equals(NAME)) { throw new InvalidInputException("invalid list-buildings argument: none expected"); } } diff --git a/src/edu/kit/informatik/cardgame/ui/command/ListResources.java b/src/edu/kit/informatik/cardgame/ui/command/ListResources.java index 4c36480..678c8b5 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/ListResources.java +++ b/src/edu/kit/informatik/cardgame/ui/command/ListResources.java @@ -8,9 +8,9 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException; import java.util.Deque; -import static edu.kit.informatik.cardgame.ui.command.CommandFactory.LIST_RESOURCES; - public final class ListResources extends Command { + public static final String NAME = "list-resources"; + @Override public void apply(CardGame game) throws LogicException { Deque resources = game.getResources(); @@ -23,10 +23,10 @@ public final class ListResources extends Command { @Override public void parse(String input) throws InvalidInputException { - if (input == null || !input.startsWith(LIST_RESOURCES)) { + if (input == null || !input.startsWith(NAME)) { throw new InvalidInputException("unknown command"); } - if (!input.equals(LIST_RESOURCES)) { + if (!input.equals(NAME)) { throw new InvalidInputException("invalid list-resources argument: none expected"); } } diff --git a/src/edu/kit/informatik/cardgame/ui/command/Reset.java b/src/edu/kit/informatik/cardgame/ui/command/Reset.java index 04bb390..0ad0b81 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/Reset.java +++ b/src/edu/kit/informatik/cardgame/ui/command/Reset.java @@ -5,9 +5,9 @@ import edu.kit.informatik.cardgame.model.CardGame; import edu.kit.informatik.cardgame.model.LogicException; import edu.kit.informatik.cardgame.ui.InvalidInputException; -import static edu.kit.informatik.cardgame.ui.command.CommandFactory.RESET; - public final class Reset extends Command { + public static final String NAME = "reset"; + @Override public void apply(CardGame game) throws LogicException { game.reset(); @@ -16,10 +16,10 @@ public final class Reset extends Command { @Override public void parse(String input) throws InvalidInputException { - if (input == null || !input.startsWith(RESET)) { + if (input == null || !input.startsWith(NAME)) { throw new InvalidInputException("unknown command"); } - if (!input.equals(RESET)) { + if (!input.equals(NAME)) { throw new InvalidInputException("invalid reset argument: none expected"); } } diff --git a/src/edu/kit/informatik/cardgame/ui/command/RollDice.java b/src/edu/kit/informatik/cardgame/ui/command/RollDice.java index 0c3ae6f..1cf9ab5 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/RollDice.java +++ b/src/edu/kit/informatik/cardgame/ui/command/RollDice.java @@ -8,10 +8,9 @@ import edu.kit.informatik.cardgame.ui.InvalidInputException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static edu.kit.informatik.cardgame.ui.command.CommandFactory.ROLL_DICE; - public final class RollDice extends Command { - private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(ROLL_DICE + "\\+?(\\d+) \\+?(\\d+)"); + public static final String NAME = "rollD"; + private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(NAME + "\\+?(\\d+) \\+?(\\d+)"); private int size; @@ -24,7 +23,7 @@ public final class RollDice extends Command { @Override public void parse(String input) throws InvalidInputException { - if (input == null || !input.startsWith(ROLL_DICE)) { + if (input == null || !input.startsWith(NAME)) { throw new InvalidInputException("unknown command"); } final Matcher matcher = ROLL_DICE_ARGUMENTS.matcher(input); diff --git a/src/edu/kit/informatik/cardgame/ui/command/Start.java b/src/edu/kit/informatik/cardgame/ui/command/Start.java index 8d76301..4a4abf1 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/Start.java +++ b/src/edu/kit/informatik/cardgame/ui/command/Start.java @@ -12,6 +12,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public final class Start extends Command { + public static final String NAME = "start"; private static final Pattern START_ARGUMENTS = Pattern.compile("start ((\\w+,){63}(\\w+))"); private Deque cards; @@ -27,7 +28,7 @@ public final class Start extends Command { @Override public void parse(String input) throws InvalidInputException { - if (input == null || !input.startsWith(CommandFactory.START)) { + if (input == null || !input.startsWith(NAME)) { throw new InvalidInputException("unknown command"); } final Matcher matcher = START_ARGUMENTS.matcher(input);