commit 63d0af1f008ef1d9888f52387a8c5b391214c2df Author: Arne Keller Date: Mon Feb 24 15:45:26 2020 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1fcb152 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out diff --git a/.idea/$PROJECT_FILE$ b/.idea/$PROJECT_FILE$ new file mode 100644 index 0000000..58b7e3e --- /dev/null +++ b/.idea/$PROJECT_FILE$ @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 0000000..02ee5c8 --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..66a6cc4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6b21223 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/qaplug_profiles.xml b/.idea/qaplug_profiles.xml new file mode 100644 index 0000000..3dfd21f --- /dev/null +++ b/.idea/qaplug_profiles.xml @@ -0,0 +1,465 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/final2.iml b/final2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/final2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/edu/kit/informatik/Main.java b/src/edu/kit/informatik/Main.java new file mode 100644 index 0000000..389c091 --- /dev/null +++ b/src/edu/kit/informatik/Main.java @@ -0,0 +1,9 @@ +package edu.kit.informatik; + +import edu.kit.informatik.ui.CommandLine; + +public final class Main { + public static void main(String[] args) { + CommandLine.startInteractive(); + } +} diff --git a/src/edu/kit/informatik/Terminal.java b/src/edu/kit/informatik/Terminal.java new file mode 100644 index 0000000..2936dd0 --- /dev/null +++ b/src/edu/kit/informatik/Terminal.java @@ -0,0 +1,129 @@ +package edu.kit.informatik; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * This class provides some simple methods for input/output from and to a terminal as well as a method to read in + * files. + * + *

Never modify this class, never upload it to Praktomat. This is only for your local use. If an assignment + * tells you to use this class for input and output never use System.out, System.err or System.in in the same + * assignment. + * + * @author ITI, VeriAlg Group + * @author IPD, SDQ Group + * @version 5.03, 2016/05/07 + */ +public final class Terminal { + + /** + * Reads text from the "standard" input stream, buffering characters so as to provide for the efficient reading + * of characters, arrays, and lines. This stream is already open and ready to supply input data and corresponds + * to keyboard input. + */ + private static BufferedReader IN = new BufferedReader(new InputStreamReader(System.in)); + + public static void reload() { + IN = new BufferedReader(new InputStreamReader(System.in)); + } + + /** + * Private constructor to avoid object generation. + * + * @deprecated Utility-class constructor. + */ + @Deprecated + private Terminal() { + throw new AssertionError("Utility class constructor."); + } + + /** + * Prints the given error-{@code message} with the prefix "{@code Error, }". + * + *

More specific, this method behaves exactly as if the following code got executed: + *

+     * Terminal.printLine("Error, " + message);
+ *
+ * + * @param message the error message to be printed + * @see #printLine(Object) + */ + public static void printError(final String message) { + Terminal.printLine("Error, " + message); + } + + /** + * Prints the string representation of an {@code Object} and then terminate the line. + * + *

If the argument is {@code null}, then the string {@code "null"} is printed, otherwise the object's string + * value {@code obj.toString()} is printed. + * + * @param object the {@code Object} to be printed + * @see String#valueOf(Object) + */ + public static void printLine(final Object object) { + System.out.println(object); + } + + /** + * Prints an array of characters and then terminate the line. + * + *

If the argument is {@code null}, then a {@code NullPointerException} is thrown, otherwise the value of {@code + * new String(charArray)} is printed. + * + * @param charArray an array of chars to be printed + * @see String#valueOf(char[]) + */ + public static void printLine(final char[] charArray) { + /* + * Note: This method's sole purpose is to ensure that the Terminal-class behaves exactly as + * System.out regarding output. (System.out.println(char[]) calls String.valueOf(char[]) + * which itself returns 'new String(char[])' and is therefore the only method that behaves + * differently when passing the provided parameter to the System.out.println(Object) + * method.) + */ + System.out.println(charArray); + } + + /** + * Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return + * ('\r'), or a carriage return followed immediately by a linefeed. + * + * @return a {@code String} containing the contents of the line, not including any line-termination characters, or + * {@code null} if the end of the stream has been reached + */ + public static String readLine() { + try { + return IN.readLine(); + } catch (final IOException e) { + /* + * The IOException will not occur during tests executed by the praktomat, therefore the + * following RuntimeException does not have to get handled. + */ + throw new RuntimeException(e); + } + } + + /** + * Reads the file with the specified path and returns its content stored in a {@code String} array, whereas the + * first array field contains the file's first line, the second field contains the second line, and so on. + * + * @param path the path of the file to be read + * @return the content of the file stored in a {@code String} array + */ + public static String[] readFile(final String path) { + try (final BufferedReader reader = new BufferedReader(new FileReader(path))) { + return reader.lines().toArray(String[]::new); + } catch (final IOException e) { + /* + * You can expect that the praktomat exclusively provides valid file-paths. Therefore + * there will no IOException occur while reading in files during the tests, the + * following RuntimeException does not have to get handled. + */ + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java new file mode 100644 index 0000000..5eed4d0 --- /dev/null +++ b/src/edu/kit/informatik/model/CardGame.java @@ -0,0 +1,4 @@ +package edu.kit.informatik.model; + +public class CardGame { +} diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java new file mode 100644 index 0000000..d544fb3 --- /dev/null +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -0,0 +1,60 @@ +package edu.kit.informatik.ui; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.Terminal; +import edu.kit.informatik.ui.command.Command; +import edu.kit.informatik.ui.command.CommandFactory; + +/** + * Interactive game runner, gets user inputs and processes commands specified. + * + * @author Arne Keller + * @version 1.0 + */ +public final class CommandLine { + /** + * Command used to quit the game and terminate the program. + */ + private static final String QUIT = "quit"; + + /** + * Utility class -> private constructor. + */ + private CommandLine() { + + } + + /** + * Start a new interactive user session. Returns when standard in is fully processed or user exits. + */ + public static void startInteractive() { + // create a new simulation + CardGame simulation = new CardGame(); + + while (true) { + String input = Terminal.readLine(); + if (input == null) { + break; // TODO remove + } + if (input.startsWith("#")) { + continue; // TODO remove + } + + if (input.startsWith(QUIT)) { + if (input.length() != QUIT.length()) { + Terminal.printError("input after quit command"); + continue; + } else { + break; + } + } + + try { + Command command = CommandFactory.getCommand(input); + command.apply(simulation); + } catch (NumberFormatException | InvalidInputException e) { + Terminal.printError(e.getMessage()); + } + } + } +} diff --git a/src/edu/kit/informatik/ui/InvalidInputException.java b/src/edu/kit/informatik/ui/InvalidInputException.java new file mode 100644 index 0000000..8a3c82f --- /dev/null +++ b/src/edu/kit/informatik/ui/InvalidInputException.java @@ -0,0 +1,7 @@ +package edu.kit.informatik.ui; + +public class InvalidInputException extends Exception { + public InvalidInputException(String message) { + super(message); + } +} diff --git a/src/edu/kit/informatik/ui/command/Build.java b/src/edu/kit/informatik/ui/command/Build.java new file mode 100644 index 0000000..45abf9f --- /dev/null +++ b/src/edu/kit/informatik/ui/command/Build.java @@ -0,0 +1,16 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +public class Build 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/Buildable.java b/src/edu/kit/informatik/ui/command/Buildable.java new file mode 100644 index 0000000..83043f2 --- /dev/null +++ b/src/edu/kit/informatik/ui/command/Buildable.java @@ -0,0 +1,16 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +public class Buildable 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/Command.java b/src/edu/kit/informatik/ui/command/Command.java new file mode 100644 index 0000000..f7e3834 --- /dev/null +++ b/src/edu/kit/informatik/ui/command/Command.java @@ -0,0 +1,27 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +/** + * Command that can be applied to a simulation. + * Commands are implemented as separate classes for easy modification and expansion. + * + * @author Arne Keller + * @version 1.0 + */ +public abstract class Command { + /** + * Apply this command to a game. + * @param game game to use + * @throws InvalidInputException on invalid user input + */ + public abstract void apply(CardGame game) throws InvalidInputException; + + /** + * 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 new file mode 100644 index 0000000..3097034 --- /dev/null +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -0,0 +1,62 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.ui.InvalidInputException; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + +/** + * Factory used to parse user input into commands. + * + * @author Arne Keller + * @version 1.0 + */ +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"; + + private static final Map> COMMANDS = new HashMap<>(); + + 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); + } + + /** + * Utility class -> private constructor. + */ + private CommandFactory() { + + } + + /** + * Parse a single line of user input into one command. + * @param input user input line + * @return a fully specified command object + * @throws InvalidInputException if user input is invalid + */ + public static Command getCommand(String input) throws InvalidInputException { + for (Map.Entry> entry : COMMANDS.entrySet()) { + if (input.startsWith(entry.getKey())) { + Command command = entry.getValue().get(); + command.parse(input); + return 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 new file mode 100644 index 0000000..1494e07 --- /dev/null +++ b/src/edu/kit/informatik/ui/command/Draw.java @@ -0,0 +1,25 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +public class Draw extends Command { + @Override + public void apply(CardGame game) throws InvalidInputException { + + } + + @Override + public void parse(String input) throws InvalidInputException { + + } + /* + Es existieren folgende Fallunterscheidungen: + •Wenn sich keine Karte mehr auf dem Stapel befindet bzw. der Befehl in einer nicht für + den Befehl vorhergesehenen Spielphase durchgeführt wird, ist dieser Spielzug nicht zulässig. + Entsprechend wird eine Fehlermeldung ausgegeben. + •Zieht der Spieler eine Karte, so wird der Name der Ressource bzw. der englischsprachige + Bezeichner aus den o.g. Kategorien (i) Ressourcen, (ii) Tiere, (iii) Katastrophe ausgegeben. + Entsprechend der gezogenen Karte kann sich die Spielphase ändern. + */ +} diff --git a/src/edu/kit/informatik/ui/command/ListBuildings.java b/src/edu/kit/informatik/ui/command/ListBuildings.java new file mode 100644 index 0000000..c1cf511 --- /dev/null +++ b/src/edu/kit/informatik/ui/command/ListBuildings.java @@ -0,0 +1,16 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +public class ListBuildings 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/ListResources.java b/src/edu/kit/informatik/ui/command/ListResources.java new file mode 100644 index 0000000..f7aa599 --- /dev/null +++ b/src/edu/kit/informatik/ui/command/ListResources.java @@ -0,0 +1,25 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +public class ListResources extends Command { + @Override + public void apply(CardGame game) throws InvalidInputException { + + } + + @Override + public void parse(String input) throws InvalidInputException { + + } + + /* + Im Erfolgsfall werden alle aktuell im Besitz befindlichen Ressourcen zeilenweise ausgegeben. Die + Ausgabe ist geordnet absteigend nach dem Aufnahmezeitpunkt der Ressourcen. Folglich sind die + obersten fünf Ressourcen die, die in der Hütte gesichert werden. Besitzt der Spieler keine Ressourcen,wird + EMPTY + ausgegeben.Im Fehlerfall (z.B. bei einem fehlerhaften Eingabeformat) wird eine aussagekräftige Fehlermeldung + beginnend mitError, ausgegeben + */ +} diff --git a/src/edu/kit/informatik/ui/command/Quit.java b/src/edu/kit/informatik/ui/command/Quit.java new file mode 100644 index 0000000..4148d95 --- /dev/null +++ b/src/edu/kit/informatik/ui/command/Quit.java @@ -0,0 +1,16 @@ +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 new file mode 100644 index 0000000..30730cd --- /dev/null +++ b/src/edu/kit/informatik/ui/command/Reset.java @@ -0,0 +1,16 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +public class Reset 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/RollDice.java b/src/edu/kit/informatik/ui/command/RollDice.java new file mode 100644 index 0000000..1355bff --- /dev/null +++ b/src/edu/kit/informatik/ui/command/RollDice.java @@ -0,0 +1,16 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +public class RollDice 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/Start.java b/src/edu/kit/informatik/ui/command/Start.java new file mode 100644 index 0000000..486b13e --- /dev/null +++ b/src/edu/kit/informatik/ui/command/Start.java @@ -0,0 +1,32 @@ +package edu.kit.informatik.ui.command; + +import edu.kit.informatik.model.CardGame; +import edu.kit.informatik.ui.InvalidInputException; + +public class Start extends Command { + @Override + public void apply(CardGame game) throws InvalidInputException { + + } + + @Override + public void parse(String input) throws InvalidInputException { + + } + /* + Dieser Befehl ermöglicht es dem Benutzer, ein neues Spiel zu starten. Dieser Befehl kann nur + dann ausgeführt werden, wenn kein Spiel aktiv ist. Zu jedem Zeitpunkt soll höchstens (den Regeln + entsprechend) ein Spiel aktiv sein. Zum Programmstart ist kein Spiel aktiv. + Es gibt einen Kartenstapel, von dem der Spieler sukzessiv Karten ziehen kann. Hierbei wird die + Reihenfolge der 64 Spielkarten im Stapel festgelegt beginnend mit der oberen aufgelegten Karte. + Es werden die englischsprachigen Bezeichner der Spielkarten, wie oben angegeben, verwendet. + Die sieben Spielkartenbezeichner (wood,metal,plastic,spider,snake,tiger,thunderstorm) + werden bei der Eingabe durch exakt ein Komma getrennt.repräsentiert in dem hier + angegebenen Eingabeformat einen Platzhalter.Eingabeformatstart + Ausgabeformat + Falls die Parameter den Spezifikationen entsprochen haben und den Regel entsprechend ein neues + Kartenspiel gestartet werden konnte, wirdOKausgegeben. Falls ein oder mehrere falsche Parameter + dem Befehl übergeben wurden, wird kein Spiel gestartet und nur eine Fehlermeldung beginnendmit + Error,ausgegeben + */ +}